def generate_chord(T=1,fs=44100,noise=True):
"""
Generate random major chord with amplitude 1
for duration T with sampling rate fs
Return chord and label (pitch class)
0 = Ab / G#
1 = A
2 = Bb / A#
3 = B
4 = C
5 = Db / C#
6 = D
7 = Eb / D#
8 = E
9 = F
10 = Gb / F#
11 = G
"""
t = np.linspace(0,T,T*fs)
n1 = np.random.randint(1+17,88-10)
chord_inversion = np.random.randint(1,6)
n3 = 0
n5 = 0
if chord_inversion == 1:
n3 = n1 + 4
n5 = n1 + 7
if chord_inversion == 2:
n3 = n1 + 7
n5 = n1 + 16
if chord_inversion == 3:
n3 = n1 + -8
n5 = n1 + 7
if chord_inversion == 4:
n3 = n1 + -8
n5 = n1 + -5
if chord_inversion == 5:
n3 = n1 + 4
n5 = n1 + -5
if chord_inversion == 6:
n3 = n1 + -8
n5 = n1 + -17
f1 = 2**((n1-49)/12.0)*440
f2 = 2**((n3-49)/12.0)*440
f3 = 2**((n5-49)/12.0)*440
phase1 = np.random.uniform(0, 2*np.pi, 1)
phase2 = np.random.uniform(0, 2*np.pi, 1)
phase3 = np.random.uniform(0, 2*np.pi, 1)
label = (n1 % 12) + 1
noise_var = np.random.uniform(0, 1, 1)
sig = signal.square(f1*2*np.pi*t + phase1) + signal.square(f2*2*np.pi*t + phase2) + signal.square(f3*2*np.pi*t + phase3)
if noise:
sig = sig + noise_var*np.random.randn(int(T*fs))
return sig/3, label-1
评论列表
文章目录