circuitpython/tests/circuitpython-manual/synthio/note/ring.py

67 lines
1.4 KiB
Python

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)
SAMPLE_SIZE = 1024
VOLUME = 32767
sine = np.array(
np.sin(np.linspace(0, 2 * np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
dtype=np.int16,
)
envelope = synthio.Envelope(
attack_time=0.1, decay_time=0.05, release_time=0.2, attack_level=0.8, sustain_level=0.8
)
synth = synthio.Synthesizer(sample_rate=48000)
bend_out = np.linspace(0, 32767, num=SAMPLE_SIZE, endpoint=True, dtype=np.int16)
def synthesize(synth):
n = synthio.Note(
frequency=440,
waveform=sine,
ring_waveform=sine,
ring_frequency=769,
envelope=envelope,
bend=synthio.LFO(bend_out, scale=50 / 1200, rate=7),
)
print(synth, n)
synth.press((n,))
yield 720
synth.release_all()
yield 36
n.ring_frequency = 0
print(synth, n)
synth.press((n,))
yield 720
synth.release_all()
yield 36
def chain(*args):
for a in args:
yield from a
# sox -r 48000 -e signed -b 16 -c 1 tune.raw tune.wav
with wave.open("ring.wav", "w") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(48000)
for n in chain(synthesize(synth)):
for i in range(n):
result, data = audiocore.get_buffer(synth)
f.writeframes(data)