-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeFreqAuditoryScene.py
1182 lines (940 loc) · 36.2 KB
/
TimeFreqAuditoryScene.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
"""
This module provides classes to declare, play and plot auditory scene populated by elements in the (time, frequency) domain
The scene is organized as a tree with Leaves as atom elements of the scene.
The tree offers a way to group atoms, and make group of groups etc...
This is useful:
- to declare structured groups of atoms (ex. shepard tone)
- to easily create repeated patterns (ex. time shifted groups)
.. _Google Python Style Guide:
http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
"""
import numpy as np
from abc import *
from matplotlib import pyplot as plt
import copy
import collections
class Node(object):
"""Node
Attributes:
List (list): list of Leaf elements in the node
delay (float): delay relative to absolute scene start time
scale (float): scaling to be applied to all elements deeper in tree
active (bool): flag declaring inclusion/exclusion from scene
index (int) : additional flag, useful for ordering items
"""
TAG = "Node"
def __init__(self, List=[], delay=0., scale=1., active=True, index=None, parent=None, draw_bbox=False):
self.List=[]
self.add(List)
self.delay = delay
self.scale = scale
self.active = active
self.index = index
self.parent = parent
self.draw_bbox = draw_bbox
def getduration(self):
"""Duration of node
Returns:
duration of node
"""
return np.max([item.delay+item.getduration() for item in self.List if item.active==True])
def getstart(self):
"""Start time relative to root"""
if self.parent is not None:
return self.parent.getstart()+self.delay
else:
return 0
def getAbsScale(self):
"""Scale relative to root"""
if self.parent is not None:
return self.parent.getAbsScale()*self.scale
else:
return 1
def getTbox(self):
start = self.getstart()
return [start,start+self.getduration()]
def getFbox(self):
Fbox = [float("inf"), float("-inf")]
for item in self.List:
Fbox_item = item.getFbox()
Fbox[1] = max(Fbox[1],Fbox_item[1])
Fbox[0] = min(Fbox[0],Fbox_item[0])
return Fbox
def getbbox(self):
"""
Get the smallest time/freq box containing elements of the node
Returns:
[tmin, tmax, fmin, fmax] (absolute time and frequency wrt origin of scene)
"""
return self.getTbox()+self.getFbox()
#rel_box = [self.getstart(), 0, float("inf"), float("-inf")] # init relative box
#for item in self.List:
# if item.active is True:
# # get subnode box
# item_box = item.getbbox()
# item_box[1]+=self.delay
# # update current node box
# rel_box[0] = rel_box[0] if rel_box[0] < item_box[0] else item_box[0]
# rel_box[2] = rel_box[2] if rel_box[2] < item_box[2] else item_box[2]
# rel_box[1] = rel_box[1] if rel_box[1] > item_box[1] else item_box[1]
# rel_box[3] = rel_box[3] if rel_box[3] > item_box[3] else item_box[3]
return rel_box
def add(self, item):
"""Populate node
Args:
item (Node/Leaf or list): element or subtree to add to node
"""
if type(item) == list:
for i in item:
self.add(i)
else:
item.parent = self
self.List.append(item)
def flatten(self, cum_delay, prod_scale):
l = []
for item in self.List:
l += item.flatten(cum_delay, prod_scale)
def print_content(self):
for item in self.List:
item.print_content()
def generate(self, fs):
"""Generate soundwave
Args:
fs (float): sampling frequency (Hz)
"""
duration = self.getduration()
x = np.zeros((int(duration*fs),))
for item in self.List:
if item.active is True:
xt = item.generate(fs)
i_start = int(item.delay*fs)
m = min(len(xt)-1, len(x)-i_start-1)
x[i_start:i_start + m] += xt[0:m]*self.scale
return x
def draw(self, ax, prop_delay, prop_scale):
"""Draw node
Args:
ax (matplotlib.axes.Axes): axe instance to plot into
prop_delay (float): propagated absolute delay of parent node
prop_scale (float): propagated absolute scale of parent node
"""
for node in self.List:
if node.active is True:
node.draw(ax, prop_delay+self.delay, prop_scale*self.scale)
if self.draw_bbox==True:
color = "black"
box = self.getbbox()
# horizontal
ax.plot([box[0], box[1]], [box[2], box[2]], color=color)
ax.plot([box[0], box[1]], [box[3], box[3]], color=color)
# vertical
ax.plot([box[0], box[0]], [box[2], box[3]], color=color)
ax.plot([box[1], box[1]], [box[2], box[3]], color=color)
class Scene(Node):
"""Scene
Scene, implements Node + has extra method to draw spectrogram
It is the root reference for time and scale
"""
TAG = "Scene"
def __init__(self, List=[]):
"""Constructor"""
super(Scene, self).__init__(List=List)
def flatten_scene(self):
"""
Clear all hierarchy in terms of embedded lists
children remember who there parent is
parents have no memory of their children
"""
self.List = [item for item in flatten(self.List)]
class Leaf(object):
"""Abstract Leaf Class
Attributes:
delay (float): delay wrt to start of parent node, default to 0.
duration (float): duration of leaf.
active (bool): flag for inclusion/exclusion in scene, default to 0.
index (int): additional index descriptor, default to None.
"""
Parent = None
TAG = "Leaf"
def __init__(self, delay=0., duration=1.,amp=1., active=True, index=None, parent=None):
"""Constructor"""
self.delay = delay
self.duration = duration
self.amp = amp
self.active = active
self.index = index
self.parent = parent
def getstart(self):
"""Start time relative to root"""
if self.parent is not None:
return self.parent.getstart()+self.delay
else:
return 0
@abstractmethod
def generate(self, fs):
raise NotImplementedError()
@abstractmethod
def getbbox(self):
raise NotImplementedError()
def getduration(self):
return self.duration
def getAbsScale(self):
"""Scale relative to root"""
if self.parent is not None:
return self.parent.getAbsScale()*self.amp
else:
return 1
@abstractmethod
def print_content(self):
raise NotImplementedError()
@abstractmethod
def makePlotOpts(self):
""" Should always return a list of dictionaries """
raise NotImplementedError()
@abstractmethod
def draw(self, ax, prop_delay, prop_scale):
raise NotImplementedError()
class Tone(Leaf):
"""Tone
Attributes:
freq (float): frequency of pure tone.
amp (float): amplitude of pure tone.
phase (float): phase of pure tone.
ramp: (float): duration of cosine ramp
"""
TAG = "Tone"
def __init__(self, freq=100., delay=0., duration=1., amp=1., ramp=0.01, phase=None, index=None, active=True):
"""Constructor
Phase is randomly set if none given.
"""
super(Tone, self).__init__(delay=delay, duration=duration, amp=amp, index=index, active=active)
self.freq = freq
self.ramp = ramp
# random phase if not specified
if phase is None:
self.phase = np.random.rand()*np.pi*2.
def generate(self, fs):
s = self.getstart()
d = self.getduration()
t = np.linspace(s, s+d, int(d*fs))
if self.freq<fs/2: # anti-alias safety
y = np.cos(2.*np.pi*self.freq*t + self.phase)
else:
y = np.zeros(t.shape)
# apply border smoothing in the form of a raising squared cosine amplitude modulation
Ltau = int(self.ramp*fs)
up = 1.-np.cos(np.linspace(0, np.pi/2., Ltau))**2
down = np.cos(np.linspace(0, np.pi/2., Ltau))**2
y[0:Ltau] *= up
y[-Ltau:] *= down
return y*self.amp
def getduration(self):
return self.duration
def getbbox(self):
return [self.delay, self.delay+self.duration, self.freq, self.freq]
def getTbox(self):
return [self.delay, self.delay+self.duration]
def getFbox(self):
return [self.freq, self.freq]
def print_content(self):
print(self.TAG+\
", freq:"+str(self.freq)+\
", amp:"+str(self.amp)+\
", duration:"+str(self.duration)+\
", delay:"+str(self.delay))
def draw(self, ax, prop_delay, prop_scale):
abs_amp = self.amp*prop_scale
map_abs_amp = sig((abs_amp-0.2))
ax.plot([prop_delay + self.delay, prop_delay + self.delay + self.duration],
[self.freq, self.freq],
lw=map_abs_amp*4.,
alpha=map_abs_amp,
color='black')
def makePlotOpts(self):
s = self.getstart()
d = self.getduration()
return [{"line":
{"tf":[s, s+d, self.freq, self.freq],
"a":self.getAbsScale()}
}]
class SAMTone(Tone):
"""Sinusoid Amplitude Modulated Tone
Attributes:
fmod (float): frequency of envelope modulator
"""
TAG = "SAMTone"
def __init__(self, freq=100., delay=0., duration=1., amp=1., fmod=10.):
"""Constructor"""
super(SAMTone, self).__init__(freq=freq,
amp=amp,
delay=delay,
duration=duration)
self.fmod = fmod
def generate(self, fs):
t = np.linspace(0., self.duration, int(self.duration*fs))
y = np.cos(2.*np.pi*self.freq*t)*(0.5+0.5*np.cos(2.*np.pi*self.fmod*t))
# apply border smoothing in the form of a raising squared cosine amplitude modulation
tau = 0.02 # 10ms
Ltau = int(tau*fs)
up = 1.-np.cos(np.linspace(0, np.pi/2., Ltau))**2
down = np.cos(np.linspace(0, np.pi/2., Ltau))**2
y[0:Ltau] *= up
y[-Ltau:] *= down
return y*self.amp
def getduration(self):
return self.duration
def print_content(self):
print(self.TAG+\
", freq:"+str(self.freq)+\
", amp:"+str(self.amp)+\
", fmod:"+str(self.fmod)+\
", duration:"+str(self.duration)+\
", delay:"+str(self.delay))
def draw(self, ax, prop_delay, prop_scale):
abs_amp = self.amp*prop_scale
map_abs_amp = sig((abs_amp-0.2))
tmod = 1./self.fmod
t = np.arange(0,self.duration,tmod)
for ti in t:
#print ti, ti+0.5*tmod, self.duration
ax.plot([prop_delay + self.delay + ti, prop_delay + self.delay + ti+0.5*tmod],
[self.freq, self.freq],
lw=map_abs_amp*4.,
alpha=map_abs_amp,
color='black')
class Sweep(Leaf):
"""Frequency Sweep
Linear interpolation in frequency domain
Attributes
freqs (list): frequency bounds of the sweep
"""
TAG = "Sweep"
def __init__(self, freqs=[100.,200.], delay=0., duration=1., amp=1.):
super(Sweep, self).__init__(amp=amp,
delay=delay,
duration=duration)
self.freqs = freqs
def generate(self, fs):
s = self.getstart()
d = self.getduration()
t = np.linspace(s, s+d, int(d*fs))
f = np.linspace(self.freqs[0], self.freqs[1], len(t))
y = np.cos(2.*np.pi*f*t)
# apply border smoothing in the form of a raising squared cosine amplitude modulation
tau = 0.02 # 10ms
Ltau = int(tau*fs)
up = 1.-np.cos(np.linspace(0, np.pi/2., Ltau))**2
down = np.cos(np.linspace(0, np.pi/2., Ltau))**2
y[0:Ltau] *= up
y[-Ltau:] *= down
return y*self.amp
def getduration(self):
return self.duration
def getbbox(self):
return [self.delay, self.delay+self.duration, min(self.freqs), max(self.freqs)]
def getTbox(self):
return [self.delay, self.delay+self.duration]
def getFbox(self):
return [min(self.freqs), max(self.freqs)]
def print_content(self):
print(self.TAG+\
", freqs:"+str(self.freqs)+\
", amp:"+str(self.amp)+\
", duration:"+str(self.duration)+\
", delay:"+str(self.delay))
def draw(self, ax, prop_delay, prop_scale):
abs_amp = self.amp*prop_scale
map_abs_amp = sig((abs_amp-0.2))
ax.plot([prop_delay+self.delay, prop_delay+self.delay+ self.duration],
[self.freqs[0], self.freqs[1]],
lw=map_abs_amp*4.,
alpha=map_abs_amp,
color='black')
def makePlotOpts(self):
s = self.getstart()
d = self.getduration()
return [{"line":
{"tf":[s, s+d, self.freqs[0], self.freqs[1]],
"a":self.getAbsScale()}
}]
class InstantaneousFrequency(Leaf):
"""Instantaneous frequency Leaf
Sound cos(f(t))
f is the instantaneous frequency
Attributes
phase (function): instantaneous phase.
i_freq (function): instantaneous frequency
env: spectral envelope
"""
TAG = "InstantaneousFrequency"
def __init__(self, phase=None, i_freq=None, delay=0., duration=1., amp=1., env=None):
super(InstantaneousFrequency, self).__init__(delay=delay,
duration=duration,
amp=amp)
self.phase = copy.deepcopy(phase)
self.i_freq = copy.deepcopy(i_freq)
self.env = env
def generate(self, fs):
s = self.getstart()
d = self.getduration()
t = np.linspace(s, s+d, int(d*fs))-self.delay
if self.phase is not None:
phase = self.phase(t) # the time instantaneous phase
ift = np.diff(phase) # the instantaneous frequency
phase = phase[:-1]
elif self.i_freq is not None:
ift = self.i_freq(t)/fs
phase = np.cumsum(ift)
if self.env is not None:
ampt = self.env.amp(ift*fs) # the instantaneous amplitude
else:
ampt = self.amp
y = np.cos(2.*np.pi*phase)*ampt
# apply border smoothing in the form of a raising squared cosine amplitude modulation
tau = 0.02 # 10ms
Ltau = int(tau*fs)
up = 1.-np.cos(np.linspace(0, np.pi/2., Ltau))**2
down = np.cos(np.linspace(0, np.pi/2., Ltau))**2
y[0:Ltau] *= up
y[-Ltau:] *= down
return y #*self.amp
def getduration(self):
return self.duration
def print_content(self):
print(self.TAG+\
", f:"+str(self.f_phase)+\
", amp:"+str(self.amp)+\
", duration:"+str(self.duration)+\
", delay:"+str(self.delay))
def draw(self, ax, prop_delay, prop_scale):
fs_plot = 2000.
n = int(self.duration*fs_plot)
t = np.linspace(0., self.duration, n ) # time support
if self.phase is not None:
phase = self.phase(t) # the time instantaneous phase
ift = np.diff(phase)*fs_plot # the instantaneous frequency
elif self.i_freq is not None:
ift = self.i_freq(t)
ift = ift[:-1]
ax.plot(prop_delay+self.delay+t[:-1],ift,
color='black')
def makePlotOpts(self):
s = self.getstart()
d = self.getduration()
return [{"function":{
"start":self.getstart(),
"delay":self.delay,
"duration":self.getduration(),
"handle": self.phase if self.phase is not None else self.i_freq,
"type": "phase" if self.phase is not None else "frequency"}}]
class SpectralEnvelope(object):
"""Abstract Spectral envelope"""
def __init__(self, env):
self.env = env
@abstractmethod
def amp(self, x):
raise NotImplementedError()
class GaussianSpectralEnvelope(SpectralEnvelope):
"""
Gaussian Spectral envelope
Attributes:
mu_log (float): log frequency mean
sigma_log (float): log frequency std
"""
def __init__(self, **kwargs):
"""Constructor
Args:
**kwargs:
mu_log (mean in log domain).
mu (mean).
sigma_log (sigma in log domain).
sigma_oct (sigma in octaves).
"""
if 'mu_log' in kwargs:
self.mu_log=kwargs['mu_log']
elif 'mu' in kwargs:
self.mu_log=np.log(kwargs['mu'])
if 'sigma_log' in kwargs:
self.sigma_log = kwargs['sigma_log']
elif 'sigma_oct' in kwargs:
self.sigma_log = kwargs['sigma_oct']*np.log(2.)
def amp(self, x):
return g_env(x, self.mu_log, self.sigma_log)
class UnitSpectralEnvelope(SpectralEnvelope):
"""
Unit Spectral envelope
"""
def __init__(self):
pass
def amp(self, x):
# returns 1
return np.ones(np.array(x).shape)
class Chord(Node):
"""Chord
Chord of pure tones
Attributes:
freqs (list): List of pure tone frequencies
amps (list): List of pure tone amplitudes
duration (float): shared duration of all tones
fb (float, optional): base frequency, when constructing form intervals
intervals (list, optional): intervals between tones
"""
TAG = "Chord"
def __init__(self, duration=1., delay=0., amps=None, ramp=0.01, env=None, index=None, **kwargs):
"""
Chord of pure tones constructor
# Multiple ways of constructing the chord
# - from frequencies
# -- and amplitude
# -- and spectral envelope
# - from a base freq and intervals
"""
super(Chord, self).__init__(delay=delay, index=index)
self.duration = duration
self.env = env
self.ramp = ramp
# constructing from preexisting tone
if 'chord' in kwargs:
self.freqs = kwargs['chord'].freqs
self.amps = kwargs['chord'].amps
self.add(kwargs['chord'].List)
self.duration = kwargs['chord'].duration
self.env = kwargs['chord'].env
else:
# Constructing frequencies
# - constructing from pairs
if 'freqs' in kwargs:
self.freqs = kwargs['freqs']
# - constructing from base and intervals
elif 'fb' in kwargs:
assert 'intervals' in kwargs
self.freqs = kwargs['fb']*np.cumprod(kwargs['intervals'])
# Constructing amplitudes
# - from input
if amps != None:
self.amps = kwargs['amps']
# - from enveloppe
else:
if env != None:
self.env = env
else:
self.env = UnitSpectralEnvelope()
self.amps = self.env.amp(self.freqs)
assert len(self.freqs) == len(self.amps)
self.build_tones()
def build_tones(self):
self.List=[]
n_chord = len(self.freqs)
for i in range(n_chord):
tone = Tone(freq=self.freqs[i],
delay=0.,
duration=self.duration,
amp=self.amps[i],
ramp=self.ramp,
index=i)
self.add(tone)
def shift_tones(self, shift=1.):
"""
Shift all tones,
either by a common shift if argument is scalar
or by individual shifts
Args:
shift (float or list): scaling of all frequencies of tones in chord
"""
if isinstance(shift, list):
assert len(shift) == len(self.freqs)
self.freqs = (np.asarray(self.freqs)*np.asarray(shift)).tolist()
self.amps = self.env.amp(self.freqs) # won't work if no enveloppe
self.build_tones()
class WhiteNoise(Leaf):
"""WhiteNoise
"""
TAG = "WhiteNoise"
def __init__(self, delay=0., duration=1., amp=1.):
super(WhiteNoise, self).__init__(delay=delay,
duration=duration,
amp=amp)
def generate(self, fs):
t = np.linspace(0., self.duration, int(self.duration*fs))
y = np.random.randn(len(t))
# apply border smoothing in the form of a raising squared cosine amplitude modulation
tau = 0.02 # 10ms
Ltau = int(tau*fs)
up = 1.-np.cos(np.linspace(0, np.pi/2., Ltau))**2
down = np.cos(np.linspace(0, np.pi/2., Ltau))**2
y[0:Ltau] *= up
y[-Ltau:] *= down
return y*self.amp
def getduration(self):
return self.duration
def getbbox(self):
return [self.delay, self.delay+self.duration, 0, float("+inf")]
def print_content(self):
print(self.TAG+\
", amp:"+str(self.amp)+\
", duration:"+str(self.duration)+\
", delay:"+str(self.delay))
def draw(self, ax, prop_delay, prop_scale):
abs_amp = self.amp*prop_scale
ax.plot([prop_delay+self.delay, prop_delay+self.delay+ self.duration],
[100, 100],color='red')
ax.plot([prop_delay+self.delay, prop_delay+self.delay+ self.duration],
[1000, 1000],color='red')
def makePlotOpts(self):
s = self.getstart()
d = self.getduration()
return [{"box":[s, s+d, 0, float("inf")]}]
class ConstantIntervalChord(Chord):
"""
ConstantIntervalChord
Attributes
fb (float): base frequency
interval (float): constant frequency interval
"""
TAG = "ConstantIntervalChord"
def __init__(self, fb=50., interval=2., duration=1., delay=0., ramp=0.01, env=None,index=None, List=[], fmin=5, fmax=40000):
"""ConstantIntervalChord constructor
Args
fb (float): base frequency
interval (float): constant frequency interval
"""
imin = int(1./np.log(interval)*np.log(fmin/fb))
imax = int(1./np.log(interval)*np.log(fmax/fb))
index = np.arange(imin, imax)
freqs = []
for i in index:
fi = interval**i*fb
freqs.append(fi)
super(ConstantIntervalChord, self).__init__(freqs=freqs,
delay=delay,
duration=duration,
ramp=ramp,
List=List,
env=env,
index=index)
self.fb = fb
self.fmin = fmin
self.fmax = fmax
class HarmonicComplexTone(Chord):
"""Harmonic Complex Tone"""
TAG = "HarmonicComplexTone"
def __init__(self, f0=100, harmonics=None, duration=1., delay=0., ramp=0.01, env=None, index=None, List=[], fmin=5, fmax=40000 ):
imax = int(fmax/f0)
if harmonics is None:
harmonics = range(1, imax)
elif isinstance(harmonics, list):
harmonics = [h for h in harmonics if h<imax]
freqs = []
for i in harmonics:
freqs.append(i*f0)
super(HarmonicComplexTone, self).__init__(freqs=freqs,
delay=delay,
duration=duration,
ramp=ramp,
List=List,
env=env,
index=index)
self.f0 = f0
self.harmonics = harmonics
self.fmin = fmin
self.fmax = fmax
class ShepardTone(ConstantIntervalChord):
"""Shepard Tone
"""
TAG = "ShepardTone"
def __init__(self, fb=50., duration=1., delay=0., ramp=0.01, env=None,index=None, List=[], fmin=5, fmax=40000):
"""Shepard Tone constructor
Args:
fb (float): base frequency
"""
super(ShepardTone, self).__init__(fb=fb,
interval=2.,
delay=delay,
duration=duration,
List=List,
env=env,
ramp=ramp,
index=index,
fmin=fmin,
fmax=fmax)
class Tritone(Node):
"""
TriTone (octave interval)
Attributes:
fb (float): base frequency of the first tone
duration_sp (float): duration of shepard tones in tritone
delay_sp (float): delay between shepard tones in tritone
"""
TAG = "Tritone"
def __init__(self, fb=50., duration_sp=1., delay_sp=0., delay=0., ramp=0.01, env=None, fmin=5, fmax=40000):
"""TriTone constructor
Args:
fb (float): base frequency of first tone
duration_sp (float): duration of shepard tones in tritone
delay_sp (float): delay between shepard tones in tritone
"""
super(Tritone, self).__init__(delay=delay)
T1 = ShepardTone(fb=fb, duration=duration_sp, delay=0., ramp=ramp, env=env, fmin=fmin, fmax=fmax,index=0)
T2 = ShepardTone(fb=fb*np.sqrt(2.), duration=duration_sp, delay=duration_sp+delay_sp, ramp=ramp, env=env, fmin=fmin, fmax=fmax,index=1)
self.add([T1, T2])
self.fb = fb
self.duration_sp = duration_sp
self.delay_sp = delay_sp
class ShepardRisset(Node):
"""ShepardRisset Tone
Attributes:
k (float): the directed speed factor of the base frequency
"""
TAG = "ShepardRisset"
def __init__(self, fb=50., interval=2., duration=1., delay=0., env=None, List=[], k=1.1, **kwargs):
"""ShepardRisset constructor
Args:
fb (float): base frequency
duration (float): duration
k (float): the directed speed factor of the base frequency
"""
super(ShepardRisset, self).__init__(delay=delay, List=List)
self.k =k
self.interval = interval
# backward construction from ending base frequency
if 'fb_end' in kwargs:
self.fb = kwargs['fb_end']*np.exp(-k*duration)
else:
self.fb = fb
fmin = 5.
fmax = 40000.
imin = np.ceil(1./np.log(interval)*np.log(fmin/self.fb))
imax = np.floor(1./np.log(interval)*np.log(fmax/self.fb))
index = np.arange(imin, imax)
self.List = []
# fixed form of temporal evolution to have constant speed over the circle
def phase(fi):
return lambda x: fi*np.exp(x*k)/k
i_restart = -1 if k < 0 else 0
f_thresh = fmin if k < 0 else fmax
# initial tones have duration set corresponding to when they cross fmax
for i in index:
fi = interval**i*self.fb
duration_tone = np.abs(1./k*np.log(f_thresh/fi))
instFreq = InstantaneousFrequency(phase=phase(fi),
duration=min(duration_tone,duration),
env=env)
self.add(instFreq)
# added tones appearing as times goes on
dt = np.abs(1./k*np.log(interval))
times = np.arange(dt,duration,dt)
for time in times:
fi = interval**index[i_restart]*self.fb
duration_tone = np.abs(1./k*np.log(f_thresh/fi))
instFreq = InstantaneousFrequency(phase=phase(fi),
delay=time,
duration=min(duration-time,duration_tone),
env=env)
self.add(instFreq)
class ShepardFM(Node):
"""ShepardFM
Shepard Tone with frequency modulated base frequency (no new incoming tones)
"""
TAG = "ShepardFM"
def __init__(self, fb=50., interval=2., duration=1., delay=0., env=None, List=[], amod=0.25, fmod=10., phase=0., **kwargs):
"""Shepard Tone FM constructor
Args:
fb: base frequency
interval: the interval between tones (2. for shepard)
delay:
amod: amplitude of the frequency modulation in terms of octave
fmod: frequency of modulation
duration: duration
env: function (log f -> amplitude)
"""
super(ShepardFM, self).__init__(delay=delay, List=List)
self.interval = interval
self.amod = amod
self.fmod = fmod
self.fb = fb
fmin = 5.
fmax = 40000.
imin = np.ceil(1./np.log(interval)*np.log(fmin/self.fb))
imax = np.floor(1./np.log(interval)*np.log(fmax/self.fb))
index = np.arange(imin, imax)
self.List = []
def inst_freq(fi):
return lambda t: fi*np.exp(np.log(interval)*amod*np.cos(2*np.pi*fmod*t+phase))
for i in index:
fi = interval**i*self.fb
instFreq = InstantaneousFrequency(i_freq=inst_freq(fi), duration=duration, env=env)
self.add(instFreq)
class ToneSequence(Node):
"""Tone Sequence
Sequence of tones of same duration, same inter-tone delay
"""
TAG = "ToneSequence"
def __init__(self, intertone_delay=0.1,
tone_duration=0.5,
freqs=None,
ramp=0.01,
List=[], delay=0., scale=1., env=None):
"""Constructor
Args:
freqs (list): list of tone frequencies
"""
super(ToneSequence, self).__init__(delay=delay, List=List, scale=scale)
self.intertone_delay=intertone_delay
self.tone_duration=tone_duration
self.freqs=freqs
self.env=env
self.ramp=ramp
assert isinstance(self.freqs, list)
self.build_tones()
def build_tones(self):
self.List=[]
runTime = 0
index = 0
for f in self.freqs:
amp = self.env.amp(f) if self.env is not None else 1.
tone = Tone(freq=f, duration=self.tone_duration, delay=runTime, amp=amp, ramp=self.ramp, index=index)
self.add(tone)
runTime += self.tone_duration + self.intertone_delay
index += 1