-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
softsynthopl2.py
1289 lines (870 loc) · 35.6 KB
/
softsynthopl2.py
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# AKAI Synth - SoundBlaster FM based synthesizer with AKAI controller
#
# OPL2-type software synthesizer
#
# Author: Stefan Haun <[email protected]>
#
# SPDX-License-Identifier: MIT
# License-Filename: LICENSES/MIT.txt
import mido
import sounddevice
import math
import copy
import numpy as np
from scipy import signal
import threading
# local modules
from midiproc import MidiMessageProcessorBase
from knobpanel import KnobPanelListener
from dispatchpanel import DispatchPanelListener
import dispatchpanel
from waveoutput import WaveSource
from waveoutput import WaveSink
# Source: https://upload.wikimedia.org/wikipedia/commons/a/ad/Piano_key_frequencies.png
SCALE_TONE_FREQUENCIES = [
261.626, # C4 (Middle C)
277.183, # C#4 / Db4
293.665, # D4
311.127, # D#4 / Eb4
329.628, # E4
349.228, # F4
369.994, # F#4 / Gb4
391.995, # G4
415.305, # G#4 / Ab4
440.000, # A4
466.164, # A#4, Bb4
493.883, # B
]
class FixedWaveSequencer(WaveSource):
"""Sequence a fixed wave once"""
def __init__(self,
samples,
blocksize,
loop_once=False):
super(FixedWaveSequencer,
self).__init__(blocksize=blocksize)
self.samples = np.array(samples)
self.idx = 0
return
# Note: using python generators sample-wise is too slow!!
def get(self):
"""return the next self.blocksize samples"""
wave = np.array([], dtype='float64')
if self.idx < len(self.samples):
e = self.idx + self.get_blocksize()
if e > len(self.samples):
e = len(self.samples)
wave = np.append(wave, self.samples[self.idx:e])
self.idx = e
# calculate remaining samples
remain = self.get_blocksize() - len(wave)
if remain > 0:
# fill with silence
wave = np.append(wave,
np.array([0] * remain, dtype='float64'))
return wave
def finished(self):
"""State if the sequencer is done with its samples"""
return self.idx >= len(self.samples)
class FixedWaveLoopSequencer(WaveSource):
"""Loop through a fixed wave"""
def __init__(self,
samples,
blocksize):
super(FixedWaveLoopSequencer,
self).__init__(blocksize=blocksize)
self.samples = np.array(samples)
# repeat samples to at least match the block size,
# so that there is at most one wrap-around during get
while len(self.samples) < self.get_blocksize():
self.samples = np.append(self.samples,
samples)
self.idx = 0
return
# Note: using python generators sample-wise is too slow!!
def get(self):
"""return the next self.blocksize samples"""
wave = np.array([], dtype='float64')
# add up tp blocksize samples to the wave, if available
e = self.idx + self.get_blocksize()
if e > len(self.samples):
e = len(self.samples)
wave = np.append(wave, self.samples[self.idx:e])
self.idx = e
# calculate remaining samples
remain = self.get_blocksize() - len(wave)
if remain > 0:
# remain is guaranteed to be < len(samples)
wave = np.append(wave,
self.samples[0:remain])
# update index
self.idx = remain
return wave
class EnvelopeParameters:
"""Parameters for wave envelope"""
def __init__(self):
super().__init__()
# Attach time in ms
self.attack = 0
# Decay time in ms
self.decay = 0
# Sustain niveau in % of attack amplitude
self.sustain = 0
# Release time in ms
self.release = 0
# True if note can be sustained
self.hold = False
return
def get_attack(self):
return self.attack
def set_attack(self, value):
old = self.attack
self.attack = value
return old
def get_decay(self):
return self.decay
def set_decay(self, value):
old = self.decay
self.decay = value
return old
def get_sustain(self):
return self.sustain
def set_sustain(self, value):
old = self.sustain
self.sustain = value
return old
def get_release(self):
return self.release
def set_release(self, value):
old = self.release
self.release = value
return old
def is_hold(self):
return self.hold
def set_hold(self, value):
old = self.hold
self.hold = value
return old
class EnvelopeState:
def __init__(self):
self.phase = EnvelopeSequencer.PHASE_INIT
self.idx = 0
self.struck = False
self.released = True
self.amp = 0
return
class EnvelopeSequencer(WaveSource):
PHASE_INIT = 0
PHASE_ATTACK = 1
PHASE_DECAY = 2
PHASE_SUSTAIN = 3
PHASE_RELEASE = 4
PHASE_TUNEDOWN = 5
PHASE_DONE = 6
def __init__(self,
generator,
blocksize = 441,
samplerate = 44100,
phase_callback = None,
done_callback = None):
super(EnvelopeSequencer,
self).__init__(blocksize=blocksize,
samplerate=samplerate,
done_callback=done_callback)
self.generator = generator
self.phase_callback = phase_callback
self.state = EnvelopeState()
return
def __del__(self):
pass
def get(self):
fragment = None
if self.generator is None:
fragement = np.array([], dtype='float64')
else:
# store old state
_state = copy.deepcopy(self.state)
fragment = self.generator.generate(self.state, self.get_blocksize())
# handle the strike flag
if self.state.struck == True:
if self.state.phase not in [EnvelopeSequencer.PHASE_INIT,
EnvelopeSequencer.PHASE_DONE]:
#print(fragment)
r = int(self.get_samplerate() * 0.007)
if r > self.get_blocksize():
r = self.get_blocksize()
fragment = np.append(np.linspace(fragment[0], 0, num=r), [0]*(self.get_blocksize()-r))
print("setting linspace from {0} to {1}".format(fragment[0], fragment[-1]))
#print(fragment)
self.state.phase = EnvelopeSequencer.PHASE_ATTACK
self.state.idx = 0
self.state.released = False
# reset the strike flag
self.state.struck = False
# handle callbacks
if _state.phase != self.state.phase:
if self.phase_callback is not None:
self.phase_callback(self, _state.phase, self.state.phase)
if self.state.phase == EnvelopeSequencer.PHASE_DONE:
self.done()
if len(fragment) != self.get_blocksize():
print("FRAGMENT LENGTH MISMATCH:", len(fragment))
return fragment
def reset(self):
self.state.phase = EnvelopeSequencer.PHASE_INIT
self.state.idx = 0
return
def strike(self):
self.state.struck = True
return
def release(self):
self.state.released = True
return
def tunedown(self):
self.state.phase = EnvelopeSequencer.PHASE_TUNEDOWN
def is_finished(self):
return self.state.phase == EnvelopeSequencer.PHASE_DONE
class EnvelopeGenerator:
def __init__(self,
samplerate = 44100,
parameter_callback = None):
super().__init__()
self.samplerate = samplerate
self.parameter_callback = parameter_callback
self.set_parameters(EnvelopeParameters())
self.env_strike = None
self.env_release = None
return
def __del__(self):
pass
def get_parameters(self):
return self.p
def set_parameters(self, parameters):
self.p = parameters
self.cache_attack = np.array([], dtype='float64')
self.cache_decay = np.array([], dtype='float64')
self.cache_release = np.array([], dtype='float64')
return
def generate(self,
state,
blocksize):
n_attack = math.ceil(self.samplerate * self.p.get_attack())
n_decay = math.ceil(self.samplerate * self.p.get_decay())
n_release = math.ceil(self.samplerate * self.p.get_release())
wave = np.array([], dtype='float64')
# Init phase is handled together with the Done phase
# Intermediate phase transitions do not have an effect here,
# as the index idx is not moved, except when all envelope
# parameters are zero, then we transistion to Done (which
# still would not make a difference.
# Handle Attack phase
if state.phase == EnvelopeSequencer.PHASE_ATTACK:
wave, self.cache_attack, state.idx = self._append_env_fragment(
wave, self.cache_attack,
n_attack, state.idx,
0, 1,
blocksize)
# Check phase transition
if state.idx == n_attack:
state.phase = EnvelopeSequencer.PHASE_DECAY
state.idx = 0
# Handle Decay phase
if state.phase == EnvelopeSequencer.PHASE_DECAY:
wave, self.cache_decay, state.idx = self._append_env_fragment(
wave, self.cache_decay,
n_decay, state.idx,
1, self.p.get_sustain(),
blocksize)
# Check phase transistion
if state.idx == n_decay:
state.phase = EnvelopeSequencer.PHASE_SUSTAIN if self.p.is_hold() else EnvelopeSequencer.PHASE_RELEASE
state.idx = 0
# Stustain phase
if state.phase == EnvelopeSequencer.PHASE_SUSTAIN:
# pad with the sustain value
# use a linear space so that in-process changes of the amplitude to not create cracks
_fragment = np.linspace(state.amp,
self.p.get_sustain(),
blocksize - len(wave))
wave = np.append(wave, _fragment)
# index does not matter here
# Check phase transition
if state.released == True:
state.phase = EnvelopeSequencer.PHASE_RELEASE
# this phase can only be left via outside intervention
# Release phase
if state.phase == EnvelopeSequencer.PHASE_RELEASE:
wave, self.cache_release, state.idx = self._append_env_fragment(
wave, self.cache_release,
n_release, state.idx,
self.p.get_sustain(), 0,
blocksize)
# Check phase transistion
if state.idx == n_release:
state.phase = EnvelopeSequencer.PHASE_TUNEDOWN
state.idx = 0
# Tunedown phase
if state.phase == EnvelopeSequencer.PHASE_TUNEDOWN:
# complete tunedown in 7 ms
loss = 1/7
# calculate loss per sample
samples_p_ms = self.samplerate / 1000
loss_per_sample = loss / samples_p_ms
# how many samples to tune down?
td_samples = math.ceil(state.amp / loss_per_sample)
# calculate fragment length within remaining block
fragment_length = blocksize-len(wave)
if td_samples > fragment_length:
td_samples = fragment_length
a1 = state.amp
a2 = state.amp - (loss_per_sample * td_samples)
if a2 < 0:
a2 = 0
_fragment = np.linspace(a1, a2, num=td_samples)
wave = np.append(wave, _fragment)
# Check phase transition
if a2 == 0:
state.phase = EnvelopeSequencer.PHASE_DONE
# Init or Done Phase
if state.phase in [EnvelopeSequencer.PHASE_INIT, EnvelopeSequencer.PHASE_DONE]:
# pad with silence
_fragment = [0] * (blocksize - len(wave))
wave = np.append(wave, _fragment)
# index does not matter here
# these phases can only be left via outside intervention
# store the last generated amplitude
state.amp = wave[-1]
return wave
def min_envelope_samples(self):
# duplicate the calculation from gen!
return math.ceil(self.samplerate * self.p.get_attack()) + math.ceil(self.samplerate * self.p.get_decay()) + math.ceil(self.samplerate * self.p.get_release())
def _append_env_fragment(self,
wave, cache,
num, idx,
val_start, val_end,
blocksize):
# choose end index based on samples left over
_end = idx + blocksize - len(wave)
if _end > num:
_end = num
# check cache
if len(cache) < _end:
_f = self._calculate_env_fragment(val_start, val_end,
num,
len(cache) - 1, _end)
cache = np.append(cache, _f)
_fragment = cache[idx:_end]
if len(wave) > 0:
wave = np.append(wave, _fragment)
else:
wave = _fragment
idx = _end
return [wave, cache, idx]
def _calculate_env_fragment(self,
val_start, val_end,
num, # length of whole segment
idx_start, idx_end):
if idx_start > idx_end:
raise ValueError("Start index must not be greater than end index!")
if num < 0:
raise ValueError("Segment length num must not be lower than zero!")
if idx_start == idx_end or num == 0:
return np.array([], dtype='float64')
_tangent = (val_end - val_start) / num
_a = val_start + _tangent * idx_start
_b = val_start + _tangent * idx_end
#_a, _b = np.interp([idx_start, idx_end],
# [0, num], [val_start, val_end])
return np.linspace(_a, _b,
num = idx_end - idx_start)
class HullCurveControls(KnobPanelListener):
def __init__(self,
knob_panel,
knobs = [4, 5, 6, 7],
parameter_callback=None):
super().__init__()
self.kp = knob_panel
self.kp.add_knob_value_listener(self)
self.knobs = knobs
self.parameter_callback = parameter_callback
# Hull curve parameters
self.hull_t_attack = 0.05 # time s
self.hull_t_decay = 0.10 # time s
self.hull_t_release = 0.25 # time s
self.hull_a_sustain = 0.90 # amplitude
# Knob to value mapping
self.knob_map = np.linspace(0, 1.7, num=128)
self.knob_map = np.power(10, self.knob_map)
self.knob_map -= 1
self.knob_map /= 9
# TODO can we get the values here?
# use the knob panel and observer mechanism to set the initial values
self.kp.set_target_value(self.knobs[0], 12) # Attack
self.kp.set_target_value(self.knobs[1], 21) # Decay
self.kp.set_target_value(self.knobs[2], 115) # Sustain
self.kp.set_target_value(self.knobs[3], 39) # Release
self.update_hull()
return
def adapt_knob_values(self, idx, value):
# set the values according to Knob
if idx == self.knobs[0]: #attack time
self.hull_t_attack = self.knob_map[value]
print("Changed attack time to ", self.hull_t_attack, "s.");
if idx == self.knobs[1]: #decay time
self.hull_t_decay = self.knob_map[value]
print("Changed decay time to ", self.hull_t_decay, "s.");
if idx == self.knobs[2]: #sustain amplitude
self.hull_a_sustain = value/127
print("Changed sustain amplitude to ", self.hull_a_sustain*100, "%.");
if idx == self.knobs[3]: #release time
self.hull_t_release = self.knob_map[value]
print("Changed release time to ", self.hull_t_release, "s.");
return
def update_hull(self):
if self.parameter_callback is not None:
env_p = EnvelopeParameters()
env_p.set_attack(self.hull_t_attack)
env_p.set_decay(self.hull_t_decay)
env_p.set_release(self.hull_t_release)
env_p.set_sustain(self.hull_a_sustain)
env_p.set_hold(True)
self.parameter_callback(env_p)
return
def process_knob_value_change(self, idx, value):
# set the values according to Knob
self.adapt_knob_values(idx, value)
self.update_hull()
return
class ModulationIndexControl(KnobPanelListener):
def __init__(self,
knob_panel,
modulation_index_callback=None):
super().__init__()
self.kp = knob_panel
self.kp.add_knob_value_listener(self)
self.modulation_index_callback = modulation_index_callback
# Knob amplitude mapping
self.knob_amp_map = np.linspace(-0.9, 0, num=64)
self.knob_amp_map = np.append(self.knob_amp_map,
np.linspace(0, 0.9, num=64))
# Knob to modulation index mapping
self.knob_midx_map = list(map(lambda x: math.floor(x),
np.linspace(0, 15, num=128)))
# use the knob panel and observer mechanism to set the initial values
self.amp = [0.5, 0.5]
self.kp.set_target_value(0, 64)
self.midx = 1
self.kp.set_target_value(1, 9)
return
def adapt_knob_values(self, idx, value):
# set the values according to Knob
if idx == 0: #amplitude distribution
v = self.knob_amp_map[value]
self.amp[0] = 1 - v
self.amp[1] = 1 + v
print("Changed amplitudes to ", self.amp[0], "and", self.amp[1], ".");
if idx == 1: #frequency multiplier
self.midx = self.knob_midx_map[value]
if self.modulation_index_callback is not None:
self.modulation_index_callback(self.midx)
print("Changed modulation index to ", self.midx, ".");
return
def process_knob_value_change(self, idx, value):
# set the values according to Knob
self.adapt_knob_values(idx, value)
return
def get_amp(self, idx):
return self.amp[idx]
def get_midx(self):
return self.midx
class WaveControls(DispatchPanelListener):
MODECOLOR = [dispatchpanel.COL_OFF,
dispatchpanel.COL_GREEN,
dispatchpanel.COL_YELLOW,
dispatchpanel.COL_RED]
WAVENOTES = [22, 23]
FMNOTE = 21
MODIDXNOTES = [65, 64]
def __init__(self,
dispatch_panel,
waveform_callback=None,
fm_callback=None,
modidx_callback=None):
super().__init__()
self.dp = dispatch_panel
dispatch_panel.add_dispatch_panel_listener(self)
self.waveform_callback = waveform_callback
self.fm_callback = fm_callback
self.modidx_callback = modidx_callback
self.waveform=[0,0]
self.set_waveform(Cell.WAVE_SINE, 0)
self.set_waveform(Cell.WAVE_SINE, 1)
self.set_fm_mode(True)
self.set_modidx(0)
return
def process_button_pressed(self, note):
print("note", note)
if note in self.WAVENOTES:
idx = self.WAVENOTES.index(note)
# set waveform
wf = self.waveform[idx] + 1
if wf >= len(WaveControls.MODECOLOR):
wf = 0
self.set_waveform(wf, idx)
if note == self.FMNOTE:
self.set_fm_mode(not self.fm_mode)
if note == self.MODIDXNOTES[0]:
self.set_modidx(self.get_modidx()-1)
if note == self.MODIDXNOTES[1]:
self.set_modidx(self.get_modidx()+1)
return
def set_waveform(self, waveform, idx=0):
_waveform = self.waveform[idx]
self.waveform[idx] = waveform
self.dp.setColor(WaveControls.WAVENOTES[idx],
WaveControls.MODECOLOR[waveform])
if self.waveform_callback is not None:
self.waveform_callback(idx, waveform)
return _waveform
def get_waveform(self, idx=0):
return self.waveform[idx]
def set_fm_mode(self, fm_mode):
self.fm_mode = fm_mode
self.dp.setColor(WaveControls.FMNOTE,
dispatchpanel.COL_GREEN if fm_mode else dispatchpanel.COL_YELLOW)
if self.fm_callback is not None:
self.fm_callback(fm_mode)
return
def get_fm_mode(self):
return self.fm_mode
def set_modidx(self, modidx):
self.modidx = modidx;
# adjust boundaries
if self.modidx < 0:
self.modidx = 0
if self.modidx > 15:
self.modidx = 15;
# set button colors (only red and off available)
self.dp.setColor(WaveControls.MODIDXNOTES[0],
(dispatchpanel.COL_RED
if self.modidx > 0
else dispatchpanel.COL_OFF))
self.dp.setColor(WaveControls.MODIDXNOTES[1],
(dispatchpanel.COL_RED
if self.modidx < 15
else dispatchpanel.COL_OFF))
if self.modidx_callback is not None:
self.modidx_callback(self.modidx)
print("Modulation index adjusted to {0}.".format(self.modidx))
return
def get_modidx(self):
return self.modidx
class Cell(WaveSource):
# wave forms
WAVE_OFF = 0
WAVE_SINE = 1
WAVE_SAWTOOTH = 2
WAVE_SQUARE = 3
def __init__(self,
chid=None,
modulator = None,
samplerate = 44100,
blocksize = 441,
done_callback = None):
super(Cell, self).__init__(chid=chid,
samplerate=samplerate,
blocksize=blocksize,
done_callback=done_callback)
#setting the modulator enables frequency modulation
self.modulator = modulator
self.waveform = None
self.frequency = None
self.midx = 1 # modulation index
self.envelope = EnvelopeGenerator(self.get_samplerate())
self.env_gen = EnvelopeSequencer(self.envelope,
self.get_blocksize(),
self.get_samplerate(),
None,
self._seq_done_callback)
self.wave_gen = None
return
def set_envelope(self, p):
if not isinstance(p, EnvelopeParameters):
raise ValueError("EnvelopeParameters expected!")
self.envelope.set_parameters(p)
return
def set_frequency(self, f):
self.frequency = f
self._update_wavegen()
return
def set_waveform(self, waveform):
self.waveform = waveform
self._update_wavegen()
return
def set_modulator(self, modulator):
self.modulator = modulator
self._update_wavegen()
return
def set_midx(self, midx):
self.midx = 1 if midx is None else midx
return
def _update_wavegen(self):
if self.frequency is None:
return None
# one period on the chosen frequency
w = 2 * np.pi * self.frequency
t = np.linspace(0, 2 * np.pi,
num=int(self.get_samplerate() // self.frequency))
if self.modulator is not None:
# When there is a modulator, this is the stored wave
wave = t
else:
wave = self._generate_wave(t)
self.wave_gen = FixedWaveLoopSequencer(wave,
self.get_blocksize())
return
def _generate_wave(self, base):
if base is None:
return None
if self.waveform == self.WAVE_SINE:
wave = np.sin(base)
elif self.waveform == self.WAVE_SAWTOOTH:
wave = signal.sawtooth(base)
elif self.waveform == self.WAVE_SQUARE:
wave = signal.square(base)
else: # unknown waveform or NONE
l = 1 if base is None else len(base)
wave = np.array([0]*l, dtype='float64')
return wave
def strike(self):
self.env_gen.strike()
return
def release(self):
self.env_gen.release()
return
def tunedown(self):
self.env_gen.tunedown()
def stop(self):
self.env_gen.reset()
return
def get(self):
wave = None
if self.wave_gen is not None:
if self.modulator is None:
wave = self.wave_gen.get()
else:
t = self.wave_gen.get()
mod = self.modulator.get()
wave = self._generate_wave(t+self.midx*mod)
wave *= self.env_gen.get()
else:
wave = np.array([0]*self.get_blocksize(), dtype='float64')
return wave
def _seq_done_callback(self, seq):
self.done()
return
class FMChannel(WaveSource):
CELL_COUNT = 2
def __init__(self,
chid=None,
samplerate=44100,
blocksize=441,
done_callback = None):
super(FMChannel, self).__init__(chid=chid,
samplerate=samplerate,
blocksize=blocksize,
done_callback=done_callback)
self.cells = []
for i in range(0, self.CELL_COUNT):
self.cells.append(
Cell(i,
None,
samplerate, blocksize,
self.cell_done_callback))
# set fm
self.set_fm_mode(True)
self.cell_active = [False]*self.CELL_COUNT
return
def set_envelope(self, idx, env_p):
if idx < 0 or idx > len(self.cells)-1:
raise ValueError("Cell index {0} is out of bounds!".format(idx))