Putting it all together

A project log for Adventures in Bleep Bloops with CircuitPython

Experiments with polyphonic pythonic tone playing

foamyguyfoamyguy 01/26/2020 at 23:080 Comments

Now we have 3 octaves of tone musical note wave files and some code that plays wave files when we press buttons. Lets smash those together and see where we end up.

import board
import audioio
import digitalio
import time
import adafruit_trellism4
trellis = adafruit_trellism4.TrellisM4Express()
trellis.pixels.brightness = 0.15
note_letters = ['c', 'd', 'e', 'f', 'g', 'a', 'b']
colors = [(255, 0, 0), (255, 127, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (46, 43, 95), (139, 0, 255)]
notes = {}

for octave in range(3,6):
    for note_letter in note_letters:
        cur_note = "%s%s" % (note_letter, octave)
        notes[cur_note] = audioio.WaveFile(open("notes/%s.wav" % (cur_note), "rb"))

audio = audioio.AudioOut(left_channel=board.A0, right_channel=board.A1)
mixer = audioio.Mixer(voice_count=7, sample_rate=8000, channel_count=2, bits_per_sample=16, samples_signed=True)

for i, color in enumerate(colors):
    trellis.pixels[i,0] = color
    trellis.pixels[i,1] = color
    trellis.pixels[i,2] = color

prev_pressed = []
while True:
    cur_keys = trellis.pressed_keys
    if cur_keys != prev_pressed:
        print("different than last iter")
        pressed_key_xs = []
        if cur_keys != []:
            for key in cur_keys:
                if key[0] < len(note_letters):
                    note_for_key = "%s%s" % (note_letters[key[0]], key[1]+3)
                    if note_for_key in notes:
              [note_for_key], voice=key[0], loop=True)

        if mixer.playing:
            for i in range(0,7):
                if i not in pressed_key_xs:
                    print("stopping %s" % i)

    prev_pressed = cur_keys

There we have it, polyphonic playback at a good volume. It seems lots of the clicking and other sounds have been minimized as well with this version of the program. There is a bit of a white noise hum in the background of everything but much better than some of the previous iterations. 

The neotrellis library makes it so easy to set the on-board pixels so why not make a nice rainbow across the buttons?

The wave file approach is kind of nice because we could probably switch out the tone files for other instruments. This will let us change the way the toy sounds without having to re-write the code, just swap out wave files.