musa-dsl 0.14.32 → 0.21.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/Gemfile +0 -1
- data/README.md +5 -1
- data/lib/musa-dsl.rb +54 -11
- data/lib/musa-dsl/core-ext.rb +7 -13
- data/lib/musa-dsl/core-ext/array-explode-ranges.rb +15 -23
- data/lib/musa-dsl/core-ext/arrayfy.rb +30 -12
- data/lib/musa-dsl/core-ext/attribute-builder.rb +194 -0
- data/lib/musa-dsl/core-ext/deep-copy.rb +185 -0
- data/lib/musa-dsl/core-ext/dynamic-proxy.rb +44 -40
- data/lib/musa-dsl/core-ext/inspect-nice.rb +40 -22
- data/lib/musa-dsl/core-ext/smart-proc-binder.rb +108 -0
- data/lib/musa-dsl/core-ext/with.rb +26 -0
- data/lib/musa-dsl/datasets.rb +8 -3
- data/lib/musa-dsl/datasets/dataset.rb +3 -0
- data/lib/musa-dsl/datasets/delta-d.rb +12 -0
- data/lib/musa-dsl/datasets/e.rb +61 -0
- data/lib/musa-dsl/datasets/gdv.rb +51 -411
- data/lib/musa-dsl/datasets/gdvd.rb +179 -0
- data/lib/musa-dsl/datasets/helper.rb +41 -0
- data/lib/musa-dsl/datasets/p.rb +68 -0
- data/lib/musa-dsl/datasets/packed-v.rb +19 -0
- data/lib/musa-dsl/datasets/pdv.rb +22 -15
- data/lib/musa-dsl/datasets/ps.rb +113 -0
- data/lib/musa-dsl/datasets/score.rb +210 -0
- data/lib/musa-dsl/datasets/score/queriable.rb +48 -0
- data/lib/musa-dsl/datasets/score/render.rb +31 -0
- data/lib/musa-dsl/datasets/score/to-mxml/process-pdv.rb +160 -0
- data/lib/musa-dsl/datasets/score/to-mxml/process-ps.rb +51 -0
- data/lib/musa-dsl/datasets/score/to-mxml/process-time.rb +153 -0
- data/lib/musa-dsl/datasets/score/to-mxml/to-mxml.rb +158 -0
- data/lib/musa-dsl/datasets/v.rb +23 -0
- data/lib/musa-dsl/generative.rb +5 -5
- data/lib/musa-dsl/generative/backboner.rb +274 -0
- data/lib/musa-dsl/generative/darwin.rb +102 -96
- data/lib/musa-dsl/generative/generative-grammar.rb +182 -187
- data/lib/musa-dsl/generative/markov.rb +56 -53
- data/lib/musa-dsl/generative/variatio.rb +234 -222
- data/lib/musa-dsl/logger.rb +1 -0
- data/lib/musa-dsl/logger/logger.rb +32 -0
- data/lib/musa-dsl/matrix.rb +1 -0
- data/lib/musa-dsl/matrix/matrix.rb +210 -0
- data/lib/musa-dsl/midi.rb +2 -2
- data/lib/musa-dsl/midi/midi-recorder.rb +54 -52
- data/lib/musa-dsl/midi/midi-voices.rb +187 -182
- data/lib/musa-dsl/music.rb +5 -5
- data/lib/musa-dsl/music/chord-definition.rb +54 -50
- data/lib/musa-dsl/music/chord-definitions.rb +13 -9
- data/lib/musa-dsl/music/chords.rb +236 -238
- data/lib/musa-dsl/music/equally-tempered-12-tone-scale-system.rb +187 -183
- data/lib/musa-dsl/music/scales.rb +331 -332
- data/lib/musa-dsl/musicxml.rb +1 -0
- data/lib/musa-dsl/musicxml/builder/attributes.rb +155 -0
- data/lib/musa-dsl/musicxml/builder/backup-forward.rb +45 -0
- data/lib/musa-dsl/musicxml/builder/direction.rb +322 -0
- data/lib/musa-dsl/musicxml/builder/helper.rb +90 -0
- data/lib/musa-dsl/musicxml/builder/measure.rb +137 -0
- data/lib/musa-dsl/musicxml/builder/note-complexities.rb +152 -0
- data/lib/musa-dsl/musicxml/builder/note.rb +577 -0
- data/lib/musa-dsl/musicxml/builder/part-group.rb +44 -0
- data/lib/musa-dsl/musicxml/builder/part.rb +67 -0
- data/lib/musa-dsl/musicxml/builder/pitched-note.rb +126 -0
- data/lib/musa-dsl/musicxml/builder/rest.rb +117 -0
- data/lib/musa-dsl/musicxml/builder/score-partwise.rb +120 -0
- data/lib/musa-dsl/musicxml/builder/typed-text.rb +43 -0
- data/lib/musa-dsl/musicxml/builder/unpitched-note.rb +112 -0
- data/lib/musa-dsl/neumalang.rb +1 -1
- data/lib/musa-dsl/neumalang/datatypes.citrus +79 -0
- data/lib/musa-dsl/neumalang/neuma.citrus +165 -0
- data/lib/musa-dsl/neumalang/neumalang.citrus +32 -242
- data/lib/musa-dsl/neumalang/neumalang.rb +373 -142
- data/lib/musa-dsl/neumalang/process.citrus +21 -0
- data/lib/musa-dsl/neumalang/terminals.citrus +67 -0
- data/lib/musa-dsl/neumalang/vectors.citrus +23 -0
- data/lib/musa-dsl/neumas.rb +5 -0
- data/lib/musa-dsl/neumas/array-to-neumas.rb +34 -0
- data/lib/musa-dsl/neumas/neuma-decoder.rb +63 -0
- data/lib/musa-dsl/neumas/neuma-gdv-decoder.rb +57 -0
- data/lib/musa-dsl/neumas/neuma-gdvd-decoder.rb +15 -0
- data/lib/musa-dsl/neumas/neumas.rb +37 -0
- data/lib/musa-dsl/neumas/string-to-neumas.rb +34 -0
- data/lib/musa-dsl/repl.rb +1 -1
- data/lib/musa-dsl/repl/repl.rb +122 -110
- data/lib/musa-dsl/sequencer.rb +1 -1
- data/lib/musa-dsl/sequencer/base-sequencer-implementation-control.rb +163 -136
- data/lib/musa-dsl/sequencer/base-sequencer-implementation-play-helper.rb +301 -286
- data/lib/musa-dsl/sequencer/base-sequencer-implementation.rb +550 -321
- data/lib/musa-dsl/sequencer/base-sequencer-public.rb +198 -176
- data/lib/musa-dsl/sequencer/base-sequencer-tick-based.rb +75 -0
- data/lib/musa-dsl/sequencer/base-sequencer-tickless-based.rb +75 -0
- data/lib/musa-dsl/sequencer/sequencer-dsl.rb +105 -85
- data/lib/musa-dsl/sequencer/timeslots.rb +34 -0
- data/lib/musa-dsl/series.rb +1 -1
- data/lib/musa-dsl/{core-ext → series}/array-to-serie.rb +1 -1
- data/lib/musa-dsl/series/base-series.rb +171 -168
- data/lib/musa-dsl/series/hash-serie-splitter.rb +134 -132
- data/lib/musa-dsl/series/holder-serie.rb +1 -1
- data/lib/musa-dsl/series/main-serie-constructors.rb +6 -1
- data/lib/musa-dsl/series/main-serie-operations.rb +807 -797
- data/lib/musa-dsl/series/proxy-serie.rb +6 -6
- data/lib/musa-dsl/series/queue-serie.rb +5 -5
- data/lib/musa-dsl/series/series.rb +2 -0
- data/lib/musa-dsl/transcription.rb +4 -0
- data/lib/musa-dsl/transcription/from-gdv-to-midi.rb +227 -0
- data/lib/musa-dsl/transcription/from-gdv-to-musicxml.rb +36 -0
- data/lib/musa-dsl/transcription/from-gdv.rb +17 -0
- data/lib/musa-dsl/transcription/transcription.rb +55 -0
- data/lib/musa-dsl/transport.rb +6 -6
- data/lib/musa-dsl/transport/clock.rb +26 -26
- data/lib/musa-dsl/transport/dummy-clock.rb +32 -30
- data/lib/musa-dsl/transport/external-tick-clock.rb +21 -20
- data/lib/musa-dsl/transport/input-midi-clock.rb +89 -80
- data/lib/musa-dsl/transport/timer-clock.rb +72 -71
- data/lib/musa-dsl/transport/timer.rb +28 -26
- data/lib/musa-dsl/transport/transport.rb +111 -93
- data/musa-dsl.gemspec +3 -3
- metadata +73 -24
- data/lib/musa-dsl/core-ext/array-apply-get.rb +0 -18
- data/lib/musa-dsl/core-ext/array-to-neumas.rb +0 -28
- data/lib/musa-dsl/core-ext/as-context-run.rb +0 -44
- data/lib/musa-dsl/core-ext/duplicate.rb +0 -134
- data/lib/musa-dsl/core-ext/key-parameters-procedure-binder.rb +0 -85
- data/lib/musa-dsl/core-ext/proc-nice.rb +0 -13
- data/lib/musa-dsl/core-ext/send-nice.rb +0 -21
- data/lib/musa-dsl/core-ext/string-to-neumas.rb +0 -27
- data/lib/musa-dsl/datasets/gdv-decorators.rb +0 -221
- data/lib/musa-dsl/generative/rules.rb +0 -282
- data/lib/musa-dsl/neuma.rb +0 -1
- data/lib/musa-dsl/neuma/neuma.rb +0 -181
@@ -1,251 +1,273 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require_relative '../core-ext/arrayfy'
|
2
|
+
require_relative '../core-ext/smart-proc-binder'
|
3
|
+
require_relative '../logger'
|
3
4
|
|
4
|
-
|
5
|
+
require_relative '../series'
|
5
6
|
|
6
|
-
|
7
|
-
attr_reader :beats_per_bar, :ticks_per_beat, :ticks_per_bar, :tick_duration, :running_position
|
8
|
-
attr_reader :everying, :playing, :moving
|
7
|
+
require_relative 'timeslots'
|
9
8
|
|
10
|
-
|
9
|
+
require_relative 'base-sequencer-tick-based'
|
10
|
+
require_relative 'base-sequencer-tickless-based'
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
module Musa
|
13
|
+
module Sequencer
|
14
|
+
class BaseSequencer
|
15
|
+
attr_reader :beats_per_bar, :ticks_per_beat
|
16
|
+
attr_reader :running_position
|
17
|
+
attr_reader :everying, :playing, :moving
|
18
|
+
attr_reader :logger
|
14
19
|
|
15
|
-
|
16
|
-
@on_block_error = []
|
20
|
+
def initialize(beats_per_bar = nil, ticks_per_beat = nil, logger: nil, do_log: nil, do_error_log: nil, log_position_format: nil)
|
17
21
|
|
18
|
-
|
19
|
-
|
22
|
+
raise ArgumentError,
|
23
|
+
"'beats_per_bar' and 'ticks_per_beat' parameters should be both nil or both have values" \
|
24
|
+
unless beats_per_bar && ticks_per_beat || beats_per_bar.nil? && ticks_per_beat.nil?
|
20
25
|
|
21
|
-
|
22
|
-
|
26
|
+
if logger
|
27
|
+
@logger = logger
|
28
|
+
else
|
29
|
+
@logger = Musa::Logger::Logger.new(sequencer: self, position_format: log_position_format)
|
23
30
|
|
24
|
-
|
25
|
-
|
31
|
+
@logger.error! if do_error_log || do_error_log.nil?
|
32
|
+
@logger.debug! if do_log
|
33
|
+
end
|
26
34
|
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
if beats_per_bar && ticks_per_beat
|
36
|
+
@beats_per_bar = Rational(beats_per_bar)
|
37
|
+
@ticks_per_beat = Rational(ticks_per_beat)
|
30
38
|
|
31
|
-
|
39
|
+
self.singleton_class.include TickBasedTiming
|
40
|
+
else
|
41
|
+
self.singleton_class.include TicklessBasedTiming
|
42
|
+
end
|
32
43
|
|
33
|
-
|
34
|
-
@playing = []
|
35
|
-
@moving = []
|
44
|
+
_init_timing
|
36
45
|
|
37
|
-
|
46
|
+
@on_debug_at = []
|
47
|
+
@on_error = []
|
38
48
|
|
39
|
-
|
40
|
-
|
49
|
+
@before_tick = []
|
50
|
+
@on_fast_forward = []
|
41
51
|
|
42
|
-
|
43
|
-
|
44
|
-
@everying.clear
|
45
|
-
@playing.clear
|
46
|
-
@moving.clear
|
52
|
+
@tick_mutex = Mutex.new
|
53
|
+
@position_mutex = Mutex.new
|
47
54
|
|
48
|
-
|
55
|
+
@timeslots = Timeslots.new
|
49
56
|
|
50
|
-
|
51
|
-
|
57
|
+
@everying = []
|
58
|
+
@playing = []
|
59
|
+
@moving = []
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
@hold_ticks += 1
|
56
|
-
else
|
57
|
-
_tick
|
58
|
-
end
|
59
|
-
end
|
61
|
+
reset
|
62
|
+
end
|
60
63
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
+
def reset
|
65
|
+
@timeslots.clear
|
66
|
+
@everying.clear
|
67
|
+
@playing.clear
|
68
|
+
@moving.clear
|
64
69
|
|
65
|
-
|
66
|
-
@score.empty?
|
67
|
-
end
|
70
|
+
@event_handlers = [EventHandler.new]
|
68
71
|
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
+
_reset_timing
|
73
|
+
end
|
72
74
|
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
def size
|
76
|
+
@timeslots.values.sum(&:size)
|
77
|
+
end
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
def empty?
|
80
|
+
@timeslots.empty?
|
81
|
+
end
|
80
82
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
def on_fast_forward(&block)
|
86
|
-
@on_fast_forward << KeyParametersProcedureBinder.new(block)
|
87
|
-
end
|
83
|
+
def run
|
84
|
+
tick until empty?
|
85
|
+
end
|
88
86
|
|
89
|
-
|
90
|
-
|
91
|
-
|
87
|
+
def event_handler
|
88
|
+
@event_handlers.last
|
89
|
+
end
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
91
|
+
def on_debug_at(&block)
|
92
|
+
@on_debug_at << SmartProcBinder.new(block)
|
93
|
+
end
|
96
94
|
|
97
|
-
|
98
|
-
|
95
|
+
def on_error(&block)
|
96
|
+
@on_error << SmartProcBinder.new(block)
|
97
|
+
end
|
99
98
|
|
100
|
-
|
99
|
+
def on_fast_forward(&block)
|
100
|
+
@on_fast_forward << SmartProcBinder.new(block)
|
101
|
+
end
|
101
102
|
|
102
|
-
|
103
|
-
|
103
|
+
def before_tick(&block)
|
104
|
+
@before_tick << SmartProcBinder.new(block)
|
105
|
+
end
|
104
106
|
|
105
|
-
|
107
|
+
def on(event, &block)
|
108
|
+
@event_handlers.last.on event, &block
|
109
|
+
end
|
106
110
|
|
107
|
-
|
108
|
-
|
109
|
-
|
111
|
+
def launch(event, *value_parameters, **key_parameters)
|
112
|
+
@event_handlers.last.launch event, *value_parameters, **key_parameters
|
113
|
+
end
|
110
114
|
|
111
|
-
|
112
|
-
|
113
|
-
end
|
115
|
+
def wait(bars_delay, with: nil, debug: nil, &block)
|
116
|
+
debug ||= false
|
114
117
|
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
+
control = EventHandler.new @event_handlers.last
|
119
|
+
@event_handlers.push control
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
+
if bars_delay.is_a? Numeric
|
122
|
+
_numeric_at position + bars_delay.rationalize, control, with: with, debug: debug, &block
|
123
|
+
else
|
124
|
+
bars_delay = Series::S(*bars_delay) if bars_delay.is_a?(Array)
|
125
|
+
bars_delay = bars_delay.instance if bars_delay
|
121
126
|
|
122
|
-
|
123
|
-
|
127
|
+
with = Series::S(*with).repeat if with.is_a?(Array)
|
128
|
+
with = with.instance if with
|
124
129
|
|
125
|
-
|
126
|
-
|
127
|
-
else
|
128
|
-
bars_delay = Series::S(*bars_delay) if bars_delay.is_a? Array
|
129
|
-
bars_delay = bars_delay.instance if bars_delay
|
130
|
+
_serie_at bars_delay.eval { |delay| position + delay }, control, with: with, debug: debug, &block
|
131
|
+
end
|
130
132
|
|
131
|
-
|
132
|
-
with = with.instance if with
|
133
|
+
@event_handlers.pop
|
133
134
|
|
134
|
-
|
135
|
-
|
135
|
+
control
|
136
|
+
end
|
136
137
|
|
137
|
-
|
138
|
+
def now(with: nil, &block)
|
139
|
+
control = EventHandler.new @event_handlers.last
|
140
|
+
@event_handlers.push control
|
138
141
|
|
139
|
-
|
140
|
-
end
|
142
|
+
_numeric_at position, control, with: with, &block
|
141
143
|
|
142
|
-
|
143
|
-
control = EventHandler.new @event_handlers.last, capture_stdout: true
|
144
|
-
@event_handlers.push control
|
144
|
+
@event_handlers.pop
|
145
145
|
|
146
|
-
|
146
|
+
control
|
147
|
+
end
|
147
148
|
|
148
|
-
|
149
|
+
def raw_at(bar_position, force_first: nil, &block)
|
150
|
+
_raw_numeric_at bar_position.rationalize, force_first: force_first, &block
|
149
151
|
|
150
|
-
|
151
|
-
|
152
|
+
nil
|
153
|
+
end
|
152
154
|
|
153
|
-
|
154
|
-
|
155
|
+
def at(bar_position, with: nil, debug: nil, &block)
|
156
|
+
debug ||= false
|
155
157
|
|
156
|
-
|
157
|
-
|
158
|
+
control = EventHandler.new @event_handlers.last
|
159
|
+
@event_handlers.push control
|
158
160
|
|
159
|
-
|
160
|
-
|
161
|
+
if bar_position.is_a? Numeric
|
162
|
+
_numeric_at bar_position.rationalize, control, with: with, debug: debug, &block
|
163
|
+
else
|
164
|
+
bar_position = Series::S(*bar_position) if bar_position.is_a? Array
|
165
|
+
bar_position = bar_position.instance if bar_position
|
161
166
|
|
162
|
-
|
163
|
-
|
167
|
+
with = Series::S(*with).repeat if with.is_a? Array
|
168
|
+
with = with.instance if with
|
164
169
|
|
165
|
-
|
166
|
-
|
167
|
-
else
|
168
|
-
bar_position = Series::S(*bar_position) if bar_position.is_a? Array
|
169
|
-
bar_position = bar_position.instance if bar_position
|
170
|
+
_serie_at bar_position, control, with: with, debug: debug, &block
|
171
|
+
end
|
170
172
|
|
171
|
-
|
172
|
-
with = with.instance if with
|
173
|
+
@event_handlers.pop
|
173
174
|
|
174
|
-
|
175
|
-
|
175
|
+
control
|
176
|
+
end
|
176
177
|
|
177
|
-
|
178
|
+
def play(serie, mode: nil, parameter: nil, after: nil, context: nil, **mode_args, &block)
|
179
|
+
mode ||= :wait
|
178
180
|
|
179
|
-
|
180
|
-
|
181
|
+
control = PlayControl.new @event_handlers.last, after: after
|
182
|
+
@event_handlers.push control
|
181
183
|
|
182
|
-
|
183
|
-
mode ||= :wait
|
184
|
+
_play serie.instance, control, context, mode: mode, parameter: parameter, **mode_args, &block
|
184
185
|
|
185
|
-
|
186
|
-
@event_handlers.push control
|
186
|
+
@event_handlers.pop
|
187
187
|
|
188
|
-
|
188
|
+
@playing << control
|
189
189
|
|
190
|
-
|
190
|
+
control.after do
|
191
|
+
@playing.delete control
|
192
|
+
end
|
191
193
|
|
192
|
-
|
194
|
+
control
|
195
|
+
end
|
193
196
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
+
def continuation_play(parameters)
|
198
|
+
_play parameters[:serie],
|
199
|
+
parameters[:control],
|
200
|
+
parameters[:nl_context],
|
201
|
+
mode: parameters[:mode],
|
202
|
+
decoder: parameters[:decoder],
|
203
|
+
__play_eval: parameters[:play_eval],
|
204
|
+
**parameters[:mode_args]
|
205
|
+
end
|
197
206
|
|
198
|
-
|
199
|
-
|
207
|
+
def every(interval, duration: nil, till: nil, condition: nil, on_stop: nil, after_bars: nil, after: nil, &block)
|
208
|
+
# nil interval means 'only once'
|
209
|
+
interval = interval.rationalize unless interval.nil?
|
200
210
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
**parameters[:mode_args]
|
209
|
-
end
|
211
|
+
control = EveryControl.new @event_handlers.last,
|
212
|
+
duration: duration,
|
213
|
+
till: till,
|
214
|
+
condition: condition,
|
215
|
+
on_stop: on_stop,
|
216
|
+
after_bars: after_bars,
|
217
|
+
after: after
|
210
218
|
|
211
|
-
|
212
|
-
binterval = binterval.rationalize
|
219
|
+
@event_handlers.push control
|
213
220
|
|
214
|
-
|
215
|
-
@event_handlers.push control
|
221
|
+
_every interval, control, &block
|
216
222
|
|
217
|
-
|
223
|
+
@event_handlers.pop
|
218
224
|
|
219
|
-
|
225
|
+
@everying << control
|
220
226
|
|
221
|
-
|
227
|
+
control.after do
|
228
|
+
@everying.delete control
|
229
|
+
end
|
222
230
|
|
223
|
-
|
224
|
-
|
225
|
-
end
|
231
|
+
control
|
232
|
+
end
|
226
233
|
|
227
|
-
|
228
|
-
|
234
|
+
def move(every: nil,
|
235
|
+
from: nil, to: nil, step: nil,
|
236
|
+
duration: nil, till: nil,
|
237
|
+
function: nil,
|
238
|
+
right_open: nil,
|
239
|
+
on_stop: nil,
|
240
|
+
after_bars: nil,
|
241
|
+
after: nil,
|
242
|
+
&block)
|
229
243
|
|
230
|
-
|
231
|
-
|
244
|
+
control = _move every: every,
|
245
|
+
from: from, to: to, step: step,
|
246
|
+
duration: duration, till: till,
|
247
|
+
function: function,
|
248
|
+
right_open: right_open,
|
249
|
+
on_stop: on_stop,
|
250
|
+
after_bars: after_bars,
|
251
|
+
after: after,
|
252
|
+
&block
|
232
253
|
|
233
|
-
|
254
|
+
@moving << control
|
234
255
|
|
235
|
-
|
236
|
-
|
237
|
-
|
256
|
+
control.after do
|
257
|
+
@moving.delete control
|
258
|
+
end
|
238
259
|
|
239
|
-
|
240
|
-
|
260
|
+
control
|
261
|
+
end
|
241
262
|
|
242
|
-
|
243
|
-
|
244
|
-
|
263
|
+
def debug(msg = nil)
|
264
|
+
@logger.debug { msg || '...' }
|
265
|
+
end
|
245
266
|
|
246
|
-
|
247
|
-
|
267
|
+
def to_s
|
268
|
+
super + ": position=#{position}"
|
269
|
+
end
|
270
|
+
end
|
248
271
|
end
|
249
|
-
|
250
|
-
alias to_s inspect
|
251
272
|
end
|
273
|
+
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Musa
|
2
|
+
module Sequencer
|
3
|
+
class BaseSequencer
|
4
|
+
module TickBasedTiming
|
5
|
+
|
6
|
+
attr_reader :position, :ticks_per_bar, :tick_duration
|
7
|
+
|
8
|
+
def tick
|
9
|
+
if @hold_public_ticks
|
10
|
+
@hold_ticks += 1
|
11
|
+
else
|
12
|
+
_tick @position_mutex.synchronize { @position += @tick_duration }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def position=(new_position)
|
17
|
+
raise ArgumentError,
|
18
|
+
"Sequencer #{self}: cannot move back. current position: #{@position} new position: #{new_position}" \
|
19
|
+
if new_position < position
|
20
|
+
|
21
|
+
_hold_public_ticks
|
22
|
+
@on_fast_forward.each { |block| block.call(true) }
|
23
|
+
|
24
|
+
_tick(@position_mutex.synchronize { @position += @tick_duration }) while @position < new_position
|
25
|
+
|
26
|
+
@on_fast_forward.each { |block| block.call(false) }
|
27
|
+
_release_public_ticks
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def _init_timing
|
33
|
+
@ticks_per_bar = Rational(beats_per_bar * ticks_per_beat)
|
34
|
+
@tick_duration = Rational(1, @ticks_per_bar)
|
35
|
+
|
36
|
+
@hold_public_ticks = false
|
37
|
+
@hold_ticks = 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def _reset_timing
|
41
|
+
@position = @position_mutex.synchronize { 1r - @tick_duration }
|
42
|
+
end
|
43
|
+
|
44
|
+
def _check_position(position)
|
45
|
+
ticks_position = position / @tick_duration
|
46
|
+
|
47
|
+
if ticks_position.round != ticks_position
|
48
|
+
original_position = position
|
49
|
+
position = ticks_position.round * @tick_duration
|
50
|
+
|
51
|
+
@logger.warn('BaseSequencer') { "._numeric_at: rounding "\
|
52
|
+
"position #{original_position} (#{original_position.to_f.round(5)}) "\
|
53
|
+
"to tick precision: #{position} (#{position.to_f.round(5)})" }
|
54
|
+
end
|
55
|
+
|
56
|
+
position
|
57
|
+
end
|
58
|
+
|
59
|
+
def _quantize(position)
|
60
|
+
(position / @tick_duration).round * @tick_duration
|
61
|
+
end
|
62
|
+
|
63
|
+
def _hold_public_ticks
|
64
|
+
@hold_public_ticks = true
|
65
|
+
end
|
66
|
+
|
67
|
+
def _release_public_ticks
|
68
|
+
@hold_ticks.times { _tick(@position_mutex.synchronize { @position += @tick_duration }) }
|
69
|
+
@hold_ticks = 0
|
70
|
+
@hold_public_ticks = false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|