Cover photo

Knowvember - Learn and write about something every day in November.

Day One - Programming Music in the SuperCollider programming language.

November is a month for challenges. Three years ago I undertook #100DaysOfBlender as a personal challenge to learn 3d modeling. I learned a ton in that time and it helped drive the beginning of my career as a cryptoartist.

Recently I have been thinking about NaNoWriMo. I have never written a novel, and done very little creative writing in my life. Nonetheless, the idea of the month-long challenge to write a novel is quite compelling. However, having spent the last few weeks listening to writers speak on their "preptober" and the different ways of preparing to write a novel in a month, I realize I am entirely unprepared to write a novel. However, I am fully prepared for the daily challenge of writing!

The Challenge

For the month of November, I will learn something new every day and reinforce that learning by writing about it.

I will do this to the best of my ability and in accordance with my schedule. If my work on a project that day requires me to learn something new, then I will write about my what I learned in the course of working on that project. On a day where I have little free time, such as Thanksgiving, I will do a little reading and write about what I learned. The amount of work is not the goal, learning and sharing is.


The SuperCollider Programming Language

I'll be honest - SuperCollider (SC) is something I've played with ever since my college days. I was introduced to it in 2006 and have used it off and on since then. But Its an incredibly complicated language and topic and there is no doubt plenty of opportunity for me to learn something new in the course of this article.

If you want to follow along, you can download SuperCollider here. It should work out of the box with Mac, Windows and Linux distros. Although its not uncommon that some configuring has to be done if you have multiple audio devices installed.

A little primer - SuperCollider is a few things:

  • A programming language

  • A language interpreter (sclang)

  • An audio synthesis server (scsynth)

  • And an IDE (SuperCollider, the app)

The cool kids in the livecoding scene run supercollider in emacs or vim, but for now we'll use the IDE.

If SC is up and running, you'll see this in the post window:

In the document, lets start the server by entering s.boot; and pressing CMD+enter or CTRL+Enter (Mac/Win respectively).

I dont know about you, but I've already gotten an error! Here it is:

The issue here is that my bluetooth headphones are in the 0/1 slots, and the mic has a low sample rate. The easy-fix is to open up Audio MIDI Setup, open up the Audio Devices screen, and change to the built-in macbook mic as the default input:

once thats done, re-run s.boot. Over here, looks like we're good to go:

Just for kicks, we can test that its working with some basic SC code to make sound:

{ SinOsc.ar * 0.5 ! 2 }.play - Theres a ton of shorthand going on - SinOsc is a UGen being sent an audio-rate message (ar) with the default values (freq: 440, phase:0, amp:1), played at half-volume so as not to make me go deaf. To stop all sounds, press Command + . (command+period). The exclamation point operator expands duplicates the signal into an array of 2. The result is a stereo signal. Without the ! 2 at the end, you'd only hear a tone from the left speaker.

WARNING! SUPERCOLLIDER WILL ABSOLUTELY OBLITERATE YOUR EAR DRUMS IF YOU'RE NOT CAREFUL! ALWAYS TEST YOUR CODE AT LOW VOLUMES.

If you're unsure about what your code is going to do, take your headphones off your ears first. You might think your volume is set to a very low level, but supercollider will prove you wrong. BE CAREFUL. Seriously, you can hurt your eardrums with this stuff.

SinOsc.ar(amp:2) <-- if you were to play that with headphones with your volume up, you'd quickly regret it. Remember command period stops all sound.

This is not intended to be a beginners tutorial, but a personal exploration. If you would like to explore SC as a beginner yourself, I recommend the Getting Started tutorials.

You should know that there are several variable types:

  • Variable declarations var whatever = 'some value' : these are scope-bound and die when the process is terminated with cmd-period.

  • Single letter variables s = Server.default.boot persist as long as the language server is running.

  • NodeSpace variables ~sound = { SinOsc.ar([400, 407] * 0.9, 0, 0.2) };

Livecoding with ProxySpaces

Most of my use has been writing compositions with the extensive library of pattern classes, but ProxySpaces are a great tool for performing live with code. By mapping control-rate and audio-rate streams to variables, you can control streams with other streams.

In the above example, I

  • create a ProxySpace for the default server (s),

  • play NodeSpace as an empty audio stream,

  • set the fadeTime to 1 second, which allows it to gradually transition from one NodeSpace variable definition to the next

  • Set the definition to a low-frequency parabolic sine wave, with a default frequency of 300 hz, at 30% volume, in stereo

  • Make a control-rate stream linked to the mouse x-position that ranges between 100 and 300

  • map that mouse control-rate to the frequency of our LFPar.

The result is a mouse-controlled synthesizer, albeit a very boring sounding one. This is just the tip of the ice-berg for dynamically controlled sound! By making interlinking controls, we can get really creative!

Its just about time for me to hit the hay tonight but lets expand this example to make an FM synthesizer sound.

In this example, I've modified our audio stream to have two new arguments - modfreq and modamp, which represent the frequency of the second tone and its amplitude. The value of that signal multiplied by its amplitude, is added to our frequency of 300 every sample. Finally on line 16, i use tanh to ensure our audio signal amplitude never goes above 1. This is a very simple example of a mouse-controlled FM synth, but one that demonstrates the power of the technique.

While this form of synthesis isnt new to me, using ProxySpace to code in supercollider sure is! I'm glad to say I know it and am excited to explore more! I hope this has given you an appetite for livecoding music.

Supercollider without sclang

Thanks to Supercollider being a synthesis server, it extends beyond its own language. Other languages are built on top of sclang that are decidedly less complicated and more approachable to beginners, such as Sonic PI which I highly recommend trying. Its based on Ruby and designed to teach children to write code and have fun doing it. It doesnt have all the complex ways of playing with audio-signals that sclang has, but its great for livecoding music!

Loading...
highlight
Collect this post to permanently own it.
mxjxn musings logo
Subscribe to mxjxn musings and never miss a post.