import board # type: ignore
import busio
import math
import digitalio
import time
from adafruit_mcp4725 import MCP4725
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
import audiomixer, audiocore

num_voices = 12
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=16000, channel_count=1, bits_per_sample=16, samples_signed=True)
path = "notes/"

notes_strings = ["C", "D_flat", "D", "E_flat", "E", "F", "G_flat", "G", "A_flat", "A", "B_flat", "B"]
volume = .5
notes_strings = [_ + ".wav" for _ in notes_strings]

audio = AudioOut(board.GP15)
audio.play(mixer)

# pins 0 through 11
button_pins = [board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8, board.GP9, board.GP10, board.GP11]
buttons = [digitalio.DigitalInOut(_) for  _ in button_pins]
for _ in buttons:
    _.switch_to_input(pull=digitalio.Pull.UP)


for i in range(len(notes_strings)):
    wave = audiocore.WaveFile(open(path + notes_strings[i], "rb"))
    mixer.voice[i].play(wave, loop=True)
    mixer.voice[i].level = 0.0

playing = [False] * 12
while True:
    for button_ in range(len(buttons)):
        button = buttons[button_]
        if not any(playing) and not button.value:
            print(f"playing button {button_} and volume {volume}")
            playing[button_] = True
            mixer.voice[button_].level = volume

        if button.value:
            playing[button_] = False
            mixer.voice[button_].level = 0