Skip to content

Instantly share code, notes, and snippets.

@rbnpi
Last active April 1, 2022 11:46
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 rbnpi/b128470c0b4260236a499896d8f0efae to your computer and use it in GitHub Desktop.
Save rbnpi/b128470c0b4260236a499896d8f0efae to your computer and use it in GitHub Desktop.
Sonic Pi 3.2.1 Controls Raspberry Pi GPIO pins
#!/usr/bin/env python3
#by Robin Newman, 28th March 2020
from gpiozero import LED
from pythonosc import osc_server
from pythonosc import dispatcher
import argparse
red=LED(4)
white=LED(10)
green=LED(16)
blue=LED(21)
yellow=LED(27)
leds=[red,white,green,blue,yellow]
def setLedNum(unusedAddr,args,num,state):
print("Received ",num,state)
if state==1:
leds[num].on()
else:
leds[num].off()
def stopLeds():
for led in leds:
led.off()
if __name__=="__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument("--ip",
default="127.0.0.1", help="The ip to listen on")
parser.add_argument("--port",
type=int, default=8000, help="The port to listen on")
args=parser.parse_args()
dispatcher=dispatcher.Dispatcher()
dispatcher.map("/ledNum",setLedNum,"num","state")
server = osc_server.ThreadingOSCUDPServer(
(args.ip, args.port), dispatcher)
print("Serving on {}".format(server.server_address))
server.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped")
stopLeds
use_osc "localhost",8000
live_loop :flasher do
for n in 0..4 do
osc "/ledNum",n,1
sleep 0.2
end
for n in 0..4 do
osc "/ledNum",n,0
sleep 0.2
end
end
These three files accompany a video on youtube illustrating Sonic Pi 3.2.1
running on a Raspberry Pi 3B+ conotrolling 5 LEDs connected to GPIO pins
using a python script incorporating an OSC server.
gpoiledsosc.py is a pythons cript which interacts with the 5 leds using the
gpiozero library. It incorporates an OSC server which looks for the incoming
osc message addressed to "/LedNum" This has two parameters. First a number between 0 and 4
which specifies which led to control, secondly a state which is 1 if the led is to be turned on
and 0 if it is to be turned off.
ledOSCtest.rb is a simple program to test the leds and osc server is working. It is loaded
into a Sonic Pi buffer and run from there. It sends a series of OSC messages of the form
"/LedNum",n,state to the server, where n is the number of the led to intercat with, and state
is 1 or 0 to indicate if it should be turned on or off.
spGPIOsteroids.rb is a larger program which is run in a Sonic Pi buffer, and which controls the leds
in a variety of sequences, but also triggers a note to sound for the duration for which a led is turned on.
Each led has a differnt note assigned to it. When a led is turned on, a note is started to sound and a reference
to that note is stored. When the led is turned off, the reference is retrieved and the note is killed to stop it
souinding. Before that its volume is reduced rapidly to zero to prevent "clicks" from sounding.
The circuitry for each led is simple the +ve leg of the led is connected to a GPIO pin and the -ve leg
is connected to ground via a 330 ohm resistor. The layout I used utilised a raspio breadboard pi bridge to connect
a breadboard to the Pi. this video details it and the connections https://youtu.be/imxyzSrO0wI
An installer for Sonic Pi 3.2.1 for Raspbian Buster will be released on April 3rd 2020 on the in-thread.sonic-pi.net site.
#Sonic Pi GPIO control on steroids
#by Robin Newman, 28th March 2020
use_osc "localhost",8000
use_synth :tb303
nlist=[:c4,:d4,:e4,:f4,:g4]
nkeylist=[:n0,:n1,:n2,:n3,:n4]
del = 0.15
define :pl do |n,state|
if state==1
set nkeylist[n], (play nlist[n],sustain: 1)
else
k=get(nkeylist[n])
control k,amp: 0,amp_slide: 0.01
sleep 0.01
kill k
end
end
define :allOff do
5.times do |i|
osc "/ledNum",i,0
pl i,0
end
end
define :allOn do
5.times do |i|
osc "/ledNum",i,1
pl i,1
end
end
define :linear do
5.times do |i|
osc "/ledNum",i,1
pl i,1
sleep del
end
5.times do |i|
osc "/ledNum",i,0
pl i,0
sleep del
end
end
define :nightRider do
setState "01010"
sleep del
setState "10101"
sleep del
end
define :thereAndBack do
5.times do |i|
osc "/ledNum",i,1
pl i,1
sleep del
end
5.times do |i|
osc "/ledNum",4-i,0
pl i,0
sleep del
end
end
define :setState do |l|
5.times do |i|
osc "/ledNum",i,l[i].to_i
pl i,l[i].to_i
end
end
define :squash do
setState "10001"
sleep del
setState "01010"
sleep del
setState "00100"
sleep del
setState "01010"
sleep del
setState "10001"
sleep del
end
sleep 1
2.times do
linear
end
4.times do
nightRider
end
allOff
2.times do
thereAndBack
end
8.times do
squash
end
allOff
2.times do
linear
end
6.times do
allOn
sleep del
allOff
sleep del
end
setState "10101"
sleep 16*del
allOff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment