Hooking SuperCollider up to Emacs on OS X

Posted on Tue Feb 09 00:00:00 -0500 2010

Update: This post is badly out of date and represented a cludgy work-around that I discovered. I haven’t put any further effort into cleaning this up or weeding out the unecessary steps as all my time is spent hacking on Overtone these days. However, this post looks to provide a much simpler solution: http://www.gloryisasilentthing.com/glory/supercollider-emacs-scel-and-the-right-way/

I think that SuperCollider is one of the most exciting music technologies currently available. It’s super powerful, Open Source, and has even got a separate server and language runtime giving everyone the opportunity to experiment with different approaches. I’m very excited to see where we go with it, and how I can utilise it with my Polynome project.

However, despite the fact that it comes shipping with a very nice live-coding enabled interface, I use Emacs, and well, I love using Emacs for editing text of all kinds. I therefore wanted to have the convenience of the live editing with the powerful text manipulation features that Emacs offers. Luckily there’s an Emacs mode for SuperCollider and I just finished hooking it all up. Unfortunately I couldn’t find a nice tutorial for doing it, but I did manage to piece together a process that finally worked for me. It wasn’t a fun time, so I’m documenting it here in the hope that someone else might benefit from my pain.

Caveat and Context

Now, I need to issue a major caveat: there’s lots of moving parts in this situation, and your parts might not even be the same as mine. So, just to be clear - this might not work for you, but hopefully it’ll get you a major part of the distance. The more similar our setups, the more likely this will help, so for clarity I’m using:

I’m also assuming that you have already installed Emacs and SuperCollider and that you have a basic working knowledge of Emacs configuration, and general UNIX hockery pokery1.

Update your Path (UNIX-style)

First up open your shell config file (~/.zshrc in my case) with your favourite editor2 and append /Applications/SuperCollider to your PATH. Check to see if it worked:

λ sclang -h
   sclang [options] [file..] [-]

   -d <path>                      Set runtime directory
   -D                             Enter daemon mode (no input)
   -g <memory-growth>[km]         Set heap growth (default 256k)
   -h                             Display this message and exit
   -l <path>                      Set library configuration file
   -m <memory-space>[km]          Set initial heap size (default 2m)
   -r                             Call Main.run on startup
   -s                             Call Main.stop on shutdown
   -u <network-port-number>       Set UDP listening port (default 57120)

Update your Path (Cocoa-Emacs-style)

Ok, you can do a few things here, particularly because Emacs on OS X does very funky things with the PATH variable:

  • In a fresh terminal update the OS X plist version of the PATH to match your newly updated version: defaults write $HOME/.MacOSX/environment PATH "$PATH" and restart your machine.
  • In your emacs config explicitly create a PATH variable: (setq path "/Applications/SuperCollider:/rest/of/PATH")(setenv "PATH" path)
  • Again, in your Emacs config, add SuperCollider to your exec-path: (push "/Applications/SuperCollider" exec-path)

Not all of these may be necessary; I did the the last two.

Creating a Fresh SCClassLibrary

Create a new place for a fresh copy of SCClassLibrary:

 mkdir ~/.sclang

Copy the SCClassLibrary from your SuperCollider install into this new place:

 cp -R /Applications/SuperCollider/SCClassLibrary ~/.sclang

Also, copy across the plugins directory:

 cp -R /Applications/SuperCollider/plugins ~/.sclang

Finally, copy across the scsynth binary (needed to boot the server and make actual sounds):

cp /Applications/SuperCollider/scsynth ~/.sclang

/Applications/SuperCollider/README\ SCLang\ OSX advises you to edit the startup method in ~/.sclang/SCClassLibrary/Platform/osx/OSXPlatform.sc. Open it up and find the startup method. Delete everything from startup { to the closing } and replace it with the following:

startup {
            if ( this.hasFeature( \emacs ) ) {

Grab a copy of scel

Now we’re ready to power up Emacs with SuperCollider goodness. I found a copy of scel on github that contained a few issues that I ironed out in my own clone3. Feel free to grab it and add it to your own emacs config. That might be as simple as downloading the tarball, or if you manage your config with git (which, by the way, is a great idea) then you could just add it as a submodule. I happen to have based my config on Phil Hagelberg’s wonderful emacs starter kit. So this can be as simple as:

cd ~/.emacs.d
git init #unless you already manage your .emacs.d with git
git submodule add git://github.com/samaaron/scel.git vendor/supercollider

Copy scel’s sc files to your new SCClassLibrary

For scel to work correctly it has a number of SC Class files that need to be compiled and loaded when sclang is started. In order to achieve this copy the sc files from scel’s sc directory into your new SCClassLibrary:

cp -R ~/.emacs.d/vendor/supercollider/sc ~/.sclang/SCClassLibrary/Common/Emacs

Configure Emacs

Now you need to teach emacs about the presence of scel. In your emacs config add the following:

(add-to-list 'load-path "~/.emacs.d/vendor/supercollider/el")
(require 'sclang)

Finally, you need to tell it where to find your SCClassLibrary and other bits and bobs. scel advises you to use M-x sclang-customize, however I just set the variables by hand:

'(sclang-auto-scroll-post-buffer t)
'(sclang-eval-line-forward nil)
'(sclang-help-path (quote ("/Applications/SuperCollider/Help")))
'(sclang-runtime-directory "~/.sclang/"))

Start up the Emacs SC Workbench

OK, well done for making it so far. I’m praying for you that things went hunky-dory. At this stage I simply restarted Emacs and hit M-x sclang-start and BOOM entered the SC Workbench. If this seems to work, and you see two buffers, a workbench and a log, try typing "Hello from Emacs".postln; in the buffer and with the cursor over it hit M-x sclang-eval-line. You should see the message pop up in the log. Finally, try booting the server with M-x sclang-server-start. You should be good to go.

Go grab a cup of tea, you’ve earned it.

  1. Please note that not all these steps may be necessary. It’s possible I accidentally slipped in some cargo-culting along the way. Please feel free to let me know if there are smarter and wiser ways of getting this to work!_

  2. Obviously, that’s Emacs ;-)

  3. I still can’t faithfully communicate how absolutely amazing github is. The fact that it enabled this process (find library, clone library, fix library, share library) in such a frictionless manner is outstanding.

blog comments powered by Disqus

Recent Posts