Skip to content

Instantly share code, notes, and snippets.

@mildsunrise
Created March 24, 2019 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildsunrise/4f28410879fa737677fbaf983af6910d to your computer and use it in GitHub Desktop.
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
#!/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