asyncio working
This commit is contained in:
@@ -20,22 +20,20 @@ class Partition:
|
||||
self.__notes = notes
|
||||
|
||||
|
||||
def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]):
|
||||
async def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]):
|
||||
now = datetime.datetime.now()
|
||||
tasks_to_wait = []
|
||||
for note in self.__notes:
|
||||
tasks_to_wait.append(
|
||||
asyncio.create_task(
|
||||
lambda: asyncio.wait(
|
||||
run_at(
|
||||
now + datetime.timedelta(milliseconds= note.get_start_time()),
|
||||
output_lambda(
|
||||
note.get_key(),
|
||||
note.get_color(),
|
||||
note.get_duration()
|
||||
)
|
||||
run_at(
|
||||
now + datetime.timedelta(milliseconds= note.get_start_time()),
|
||||
output_lambda(
|
||||
note.get_key(),
|
||||
note.get_color(),
|
||||
note.get_duration()
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
asyncio.wait(tasks_to_wait)
|
||||
await asyncio.wait(tasks_to_wait)
|
||||
|
||||
Binary file not shown.
@@ -3,16 +3,22 @@ from chroma_case.Note import Note
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
def printing(key, color, duration):
|
||||
async def printing(key, color, duration):
|
||||
print(f"key: {key}, c:{color} for {duration / 1000}s")
|
||||
await asyncio.sleep(duration / 1000)
|
||||
print(f"end of {key}")
|
||||
|
||||
|
||||
def main():
|
||||
n = Note("fa", (0, 255, 0), 1000, 1000)
|
||||
|
||||
p = Partition("test", [n])
|
||||
async def main():
|
||||
n = Note("fa", (0, 255, 0), 1000, 7000)
|
||||
|
||||
p.play(printing)
|
||||
p = Partition("test",
|
||||
[n,
|
||||
Note("fa#", (234, 255, 0), 3000, 2000)]
|
||||
)
|
||||
|
||||
await p.play(printing)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user