Class: Musa::Sequencer::BaseSequencer

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/sequencer/timeslots.rb,
lib/musa-dsl/sequencer/base-sequencer.rb,
lib/musa-dsl/sequencer/base-sequencer-tick-based.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation.rb,
lib/musa-dsl/sequencer/base-sequencer-tickless-based.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation-move.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation-play.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation-play-timed.rb,
lib/musa-dsl/sequencer/base-sequencer-implementation-play-helper.rb

Defined Under Namespace

Modules: TickBasedTiming, TicklessBasedTiming Classes: Timeslots

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beats_per_bar = nil, ticks_per_beat = nil, offset: nil, logger: nil, do_log: nil, do_error_log: nil, log_position_format: nil) ⇒ BaseSequencer

Returns a new instance of BaseSequencer.



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
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 21

def initialize(beats_per_bar = nil, ticks_per_beat = nil,
               offset: nil,
               logger: nil,
               do_log: nil, do_error_log: nil, log_position_format: nil)

  unless beats_per_bar && ticks_per_beat || beats_per_bar.nil? && ticks_per_beat.nil?
    raise ArgumentError, "'beats_per_bar' and 'ticks_per_beat' parameters should be both nil or both have values"
  end

  if logger
    @logger = logger
  else
    @logger = Musa::Logger::Logger.new(sequencer: self, position_format: log_position_format)

    @logger.fatal!
    @logger.error! if do_error_log || do_error_log.nil?
    @logger.debug! if do_log
  end

  @offset = offset || 0r

  if beats_per_bar && ticks_per_beat
    @beats_per_bar = Rational(beats_per_bar)
    @ticks_per_beat = Rational(ticks_per_beat)

    singleton_class.include TickBasedTiming
  else
    singleton_class.include TicklessBasedTiming
  end

  _init_timing

  @on_debug_at = []
  @on_error = []

  @before_tick = []
  @on_fast_forward = []

  @tick_mutex = Mutex.new
  @position_mutex = Mutex.new

  @timeslots = Timeslots.new

  @everying = []
  @playing = []
  @moving = []

  reset
end

Instance Attribute Details

#beats_per_barObject (readonly)

Returns the value of attribute beats_per_bar.



15
16
17
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 15

def beats_per_bar
  @beats_per_bar
end

#everyingObject (readonly)

Returns the value of attribute everying.



18
19
20
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 18

def everying
  @everying
end

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 19

def logger
  @logger
end

#movingObject (readonly)

Returns the value of attribute moving.



18
19
20
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 18

def moving
  @moving
end

#offsetObject (readonly)

Returns the value of attribute offset.



16
17
18
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 16

def offset
  @offset
end

#playingObject (readonly)

Returns the value of attribute playing.



18
19
20
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 18

def playing
  @playing
end

#running_positionObject (readonly)

Returns the value of attribute running_position.



17
18
19
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 17

def running_position
  @running_position
end

#ticks_per_beatObject (readonly)

Returns the value of attribute ticks_per_beat.



15
16
17
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 15

def ticks_per_beat
  @ticks_per_beat
end

Instance Method Details

#_rescue_error(e) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation.rb', line 120

def _rescue_error(e)
  @logger.error('BaseSequencer') { e.to_s }
  @logger.error('BaseSequencer') { e.full_message(highlight: true, order: :top) }

  @on_error.each do |block|
    block.call e
  end
end

#at(bar_position, debug: nil, &block) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 164

def at(bar_position, debug: nil, &block)
  debug ||= false

  control = EventHandler.new @event_handlers.last
  @event_handlers.push control

  if bar_position.is_a? Numeric
    _numeric_at bar_position.rationalize, control, debug: debug, &block
  else
    bar_position = Series::S(*bar_position) if bar_position.is_a? Array
    bar_position = bar_position.instance if bar_position

    _serie_at bar_position, control, debug: debug, &block
  end

  @event_handlers.pop

  control
end

#before_tick(&block) ⇒ Object



115
116
117
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 115

def before_tick(&block)
  @before_tick << Musa::Extension::SmartProcBinder::SmartProcBinder.new(block)
end

#continuation_play(parameters) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 211

def continuation_play(parameters)
  _play parameters[:serie],
        parameters[:control],
        parameters[:neumalang_context],
        mode: parameters[:mode],
        decoder: parameters[:decoder],
        __play_eval: parameters[:play_eval],
        **parameters[:mode_args]
end

#debug(msg = nil) ⇒ Object



315
316
317
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 315

def debug(msg = nil)
  @logger.debug { msg || '...' }
end

#empty?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 86

def empty?
  @timeslots.empty?
end

#event_handlerObject



99
100
101
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 99

def event_handler
  @event_handlers.last
end

#every(interval, duration: nil, till: nil, condition: nil, on_stop: nil, after_bars: nil, after: nil, &block) ⇒ Object



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
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 253

def every(interval,
          duration: nil, till: nil,
          condition: nil,
          on_stop: nil,
          after_bars: nil, after: nil,
          &block)

  # nil interval means 'only once'
  interval = interval.rationalize unless interval.nil?

  control = EveryControl.new @event_handlers.last,
                             duration: duration,
                             till: till,
                             condition: condition,
                             on_stop: on_stop,
                             after_bars: after_bars,
                             after: after

  @event_handlers.push control

  _every interval, control, &block

  @event_handlers.pop

  @everying << control

  control.after do
    @everying.delete control
  end

  control
end

#launch(event, *value_parameters, **key_parameters) ⇒ Object



123
124
125
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 123

def launch(event, *value_parameters, **key_parameters)
  @event_handlers.last.launch event, *value_parameters, **key_parameters
end

#move(every: nil, from: nil, to: nil, step: nil, duration: nil, till: nil, function: nil, right_open: nil, on_stop: nil, after_bars: nil, after: nil, &block) ⇒ Object



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
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 286

def move(every: nil,
         from: nil, to: nil, step: nil,
         duration: nil, till: nil,
         function: nil,
         right_open: nil,
         on_stop: nil,
         after_bars: nil,
         after: nil,
         &block)

  control = _move every: every,
                  from: from, to: to, step: step,
                  duration: duration, till: till,
                  function: function,
                  right_open: right_open,
                  on_stop: on_stop,
                  after_bars: after_bars,
                  after: after,
                  &block

  @moving << control

  control.after do
    @moving.delete control
  end

  control
end

#now(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 147

def now(&block)
  control = EventHandler.new @event_handlers.last
  @event_handlers.push control

  _numeric_at position, control, &block

  @event_handlers.pop

  control
end

#on(event, &block) ⇒ Object



119
120
121
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 119

def on(event, &block)
  @event_handlers.last.on event, &block
end

#on_debug_at(&block) ⇒ Object



103
104
105
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 103

def on_debug_at(&block)
  @on_debug_at << Musa::Extension::SmartProcBinder::SmartProcBinder.new(block)
end

#on_error(&block) ⇒ Object



107
108
109
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 107

def on_error(&block)
  @on_error << Musa::Extension::SmartProcBinder::SmartProcBinder.new(block)
end

#on_fast_forward(&block) ⇒ Object



111
112
113
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 111

def on_fast_forward(&block)
  @on_fast_forward << Musa::Extension::SmartProcBinder::SmartProcBinder.new(block)
end

#play(serie, mode: nil, parameter: nil, after_bars: nil, after: nil, context: nil, **mode_args, &block) ⇒ Object



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
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 184

def play(serie,
         mode: nil,
         parameter: nil,
         after_bars: nil,
         after: nil,
         context: nil,
         **mode_args,
         &block)

  mode ||= :wait

  control = PlayControl.new @event_handlers.last, after_bars: after_bars, after: after
  @event_handlers.push control

  _play serie.instance, control, context, mode: mode, parameter: parameter, **mode_args, &block

  @event_handlers.pop

  @playing << control

  control.after do
    @playing.delete control
  end

  control
end

#play_timed(timed_serie, at: nil, on_stop: nil, after_bars: nil, after: nil, &block) ⇒ Object



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
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 221

def play_timed(timed_serie,
               at: nil,
               on_stop: nil,
               after_bars: nil, after: nil,
               &block)

  at ||= position

  control = PlayTimedControl.new(@event_handlers.last,
                                 on_stop: on_stop, after_bars: after_bars, after: after)

  control.on_stop do
    control.do_after.each do |do_after|
      _numeric_at position + do_after[:bars], control, &do_after[:block]
    end
  end

  @event_handlers.push control

  _play_timed(timed_serie.instance, at, control, &block)

  @event_handlers.pop

  @playing << control

  control.after do
    @playing.delete control
  end

  control
end

#quantize_position(position, warn: nil) ⇒ Object



90
91
92
93
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 90

def quantize_position(position, warn: nil)
  warn ||= false
  _quantize_position(position, warn: warn)
end

#raw_at(bar_position, force_first: nil, &block) ⇒ Object



158
159
160
161
162
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 158

def raw_at(bar_position, force_first: nil, &block)
  _raw_numeric_at bar_position.rationalize, force_first: force_first, &block

  nil
end

#resetObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 71

def reset
  @timeslots.clear
  @everying.clear
  @playing.clear
  @moving.clear

  @event_handlers = [EventHandler.new]

  _reset_timing
end

#runObject



95
96
97
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 95

def run
  tick until empty?
end

#sizeObject



82
83
84
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 82

def size
  @timeslots.values.sum(&:size)
end

#to_sObject



319
320
321
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 319

def to_s
  super + ": position=#{position}"
end

#wait(bars_delay, debug: nil, &block) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/musa-dsl/sequencer/base-sequencer.rb', line 127

def wait(bars_delay, debug: nil, &block)
  debug ||= false

  control = EventHandler.new @event_handlers.last
  @event_handlers.push control

  if bars_delay.is_a? Numeric
    _numeric_at position + bars_delay.rationalize, control, debug: debug, &block
  else
    bars_delay = Series::S(*bars_delay) if bars_delay.is_a?(Array)
    bars_delay = bars_delay.instance if bars_delay

    _serie_at bars_delay.with { |delay| position + delay }, control, debug: debug, &block
  end

  @event_handlers.pop

  control
end