Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to play notes according to a Float stream coming from i.e. GNU Radio
#!/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
You can’t perform that action at this time.