Skip to content

Commit 2062b2b

Browse files
committed
synthio: add a noise program to the manual tests
1 parent 1d1907b commit 2062b2b

File tree

1 file changed

+71
-0
lines changed
  • tests/circuitpython-manual/synthio/note

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
3+
sys.path.insert(
4+
0, f"{__file__.rpartition('/')[0] or '.'}/../../../../frozen/Adafruit_CircuitPython_Wave"
5+
)
6+
7+
import random
8+
import audiocore
9+
import synthio
10+
from ulab import numpy as np
11+
import adafruit_wave as wave
12+
13+
random.seed(9)
14+
SAMPLE_SIZE = 1024
15+
VOLUME = 14700
16+
noise = np.array(
17+
[random.randint(-32768, 32767) for i in range(SAMPLE_SIZE)],
18+
dtype=np.int16,
19+
)
20+
21+
envelope = synthio.Envelope(
22+
attack_time=0,
23+
decay_time=0.2,
24+
sustain_level=0,
25+
)
26+
27+
synth = synthio.Synthesizer(sample_rate=48000)
28+
29+
30+
def randf(lo, hi):
31+
return random.random() * (hi - lo) + lo
32+
33+
34+
def synthesize(synth):
35+
notes = [
36+
synthio.Note(
37+
frequency=synthio.midi_to_hz(1 + i),
38+
waveform=noise,
39+
envelope=envelope,
40+
bend_mode=synthio.BendType.SWEEP,
41+
bend_depth=random.choice((-1, 1)),
42+
bend_rate=randf(4, 12),
43+
)
44+
for i in range(12)
45+
]
46+
47+
random.seed(9)
48+
for _ in range(16):
49+
n = random.choice(notes)
50+
d = random.randint(30, 60)
51+
print(n, d)
52+
synth.press((n,))
53+
yield d
54+
synth.release_all()
55+
yield 36
56+
57+
58+
def chain(*args):
59+
for a in args:
60+
yield from a
61+
62+
63+
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
64+
with wave.open("noise.wav", "w") as f:
65+
f.setnchannels(1)
66+
f.setsampwidth(2)
67+
f.setframerate(48000)
68+
for n in chain(synthesize(synth)):
69+
for i in range(n):
70+
result, data = audiocore.get_buffer(synth)
71+
f.writeframes(data)

0 commit comments

Comments
 (0)