-
Notifications
You must be signed in to change notification settings - Fork 0
/
petal.rb
318 lines (277 loc) · 8.75 KB
/
petal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#--
# petal.rb
# specs:
# function:
# `d1`〜`d9` ループ
# `cps`, `bps`, `bpm` テンポ設定 [Tempo]
# `hush` ループを全て停止
# `solo :d1, 'bd*4'` d1のみループ(d1の前に":", 後ろに","を付ける。それ以外はそのまま)
# operator:
# `:` インデックス指定
# `*` 繰り返し [Repetition - repeat as many times]
# `/` 繰り返し [Repetition - occur less often]
# `(k,n,r)` ユークリッドリズム(k: num_accents, n: size, r: beat_rotations) [Euclidean Sequences]
# value:
# `~` 休符 [Rest]
# `:silence` ループ停止 [Silence] `d1 :silence`
# `` ループ停止 `d1`
# option:
# `n:` インデックス指定 [Sample Selection] `:`の指定を上書きします
# `gain:`, `amp:` 音量(0〜∞)
# `pan:` パン(-1.0〜1.0)
# `speed:`, `rate:` 再生レート(-∞〜∞)
# `density:` ループ回数を増やす(0〜∞)
# `slow:` ループ回数を減らす(0〜∞), `slow: 2`は`density: 0.5`と同じです
# `stretch:` ビートストレッチ(`:b`,`'b'`), ピッチストレッチ(`:p`,`'p'`)のいずれかを指定
#
# example:
# cps(1)
# d1 'bd(5,8,2)', speed: 'rand -3 4', n: 'irand 64'
# d2 'sn sn/4 ~ sn', speed: '1 -0.5 2 -1.5', pan: 'rand -1 1'
# d3 :loop_garzul, stretch: :p, slow: 32, gain: 0.5
#++
require_relative './petal_data.rb'
require_relative './petal_parser.rb'
require_relative './petal_util.rb'
module PetalLang
module Petal
include PetalLang::LoopHolder
include PetalLang::Util
@@seconds_per_cycle = 1.0
@@bpm = 60
@@solo = nil
# @@dirt_dir = '~/Library/Application Support/SuperCollider/downloaded-quarks/Dirt-Samples'
# @@dirt_dir = '~/Dirt-Samples'
@@dirt_dir = File.dirname(__FILE__) + '/Dirt-Samples'
@@use_fx_with_petal = true
def play_array(arry, dur, loop_index, cycle)
if !arry || arry.count == 0
sleep dur.to_f
return
end
count = arry.count
dur_per_element = dur.to_f / count
arry.each do |element|
if element.is_a?(Array)
play_array(element, dur_per_element, loop_index, cycle)
else
play_sound(element, dur_per_element, loop_index, cycle)
end
end
end
def calc_rand(r)
if r.instance_of?(Rand)
rrand(r.min.to_f, r.max.to_f)
elsif r.instance_of?(IRand)
rrand_i(r.min.to_i, r.max.to_i)
end
end
def play_sound(sound, dur, loop_index, cycle)
dbg "sound: #{sound}"
dbg "dur: #{dur}"
dbg "cycle.random_gain: #{cycle.random_gain}"
dbg "cycle.random_pan: #{cycle.random_pan}"
dbg "cycle.random_speed: #{cycle.random_speed}"
if Sound::REST.equal?(sound)
# 休符
sleep dur
elsif sound.divisor > 1 && loop_index.modulo(sound.divisor) != 0
# '/'で、loop_index % divisorが0以外は、休符
sleep dur
else
index = cycle.random_n ? calc_rand(cycle.random_n) : sound.index
amp = cycle.random_gain ? calc_rand(cycle.random_gain) : sound.amp
pan = cycle.random_pan ? calc_rand(cycle.random_pan) : sound.pan
rate = cycle.random_speed ? calc_rand(cycle.random_speed) : sound.rate
play_sample(sound.name, index, dur, amp, pan, rate, cycle.stretch)
end
end
def play_sample(name, index, dur_per_sample, amp, pan, rate, stretch)
if name.is_a?(Symbol)
case stretch
when :b, 'b'
# beat_stretch指定の場合はrate:を無視する
sample name, amp: amp, pan: pan, beat_stretch: dur_per_sample
when :p, 'p'
sample name, amp: amp, pan: pan, rate: rate, pitch_stretch: dur_per_sample
else
sample name, amp: amp, pan: pan, rate: rate
end
else
sample_dir = File.expand_path(@@dirt_dir + '/' + name) + '/'
dbg "sample_dir: #{sample_dir}"
dbg "index: #{index}"
dbg "rate: #{rate}"
case stretch
when :b, 'b'
# beat_stretch指定の場合はrate:を無視する
sample sample_dir, index, amp: amp, pan: pan, beat_stretch: dur_per_sample
when :p, 'p'
sample sample_dir, index, amp: amp, pan: pan, rate: rate, pitch_stretch: dur_per_sample
else
sample sample_dir, index, amp: amp, pan: pan, rate: rate
end
end
sleep dur_per_sample
end
def set_dirt_dir(dir)
@@dirt_dir = dir
end
def set_seconds_per_cycle(sec)
@@seconds_per_cycle = sec
end
def get_seconds_per_cycle
@@seconds_per_cycle
end
def set_bpm(beat_per_min)
@@bpm = beat_per_min
end
def get_bpm
@@bpm
end
def dirt_stop(loop_name)
sub_number = @@loop_sub_numbers[loop_name]
unless sub_number.nil?
loop_symbol = "#{loop_name}_#{sub_number}".intern
live_loop loop_symbol, sync: :d0 do
stop
end
end
end
def dirt_hush
@@solo = :d0
for i in 1..9 do
s = "d#{i}".intern
dirt_stop(s)
end
end
def dirt_solo(loop_name, sound = nil, **option_hash)
@@solo = loop_name
for i in 1..9 do
s = "d#{i}".intern
next if s == loop_name
dirt_stop(s)
end
dirt loop_name, sound, **option_hash
end
def dirt(loop_name, sound = nil, **option_hash)
dbg "sound: #{sound}"
dbg "option_hash: #{option_hash}"
return if !@@solo.nil? && @@solo != loop_name # 他のループがsoloの場合
cycle = Parser.parse(@@bpm, sound, **option_hash)
previous_number = @@loop_sub_numbers[loop_name]
dbg "previous_number: #{previous_number}"
next_number = if previous_number.nil?
0
elsif !@@use_fx_with_petal
previous_number.to_i
else
previous_number.to_i + 1
end
dbg "next_number: #{next_number}"
next_loop = "#{loop_name}_#{next_number}".intern
@@loop_sub_numbers[loop_name] = next_number.to_i
dur = @@seconds_per_cycle
live_loop :d0 do
use_bpm @@bpm
sleep dur
@@solo = nil
end
dbg "next_loop: #{next_loop}"
dbg "cycle.sound_array.empty?: #{cycle.sound_array.empty?}"
live_loop next_loop, sync: :d0 do
stop if cycle.sound_array.empty?
loop_index = tick
dbg "loop_index: #{loop_index}"
use_bpm cycle.bpm
play_array(cycle.sound_array, dur, loop_index, cycle)
end
if @@use_fx_with_petal && !previous_number.nil?
previous_loop = "#{loop_name}_#{previous_number}".intern
live_loop previous_loop, sync: :d0 do
stop
end
end
end
end
end
include PetalLang::Petal
def cps(cycle_per_sec)
seconds_per_cycle = get_seconds_per_cycle
beat_per_min = 60.0 * cycle_per_sec * seconds_per_cycle
set_bpm beat_per_min
end
def bps(beat_per_sec)
# cps(1) == bps(120/60) == bpm(120)
# https://tidalcycles.org/patterns.html#tempo
seconds_per_cycle = get_seconds_per_cycle
beat_per_min = beat_per_sec * 60.0 / (2.0 * seconds_per_cycle)
set_bpm beat_per_min
end
def bpm(beat_per_min)
seconds_per_cycle = get_seconds_per_cycle
beat_per_min /= (2.0 * seconds_per_cycle)
set_bpm beat_per_min
end
def d1(sound = nil, **option_hash)
dirt :d1, sound, **option_hash
end
def d2(sound = nil, **option_hash)
dirt :d2, sound, **option_hash
end
def d3(sound = nil, **option_hash)
dirt :d3, sound, **option_hash
end
def d4(sound = nil, **option_hash)
dirt :d4, sound, **option_hash
end
def d5(sound = nil, **option_hash)
dirt :d5, sound, **option_hash
end
def d6(sound = nil, **option_hash)
dirt :d6, sound, **option_hash
end
def d7(sound = nil, **option_hash)
dirt :d7, sound, **option_hash
end
def d8(sound = nil, **option_hash)
dirt :d8, sound, **option_hash
end
def d9(sound = nil, **option_hash)
dirt :d9, sound, **option_hash
end
def hush
dirt_hush
end
def solo(loop_name, sound = nil, **option_hash)
dirt_solo loop_name, sound, **option_hash
end
def use_fx_with_petal(use = true)
@@use_fx_with_petal = use
end
def dirt_names
path = File.expand_path(@@dirt_dir)
entries = Dir.entries(path)
entries -= ['.']
entries -= ['..']
entries -= ['Dirt-Samples.quark']
# entries.map { |e| path + e }
end
def dirt_sample(name, index = 0)
path = File.expand_path(@@dirt_dir + '/' + name) + '/'
entries = Dir.entries(path)
entries -= ['.']
entries -= ['..']
i = index % entries.count
path + entries[i]
end
def dirt_samples(name)
path = File.expand_path(@@dirt_dir + '/' + name) + '/'
entries = Dir.entries(path)
entries -= ['.']
entries -= ['..']
entries.map { |e| path + e }
end
def euclidean_rhythm(num_accents, size, beat_rotations = nil)
PetalLang::Parser.euclidean_rhythm(num_accents, size, beat_rotations)
end