-
Notifications
You must be signed in to change notification settings - Fork 3
/
synth.py
240 lines (186 loc) · 7.02 KB
/
synth.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
import wave
import threading
from pathlib import Path
from importlib import import_module
from functools import lru_cache, partial
from contextlib import contextmanager
import numpy as np
import soundcard as sc
SAMPLERATE = 44100 # default sample rate
def sine_wave(duration, frequency, ampl=1.0, samplerate=SAMPLERATE):
frames = int(duration * samplerate)
x = np.linspace(0, duration, frames)
assert len(x) == frames
return (0.5 * ampl) * np.sin(x * frequency * np.pi * 2)
def release_time(atk, dcy, samplelen, samplerate=SAMPLERATE):
return samplelen / samplerate * 1000 - (atk + dcy)
def envelope(attack_time, decay_time, sustain_level, release_time, frames):
assert isinstance(frames, int)
attack_frames = int(frames * attack_time)
decay_frames = int(frames * decay_time)
release_frames = int(frames * release_time)
sustain_frames = frames - attack_frames - decay_frames - release_frames
return np.concatenate([
np.linspace(0, 1, attack_frames),
np.linspace(1, sustain_level, decay_frames),
np.linspace(sustain_level, sustain_level, sustain_frames),
np.linspace(sustain_level, 0, release_frames),
])
def envelope_ms(attack_time, decay_time, sustain_level, release_time, frames, samplerate=SAMPLERATE):
assert isinstance(frames, int)
attack_frames = int(attack_time / 1000 * samplerate)
decay_frames = int(decay_time / 1000 * samplerate)
release_frames = int(release_time / 1000 * samplerate)
padding_frames = frames - attack_frames - decay_frames - release_frames
attack_frames = np.clip(attack_frames, 0, None)
decay_frames = np.clip(decay_frames, 0, None)
release_frames = np.clip(release_frames, 0, None)
padding_frames = np.clip(padding_frames, 0, None)
return np.concatenate([
np.linspace(0, 1, attack_frames),
np.linspace(1, sustain_level, decay_frames),
np.linspace(sustain_level, 0, release_frames),
np.linspace(0, 0, padding_frames)
])[:frames]
@lru_cache()
def lowpass_noise(cutoff, duration, samplerate=SAMPLERATE):
frames = int(duration*samplerate)
# # low pass filter implementation without fft
# # len(convolution) = len(signal) + len(kernel) - 1
# kernel_half_duration = 1
# t = np.linspace(
# -kernel_half_duration,
# kernel_half_duration,
# 2 * kernel_half_duration * samplerate
# )
# kernel = 2 * cutoff * np.sinc(2 * cutoff * t)
noise = np.random.normal(0, 0.2, frames)
fd_noise = np.fft.rfft(noise)
freq = np.fft.rfftfreq(noise.size, d=1/samplerate)
print(len(freq[freq < cutoff]))
fd_noise[freq > cutoff] = 0
noise = np.fft.irfft(fd_noise)
# noise = np.convolve(noise, kernel)
return noise
@lru_cache()
def bandpass_noise(cutoffl, cutoffh, duration, samplerate=SAMPLERATE):
frames = int(duration*samplerate)
noise = np.random.normal(0, 0.2, frames)
fd_noise = np.fft.rfft(noise)
freq = np.fft.rfftfreq(noise.size, d=1/samplerate)
fd_noise[freq < cutoffl] = 0
fd_noise[freq > cutoffh] = 0
noise = np.fft.irfft(fd_noise)
return noise
class Synth:
def __init__(self, output):
self.output = output
def play(self, *args):
self.play_mix(args)
def play_mix(self, mix):
concatenated = [np.concatenate(list(map(list, waves))) for waves in mix]
longest = len(max(concatenated, key=lambda x: len(x)))
for idx, ary in enumerate(concatenated):
zeros = np.zeros([longest-len(ary)])
concatenated[idx] = np.block([ary, zeros])
self.output.play_wave(sum(concatenated))
def play_wave(self, wave):
self.output.play_wave(wave)
class Queue0:
"""Bufferless Queue"""
def __init__(self):
self.mutex = threading.Lock()
self.not_empty = threading.Condition(self.mutex)
self.not_full = threading.Condition(self.mutex)
self.waiters = 0
self.data = []
def put(self, item, interrupt_delay=None):
with self.not_full:
while not self.waiters:
self.not_full.wait(timeout=interrupt_delay)
self.waiters -= 1
self.data.append(item)
self.not_empty.notify()
def get(self, interrupt_delay=None):
with self.not_empty:
self.waiters += 1
self.not_full.notify()
while not self.data:
self.not_empty.wait(timeout=interrupt_delay)
item = self.data.pop()
return item
def __iter__(self):
return self
def __next__(self):
return self.get()
class SoundcardOutput:
def __init__(self, speaker):
self.speaker = speaker
self.thread = None
def play_wave(self, wave):
self.queue.put(wave, interrupt_delay=0.1)
def __enter__(self):
if self.thread:
raise RuntimeError("already running")
self.queue = Queue0()
self.thread = threading.Thread(target=self._feed_thread, daemon=True)
self.thread.start()
return self
def __exit__(self, *args):
pass
def _feed_thread(self):
for item in self.queue:
self.speaker.play(item)
@contextmanager
def open_sc_stream(samplerate=SAMPLERATE, buffer_duration=1.0):
speaker = sc.default_speaker()
print(speaker)
blocksize = int(samplerate * buffer_duration)
with speaker.player(samplerate=samplerate, blocksize=blocksize) as player:
# player.channels = [-1]
with SoundcardOutput(player) as output:
yield output
class MyBuffer(bytearray):
def play_wave(self, data):
self.extend(np.int16(np.clip(data, -1, 1) * 32767))
def _write_wav_file(filename, sample_rate, stream):
with wave.open(filename, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.setnframes(len(stream))
wf.writeframes(stream)
@contextmanager
def create_wav_file(filename, sample_rate=SAMPLERATE):
stream = MyBuffer()
try:
yield Synth(stream)
finally:
_write_wav_file(filename, sample_rate, stream)
@contextmanager
def open_soundcard_synth(sample_rate=SAMPLERATE):
with open_sc_stream() as stream:
yield Synth(stream)
def run_synth(callable, output=None, **kwargs):
if output is None:
context_function = open_soundcard_synth
elif isinstance(output, str):
context_function = partial(create_wav_file, output)
try:
with context_function(**kwargs) as synth:
callable(synth)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
# TODO: use argparse
import sys
outfile = None
if len(sys.argv) >= 2:
scorename = sys.argv[1]
if len(sys.argv) == 3:
outfile = sys.argv[2]
# TODO: handle multiple scores with the same name
scorefile = next(f for f in Path('.').glob('**/*.py') if f.stem == scorename)
# scores/ezio/ezio0.py -> scores.ezio.ezio0
module = '.'.join([*scorefile.parent.parts, scorefile.stem])
run_synth(import_module(module).make_music, output=outfile)