Skip to content

Commit 0723590

Browse files
authored
Merge pull request #21 from FoamyGuy/master
adding wave file synth example
2 parents ae2079f + 621066b commit 0723590

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+109
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
This synthesizer is loaded with wave files for 3 octaves of notes each in 4 different waveforms.
3+
It uses Mixer to play up to 7 notes at once.
4+
Play notes with the rainbow buttons. Change waveform types ith the white buttons in the last column.
5+
"""
6+
import board
7+
from audiocore import WaveFile
8+
from audioio import AudioOut
9+
from audiomixer import Mixer
10+
import adafruit_trellism4
11+
12+
# trellis helper object
13+
trellis = adafruit_trellism4.TrellisM4Express()
14+
# low brightness on the neopixles
15+
trellis.pixels.brightness = 0.05
16+
# each musical note letter
17+
note_letters = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
18+
# colors of the rainbow
19+
colors = [
20+
(255, 0, 0), (255, 127, 0), (255, 255, 0),
21+
(0, 255, 0), (0, 0, 255), (56, 43, 105), (139, 0, 255)]
22+
23+
# dictionary holding note string to wave file value.
24+
# e.g. {... "sined4": audioio.WaveFile(open("notes/sine/d4.wav")), ...}
25+
notes = {}
26+
27+
# list of all waveform types
28+
WAVE_TYPES = ["sine", "square", "sawtooth", "triangle"]
29+
30+
# current waveform type. Will get changed from the last column
31+
current_wave_type = "sine"
32+
33+
# load the notes dictionary
34+
for wave_type in WAVE_TYPES:
35+
for octave in range(3, 6): # [3,4,5]
36+
for note_letter in note_letters:
37+
# note with octave e.g. a4
38+
cur_note = "{}{}".format(note_letter, octave)
39+
# add wave file to dictionary
40+
key = "{}{}".format(wave_type, cur_note)
41+
notes[key] = WaveFile(
42+
open("notes/{}/{}.wav".format(wave_type, cur_note), "rb"))
43+
44+
# main audio object
45+
audio = AudioOut(left_channel=board.A0, right_channel=board.A1)
46+
# mixer to allow pylyphonic playback
47+
mixer = Mixer(
48+
voice_count=8, sample_rate=8000,
49+
channel_count=2, bits_per_sample=16,
50+
samples_signed=True)
51+
52+
audio.play(mixer)
53+
54+
# turn on the rainbow lights
55+
for i, color in enumerate(colors):
56+
trellis.pixels[i, 0] = color
57+
trellis.pixels[i, 1] = color
58+
trellis.pixels[i, 2] = color
59+
60+
# list of keys pressed on the previous iteration
61+
prev_pressed = []
62+
63+
# voice recycling variables
64+
available_voices = [1, 2, 3, 4, 5, 6, 7]
65+
# key to voice dictionary e.g. {... (1,2):4, (1,3):3, ...}
66+
used_voices = {}
67+
68+
# waveform selector in the last column
69+
# default to index 0 sine
70+
trellis.pixels[7, 0] = (255, 255, 255)
71+
while True:
72+
cur_keys = trellis.pressed_keys
73+
# if the keys are different from previous iteration
74+
if cur_keys != prev_pressed:
75+
# loop over currently pressed keys
76+
for key in cur_keys:
77+
# if it's a note key. First 7 columns.
78+
if key[0] < len(note_letters):
79+
# if we aren't already playing this note and we have available voice
80+
if key not in used_voices.keys() and available_voices:
81+
# build not string
82+
note_for_key = "{}{}".format(note_letters[key[0]], key[1]+3)
83+
note_to_play = "{}{}".format(current_wave_type, note_for_key)
84+
# if the note exists in the notes dictionary
85+
if note_to_play in notes:
86+
# get an available voice
87+
voice_to_use = available_voices.pop()
88+
used_voices[key] = voice_to_use
89+
# play the note
90+
mixer.play(notes[note_to_play], voice=voice_to_use, loop=True)
91+
else: # last column
92+
current_wave_type = WAVE_TYPES[key[1]]
93+
# turn off all last column pixels
94+
for y_pixel in range(0, 4):
95+
trellis.pixels[7, y_pixel] = (0, 0, 0)
96+
# turn on selected
97+
trellis.pixels[7, key[1]] = (255, 255, 255)
98+
if mixer.playing:
99+
# loop over each note that is playing
100+
for key in used_voices:
101+
# if the key is no longer down
102+
if key not in cur_keys:
103+
# stop playing
104+
mixer.stop_voice(used_voices[key])
105+
# recycle voice
106+
available_voices.append(used_voices[key])
107+
used_voices.pop(key, None)
108+
# update variable for next iteration
109+
prev_pressed = cur_keys

0 commit comments

Comments
 (0)