Last active
July 26, 2021 23:32
-
-
Save todbot/b3fbf5ee770cbbdd9e8a81d8ca9a1206 to your computer and use it in GitHub Desktop.
play random melody in scale, w/ LEDS & LCD accompaniment (CircuitPython on Adafruit FunHouse) video: https://twitter.com/todbot/status/1416139823383941120
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# random_notes_scale2.py - play random melody in scale, w/ LEDS & LCD accompaniment | |
# 16 Jul 2021 - @todbot | |
import time, random, math | |
import board, digitalio, pwmio | |
import adafruit_dotstar # or neopixel | |
import displayio, terminalio | |
from adafruit_display_text import label | |
leds = adafruit_dotstar.DotStar(board.DOTSTAR_CLOCK, board.DOTSTAR_DATA, 5, brightness=0.1) | |
mylabel = label.Label(terminalio.FONT, text="Music???", x=40,y=50, scale=4, color=0xff00ff) | |
display = board.DISPLAY | |
display.rotation=0 | |
display.show(mylabel) | |
f0 = 220 # A3 | |
fr = 2**(1/12) # freqency spacing between notes, approx 1.06 | |
scale = ( 0, 2, 4, 5, 7, 9, 10) # mixolydian | |
out1 = pwmio.PWMOut(board.SPEAKER,frequency=f0,duty_cycle=45000,variable_frequency=True) | |
while True: | |
octave = random.randint(-1,1) | |
nsemi = random.choice(scale) - 5 + 12*octave # pick random semitone from scale | |
f_new = min(max(f0 * fr**nsemi, f0//2), f0*4) # get new note freqency, A2 to A5, approx | |
out1.frequency = int(f_new) | |
crand = int(random.randint(0,0xffffff)) # random color | |
leds[ int(f_new) % len(leds) ] = crand # put rand color on LED based on frequency | |
print(time.monotonic(),out1.frequency,nsemi,octave) | |
mylabel.x = random.randint(5,50) # move label around a bit for fun | |
mylabel.y = random.randint(10,200) | |
mylabel.color = crand | |
display.refresh() | |
time.sleep(0.12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment