aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarel Kočí <karel.koci@nic.cz>2016-12-23 11:19:45 +0100
committerKarel Kočí <karel.koci@nic.cz>2016-12-23 11:19:45 +0100
commit30db82f5ed0d40b10967f1a8735a4f69212c019f (patch)
tree92d0989cc06c77b6d60ef08e3c24bc2429785f3d
parent19bb576edefd1bf8ecc9a03c1d4f7a233a9d3369 (diff)
downloadturris-light-organ-30db82f5ed0d40b10967f1a8735a4f69212c019f.tar.gz
turris-light-organ-30db82f5ed0d40b10967f1a8735a4f69212c019f.tar.bz2
turris-light-organ-30db82f5ed0d40b10967f1a8735a4f69212c019f.zip
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).
-rwxr-xr-xtlo-midi.py10
1 files 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