Running Sonic Pi from Doom Emacs

Hi! I’m trying to run Sonic Pi through this Emacs package: sonic-pi.el on macOS Catalina 10.15.6. This package is quite old and has not been updated recently. However, it looks like some people were quite successful using it. I’m opening this topic to discuss the various modifications one should make in order to make it run properly.

In order to load the package, I appended the following lines to my config file:

   (setq load-path (cons "/Users/my_name/.doom.d/sonic_pi/" load-path))
   (setq sonic-pi-path "/Applications/Sonic.app") ; deleted the whitespace
   (require 'sonic-pi)

Doing so, sonic-pi mode is detected and usable when playing around with .rb files. However, it looks like some variables are wrongly hardcoded directly inside the sonic-pi.el file used by the package. The defaults, line 57 and 58 of sonic-pi.el are:

(defvar sonic-pi-server-bin             "server/bin/sonic-pi-server.rb")
(defvar sonic-pi-compile-extensions-bin "server/bin/compile-extensions.rb")

I’ve tried to update these lines so they can reflect the new changes to the internal directory structure:

(defvar sonic-pi-server-bin             "app/server/ruby/bin/sonic-pi-server.rb")
(defvar sonic-pi-compile-extensions-bin "server/bin/compile-extensions.rb")

Apparently, this isn’t enough. When trying to jack-in Sonic Pi, the following message appears: Could not find a sonic-pi server in sonic-pi-path.

Has anyone been able to workaround this problem? I’ve seen that some pull requests were made, but not taken into account so far.

PS: note that Doom Emacs is nothing more than a thin Evil/Vim layer on top of a vanilla Emacs instance. A regular version of Emacs may behave just the same.

I’ve not used the package, but had a brief look. There will also be changes necessary in the code in the sonic-pi-osc.el file, as communication ports used by Sonic Pi have changed from version 3. In particular port 4557 is now allocated dynamically. You can find the value currently used for you SP at the beginning of the server-output log file. It is possible to use code to read this as is done in the updated sonic-pi-cli gem.
Also internal osc messages start with a guid value (which the user supplies) and I’m not sure that this is built in here. @josephwilk who developed the sonic-pi.el system may be able to help if you post an issue on the github repository.

Hi @Bubo,

I had to do the following to get it running:

  1. Edit sonic-pi.el; change line 57/58 to:
(defvar sonic-pi-server-bin             "app/server/ruby/bin/sonic-pi-server.rb")
(defvar sonic-pi-compile-extensions-bin "app/server/ruby/bin/compile-extensions.rb")
  1. Remove: ~/.emacs.d/.local/straight/build/sonic-pi/sonic-pi.elc

  2. Do a ~/.emacs.d/bin/doom sync to update Doom Emacs

Hope this helps.

Ahh… concerning Midi/OSC; I think it will work if you add this at the top of the file you are evaluating:

@osc_server ||=  SonicPi::OSC::UDPServer.new(4559, use_decoder_cache: true) #__nosave__

I am sure you will have to apply what @robin.newman wrote. But I have not checked this for quite some time, but if you don’t need Midi or OSC you can skip this anyway.

See https://github.com/repl-electric/sonic-pi.el/issues/19#issuecomment-345222832

@Martin: how have you defined paths in your general configuration file? I corrected the sonic-pi.el file according to your indications but wasn’t able to boot anything.

Actually, something might be really wrong with my installation because I wasn’t able to remove the sonic-pi.elc file you mentioned. It simply wasn’t there, as well as the /build/sonic-pi folder.

@robin.newman : will do! I’m sure that’s something that can be quickly fixed once installation is done. I’ll have a look at sonic-pi-cli in order to see how that feature is implemented.

This is what I have in my ~/.doom.d/config.el:

(use-package sonic-pi
  :init
    (setq sonic-pi-path "~/bin/sp32/sonic-pi/")
  )

Do not be confused by that, I have a self-compiled version 3.2 under Linux in ~/bin/sp32/

Additionally you will need in ~/.doom.d/package.el:

(package! sonic-pi
  :recipe (:host github :repo "repl-electric/sonic-pi.el"))

As to the missing elc file:

I am by no means expert concerning doom; I don’t know about the logic of the internal doom optimisation features but I guess, this is what’s it about.

Did the doom installation run through without errors? Then I guess you should have something like that in .emacs.d/.local:

doom-dot-local

repos does contain everything from (m)elpa (such as the package sonic-pi) and build contains 1. links to the files in repos (e. g. to sonic-pi.Well, I am by no means expert concerning doomel) and lisp compiled *.elc-files. This is obviously a result of doom’s way to optimize package loading and startup time.

I am afraid I can’t say more about that …

I understand now. We installed the package using a different method. I think that your way of installing it is the recommended one, the one that integrates well with the whole Doom Emacs ecosystem.

I’ve tried to play around some more with the configuration. I’m still unable to connect to or boot a server. I’m rather clueless about what can be wrong with the way I configured it. After trying many fixes and hacks, I guess that the best would be just to pick a different editor or try to connect to a running instance using other tools or methods.

Thanks for your help. I’ll update the topic if I get any luck using other ways to run SP with Doom Emacs.

Hi @Bubo,

Out of interest, what are your main issues with the default Sonic Pi editor which are making you want to use something else?

Hi @samaaron,

I haven’t encountered any issues with the default editor per se. It’s full of interesting features, like having access to the documentation whenever you need it, or sample autocompletion. One of the features I really like is the ability to see the logs in realtime. I used this window quite a lot when dealing with network synchronization for many players in the past. However, I just don’t feel that well when writing in the default editor for a few reasons.

The most obvious reason is that I can’t use Vim modal editing and I can’t freely jump wherever I want in the text with just a few keystrokes, rename or insert things easily. My keyboard layout makes switching between buffers or use some advanced Sonic Pi keybindings a bit painful (actually, pretty much anything is painful on AZERTY keyboards if it’s just tuned on default settings). I consciously rewired my muscular memory and brain to use vim keybindings, because I felt that it was the right thing to do in order to master the motoric skills needed for live-coding with ease.

Emacs is also pretty much an extension to my computer… I use it to take notes, design instruments on SuperCollider / Overtone, to do programming or to interact with various other live-coding languages. As I find myself writing more and more academic stuff about live-coding these days, the ability to switch between writings / papers, languages, REPLs and other stuff is quite cool. I’m trying to keep everything nicely contained in one environment I can model and extend.

1 Like