Init.rb code stopping by itself + running headless

Hello,

I have a raspberry pi 4b running Sonic Pi 4.3.0.1 (32 bit). I am new to Sonic Pi.

I’ve followed the instructions from Robin’s post Sonic Pi for standalone installations - #4 by robin.newman to run Sonic Pi automatically using the first method (GUI).

I’ve put a small piece of code in the Init.rb file to test and I am able to boot and have it start automatically. However something weird happens, after 20 loops it stops and throws and error. If I stop and start again, the very same code plays indefinitely with no problem.

Also another weird thing is that the GUI only shows the code pane and nothing else (see image) so I cannot see the logs and thus, the error being thrown. The view menu shows that the logs (and others) pane are checked so they should appear but they are not. I am booting the Pi into desktop without a physical display plugged in and I VNC into the desktop to see what’s happening, is that why the GUI is weird?

Any help is appreciated.

Thanks

There are one or two problems you will encounter running Sonic Pi on a headless Pi using VNCviewer. The main one is that it will be sluggish to the point of being nearly inoperable if you have the scope function running, as it wont work properly over vnc with a headless pi.
You can turn this off with Alt+O on a PC or Command+O on a Mac depending on what you are running VNCviewer on. or from the io menu
Secondly you should configure the Pi to optimise it for VNC use. Using the Raspberry Pi configuration in the preferences set the display headless resolution to say 19280x1080
The switch to the performancfe section and reduce the GPU memory to 16 (YOU don;t need to allocate it much memory if you are not using the GPU as you are running headless.

These operations will require a reboot to initialise them.

As far as your init.rb code is concerned, this can also cause problems as you have in effect an endless loop that never allows other code a lookin. IF for example you want to set up additional functions in your init.rb file
Instead you should write it as a live loop.
eg

live_loop :anyname do
  play 60,amp: 0.5
  sleep 0.7
end

I have this running happily on a Pi400 using a usb audio interface, as you can’t use HDMI sound without your local screen! on a Pi4 you could use a usbv sound interface or the audio jack, selecting what you use by right clicking the volume control icon

Of course when you press stop on the Pi it will stop your live loop running

Hope this helps

EDIT of course if you run VNC to use an additional screen and have a screen connected directly to your PI then you don’t need to turn off the Scope or change the GPU setting (64 I suggest in this case)

Thank you for the reply, I’ll address your answer point by point.

I don’t really need or care to see the scope. In the end I will run the Pi without a display or VNC. I installed VNC just so I can remote while I setup the Pi, it’s not meant to be used during normal operation.Also, I tried to do the second method you described in your post to run headless but it wasn’t working and I ran out of patience.

(As a side note I believe that there might be a mistake in the instructions you did on that post.
When you refer to the “startsp.sh” file, shouldn’t that be “startup.sh” ? There are a few conflicting commands that makes me think it should be name “startup.sh”)

cd ~/Desktop
nano startsp.sh
chmod +x startsp.sh
@reboot /home/pi/Desktop/startup.sh &

Anyway, back to my problem

Already had that configured, thanks

Thank you, I just updated that now.

Not really sure I understand the “endless loop” thing. I plan to run Sonic-Pi headless for an install and need the code to run upon boot, from my research it needs to live in the init.rb file. I wasn’t aware that the init.rb executes differently than code written in the GUI, is that correct?

In any case I copied your snippet to Init.rb and I still get a error, however I managed to see the error codes that I am pasting below:

Runtime Error: [buffer eval, line 7] - SonicPi::Lang::Core::TimingError
Thread death +--> :live_loop_anyname
 Timing Exception: thread got too far behind time
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:4331:in `sleep'
eval:7:in `block (3 levels) in __spider_eval'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2206:in `block in live_loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2226:in `block (2 levels) in live_loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2141:in `block in loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2137:in `loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2137:in `loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/lang/core.rb:2224:in `block in live_loop'
/opt/sonic-pi/app/server/ruby/lib/sonicpi/runtime.rb:1169:in `block (2 levels) in __in_thread'

One last thing in reference to my not seeing the UI properly it seems that the GUI for Sonic-Pi is larger than the resolution I have set for the pi. I can fix it by minimizing and maximizing again. Not a huge problem.

Thanks

Oops sorry yes the startsp.sh file should have the same name as that used in the crontab file. Doesn’t matter what it is as long as it is the same.

as far as the SP screen being too large for your remote viewer, the other thing you can do is drag the screen by holding the Alt (Otpion) key down and dragging the screen with your mouse until you see the edges which you can then drag in the normal way to resize.

For my headless projects I have tended to use the second method and then communicate with Sonic Pi via the sonic-pi-cli gem. For example this quite old project which worked with TouchOSC to create a Sonic Pi jukebox. The description and code needs updated both for changes to the OSC command implementation in Sonic Pi which changed a few years ago, and also for the command line gem sonic-pi-cli used in the program which needs to be updated for sonic-pi version 4
I have upgraded the project but haven’t republished it. The link also links to a video showing the project in action.

Okay so what I’m trying to achieve is simply not possible? I’d still be interested to simply get my init.rb code working.

Is I say it worked OK on my Pi400. I will investigate this further, because it should work for you. However I have visitors staying at present and don’t have a great deal of time available just now. Tell me how much RAM is on your Pi4b?
What code do you actually want to run headless? Presumably not just a repeating tone.
EDIT if you want, feel free to Direct Message me, if it is easier.

Hello,

After some trial and error I managed to find another way to run Sonic-Pi on startup that works pretty well. I realized that the shortcut Alt+R runs the currently loaded code in Sonic-Pi, all you need to do is to send the shortcut keystroke via command line after a delay on booth and the code will run.

I’m going to detail how I did it here in case someone finds that thread later on.

First step is to get Sonic Pi GUI to autostart following the instruction outline by Robin in his post

cd ~
mkdir -p .config/autostart
cp /usr/share/applications/sonic-pi.desktop   .config/autostart

This will get Sonic-Pi to boot automatically on start.

Next, install the keystroke tool xdotools (documentation here)

apt-get install xdotool

After, you will need to create a shell script file somewhere to write the xdotools commands to be executed on boot. I create mine on the desktop and called it autoplay.sh.

Navigate to desktop and create a new bash file:

sudo nano autoplay.sh

In the file enter these lines:

#!/bin/bash
export XAUTHORITY=/home/admin/.Xauthority; export DISPLAY=:0; xdotool key Return
sleep 60
xdotool key alt+r
  • line 1 = wizard magic line to make code work
  • line 2 = I’m not too sure why its needed but I had some errors trying to send keystroke and this fixed it. Found on this stackoverflow post. Also keep in mind that my user is called admin, if yours is the default Pi, change it in that line.
  • line 3 = Delay in seconds before the rest of the code executes, in this case 60 secs. This is so the RPI and Sonic-Pi have time to boot.
  • line 4 = The actual keystroke command that sends alt+r

Save/Exit nano

Now I don’t remember if I had to run the chmod command to make it executable but in any case here it is.

chmod +x autoplay.sh

You can follow more detailed instructions about shell script creation such as this one.

Next you will need to edit the rc.local file to add the script on startup. (more info here)

sudo nano /etc/rc.local

Add the following insctruction before the “exit 0” line

sh /home/admin/Desktop/autoplay.sh

Save/exit

Reboot your Pi and Sonic-Pi should run after 60 secs.

With this method you still need to manually copy the code and save. When Sonic-Pi boots, it loads the latest saved code. This doesn’t bother me since the idea is that once the project/code is done, I will copy one last time and save. After that I won’t need to change it and it will load and play automatically on boot.

Now there is still a weird bug where the BPM is slow a the start and slowly ramps up to the normal tempo, it’s annoying and hopefully there is a solution but if not I can work around it.

There you go, hope that helps someone!

Thanks