Generating Music With SageMath And Sonic Pi

In this post, we will see how to generate music with Python/SageMath and Sonic Pi. Sonic Pi itself is quite nice to make music, but python is an attractive option due to large number of math libraries written for it. So, here are the steps:

1 Install PyOSC

Open Sage subshell

sage -sh

Install PyOSC library

pip install PyOSC

2 Run Sonic Pi and sync with an OSC URL

1
2
3
4
5
6
7
use_synth :piano

live_loop :foo do
  use_real_time
  a, b = sync "/osc/trigger/play"
  play_chord [a, b]
end

3 Pass messages to the OSC URL and play notes

import OSC
from time import sleep
from math import pi, cos, sin

addr = ('localhost', 4559)
c = OSC.OSCClient()
c.connect(addr)

def send_message(url, *args):
    msg = OSC.OSCMessage()
    msg.setAddress(url)
    for l in args:
        msg.append(l)
    c.send(msg)

for x in range(1,10):
    for y in range(1,20):
        send_message("/trigger/play", float(x*10), float(y*5)) # float's not required in python, but throws error in Sage without it
        sleep(0.1)