From 30db82f5ed0d40b10967f1a8735a4f69212c019f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Fri, 23 Dec 2016 11:19:45 +0100 Subject: Make timing more precise I used busy loop for bigger precision, it is not optimal because of that, but it ensures more precision than time.sleep function (which can miss deadline by milliseconds). --- tlo-midi.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tlo-midi.py b/tlo-midi.py index d7776ed..a4b458e 100755 --- a/tlo-midi.py +++ b/tlo-midi.py @@ -103,20 +103,24 @@ def note_to_line(note): return int(note / threshold - 1) -def note(note, velocity, time_suspend, off=False): +def note(note, velocity, off=False): if off: velocity = 0 - time.sleep(time_suspend) # Suspend for given delay data_line[note_to_line(note)] = velocity # Now do the work +work_time = int(time.time()*1000000) for msg in midi: if (msg.type == "note_on" or msg.type == "note_off") and \ msg.channel == channel: - note(msg.note, msg.velocity, msg.time, msg.type == "note_off") + note(msg.note, msg.velocity, msg.type == "note_off") elif args.v: print("Ignoring message: " + str(msg)) + work_time = work_time + int(msg.time*1000000) + # Lets use busy delay to minimalize possibility of missing it + while work_time > int(time.time()*1000000): + pass output_line() # Note: We are not handling beats at all. This just waits for given time when it's -- cgit v1.2.3