Skip to content

Instantly share code, notes, and snippets.

@rbnpi
Created January 23, 2018 14:33
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 rbnpi/35d4e74ffc87d9b2d604e1daa9462172 to your computer and use it in GitHub Desktop.
Save rbnpi/35d4e74ffc87d9b2d604e1daa9462172 to your computer and use it in GitHub Desktop.
Driving Rasp.Io Inspiring from Sonic Pi. I usually run Sonic Pi on my mac and drive the Inspiring on a separate raspberry pi, but can be same Pi. To accompany talk given at CamJam on Sat 27Jan 2018
#inspiring sonic pi driver
#start sudo inspserver.py --ip 172.20.10.6 on client
use_osc "localhost",4559 #osc destination for the local machine
remote_ip_address="172.20.10.6" #adjust this for your setup.
# uncomment next to lines to shutdown client raspberry pi
##| osc_send remote_ip_address,8000, "/hello/shutdown"
##| stop
use_bpm 140 #initial setting of bpm (set to 160 by the first bpm_mul
use_cue_logging false
use_osc_logging false
delay=0
##| l1= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
##| l2= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
##| l3= [1, 4, 7, 10, 13, 16, 19, 22]
##| l4= [1, 5, 9, 13, 17, 21]
##| l5= [1, 7, 13, 19]
##| l6= [1, 9, 17]
##| l7= [1, 13]
s=[:tri,:pulse,:saw,:piano]
set :stopflag,false
set :stopall,false
at 121 do
set:stopflag,true
end
sleep 1
live_loop :send do
use_real_time
6.step(0,-1) do |y|
st=[1,2,3,4,6,8,12][y]
p=[0,1].choose
syn=s.choose
##| w=[0,1,2,3,5,7,11][y]
##| (24-w).step(1,-st) do |x|
for x in (1..24).step(st) do
osc "hello/play",delay,x,0.1,x+1.to_f/25,syn,60
osc_send remote_ip_address,8000, "/hello/play",delay,x,0.1,x.to_f/24,syn,60,0,0,[((x+y)%7+1),y%7+1][p],1
sleep 0.2
end
w=[0,1,2,3,5,7,11][y]
(24-w).step(1,-st) do |x|
##| for x in (1..24).step(st) do
osc "hello/play",delay,x,0.1,x+1.to_f/25,:tri,60
osc_send remote_ip_address,8000, "/hello/play",delay,x,0.1,x.to_f/24,syn,60,0,0,0,1
sleep 0.2
end
end
6.step(0,-1) do |y|
st=[1,2,3,4,6,8,12][y]
p=[0,1].choose
syn=s.choose
w=[0,1,2,3,5,7,11][y]
(24-w).step(1,-st) do |x|
#for x in (1..24).step(st) do
osc "hello/play",delay,x,0.1,x+1.to_f/25,syn,60
osc_send remote_ip_address,8000, "/hello/play",delay,x,0.1,x.to_f/24,syn,60,0,0,[((x+y)%7+1),y%7+1][p],1
sleep 0.2
end
##| w=[0,1,2,3,5,7,11][y]
##| (24-w).step(1,-st) do |x|
for x in (1..24).step(st) do
osc "hello/play",delay,x,0.1,x+1.to_f/25,:tri,60
osc_send remote_ip_address,8000, "/hello/play",delay,x,0.1,x.to_f/24,syn,60,0,0,0,1
sleep 0.2
end
end
if get(:stopflag)
set :stopall,true
osc_send remote_ip_address,8000, "/hello/kill"
stop
end
end
live_loop :playit do
use_real_time
b = sync "/osc/hello/play"
use_synth b[4]
play b[1]+60,amp: b[3],sustain: b[2],release: 0.1
sleep b[2]
stop if get(:stopall)
end
#!/usr/bin/env python3
"""Small example OSC server
This program listens to several addresses, and prints some information about
received packets.
"""
import argparse
from time import sleep
import apa
import os
numleds = 24
delay = 0.04
ledstrip = apa.Apa(numleds)
ledstrip.flush_leds()
ledstrip.write_leds()
from pythonosc import dispatcher
from pythonosc import osc_server
def handle_shutdown(unused_addr):
ledstrip.led_set(1,20,255,0,0)
ledstrip.write_leds()
sleep(0.5)
ledstrip.zero_leds()
ledstrip.reset_leds()
os.system("halt")
def handle_kill(unused_addr):
ledstrip.zero_leds()
ledstrip.reset_leds()
def handle_play(unused_addr,args, delay,n,
d,v,s,current_bpm,ledoffset,partoffset,iteration,leave):
print(n,d,v,s,current_bpm,ledoffset,partoffset,iteration,leave)
curLed=(int(n)-ledoffset)+partoffset*(iteration-1)
curLed=abs(curLed%24)
brightness=int(v*30)
sleep(delay)
if iteration == 0:
ledstrip.led_set(curLed,brightness,0,0,0)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,0,0,255)
ledstrip.write_leds()
if iteration == 1:
ledstrip.led_set(curLed,brightness,0,0,255)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,0,0,255)
ledstrip.write_leds()
elif iteration == 2:
ledstrip.led_set(curLed,brightness,0,255,0)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,0,255,0)
ledstrip.write_leds()
elif iteration == 3:
ledstrip.led_set(curLed,brightness,255,0,0)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,255,0,0)
ledstrip.write_leds()
elif iteration == 4:
ledstrip.led_set(curLed,brightness,50,0,50)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,50,0,50)
ledstrip.write_leds()
elif iteration == 5:
ledstrip.led_set(curLed,brightness,50,50,0)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,50,50,0)
ledstrip.write_leds()
elif iteration == 6:
ledstrip.led_set(curLed,brightness,0,50,50)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,0,50,50)
ledstrip.write_leds()
elif iteration ==7:
ledstrip.led_set(curLed,brightness,255,255,255)
ledstrip.write_leds()
if leave == 0:
sleep(d*60/current_bpm*0.9)
ledstrip.led_set(curLed,0,255,255,255)
ledstrip.write_leds()
if __name__ == "__main__":
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("/hello/play",handle_play,"delay", "n","d","v","s","current_bpm","ledoffset","partoffset","iteration","leave")
dispatcher.map("/hello/kill",handle_kill)
dispatcher.map("/hello/shutdown",handle_shutdown)
server = osc_server.ThreadingOSCUDPServer(
(args.ip, args.port), dispatcher)
print("Serving on {}".format(server.server_address))
server.serve_forever()
@rbnpi
Copy link
Author

rbnpi commented Jan 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment