Skip to content

Commit

Permalink
timed sleep & key_press_duration as song config
Browse files Browse the repository at this point in the history
  • Loading branch information
3096 committed Mar 29, 2021
1 parent 1ae4510 commit db8d973
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
18 changes: 7 additions & 11 deletions midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ctypes
import json
import os
import time
from typing import List

import mido
Expand Down Expand Up @@ -29,7 +30,6 @@
STOP_KEY_COMBO = [pynput.keyboard.Key.space]
RELOAD_CONFIG_KEY = pynput.keyboard.KeyCode.from_char('`')
CONFIG_FILE_NAME = "midi_config.json"
KEY_PRESS_DURATION = 0.01


class NoteKeyMap:
Expand Down Expand Up @@ -77,6 +77,8 @@ def __init__(self, song_config: dict):
self.channel_filter = default_if_invalid(song_config, "channel_filter", list, [])
self.track_filter = default_if_invalid(song_config, "track_filter", list, [])
self.no_hold = default_if_invalid(song_config, "no_hold", bool, True)
if self.no_hold:
self.key_press_duration = default_if_invalid(song_config, "key_press_duration", float, 0.01)
self.skip_start_time = default_if_invalid(song_config, "skip_start", (int, float), 0)
self.root_note = default_if_invalid(song_config, "root_note", int, None)
self.use_auto_root = self.root_note is None
Expand Down Expand Up @@ -186,7 +188,7 @@ async def play(self, song_config: SongConfig):
# play
print('start playing')
fast_forward_time = song_config.skip_start_time
sleep_debt = 0
last_clock = time.time()
for msg in mid:
# check for stop
if not self.play_task_active:
Expand All @@ -200,23 +202,17 @@ async def play(self, song_config: SongConfig):
fast_forward_time -= msg.time
continue
elif msg.time > 0:
# msg sleep
if song_config.no_hold:
sleep_debt_repay = min(sleep_debt, msg.time)
sleep_debt -= sleep_debt_repay
await asyncio.sleep(msg.time - sleep_debt_repay)
else:
await asyncio.sleep(msg.time)
await asyncio.sleep(msg.time - (time.time() - last_clock))
last_clock += msg.time

# press keys
if msg.type == "note_on" \
and (len(song_config.channel_filter) == 0 or msg.channel in song_config.channel_filter):
if key := note_key_map.get_key(msg.note):
keyboard.press(key)
if song_config.no_hold:
await asyncio.sleep(KEY_PRESS_DURATION)
await asyncio.sleep(song_config.key_press_duration)
keyboard.release(key)
sleep_debt += KEY_PRESS_DURATION

elif not song_config.no_hold and msg.type == "note_off" \
and (len(song_config.channel_filter) == 0 or msg.channel in song_config.channel_filter):
Expand Down
10 changes: 10 additions & 0 deletions midi_config.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -21,6 +22,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -35,6 +37,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -49,6 +52,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -63,6 +67,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -77,6 +82,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -91,6 +97,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -105,6 +112,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -119,6 +127,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand All @@ -133,6 +142,7 @@
"channel_filter": [],
"track_filter": [],
"no_hold": true,
"key_press_duration": 0.01,
"skip_start": 0,
"root_note": "auto",
"auto_root_lowest": 48,
Expand Down

0 comments on commit db8d973

Please sign in to comment.