A Pharo client for SuperCollider's audio server: run scsynth as a separate process and play sounds over OSC
[](https://github.com/pharo-project/Pharo) # Scsynth A Pharo client for [SuperCollider](https://supercollider.github.io)'s audio server, scsynth. It starts, supervises, and stops scsynth as a separate process, loads sound files into it, and plays them with per-play pitch, volume, and sample-accurate delay. All audio runs in scsynth: there is no FFI audio in the image, and if the server crashes, the image keeps running and supervision restarts it. ## Pharo Version Policy This project is developed on Pharo 14, the only version it has been tested on. ## Installation Load the project into a Pharo image with Metacello: ```smalltalk Metacello new githubUser: 'Gabriel-Darbord' project: 'Scsynth' commitish: 'main' path: 'src'; baseline: 'Scsynth'; load ``` SuperCollider must be installed on the host. Only macOS has been tested so far. macOS (the server is then `/Applications/SuperCollider.app/Contents/Resources/scsynth`): ```sh brew install --cask supercollider ``` Linux (the server is then `scsynth` in the `PATH`): ```sh sudo apt install supercollider-server ``` Windows: install SuperCollider from [its downloads page](https://supercollider.github.io/downloads); the newest `C:\Program Files\SuperCollider*\scsynth.exe` is used. ## Quick Start ```smalltalk server := ScsynthServer new. server start. "launch scsynth, or adopt the one this image left running" sound := server loadSound: '/path/to/boom.wav'. "WAV, AIFF, FLAC, Ogg, or MP3, held in memory" sound play. sound playRate: 1.5 amplitude: 0.5 delay: 0.25. "a fifth higher, half as loud, in 250 ms" server startSupervising. "restart if it dies or the default output device changes" server stop. ``` `server options` sets the executable, the UDP port (57110), the hardware buffer (256 frames, about 5 ms), and the output device (by default, the system's default output). ## How It Works - Pharo talks OSC over UDP with its own sockets. Plays are sent without waiting; loading sounds and status requests wait for scsynth's answer without blocking other processes. - Commands run with LibC's `system()`, always in the background, because `system()` blocks the whole VM while it runs. On Unix they first close the descriptors inherited from the VM, so scsynth never holds the image's sockets, such as a server's listening port. The server's output goes to a log file next to the image (`scsynth/`). - The two sample players (mono and stereo) are embedded as compiled SynthDefs and sent on every start, so neither sclang nor `.scsyndef` files are needed. - On macOS the server is pinned to the current default output device; supervision restarts it on the new device when the default changes, and reloads its sounds into the same buffers. - Each image runs its own server. A server holds a token from the image that launched it, so an image adopts only a server it left running (after a crash, for example) and never stops another image's. When another image's server holds the port, the next free port is used, and `server options port` gives it. - A server that stops answering, as audio systems stall while devices change, gets about 15 seconds of supervision checks to recover before it is replaced. Stopping kills a server that does not quit, and launching first stops any server the image lost track of, so none is left behind. - Running servers are stopped when the image quits. ## Documentation The class comments of `ScsynthServer`, `ScsynthSound`, `ScsynthOptions`, and `ScsynthPlatform` describe the API; `ScsynthServerTest` runs a real server (skipped when SuperCollider is not installed).