-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
182 lines (135 loc) · 4.67 KB
/
player.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
#!/usr/bin/python
import numpy as np
import simpleaudio as sa
import threading as th
import time
import matplotlib.pyplot as plt
step_table = {
'b' : 2,
'c' : 3,
'd' : 5,
'e' : 7,
'f' : 8,
'g' : 10,
'a' : 12
}
sample_notes = [
[('0c',16),('4e',16),('4g',16)],
[('0b',16),('0e',16),('0g',16),('4d',16),('8g',16)],
[('0a',8),('2a#',8),('4c',8),('6c#',8)]
]
sample_notes2 = [
[('0a', 16)]
]
sample_rate = 44100
note_period = 1.25
finger_num = 10
hrm_amp = [0.17, 0.985, 0.185, 0.110, 0.26, 0.095, 0.080] # timbre
class SoundGenerator(th.Thread):
def __init__(self, threadID):
self.id = threadID
def run(self):
# play sound
pass
class NoteHolder():
def __init__(self, name):
self.name = name
self.note = np.zeros(1)
self.isEmpty = True
def popNoteSlice(self):
if self.timeleft == 0:
print(self.name, 'is empty!')
return 0
note_slice = self.note[:self.maxIdx]
self.note = self.note[self.maxIdx:]
self.timeleft -= 1
if(self.timeleft==0): self.reset()
print('note slice:', note_slice, len(note_slice))
return note_slice
def insertNote(self, note, duration):
self.note = getNotePlayable(note, hrm_amp, note_period)
self.note_len = len(self.note)
self.timeleft = duration
self.isEmpty = False
self.maxIdx = self.note_len//16
print('note:', len(self.note))
def reset(self):
self.note = np.zeros(1)
self.note_len = len(self.note)
self.isEmpty = True
def getADSR(sample_rate, note_period):
# sound envelope: env_<comp> = (<duration> , <amplitude>)
env_a = (0.05, 1.0)
env_d = (0.1, 0.15)
env_s = (0.1, env_d[1])
env_r = (round(note_period - env_a[0] - env_d[0] - env_s[0], 2), 0)
adsr_t = [env_a[0], env_d[0], env_s[0], env_r[0]]
adsr_a = [env_a[1], env_d[1], env_s[1], env_r[1]]
adsr = [
np.linspace(0, adsr_a[0], adsr_t[0] * sample_rate, False),
np.linspace(adsr_a[0], adsr_a[1], adsr_t[1] * sample_rate, False),
np.linspace(adsr_a[1], adsr_a[2], adsr_t[2] * sample_rate, False),
np.linspace(adsr_a[2], adsr_a[3], adsr_t[3] * sample_rate, False)
]
return np.hstack(adsr)
def getNotePlayable(note, hrm_amp, note_period):
steps = step_table[note[0]]
if len(note) > 1:
if note[1] == '#':
steps += 1
elif note[1] == '_':
steps -= 1
else:
print(">> Error: Invalid note suffix! Only '#' and '_' are valid suffixes.")
return 0
note_freq = 440 * 2**(steps / 12)
print('note f:', note_freq)
t = np.linspace(0, note_period, note_period * sample_rate, False)
note_values = np.zeros(t.shape)
for i in range(7):
note_values += np.sin(2 * np.pi * note_freq*(0.5*(1+i)) * t) * hrm_amp[i]
note_playable = 2**15 / np.max(abs(note_values)) * (note_values * getADSR(sample_rate, note_period))
return note_playable
def generateNoteStream(note_data, finger_num):
note_holders = []
note_stream = []
holder_model = NoteHolder('model')
holder_model.insertNote('c', 0)
note_model = holder_model.note
stream_slice = np.zeros(len(note_model)//16)
for i in range(finger_num):
note_holders.append(NoteHolder('finger'+str(i)))
for notes in note_data:
for beat in range(16):
for note in notes:
if note[0][-1] == '#' or note[0][-1] == '_':
note_arg = note[0][-2:]
note_beat = note[0][:-2]
else:
note_arg = note[0][-1]
note_beat = note[0][:-1]
note_duration = note[1]
if int(note_beat) == beat:
for holder in note_holders:
if holder.isEmpty:
holder.insertNote(note_arg, note_duration)
break
stream_slice = np.zeros(stream_slice.shape)
for holder in note_holders:
if not holder.isEmpty:
stream_slice += holder.popNoteSlice()
note_stream.extend(stream_slice)
return np.array(note_stream) * (2**15 * 0.8) / abs(np.max(note_stream))
def playStream(note_stream):
playback = sa.play_buffer(note_stream, 1, 2, sample_rate)
plt.plot(note_stream)
plt.show()
playback.wait_done()
def main():
note_stream = generateNoteStream(sample_notes, finger_num)
note_stream = note_stream.astype(np.int16)
print(np.max(note_stream))
print(len(note_stream), note_stream)
playStream(note_stream)
if __name__ == '__main__':
main()