Maximum characters limit per buffer?

Hi ,

I’m working on a big project with Sonic Pi, designing it to playing live music in a dance project. The code defines a complex functionality for a MIDI keyboard.

While coding, I found that the code won’t run if it contains over a certain number of characters (8846, in my case), even if the characters beyond the limit are commented out.

Has anyone ever come upon such an issue?

1 Like

Yes - I can’t remember the exact limit myself but I have hit it previously and Sam confirmed that the limit exists (due to the underlying technology I think).

1 Like

Yes, this is a known limitation of the ( UDP) method Sonic Pi uses to communicate between the displayed buffers in the GUI and the ruby server.
There are various solutions.
First, you can split the code between more that one buffer. (It is possible to run more than one buffer at a time. ALL code is only stopped when you press the stop button.
Secondly, (and I use this technique a great deal with my code, most of which is for long linear pieces) you can use the run_file command. So of you save your code in a file MyLongPiece.rb in a folder pieces on your Pi home directory, you can run it using
run_file "/home/pi/pieces/MyLongPiece.rb"
typed into an empty buffer on Sonic Pi
and it will work when you run that, even though it is too long to run in a buffer. You will also find that the max size of the buffer is different on different platforms. I find it is smaller when running on a Mac than on a Raspberry Pi.
You can also use the run_file command from the sonic-pi-cli gem which you can install

Hope this helps.


That’s super helpful, thank you both!

A following question, raised while trying to implement the run_file solution:
How can I use functions that were defined in the file called by run_file? is there any method of setting “Time State functions” or any other type of global functions?


Hi @avishh,

at the current point in time, all functions are global and persist in memory for the duration of the app’s runtime. You can therefore run a buffer/external file which defines a bunch of functions and they’ll be around until you close Sonic Pi :slight_smile:

I hope that this helps.

1 Like

Sure it does, thanks!

Is there any way of setting an array and then setting a specific value of that array?

This helper function might be a way around it, but I’d assume there must be a cleaner one:

define :setarray do |arr, n, x|
  dummy = get(arr)
  set arr, dummy[0,n] + [x] + dummy[n+1, a.length]

I think your example IS the way to do it. The set command stores immutable objects, so you cant change values in an array retrieved using a get command. However, as per your example you can create effectively a new array using your setarray function and store that using the set command. I have used this in several projects.