Skip to content

Instantly share code, notes, and snippets.

@santolucito
Created February 8, 2021 14:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santolucito/dd0b68edd6f8c64aaf89e59f2d8c3f00 to your computer and use it in GitHub Desktop.
Save santolucito/dd0b68edd6f8c64aaf89e59f2d8c3f00 to your computer and use it in GitHub Desktop.
Sonic Pi + Python + ESP32
'''
this python code will listen for serial messages,
and pass that trigger on to SonicPi (code below) as an OSC message, triggering a sound
'''
from pythonosc import osc_message_builder
from pythonosc import udp_client
import serial
import sys
import time
#select the correct port and baud rate
ser = serial.Serial('COM9', 9600)
while True:
try:
ser_bytes = ser.readline()
serialVal = str(ser_bytes, 'ascii').strip()
if(serialVal == "0"):
sender = udp_client.SimpleUDPClient('127.0.0.1', 4560)
sender.send_message('/trigger/prophet', [70, 100, 1])
time.sleep(2)
except:
print(sys.exc_info()[0])
break
'''
--- this code goes in SonicPi---
live_loop :foo do
use_real_time
a, b, c = sync "/osc*/trigger/prophet"
synth :prophet, note: a, cutoff: b, sustain: c
end
------ end SonicPi Code ------
--- Arduino ---
You need code on your microcontroller that does Serial.println("0") under the conditions you want the sound to play.
As an example: https://gist.github.com/santolucito/22d68fd78d1359d4b865270ddbf4fecd
---
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment