handleNote basics

This commit is contained in:
GitBluub
2022-12-21 21:37:37 +09:00
parent 4e73f25238
commit 4155b2ce71
4 changed files with 60 additions and 44 deletions
+10
View File
@@ -0,0 +1,10 @@
class Key:
def __init__(self, key: int, start: int, duration: int):
self.key = key
self.start = start
self.duration = duration
def __str__(self):
return f"{self.key} ({self.start} - {self.duration})"
+1 -3
View File
@@ -1,5 +1,3 @@
class Note:
def __init__(self, start_time, data) -> None:
@@ -10,4 +8,4 @@ class Note:
return self.__start_time
def get_data(self):
return self.__data
return self.__data
+3 -28
View File
@@ -1,40 +1,15 @@
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
from .Key import Key
class Partition:
def __init__(self, name:str, notes:list[Note]) -> None:
def __init__(self, name:str, notes:list[Key]) -> None:
self.__name = name
self.__notes = notes
async def play(self, output_lambda:Callable[[object], None]):
now = datetime.datetime.now()
tasks_to_wait = []
for note in self.__notes:
tasks_to_wait.append(
asyncio.create_task(
run_at(
now + datetime.timedelta(milliseconds= note.get_start_time()),
output_lambda(note.get_data())
)
)
)
await asyncio.wait(tasks_to_wait)
def __repr__(self):
r = f"{self.__name}\n"
for i in self.__notes:
r += f"{i.get_data()}\n"
r += f"{i.__repr__()}\n"
return r