forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbiquad.py
More file actions
61 lines (51 loc) · 1.67 KB
/
biquad.py
File metadata and controls
61 lines (51 loc) · 1.67 KB
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
import sys
sys.path.insert(
0, f"{__file__.rpartition('/')[0] or '.'}/../../../../frozen/Adafruit_CircuitPython_Wave"
)
import random
import audiocore
import synthio
from ulab import numpy as np
import adafruit_wave as wave
random.seed(9)
envelope = synthio.Envelope(
attack_time=0.15, decay_time=0, release_time=0.08, attack_level=1.0, sustain_level=1.0
)
SAMPLE_SIZE = 1024
VOLUME = 14700
sine = np.array(
np.sin(np.linspace(0, 2 * np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
dtype=np.int16,
)
noise = np.array([random.randint(-VOLUME, VOLUME) for i in range(SAMPLE_SIZE)], dtype=np.int16)
bend_out = np.linspace(0, 32767, num=SAMPLE_SIZE, endpoint=True, dtype=np.int16)
def synthesize(synth):
for waveform in (sine, None, noise):
for biquad in (
None,
synth.low_pass_filter(120),
):
for midi_note in range(24, 90, 3):
n = synthio.Note(
frequency=synthio.midi_to_hz(midi_note),
envelope=envelope,
filter=biquad,
waveform=waveform,
bend=synthio.LFO(bend_out, once=True, rate=1 / 2, scale=5),
)
synth.press(n)
print(n.frequency)
yield 24
synth.release_all()
yield 16
yield 24
yield 48
with wave.open("biquad.wav", "w") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(48000)
synth = synthio.Synthesizer(sample_rate=48000)
for n in synthesize(synth):
for i in range(n):
result, data = audiocore.get_buffer(synth)
f.writeframes(data)