Let's get to some real-world examples! These are adapted from Tod Kurt's excellent CircuitPython Synthio Tricks page.
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) synth = synthio.Synthesizer(sample_rate=44100) audio.play(synth) while True: synth.press(65) # midi note 65 = F4 time.sleep(0.5) synth.release(65) # release the note we pressed time.sleep(2)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) synth = synthio.Synthesizer(sample_rate=44100) audio.play(synth) while True: synth.press((65, 69, 72)) # midi note 65 = F4 time.sleep(1) synth.release((65, 69, 72)) # release the note we pressed time.sleep(2)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import audiomixer import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048) synth = synthio.Synthesizer(channel_count=1, sample_rate=22050) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.4 while True: synth.press((65, 69, 72)) # midi note 65 = F4 time.sleep(0.5) synth.release((65, 69, 72)) # release the note we pressed time.sleep(0.5) mixer.voice[0].level = (mixer.voice[0].level - 0.1) % 0.4 # reduce volume each pass
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import audiomixer import synthio # for PWM audio with an RC filter #import audiopwmio #audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048) amp_env_slow = synthio.Envelope( attack_time=0.2, sustain_level=1.0, release_time=0.8 ) amp_env_fast = synthio.Envelope( attack_time=0.1, sustain_level=0.5, release_time=0.2 ) synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_slow) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.2 while True: synth.envelope = amp_env_slow synth.press(46) time.sleep(1.25) synth.release(46) time.sleep(1.25) synth.envelope = amp_env_fast synth.press(51) time.sleep(1.25) synth.release(51) time.sleep(1.25)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import audiomixer import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048) amp_env_slow = synthio.Envelope( attack_time=0.2, sustain_level=1.0, release_time=0.8 ) synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_slow) # set up filters frequency = 2000 resonance = 1.5 lpf = synth.low_pass_filter(frequency, resonance) hpf = synth.high_pass_filter(frequency, resonance) bpf = synth.band_pass_filter(frequency, resonance) note1 = synthio.Note(frequency=330, filter=None) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.2 while True: # no filter note1.filter = None synth.press(note1) time.sleep(1.25) synth.release(note1) time.sleep(1.25) # lpf note1.filter = lpf synth.press(note1) time.sleep(1.25) synth.release(note1) time.sleep(1.25) # hpf note1.filter = hpf synth.press(note1) time.sleep(1.25) synth.release(note1) time.sleep(1.25) # bpf note1.filter = bpf synth.press(note1) time.sleep(1.25) synth.release(note1) time.sleep(1.25)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import board import digitalio import audiomixer import synthio import usb_midi import adafruit_midi from adafruit_midi.note_on import NoteOn from adafruit_midi.note_off import NoteOff # note for ESP32-S2 boards, due to not enough available endpoints, # to enable USB MIDI, create a "boot.py" with following in it, then power cycle board: # import usb_hid # import usb_midi # usb_hid.disable() # usb_midi.enable() # print("enabled USB MIDI, disabled USB HID") # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048) midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0) amp_env_med = synthio.Envelope( attack_time=0.05, sustain_level=0.8, release_time=0.2 ) synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_med) note1 = synthio.Note(frequency=330, filter=None) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.2 while True: msg = midi.receive() if isinstance(msg, NoteOn) and msg.velocity != 0: print("noteOn: ", msg.note, "vel:", msg.velocity) note1.frequency = synthio.midi_to_hz(msg.note) synth.press(note1) elif isinstance(msg, NoteOff) or isinstance(msg, NoteOn) and msg.velocity == 0: print("noteOff:", msg.note, "vel:", msg.velocity) note1.frequency = synthio.midi_to_hz(msg.note) synth.release(note1)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import board import busio import digitalio import audiomixer import synthio import usb_midi import adafruit_midi from adafruit_midi.note_on import NoteOn from adafruit_midi.note_off import NoteOff from adafruit_midi.control_change import ControlChange # note for ESP32-S2 boards, due to not enough available endpoints, # to enable USB MIDI, create a "boot.py" with following in it, then power cycle board: # import usb_hid # import usb_midi # usb_hid.disable() # usb_midi.enable() # print("enabled USB MIDI, disabled USB HID") # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048) midi_channel = 1 uart = busio.UART(tx=board.TX, rx=board.RX, baudrate=31250, timeout=0.001) midi_usb = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0) midi_uart = adafruit_midi.MIDI(midi_in=uart, in_channel=midi_channel-1) amp_env_med = synthio.Envelope( attack_time=0.05, sustain_level=0.8, release_time=0.2 ) synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_med) # set up filters filter_freq = 4000 filter_res = 0.5 filter_freq_lo = 100 # filter lowest freq filter_freq_hi = 4500 # filter highest freq filter_res_lo = 0.5 # filter q lowest value filter_res_hi = 2.0 # filter q highest value lpf = synth.low_pass_filter(filter_freq, filter_res) note1 = synthio.Note(frequency=330, filter=lpf) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.2 def map_range(s, a1, a2, b1, b2): return b1 + ((s - a1) * (b2 - b1) / (a2 - a1)) while True: note1.filter = synth.low_pass_filter(filter_freq, filter_res) msg = midi_uart.receive() or midi_usb.receive() if isinstance(msg, NoteOn) and msg.velocity != 0: print("noteOn: ", msg.note, "vel:", msg.velocity) note1.frequency = synthio.midi_to_hz(msg.note) synth.press(note1) elif isinstance(msg, NoteOff) or isinstance(msg, NoteOn) and msg.velocity == 0: print("noteOff:", msg.note, "vel:", msg.velocity) note1.frequency = synthio.midi_to_hz(msg.note) synth.release(note1) elif isinstance(msg, ControlChange): print("CC", msg.control, "=", msg.value) if msg.control == 21: # filter cutoff filter_freq = map_range(msg.value, 0, 127, filter_freq_lo, filter_freq_hi) elif msg.control == 22: # filter Q filter_res = map_range(msg.value, 0, 127, filter_res_lo, filter_res_hi) elif msg.control == 7: # volume mixer.voice[0].level = map_range(msg.value, 0, 127, 0.0, 1.0)
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import board import digitalio import audiomixer import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=44100, buffer_size=4096) synth = synthio.Synthesizer(channel_count=1, sample_rate=44100) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.1 lfo = synthio.LFO(rate=0.6, scale=0.05) # 1 Hz lfo at 0.25% midi_note = 52 note = synthio.Note(synthio.midi_to_hz(midi_note), bend=lfo) synth.press(note) while True: pass
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt # # SPDX-License-Identifier: MIT import time import board import digitalio import audiomixer import synthio # for PWM audio with an RC filter # import audiopwmio # audio = audiopwmio.PWMAudioOut(board.GP10) # for I2S audio with external I2S DAC board import audiobusio # I2S on Audio BFF or Amp BFF on QT Py: # audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1) # I2S audio on PropMaker Feather RP2040 power = digitalio.DigitalInOut(board.EXTERNAL_POWER) power.switch_to_output(value=True) audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) mixer = audiomixer.Mixer(channel_count=1, sample_rate=44100, buffer_size=4096) synth = synthio.Synthesizer(channel_count=1, sample_rate=44100) audio.play(mixer) mixer.voice[0].play(synth) mixer.voice[0].level = 0.2 lfo_tremolo = synthio.LFO(rate=0.8, scale=0.3, offset=0.7) midi_note = 50 note1 = synthio.Note(synthio.midi_to_hz(midi_note), amplitude=lfo_tremolo) synth.press(note1) while True: print(lfo_tremolo.value) time.sleep(0.05)
Text editor powered by tinymce.