Created
March 24, 2019 10:39
-
-
Save mildsunrise/4f28410879fa737677fbaf983af6910d to your computer and use it in GitHub Desktop.
Script to play notes according to a Float stream coming from i.e. GNU Radio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# $ apt install libasound2-dev libjack-jackd2-dev zynaddsubfx | |
# $ pip3 install python-rtmidi mido | |
# Then run Zynaddsub, send a float stream from GNU Radio to UDP localhost:8011 | |
from socket import socket, SOCK_DGRAM | |
from struct import unpack | |
import math, mido | |
def value_to_note(x): | |
dbs = 10 * math.log10(x) | |
note = (dbs+30) * 2 + 64 | |
return min(max(int(round(note)), 1), 90) | |
output = mido.open_output("ZynAddSubFX:ZynAddSubFX 129:0") | |
so = socket(type=SOCK_DGRAM) | |
so.bind(("127.0.0.1", 8011)) | |
old_note = None | |
try: | |
while True: | |
value = unpack("f", so.recv(127))[0] | |
note = value_to_note(value) | |
print("Value: {:5.1f} Note: {:2}".format(value, note)) | |
if old_note != note: | |
if old_note != None: | |
output.send(mido.Message('note_off', note=old_note)) | |
output.send(mido.Message('note_on', note=note, velocity=100)) | |
old_note = note | |
except KeyboardInterrupt as e: | |
output.send(mido.Message('note_off', note=old_note)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment