musa-dsl 0.22.5 → 0.23.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -1
  3. data/lib/musa-dsl.rb +14 -8
  4. data/lib/musa-dsl/core-ext/deep-copy.rb +12 -1
  5. data/lib/musa-dsl/core-ext/inspect-nice.rb +1 -2
  6. data/lib/musa-dsl/core-ext/smart-proc-binder.rb +13 -11
  7. data/lib/musa-dsl/datasets/p.rb +38 -15
  8. data/lib/musa-dsl/datasets/score/to-mxml/process-pdv.rb +14 -12
  9. data/lib/musa-dsl/datasets/score/to-mxml/process-ps.rb +32 -6
  10. data/lib/musa-dsl/datasets/score/to-mxml/to-mxml.rb +24 -10
  11. data/lib/musa-dsl/generative/backboner.rb +6 -11
  12. data/lib/musa-dsl/generative/generative-grammar.rb +1 -3
  13. data/lib/musa-dsl/generative/markov.rb +10 -6
  14. data/lib/musa-dsl/logger/logger.rb +6 -1
  15. data/lib/musa-dsl/midi/midi-voices.rb +8 -7
  16. data/lib/musa-dsl/music/scales.rb +1 -1
  17. data/lib/musa-dsl/neumalang/neumalang.rb +1 -1
  18. data/lib/musa-dsl/neumas/array-to-neumas.rb +1 -1
  19. data/lib/musa-dsl/sequencer/base-sequencer-implementation-play-helper.rb +2 -2
  20. data/lib/musa-dsl/sequencer/base-sequencer-implementation-play-timed.rb +2 -1
  21. data/lib/musa-dsl/sequencer/base-sequencer-implementation.rb +2 -0
  22. data/lib/musa-dsl/sequencer/base-sequencer-tick-based.rb +4 -2
  23. data/lib/musa-dsl/sequencer/sequencer-dsl.rb +6 -6
  24. data/lib/musa-dsl/series/base-series.rb +293 -144
  25. data/lib/musa-dsl/series/buffer-serie.rb +236 -0
  26. data/lib/musa-dsl/series/hash-or-array-serie-splitter.rb +145 -115
  27. data/lib/musa-dsl/series/main-serie-constructors.rb +249 -156
  28. data/lib/musa-dsl/series/main-serie-operations.rb +331 -318
  29. data/lib/musa-dsl/series/proxy-serie.rb +25 -41
  30. data/lib/musa-dsl/series/quantizer-serie.rb +38 -38
  31. data/lib/musa-dsl/series/queue-serie.rb +39 -43
  32. data/lib/musa-dsl/series/series-composer.rb +316 -0
  33. data/lib/musa-dsl/series/series.rb +5 -1
  34. data/lib/musa-dsl/series/timed-serie.rb +119 -130
  35. data/musa-dsl.gemspec +13 -3
  36. metadata +9 -9
  37. data/.ruby-version +0 -1
  38. data/lib/musa-dsl/series/holder-serie.rb +0 -87
@@ -1,15 +1,15 @@
1
1
  require_relative '../core-ext/arrayfy'
2
2
  require_relative '../core-ext/smart-proc-binder'
3
3
 
4
+ require_relative 'base-series'
5
+
4
6
  using Musa::Extension::Arrayfy
5
7
  using Musa::Extension::ExplodeRanges
6
8
 
7
9
  # TODO: añadir en for: steps: (nº de pasos en los que repartir el incremento)
8
10
 
9
11
  module Musa
10
- module Series
11
- extend self
12
-
12
+ module Series::Constructors
13
13
  def NIL
14
14
  NilSerie.new
15
15
  end
@@ -18,10 +18,6 @@ module Musa
18
18
  FromArray.new values.explode_ranges
19
19
  end
20
20
 
21
- def _SE(values, extends:)
22
- FromArray.new values.explode_ranges, extends
23
- end
24
-
25
21
  def H(**series_hash)
26
22
  FromHashOfSeries.new series_hash, false
27
23
  end
@@ -63,8 +59,8 @@ module Musa
63
59
  end
64
60
  end
65
61
 
66
- def MERGE(*values)
67
- Sequence.new(values)
62
+ def MERGE(*series)
63
+ Sequence.new(series)
68
64
  end
69
65
 
70
66
  def RND1(*values, from: nil, to: nil, step: nil, random: nil)
@@ -104,33 +100,35 @@ module Musa
104
100
  ###
105
101
 
106
102
  class NilSerie
107
- include Serie
108
- def _next_value
109
- nil
110
- end
103
+ include Musa::Series::Serie.base
104
+
105
+ def _next_value; nil; end
111
106
  end
112
107
 
113
108
  private_constant :NilSerie
114
109
 
115
110
  class FromArray
116
- include Serie
117
-
118
- attr_reader :values
111
+ include Musa::Series::Serie.base
119
112
 
120
113
  def initialize(values = nil, extends = nil)
121
- @values = values.clone.freeze
122
- @index = 0
114
+ @values = values
115
+ mark_as_prototype!
123
116
 
124
- extends.arrayfy.each { |e| extend(e) }
117
+ x = self
118
+ extends.arrayfy.each do |e|
119
+ x.extend(e)
120
+ end
125
121
 
126
- mark_as_prototype!
122
+ init
127
123
  end
128
124
 
129
- def _restart
125
+ attr_accessor :values
126
+
127
+ private def _init
130
128
  @index = 0
131
129
  end
132
130
 
133
- def _next_value
131
+ private def _next_value
134
132
  if @values && @index < @values.size
135
133
  value = @values[@index]
136
134
  @index += 1
@@ -142,31 +140,30 @@ module Musa
142
140
  end
143
141
  end
144
142
 
145
- private_constant :FromArray
143
+ # private_constant :FromArray
146
144
 
147
145
  class Sequence
148
- include Serie
149
-
150
- attr_reader :sources
146
+ include Musa::Series::Serie.with(sources: true)
151
147
 
152
148
  def initialize(series)
153
- @sources = if series[0].prototype?
154
- series.collect(&:prototype).freeze
155
- else
156
- series.collect(&:instance)
157
- end
158
-
159
- _restart false
149
+ self.sources = series
160
150
 
161
- mark_regarding! series[0]
151
+ init
162
152
  end
163
153
 
164
- def _restart(restart_sources = true)
154
+ attr_accessor :sources
155
+
156
+ private def _init
165
157
  @index = 0
166
- @sources[@index].restart if restart_sources
158
+ @restart_sources = false
167
159
  end
168
160
 
169
- def _next_value
161
+ private def _restart
162
+ @sources[0].restart
163
+ @restart_sources = true
164
+ end
165
+
166
+ private def _next_value
170
167
  value = nil
171
168
 
172
169
  if @index < @sources.size
@@ -175,7 +172,7 @@ module Musa
175
172
  if value.nil?
176
173
  @index += 1
177
174
  if @index < @sources.size
178
- @sources[@index].restart
175
+ @sources[@index].restart if @restart_sources
179
176
  value = next_value
180
177
  end
181
178
  end
@@ -192,27 +189,40 @@ module Musa
192
189
  private_constant :Sequence
193
190
 
194
191
  class FromEvalBlockWithParameters
192
+ include Musa::Series::Serie.with(smart_block: true)
193
+
195
194
  using Musa::Extension::DeepCopy
196
195
 
197
- include Serie
198
- include Musa::Extension::SmartProcBinder
196
+ def initialize(*parameters, **key_parameters, &block)
197
+ raise ArgumentError, 'Yield block is undefined' unless block
199
198
 
200
- attr_reader :block
199
+ @original_value_parameters = parameters
200
+ @original_key_parameters = key_parameters
201
201
 
202
- def initialize(*values, **key_values, &block)
203
- raise ArgumentError, 'Yield block is undefined' unless block
202
+ self.proc = block
204
203
 
205
- @original_value_parameters = values
206
- @original_key_parameters = key_values
204
+ mark_as_prototype!
207
205
 
208
- @block = SmartProcBinder.new(block)
206
+ init
207
+ end
209
208
 
210
- _restart
209
+ def parameters
210
+ @original_value_parameters
211
+ end
211
212
 
212
- mark_as_prototype!
213
+ def parameters=(values)
214
+ @original_value_parameters = values
215
+ end
216
+
217
+ def key_parameters
218
+ @original_key_parameters
213
219
  end
214
220
 
215
- def _restart
221
+ def key_parameters=(key_values)
222
+ @original_key_parameters = key_values
223
+ end
224
+
225
+ private def _init
216
226
  @value_parameters = @original_value_parameters.clone(deep: true)
217
227
  @key_parameters = @original_key_parameters.clone(deep: true)
218
228
 
@@ -220,9 +230,9 @@ module Musa
220
230
  @value = nil
221
231
  end
222
232
 
223
- def _next_value
233
+ private def _next_value
224
234
  @value = if !@value.nil? || @value.nil? && @first
225
- @value = @block.call(*@value_parameters, last_value: @value, **@key_parameters)
235
+ @value = @block.call(*@value_parameters, last_value: @value, caller: self, **@key_parameters)
226
236
  else
227
237
  nil
228
238
  end
@@ -235,9 +245,7 @@ module Musa
235
245
  private_constant :FromEvalBlockWithParameters
236
246
 
237
247
  class ForLoop
238
- include Serie
239
-
240
- attr_reader :from, :to, :step
248
+ include Musa::Series::Serie.base
241
249
 
242
250
  def initialize(from, to, step)
243
251
  @from = from
@@ -246,22 +254,39 @@ module Musa
246
254
 
247
255
  sign_adjust_step
248
256
 
249
- _restart
250
-
251
257
  mark_as_prototype!
258
+
259
+ init
252
260
  end
253
261
 
254
- def _restart
255
- @value = @from
262
+ attr_reader :from, :to, :step
263
+
264
+ def from=(value)
265
+ @from = value
266
+ sign_adjust_step
256
267
  end
257
268
 
258
- def _next_value
269
+ def to=(value)
270
+ @to = value
271
+ sign_adjust_step
272
+ end
273
+
274
+ def step=(value)
275
+ @step = value
276
+ sign_adjust_step
277
+ end
278
+
279
+ private def _init
280
+ @value = @from - @step
281
+ end
282
+
283
+ private def _next_value
259
284
  if @value
260
- value = @value
261
285
  @value += @step
286
+ value = @value
262
287
  end
263
288
 
264
- @value = nil if @to && @value && (@value > @to && @step.positive? || @value < @to && @step.negative?)
289
+ @value = nil if @to && @value && (@value >= @to && @step.positive? || @value <= @to && @step.negative?)
265
290
 
266
291
  value
267
292
  end
@@ -278,24 +303,24 @@ module Musa
278
303
  private_constant :ForLoop
279
304
 
280
305
  class RandomValueFromArray
281
- include Serie
282
-
283
- attr_reader :values, :random
306
+ include Musa::Series::Serie.base
284
307
 
285
308
  def initialize(values, random)
286
- @values = values.clone.freeze
309
+ @values = values
287
310
  @random = random
288
311
 
289
- _restart
290
-
291
312
  mark_as_prototype!
313
+
314
+ init
292
315
  end
293
316
 
294
- def _restart
317
+ attr_accessor :values, :random
318
+
319
+ private def _init
295
320
  @value = nil
296
321
  end
297
322
 
298
- def _next_value
323
+ private def _next_value
299
324
  if @value
300
325
  nil
301
326
  else
@@ -307,9 +332,7 @@ module Musa
307
332
  private_constant :RandomValueFromArray
308
333
 
309
334
  class RandomNumberFromRange
310
- include Serie
311
-
312
- attr_reader :from, :to, :step, :random
335
+ include Musa::Series::Serie.base
313
336
 
314
337
  def initialize(from, to, step, random)
315
338
  @from = from
@@ -320,16 +343,39 @@ module Musa
320
343
 
321
344
  @random = random
322
345
 
323
- _restart
324
-
325
346
  mark_as_prototype!
347
+
348
+ init
326
349
  end
327
350
 
328
- def _restart
351
+ attr_reader :from
352
+
353
+ def from=(value)
354
+ @from = value
355
+ sign_adjust_step
356
+ end
357
+
358
+ attr_reader :to
359
+
360
+ def to=(value)
361
+ @to = value
362
+ sign_adjust_step
363
+ end
364
+
365
+ attr_reader :step
366
+
367
+ def step=(value)
368
+ @step = value
369
+ sign_adjust_step
370
+ end
371
+
372
+ attr_accessor :random
373
+
374
+ private def _init
329
375
  @value = nil
330
376
  end
331
377
 
332
- def _next_value
378
+ private def _next_value
333
379
  if @value
334
380
  nil
335
381
  else
@@ -346,24 +392,25 @@ module Musa
346
392
  private_constant :RandomNumberFromRange
347
393
 
348
394
  class RandomValuesFromArray
349
- include Serie
350
-
351
- attr_reader :values, :random
395
+ include Musa::Series::Serie.base
352
396
 
353
397
  def initialize(values, random)
354
398
  @values = values.clone.freeze
355
399
  @random = random
356
400
 
357
- _restart
358
-
359
401
  mark_as_prototype!
402
+
403
+ init
360
404
  end
361
405
 
362
- def _restart
406
+ attr_accessor :values
407
+ attr_accessor :random
408
+
409
+ private def _init
363
410
  @available_values = @values.dup
364
411
  end
365
412
 
366
- def _next_value
413
+ private def _next_value
367
414
  value = nil
368
415
  unless @available_values.empty?
369
416
  i = @random.rand(0...@available_values.size)
@@ -377,9 +424,7 @@ module Musa
377
424
  private_constant :RandomValuesFromArray
378
425
 
379
426
  class RandomNumbersFromRange
380
- include Serie
381
-
382
- attr_reader :from, :to, :step, :random
427
+ include Musa::Series::Serie.base
383
428
 
384
429
  def initialize(from, to, step, random)
385
430
  @from = from
@@ -390,16 +435,39 @@ module Musa
390
435
 
391
436
  @random = random
392
437
 
393
- _restart
394
-
395
438
  mark_as_prototype!
439
+
440
+ init
396
441
  end
397
442
 
398
- def _restart
443
+ attr_reader :from
444
+
445
+ def from=(value)
446
+ @from = value
447
+ adjust_step
448
+ end
449
+
450
+ attr_reader :to
451
+
452
+ def to=(value)
453
+ @to = value
454
+ adjust_step
455
+ end
456
+
457
+ attr_reader :step
458
+
459
+ def step=(value)
460
+ @step = value
461
+ adjust_step
462
+ end
463
+
464
+ attr_accessor :random
465
+
466
+ private def _init
399
467
  @available_steps = (0..@step_count).to_a
400
468
  end
401
469
 
402
- def _next_value
470
+ private def _next_value
403
471
  value = nil
404
472
  unless @available_steps.empty?
405
473
  i = @random.rand(0...@available_steps.size)
@@ -418,37 +486,27 @@ module Musa
418
486
  private_constant :RandomNumbersFromRange
419
487
 
420
488
  class FromHashOfSeries
421
- include Serie
422
-
423
- attr_reader :sources, :cycle
489
+ include Musa::Series::Serie.with(sources: true)
424
490
 
425
491
  def initialize(hash_of_series, cycle_all_series)
426
- @sources = hash_of_series.clone.transform_values(&:prototype).freeze
427
- @cycle = cycle_all_series
492
+ self.sources = hash_of_series
493
+ self.cycle = cycle_all_series
428
494
 
429
- _restart false
430
-
431
- mark_as_prototype!
495
+ init
432
496
  end
433
497
 
434
- def _restart(restart_sources = true)
498
+ attr_accessor :cycle
499
+
500
+ private def _init
435
501
  @have_current = false
436
502
  @value = nil
437
-
438
- if restart_sources
439
- @sources.each do |_key, serie|
440
- serie.restart
441
- end
442
- end
443
503
  end
444
504
 
445
- def _soft_restart
446
- @sources.each do |_key, serie|
447
- serie.restart if serie.current_value.nil?
448
- end
505
+ private def _restart
506
+ @sources.values.each(&:restart)
449
507
  end
450
508
 
451
- def _next_value
509
+ private def _next_value
452
510
  unless @have_current && @value.nil?
453
511
  pre_value = @sources.collect { |key, serie| [key, serie.peek_next_value] }.to_h
454
512
 
@@ -476,43 +534,39 @@ module Musa
476
534
 
477
535
  @value
478
536
  end
537
+
538
+ private def _soft_restart
539
+ @sources.each do |_key, serie|
540
+ serie.restart if serie.current_value.nil?
541
+ end
542
+ end
479
543
  end
480
544
 
481
545
  private_constant :FromHashOfSeries
482
546
 
483
547
  class FromArrayOfSeries
484
- include Serie
485
-
486
- attr_reader :sources, :cycle
548
+ include Musa::Series::Serie.with(sources: true)
487
549
 
488
550
  def initialize(series_array, cycle_all_series)
489
- @sources = series_array.collect(&:prototype).freeze
490
- @cycle = cycle_all_series
491
-
492
- _restart false
551
+ self.sources = series_array
552
+ self.cycle = cycle_all_series
493
553
 
494
- mark_as_prototype!
554
+ init
495
555
  end
496
556
 
497
- def _restart(restart_sources = true)
557
+ attr_accessor :cycle
558
+
559
+ private def _init
498
560
  @have_current = false
499
561
  @value = nil
500
-
501
- if restart_sources
502
- @sources.each do |serie|
503
- serie.restart if serie.current_value.nil?
504
- end
505
- end
506
562
  end
507
563
 
508
- def _soft_restart
509
- @sources.each do |serie|
510
- serie.restart if serie.current_value.nil?
511
- end
564
+ private def _restart
565
+ @sources.each(&:restart)
512
566
  end
513
567
 
514
- def _next_value
515
- unless @have_current && @value.nil?
568
+ private def _next_value
569
+ unless @sources.empty? || @have_current && @value.nil?
516
570
  pre_value = @sources.collect(&:peek_next_value)
517
571
 
518
572
  nils = 0
@@ -539,14 +593,18 @@ module Musa
539
593
 
540
594
  @value
541
595
  end
596
+
597
+ private def _soft_restart
598
+ @sources.each do |serie|
599
+ serie.restart if serie.current_value.nil?
600
+ end
601
+ end
542
602
  end
543
603
 
544
604
  private_constant :FromArrayOfSeries
545
605
 
546
606
  class SinFunction
547
- include Serie
548
-
549
- attr_reader :start, :steps, :amplitude, :center
607
+ include Musa::Series::Serie.base
550
608
 
551
609
  def initialize(start, steps, amplitude, center)
552
610
  @start = start.to_f
@@ -557,16 +615,44 @@ module Musa
557
615
 
558
616
  update
559
617
 
560
- _restart
561
-
562
618
  mark_as_prototype!
619
+
620
+ init
621
+ end
622
+
623
+ attr_reader :start
624
+
625
+ def start=(value)
626
+ @start = value.to_f
627
+ update
628
+ end
629
+
630
+ attr_reader :steps
631
+
632
+ def steps=(value)
633
+ @steps = value
634
+ update
635
+ end
636
+
637
+ attr_reader :amplitude
638
+
639
+ def amplitude=(value)
640
+ @amplitude = value
641
+ update
642
+ end
643
+
644
+ attr_reader :center
645
+
646
+ def center=(value)
647
+ @center = value
648
+ update
563
649
  end
564
650
 
565
- def _restart
651
+ private def _init
566
652
  @position = 0
567
653
  end
568
654
 
569
- def _next_value
655
+ private def _next_value
570
656
  value = nil
571
657
  unless @position >= @steps
572
658
  value = Math.sin(@offset + @step_size * @position) * (@amplitude / 2.0) + @center
@@ -575,13 +661,7 @@ module Musa
575
661
  value
576
662
  end
577
663
 
578
- def to_s
579
- "offset: #{@offset.round(3)}rd amplitude: #{@amplitude.round(3)} center: #{@center.round(3)} length: #{@length} step_size: #{@step_size.round(6)}"
580
- end
581
-
582
- private
583
-
584
- def update
664
+ private def update
585
665
  y = 2 * (@start - @center) / @amplitude
586
666
  warn "WARNING: value for offset calc #{y} is outside asin range" if y < -1 || y > 1
587
667
  y = 1.0 if y > 1.0 # por los errores de precisión infinitesimal en el cálculo de y cuando es muy próximo a 1.0
@@ -590,25 +670,28 @@ module Musa
590
670
  @offset = Math.asin(y)
591
671
  @step_size = 2.0 * Math::PI / @steps
592
672
  end
673
+
674
+ def to_s
675
+ "offset: #{@offset.round(3)}rd amplitude: #{@amplitude.round(3)} center: #{@center.round(3)} length: #{@length} step_size: #{@step_size.round(6)}"
676
+ end
593
677
  end
594
678
 
595
679
  private_constant :SinFunction
596
680
 
597
681
  class Fibonacci
598
- include Serie
682
+ include Musa::Series::Serie.base
599
683
 
600
684
  def initialize
601
- _restart
602
-
603
685
  mark_as_prototype!
686
+ init
604
687
  end
605
688
 
606
- def _restart
689
+ private def _init
607
690
  @a = 0
608
691
  @b = 1
609
692
  end
610
693
 
611
- def _next_value
694
+ private def _next_value
612
695
  initial_b = @b
613
696
  @b = @a + @b
614
697
  @a = initial_b
@@ -624,20 +707,30 @@ module Musa
624
707
  private_constant :Fibonacci
625
708
 
626
709
  class HarmonicNotes
627
- include Serie
628
-
629
- attr_reader :error, :extended
710
+ include Musa::Series::Serie.base
630
711
 
631
712
  def initialize(error, extended)
632
713
  @error = error
633
714
  @extended = extended
634
715
 
635
- _restart
636
-
637
716
  mark_as_prototype!
717
+
718
+ init
719
+ end
720
+
721
+ attr_reader :error
722
+
723
+ def error=(value)
724
+ @error = value
725
+ end
726
+
727
+ attr_reader :extended
728
+
729
+ def extended=(value)
730
+ @extended = value
638
731
  end
639
732
 
640
- def _restart
733
+ def _init
641
734
  @harmonic = 0
642
735
  end
643
736