Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active May 11, 2023 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todbot/a8b7bc0ea9cf850970ed2f1eb4e1db86 to your computer and use it in GitHub Desktop.
Save todbot/a8b7bc0ea9cf850970ed2f1eb4e1db86 to your computer and use it in GitHub Desktop.
Show new synthio.Note API that has per-note envelope, waveform, vibrato, tremolo, and frequency
# synthio_note_vibrato_demo.py --
# 10 May 2023 - @todbot / Tod Kurt
# Uses cheapie PCM5102 DAC on QTPY RP2040
import time,random
import board, analogio
import audiobusio, audiomixer
import synthio
import ulab.numpy as np
# qtpy rp2040 SPI pins
lck_pin, bck_pin, dat_pin = board.MISO, board.MOSI, board.SCK
SAMPLE_RATE = 28000 # clicks @ 36kHz & 48kHz on rp2040
SAMPLE_SIZE = 256
VOLUME = 12000
waveform = np.linspace(VOLUME, -VOLUME, num=SAMPLE_SIZE, dtype=np.int16)
amp_env = synthio.Envelope(attack_time=0.3, decay_time = 0.05, release_time=0.4,
attack_level=1, sustain_level=1.0)
synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE, waveform=waveform, envelope=amp_env)
audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin)
mixer = audiomixer.Mixer(voice_count=1, sample_rate=SAMPLE_RATE, channel_count=1,
bits_per_sample=16, samples_signed=True, buffer_size=2048 ) # buffer_size=4096 )
audio.play(mixer)
mixer.voice[0].play(synth)
notes = [45, 57, 52]
last_note_time = 0
note_i=0
while True:
if time.monotonic() - last_note_time > 1.0:
last_note_time = time.monotonic()
n = notes[note_i] # get a MIDI note number (e.g. 45)
freq = synthio.midi_to_hz(n) # turn that into a frequency in Hertz
note = synthio.Note( frequency=freq, envelope=amp_env,
vibrato_depth=0.1, vibrato_rate=5 )
synth.release_all_then_press( (note,) )
note_i = (note_i+1) % len(notes)
@todbot
Copy link
Author

todbot commented May 11, 2023

Here's what it sounds like:

synthio_note_vibrato_demo.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment