Skip to content

Commit a980f68

Browse files
authored
Merge pull request #2629 from jedgarpark/synthio-fundamentals
synthio fundamentals example code and wave files
2 parents c6ddbde + 8732b70 commit a980f68

18 files changed

+884
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import synthio
9+
10+
# for PWM audio with an RC filter
11+
# import audiopwmio
12+
# audio = audiopwmio.PWMAudioOut(board.GP10)
13+
14+
# for I2S audio with external I2S DAC board
15+
import audiobusio
16+
17+
# I2S on Audio BFF or Amp BFF on QT Py:
18+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
19+
20+
# I2S audio on PropMaker Feather RP2040
21+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
22+
power.switch_to_output(value=True)
23+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
24+
25+
synth = synthio.Synthesizer(sample_rate=44100)
26+
audio.play(synth)
27+
28+
while True:
29+
synth.press(65) # midi note 65 = F4
30+
time.sleep(0.5)
31+
synth.release(65) # release the note we pressed
32+
time.sleep(2)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import synthio
9+
10+
# for PWM audio with an RC filter
11+
# import audiopwmio
12+
# audio = audiopwmio.PWMAudioOut(board.GP10)
13+
14+
# for I2S audio with external I2S DAC board
15+
import audiobusio
16+
17+
# I2S on Audio BFF or Amp BFF on QT Py:
18+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
19+
20+
# I2S audio on PropMaker Feather RP2040
21+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
22+
power.switch_to_output(value=True)
23+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
24+
25+
synth = synthio.Synthesizer(sample_rate=44100)
26+
audio.play(synth)
27+
28+
while True:
29+
synth.press((65, 69, 72)) # midi note 65 = F4
30+
time.sleep(1)
31+
synth.release((65, 69, 72)) # release the note we pressed
32+
time.sleep(2)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import audiomixer
9+
import synthio
10+
11+
# for PWM audio with an RC filter
12+
# import audiopwmio
13+
# audio = audiopwmio.PWMAudioOut(board.GP10)
14+
15+
# for I2S audio with external I2S DAC board
16+
import audiobusio
17+
18+
# I2S on Audio BFF or Amp BFF on QT Py:
19+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
20+
21+
# I2S audio on PropMaker Feather RP2040
22+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
23+
power.switch_to_output(value=True)
24+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
25+
26+
mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048)
27+
synth = synthio.Synthesizer(channel_count=1, sample_rate=22050)
28+
29+
audio.play(mixer)
30+
mixer.voice[0].play(synth)
31+
mixer.voice[0].level = 0.4
32+
33+
while True:
34+
synth.press((65, 69, 72)) # midi note 65 = F4
35+
time.sleep(0.5)
36+
synth.release((65, 69, 72)) # release the note we pressed
37+
time.sleep(0.5)
38+
mixer.voice[0].level = (mixer.voice[0].level - 0.1) % 0.4 # reduce volume each pass
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import audiomixer
9+
import synthio
10+
11+
# for PWM audio with an RC filter
12+
#import audiopwmio
13+
#audio = audiopwmio.PWMAudioOut(board.GP10)
14+
15+
# for I2S audio with external I2S DAC board
16+
import audiobusio
17+
18+
# I2S on Audio BFF or Amp BFF on QT Py:
19+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
20+
21+
# I2S audio on PropMaker Feather RP2040
22+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
23+
power.switch_to_output(value=True)
24+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
25+
26+
mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048)
27+
28+
amp_env_slow = synthio.Envelope(
29+
attack_time=0.2,
30+
sustain_level=1.0,
31+
release_time=0.8
32+
)
33+
34+
amp_env_fast = synthio.Envelope(
35+
attack_time=0.1,
36+
sustain_level=0.5,
37+
release_time=0.2
38+
)
39+
40+
41+
synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_slow)
42+
43+
audio.play(mixer)
44+
mixer.voice[0].play(synth)
45+
mixer.voice[0].level = 0.2
46+
47+
while True:
48+
synth.envelope = amp_env_slow
49+
synth.press(46)
50+
time.sleep(1.25)
51+
synth.release(46)
52+
time.sleep(1.25)
53+
54+
synth.envelope = amp_env_fast
55+
synth.press(51)
56+
time.sleep(1.25)
57+
synth.release(51)
58+
time.sleep(1.25)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import audiomixer
9+
import synthio
10+
11+
# for PWM audio with an RC filter
12+
# import audiopwmio
13+
# audio = audiopwmio.PWMAudioOut(board.GP10)
14+
15+
# for I2S audio with external I2S DAC board
16+
import audiobusio
17+
18+
# I2S on Audio BFF or Amp BFF on QT Py:
19+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
20+
21+
# I2S audio on PropMaker Feather RP2040
22+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
23+
power.switch_to_output(value=True)
24+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
25+
26+
mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048)
27+
28+
amp_env_slow = synthio.Envelope(
29+
attack_time=0.2,
30+
sustain_level=1.0,
31+
release_time=0.8
32+
)
33+
34+
synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_slow)
35+
36+
# set up filters
37+
frequency = 2000
38+
resonance = 1.5
39+
lpf = synth.low_pass_filter(frequency, resonance)
40+
hpf = synth.high_pass_filter(frequency, resonance)
41+
bpf = synth.band_pass_filter(frequency, resonance)
42+
43+
note1 = synthio.Note(frequency=330, filter=None)
44+
45+
audio.play(mixer)
46+
mixer.voice[0].play(synth)
47+
mixer.voice[0].level = 0.2
48+
49+
while True:
50+
# no filter
51+
note1.filter = None
52+
synth.press(note1)
53+
time.sleep(1.25)
54+
synth.release(note1)
55+
time.sleep(1.25)
56+
57+
# lpf
58+
note1.filter = lpf
59+
synth.press(note1)
60+
time.sleep(1.25)
61+
synth.release(note1)
62+
time.sleep(1.25)
63+
64+
# hpf
65+
note1.filter = hpf
66+
synth.press(note1)
67+
time.sleep(1.25)
68+
synth.release(note1)
69+
time.sleep(1.25)
70+
71+
# bpf
72+
note1.filter = bpf
73+
synth.press(note1)
74+
time.sleep(1.25)
75+
synth.release(note1)
76+
time.sleep(1.25)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2023 John Park and @todbot / Tod Kurt
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
import digitalio
7+
import audiomixer
8+
import synthio
9+
import usb_midi
10+
import adafruit_midi
11+
from adafruit_midi.note_on import NoteOn
12+
from adafruit_midi.note_off import NoteOff
13+
14+
# note for ESP32-S2 boards, due to not enough available endpoints,
15+
# to enable USB MIDI, create a "boot.py" with following in it, then power cycle board:
16+
# import usb_hid
17+
# import usb_midi
18+
# usb_hid.disable()
19+
# usb_midi.enable()
20+
# print("enabled USB MIDI, disabled USB HID")
21+
22+
23+
# for PWM audio with an RC filter
24+
# import audiopwmio
25+
# audio = audiopwmio.PWMAudioOut(board.GP10)
26+
27+
# for I2S audio with external I2S DAC board
28+
import audiobusio
29+
30+
# I2S on Audio BFF or Amp BFF on QT Py:
31+
# audio = audiobusio.I2SOut(bit_clock=board.A3, word_select=board.A2, data=board.A1)
32+
33+
# I2S audio on PropMaker Feather RP2040
34+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
35+
power.switch_to_output(value=True)
36+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
37+
38+
mixer = audiomixer.Mixer(channel_count=1, sample_rate=22050, buffer_size=2048)
39+
40+
midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0)
41+
42+
amp_env_med = synthio.Envelope(
43+
attack_time=0.05,
44+
sustain_level=0.8,
45+
release_time=0.2
46+
)
47+
48+
synth = synthio.Synthesizer(channel_count=1, sample_rate=22050, envelope=amp_env_med)
49+
50+
note1 = synthio.Note(frequency=330, filter=None)
51+
52+
audio.play(mixer)
53+
mixer.voice[0].play(synth)
54+
mixer.voice[0].level = 0.2
55+
56+
while True:
57+
msg = midi.receive()
58+
if isinstance(msg, NoteOn) and msg.velocity != 0:
59+
print("noteOn: ", msg.note, "vel:", msg.velocity)
60+
note1.frequency = synthio.midi_to_hz(msg.note)
61+
synth.press(note1)
62+
elif isinstance(msg, NoteOff) or isinstance(msg, NoteOn) and msg.velocity == 0:
63+
print("noteOff:", msg.note, "vel:", msg.velocity)
64+
note1.frequency = synthio.midi_to_hz(msg.note)
65+
synth.release(note1)

0 commit comments

Comments
 (0)