first push
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class Note:
|
||||||
|
def __init__(self, key, color, start_time, duration) -> None:
|
||||||
|
|
||||||
|
self.__key = key
|
||||||
|
self.__color = color
|
||||||
|
self.__start_time = start_time
|
||||||
|
self.__duration = duration
|
||||||
|
|
||||||
|
def get_key(self):
|
||||||
|
return self.__key
|
||||||
|
|
||||||
|
def get_start_time(self):
|
||||||
|
return self.__start_time
|
||||||
|
|
||||||
|
def get_duration(self):
|
||||||
|
return self.__duration
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import asyncio, datetime
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from .Note import Note
|
||||||
|
|
||||||
|
async def wait_until(dt):
|
||||||
|
# sleep until the specified datetime
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
await asyncio.sleep((dt - now).total_seconds())
|
||||||
|
|
||||||
|
async def run_at(dt, coro):
|
||||||
|
await wait_until(dt)
|
||||||
|
return await coro
|
||||||
|
|
||||||
|
|
||||||
|
class Partition:
|
||||||
|
|
||||||
|
def __init__(self, name:str, notes:list[Note]) -> None:
|
||||||
|
|
||||||
|
self.__name = name
|
||||||
|
self.__notes = notes
|
||||||
|
|
||||||
|
|
||||||
|
async def play(self, output_lambda:Callable([str, tuple[int, int, int], int], None)):
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
for note in self.__notes:
|
||||||
|
asyncio.create_task(run_at(
|
||||||
|
now + datetime.time(milliseconds = note.get_start_time()),
|
||||||
|
output_lambda(
|
||||||
|
note.get_key(),
|
||||||
|
note.get_color(),
|
||||||
|
note.get_duration()
|
||||||
|
)
|
||||||
|
))
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import board
|
||||||
|
import neopixel
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
colorToFill = (0, 0, 0)
|
||||||
|
pixels = neopixel.NeoPIxel(board.D18, 20, brightness=0.01)
|
||||||
|
|
||||||
|
notePixels = { 'si': [0, 1],
|
||||||
|
'la#': [2, 3],
|
||||||
|
'la': [4, 5],
|
||||||
|
'sol#':[6],
|
||||||
|
'sol':[7, 8, 9],
|
||||||
|
'fa#':[10],
|
||||||
|
'fa':[11, 12, 13],
|
||||||
|
'mi':[14, 15, 16],
|
||||||
|
're#':[17],
|
||||||
|
're':[18, 19],
|
||||||
|
'do#':[],
|
||||||
|
'do':[]}
|
||||||
|
|
||||||
|
def playNote(color, secondsToStay, pixelsToFill):
|
||||||
|
for pixelIndex in pixelsToFill:
|
||||||
|
pixels[pixelIndex] = color
|
||||||
|
time.sleep(secondsToStay)
|
||||||
|
|
||||||
|
def incomingNote(color, time, pixelsToFill):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def launchMusic(noteList):
|
||||||
|
|
||||||
|
pixels.fill(0,0,0)
|
||||||
|
pixels.write()
|
||||||
|
|
||||||
|
for notes, tempo in noteList:
|
||||||
|
for note in notes:
|
||||||
|
playNote((255, 0, 0), tempo, notePixels[note.lower()])
|
||||||
|
pixels.fill(colorToFill)
|
||||||
|
pixels.write()
|
||||||
|
|
||||||
|
music = [
|
||||||
|
(['sol'], 1),
|
||||||
|
(['sol'], 1),
|
||||||
|
(['sol'], 1),
|
||||||
|
(['re#'], 1),
|
||||||
|
(['la#'], 0.5),
|
||||||
|
(['sol'], 0.5),
|
||||||
|
(['re#'], 1),
|
||||||
|
(['la#'], 0.5),
|
||||||
|
]
|
||||||
|
|
||||||
|
launchMusic(music)
|
||||||
Reference in New Issue
Block a user