Skip to content

Commit f52bb65

Browse files
committed
synthio: add some new manual tests
1 parent 62e6de8 commit f52bb65

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
SAMPLE_SIZE = 1024
14+
VOLUME = 32767
15+
sine = np.array(
16+
np.sin(np.linspace(0, 2 * np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
17+
dtype=np.int16,
18+
)
19+
20+
envelope = synthio.Envelope(attack_time=0.05, decay_time=8, release_time=0.25, sustain_level=0)
21+
fast_decay_envelope = synthio.Envelope(
22+
attack_time=0.05, decay_time=0.25, release_time=0.25, sustain_level=0
23+
)
24+
25+
synth = synthio.Synthesizer(sample_rate=48000)
26+
27+
28+
def synthesize(synth):
29+
notes = (synthio.Note(frequency=440, waveform=sine, envelope=envelope),)
30+
synth.press(notes)
31+
yield 360
32+
notes[0].envelope = fast_decay_envelope
33+
yield 180
34+
synth.release_all()
35+
36+
37+
def synthesize2(synth):
38+
notes = (synthio.Note(frequency=440, waveform=sine, envelope=envelope),)
39+
synth.press(notes)
40+
yield 360
41+
synth.release_all()
42+
yield 180
43+
44+
45+
def chain(*args):
46+
for a in args:
47+
yield from a
48+
49+
50+
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
51+
with wave.open("envelope.wav", "w") as f:
52+
f.setnchannels(1)
53+
f.setsampwidth(2)
54+
f.setframerate(48000)
55+
for n in chain(synthesize(synth), synthesize2(synth)):
56+
for i in range(n):
57+
result, data = audiocore.get_buffer(synth)
58+
f.writeframes(data)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
15+
envelope = synthio.Envelope(
16+
attack_time=0.1, decay_time=0.05, release_time=0.2, attack_level=0.8, sustain_level=0.8
17+
)
18+
19+
h = np.array(
20+
[
21+
-0.001229734800309099,
22+
-0.008235561806605458,
23+
-0.015082497016061390,
24+
-0.020940136918319988,
25+
-0.024981800822463429,
26+
-0.026464233332370746,
27+
-0.024803890156806906,
28+
-0.019642276775473012,
29+
-0.010893620860173042,
30+
0.001230341899766145,
31+
0.016221637398855598,
32+
0.033304135659230648,
33+
0.051486665261155681,
34+
0.069636961761409016,
35+
0.086570197432542767,
36+
0.101144354207918147,
37+
0.112353938422488253,
38+
0.119413577288191297,
39+
0.121823886314051028,
40+
0.119413577288191297,
41+
0.112353938422488253,
42+
0.101144354207918147,
43+
0.086570197432542767,
44+
0.069636961761409016,
45+
0.051486665261155681,
46+
0.033304135659230648,
47+
0.016221637398855598,
48+
0.001230341899766145,
49+
-0.010893620860173042,
50+
-0.019642276775473012,
51+
-0.024803890156806906,
52+
-0.026464233332370746,
53+
-0.024981800822463429,
54+
-0.020940136918319988,
55+
-0.015082497016061390,
56+
-0.008235561806605458,
57+
-0.001229734800309099,
58+
]
59+
)
60+
61+
filter_coeffs = np.array(h[::-1] * 32768, dtype=np.int16)
62+
63+
synth = synthio.Synthesizer(sample_rate=48000, filter=filter_coeffs)
64+
65+
66+
def synthesize(synth):
67+
n = synthio.Note(
68+
frequency=120,
69+
envelope=envelope,
70+
filter=False,
71+
)
72+
73+
print(synth, n)
74+
synth.press((n,))
75+
for _ in range(20):
76+
n.frequency *= 1.0595
77+
yield 36
78+
synth.release_all()
79+
yield 36
80+
81+
n.filter = True
82+
n.frequency = 120
83+
synth.press((n,))
84+
for _ in range(20):
85+
n.frequency *= 1.0595
86+
yield 36
87+
synth.release_all()
88+
yield 36
89+
90+
91+
def chain(*args):
92+
for a in args:
93+
yield from a
94+
95+
96+
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
97+
with wave.open("fir.wav", "w") as f:
98+
f.setnchannels(1)
99+
f.setsampwidth(2)
100+
f.setframerate(48000)
101+
for n in chain(synthesize(synth)):
102+
for i in range(n):
103+
result, data = audiocore.get_buffer(synth)
104+
f.writeframes(data)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
sine = np.array(
17+
np.sin(np.linspace(0, 2 * np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
18+
dtype=np.int16,
19+
)
20+
21+
envelope = synthio.Envelope(
22+
attack_time=0.1, decay_time=0.05, release_time=0.2, attack_level=0.8, sustain_level=0.8
23+
)
24+
25+
synth = synthio.Synthesizer(sample_rate=48000, channel_count=2)
26+
27+
28+
def synthesize(synth):
29+
n = synthio.Note(
30+
frequency=440,
31+
waveform=sine,
32+
envelope=envelope,
33+
)
34+
35+
print(synth, n)
36+
synth.press((n,))
37+
for p in range(-10, 11, 1):
38+
n.panning = p / 10
39+
yield 36
40+
synth.release_all()
41+
yield 36
42+
43+
44+
def chain(*args):
45+
for a in args:
46+
yield from a
47+
48+
49+
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
50+
with wave.open("panning.wav", "w") as f:
51+
f.setnchannels(2)
52+
f.setsampwidth(2)
53+
f.setframerate(48000)
54+
for n in chain(synthesize(synth)):
55+
for i in range(n):
56+
result, data = audiocore.get_buffer(synth)
57+
f.writeframes(data)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 = 32767
16+
sine = np.array(
17+
np.sin(np.linspace(0, 2 * np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
18+
dtype=np.int16,
19+
)
20+
21+
envelope = synthio.Envelope(
22+
attack_time=0.1, decay_time=0.05, release_time=0.2, attack_level=0.8, sustain_level=0.8
23+
)
24+
25+
synth = synthio.Synthesizer(sample_rate=48000)
26+
27+
28+
def synthesize(synth):
29+
n = synthio.Note(
30+
frequency=120,
31+
waveform=sine,
32+
ring_waveform=sine,
33+
ring_frequency=769,
34+
envelope=envelope,
35+
bend_mode=synthio.BendType.VIBRATO,
36+
bend_depth=50 / 1200,
37+
bend_rate=7,
38+
)
39+
40+
print(synth, n)
41+
synth.press((n,))
42+
yield 720
43+
synth.release_all()
44+
yield 36
45+
46+
47+
def chain(*args):
48+
for a in args:
49+
yield from a
50+
51+
52+
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
53+
with wave.open("ring.wav", "w") as f:
54+
f.setnchannels(1)
55+
f.setsampwidth(2)
56+
f.setframerate(48000)
57+
for n in chain(synthesize(synth)):
58+
for i in range(n):
59+
result, data = audiocore.get_buffer(synth)
60+
f.writeframes(data)

0 commit comments

Comments
 (0)