Sonic pi as a scripting language?

Hi all, I am very new to sonic-pi, its possible that I may have missed what I am looking for.
is there any provision in sonic-pi for scripting, or programing?
I would like to create some instruments using microcontrolers, that interact with a program to create music in sonic pi.

I understand that I could take the source-code and write in my own listener that then processes commands, but Im just wondering if there is already something in the sonic-pi program that allows for sending command-line arguments or listens for things like key presses on whatever port.

Is there any provision in sonic-pi for scripting, or programing?

Yes . Though many seem to use it otherwise ,
music can definately be programmed .
( See ’ language ’ in the ’ help ’ file and ’ samples ’ on the
web page )

I would like to create some instruments using microcontrolers,
that interact with a program to create music in sonic pi.

MUCH simpler to use the instruments built into SPi .

something in the sonic-pi program that allows for sending command-line arguments or listens for things like key presses on whatever port.

Try Python or Basic . Basic has built in keyboard functions , as does
the PyGame module of Python .
For microprocessors have You looked at MicroPython and / or
Circuit Python ? ( and the available boards )

That may be, but Sonic Pi is designed to be able to send and receive commands to/from external devices via MIDI or OSC - so if the homemade instrument is built to send and/or receive either of those, that’s perfectly fine too :man_shrugging:

1 Like

First off, welcome @maker0 :grinning_face_with_smiling_eyes:

Regarding the title of this topic, it’s worth keeping in mind that Sonic Pi is not designed as an all-purpose language - rather, it’s specifically designed to do things like make music with synthesised notes from software instruments, play back recorded sounds, manipulate these with fx, and create rhythms and melodies over time to structure all of these things - as well as talk to external devices that understand MIDI or OSC. In other words, it’s mainly a music coding language.

Regarding command line access, this is not yet supported, but it is on the wishlist.

There are definitely people here who have built homemade instruments like you’re thinking of, that have been used with Sonic Pi - but mainly through things like MIDI or OSC commands as I have mentioned.

1 Like

@ethan
Should SPi be able to write raw midi to Timidity on a RPi ?
I have tried with no success .
The hookup shows up on the preferances ,
but no sound ?

That’s probably a question for @robin.newman or someone else who has more experience using MIDI through Sonic Pi - I’ve not used it myself - all I know is that people have quite successfully used Sonic Pi to send MIDI commands to external devices over the last few years - including from a Raspberry Pi.

It’s hard to tell without seeing log information exactly what the cause of your no sound issue is (for which a separate topic would be better). It may be that jack is not configured correctly, or it might be something more related to your OS or old version of Sonic Pi - in any case, you are still encouraged to upgrade Sonic Pi and Raspbian/Raspberry Pi OS (not least because the standard (ie non- Long Term Support) version of Debian Stretch, the OS which Raspbian Stretch is based on, reached End Of Life in July 2020).

I regularly use Raspberry Pi with QSynth and with Helm (two software midi synths) and this works well. I’ll take a look at timidity and get back to you. I usually use QJackctl to route the audio from these back into Sonic Pi and also to make the midiconnections (using the alsatab), although helm does it more directly.

I’ve had a play with timidity, but it is a bit of a pig to sort out. (I must say the last time I used it was with a very early Pi in 2013!!)
So far I’ve got it playing from a midi keyboard, but with very poor latency.
Is there a reason why you specifically want to use it? QSynth is so much easier to get going, and works very well with SP.

I’ve had a play with timidity, but it is a bit of a pig to sort out.
So far I’ve got it playing from a midi keyboard, but with very poor latency.

I am only interested in ’ scripting ’

Is there a reason why you specifically want to use it?
QSynth is so much easier to get going, and works very well with SP.

For ’ scripting ’ ? … I . E .

# Middle C
loop do
midi_raw 144, 60, 127
sleep 0.5
midi_raw 144,60,0
end

I get …


This works with Python …

# 3 B + ...... Stretch
# open terminal .... enter: " timidity -iA "
# leave terminal open

import os; import pygame
os.system ("sudo modprobe snd-virmidi")
os.system ("aconnect 20:00 128:00")
pygame.display.set_mode([1,1])

fo=open("/dev/snd/midiC1D0","wb",0)
note=60; patch=0
print("Any arrow key to start")

while True:
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      mmsg=[144,note,0];fo.write(bytes(mmsg))
      if event.key == pygame.K_UP: note=note+7
      if event.key == pygame.K_DOWN: note=note-7
      if event.key== pygame.K_RIGHT: patch=patch+1
      if event.key==pygame.K_LEFT: patch=patch-1
      if patch < 0 : patch = 0
      if patch > 127 : patch = 127
      if note < 0 : note = 0
      if note > 127 : note = 127
    elif event.type == pygame.KEYUP:
      mmsg=[192,patch];fo.write(bytes(mmsg))
      mmsg=[144,note,127];fo.write(bytes(mmsg))
     # mmsg=[144,note,0];fo.write(bytes(mmsg))
      print(); print(); print(); print(); print(); print(); print()
      print("  Up - Down  arrows for note  =", note)
      print("Left - Right arrows for patch =", patch)
      print("                    Frequency ~", round(2**((note-69)/12)*440),"Hz")

Buster has altered the Alsa Midi addresses …

# Middle C
loop do
midi_raw 144, 60, 127
sleep 0.5
midi_raw 144,60,0
end

Which version of Sonic Pi are you using which gives the error? The code here runs fine on my 3.3.1 version, on the latest RP OS

What synth ?
Do you get sound ?

I was talking specifically about the runtime error in the Sonic Pi code. That is why I asked what version you were using because if memory serves me correcly I think there were errors with the raw_midi command in the first versions that supported it.
I tested that program (on a Pi4 running the latest RP OS) connected to QSynth and it worked fine.
As far as timidity goes, I have your python script running OK on latest RP OS, and I can also run timidity connected (using aconnect) directly to my midi keyboard (although latency there is poor)
I have tried various virtual ports to connect SP to timidity but so far unsuccesfully, although that works with other synths.

connected to QSynth

So You use FluidSynth … ?

yes or other .sf2 synth package like TimGMGmb.sf2 Edit or Helm

3 B + … Buster … SPi . deb … Timidity …

Works but with strangeness …