Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created December 23, 2023 16:53
Show Gist options
  • Save rsbohn/dee4ef7191360ff9061cfa570210bd96 to your computer and use it in GitHub Desktop.
Save rsbohn/dee4ef7191360ff9061cfa570210bd96 to your computer and use it in GitHub Desktop.
Plays random notes.
"MusicBox"
# SPDX-FileCopyrightText: 2023 Randall Bohn
# SPDX-License-Identifier: MIT
# Feather RP2040 with speaker on A0
import time
import board
import audiomixer
import audiopwmio
import random
import synthio
RATE=44000
dac = audiopwmio.PWMAudioOut(board.A0)
mix = audiomixer.Mixer(channel_count=1, sample_rate=RATE)
synth = synthio.Synthesizer(sample_rate=RATE)
slow = synthio.Envelope(attack_time=0.2, sustain_level=1.0, release_time=0.8)
pluck = synthio.Envelope(attack_time=0, decay_time=0.4, sustain_level=0)
synth.envelope=pluck
dac.play(mix)
mix.voice[0].play(synth)
mix.voice[0].level=0.2
aeolian = [55, 57, 58, 60, 62, 63, 65]
b_flat = [58, 60, 62, 63, 65, 67, 69]
while True:
note = random.choice(aeolian)
synth.press(note)
time.sleep(random.choice([0.2,0.4,0.4,0.4,0.8]))
synth.release(note)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment