Wednesday, February 16, 2005

Server

In the introduction, we executed some code:

 Sever.local.boot

Server is a class. We can tell it is a class because in SuperCollider, all class names start with capital letters. Nothing else may start with a capital letter.

local is a message. Supercollider uses a syntax called receiver notation, which looks like:

 object.message(argument1, argument2, . . . argumentN)

Or

 Class.message(argument1, argument2, . . . argumentN)

The arguments are optional. So “Server.local” takes the class “Server” and passes the message “local” to it.

When we send a message to an object or a class, we get a return value. The object or class gives us something back. Often, but not always, what we get back is the object or class that we just sent the message to. Sometimes, however, we get back something else. Server.local returns an object that refers to the localhost server.

Server.local is a getter message. The Server class contains some data that belongs to the class rather than to any particular instance of the class. We can get at that data by using a getter message. Getter messages return data stored within an object or a class. Setter messages have a similar concept. They set a piece of data within an object or a class. We’ll talk about them more later.

We then take the result of Server.local (which is an object that refers to the localhost server) and send that object the message “boot.” Expressions are evaluated left to right, so Server.local.boot is equivalent to (Server.local).boot. An expression is a bit of code that returns a value. Server.local is an expression because it returns something. Server.local.boot is also an expression because it also returns something.

Server.local is not, itself, the localhost server, as that is a separate process. However, Server.local contains information about the localhost server. The object knows how to communicate with the Server via OSC. So when we tell it “boot”, it translates that for us into an OSC message and sends that message to the separate Server process.

  • Class names must start with capital letters and are the only things that start with capital letters.
  • We pass messages in receiver notation as object.message(argument) or Class.message(argument)
  • Classes and objects return something when you send them messages.
  • Getter messages return data stored within an object or a class.
  • Expressions are bits of code that return something. They are evaluated from left to right.
  • Server.local is an object which can do OSC communication for us.

Object Oriented Programming

Programs and Algorithms

An algorithm is a step-by-step way to solve a problem or complete a task. You can think of it like a musical score. You play a score in order from left to right, playing each note or rest one after each other, jumping backwards in case of repeats and forwards in cases of things like second endings or codas. In the same way, you can tell your computer to play a B for two seconds and then an A and tell it to repeat back and so on. You're able to tell your computer to do more complicated things, like play a C if you move your mouse to the upper right hand corner and a D in the left hand corner.

For example, imagine making a toasted bagel with cream cheese. Your algorithm might go like this:

  1. Get a bagel
  2. Cut bagel in half
  3. Toast the bagel
  4. Spread cream cheese on each half, on the side that was cut.

Programs are coded according to algorithms. A program is a series of instructions that a computer follows to complete a task. In order to communicate instructions to your computer, you need to be able to speak a language in common with it. The native language of computers is called machine code and is made up of nothing but ones and zeros. Every kind of computer speaks a different dialect of machine code. Fortunately, there exist programming languages that are easier for humans to learn. You write your instructions (or code) in the SuperCollider language and when you evaluate them (by highlighting them and pressing enter), the interpreter translates them to machine code. Your program is made up of the lines of code that you write.

  • You start by thinking of your algorithm and then create a program to implement it.

Objects and Classes: a theoretical example

Let’s use our bagel algorithm to create pseudo-code that looks like object-oriented code. Pseudo-code is a mockup of a program. It will look like a SuperCollider program would look if it dealt with food instead of sounds.

Our algorithm starts with:

  1. Get a bagel

In real life, we would get a bagel from a bag of bagels. In an object-oriented language, we would think of the bagel as an object that we could manipulate. An object is an entity containing data and methods for accessing that data. The definition of a type object is called a class.

A bagel object would probably contain information about whether it was cut or toasted and what toppings it would have. Classes define objects and are a blueprint for object creation. They also can create objects. Classes contain methods called constructors that create new instances of the class and initialize them. We would send a message to the Bagel class asking it to make us a bagel object.

 Bagel.new;

Bagel is the class. new is the message. Once we have the new object, we need to remember it, so let’s change that to give it a name:

 my_bagel = Bagel.new;

my_bagel is the name of the newly created bagel. The equals sign is an assignment statement. my_bagel gets the new bagel. We’ll come back to this later.

  1. Cut bagel in half

In real life, we would do this with a knife. In object oriented programming, objects provide their own methods for changing their state. We send a message to the bagel saying we want it cut.

 my_bagel.cut;

my_bagel is the object. cut is the message.

  1. Toast the bagel

Again, in real life, we would put the bagel in a toaster. But, since this is an object, we send a message telling it we want it toasted.

 my_bagel.toast;
  1. Spread cream cheese on each half, on the side that was cut.

The bagel will know what to do. The author of the class has already written a method that applies a topping to each half. We just have to specify which topping we want, by using an argument. Arguments are additional data we pass along while sending a message to an object.

 my_bagel.spread(cream_cheese);

So, adding in the cream_cheese creation, our pseudo-code bagel program would look like:

 (

  var my_bagel, cream_cheese;
 
  my_bagel = Bagel.new;
  cream_cheese = Cheese.new(\cream);
 
  my_bagel.cut;
  my_bagel.toast;
  my_bagel.spread(cream_cheese);
 
 )
  • Objects are instances of the Classes that define them.
  • Objects contain data and methods to access them.
  • Classes are factories for creating new instances of objects.
  • We communicate with objects and classes by sending them messages.
  • We can specify additional information by providing arguments with our messages.

Additional reading on programs, algorithms and pseudo-code

Sunday, February 06, 2005

Introduction

What is SuperCollider?

According to http://supercollider.sf.net/:

SuperCollider is a state of the art, realtime sound synthesis server as well as an interpreted Object Oriented language which is based on Smalltalk but with C language family syntax. The language functions as a network client to the sound synthesis server.

SC was written by James McCartney over a period of many years. It is now an open source GPL'd project maintained and developed by James and a few others.

Which is to say that SuperCollider is a tool to help you use your computer to make sounds. It’s free and open source. That means that you can look at how SuperCollider was written and modify it, share it with other people and use it any way you want.

SuperCollider has a steeper learning curve than some other music programs like MAX, but it is more flexible and more powerful. This book is written for people who have not programmed before. If you can use your computer to do things like edit a document and surf the web, you can learn to program.

What Can You Do With It?

  1. Digital synthesis
    Supercollider can make any sound that can be created by DSP.
  2. FX processing
    SC can do delays, filters, etc and can tweak a line-in or a pre-existing sound file in any way that you can think to program
  3. Algorithmic composition
    Supercollider can generate sounds and play them, and it can also generate MIDI files that can be opened by Finale, Sibelius or other notation software and arranged for real instruments

Two Applications for the Price of One

SuperCollider is actually two applications. One application is an interpreter: an application designed to execute your Object Oriented programs written in the SuperCollider language.

The other program is an extremely fast and efficient sound synthesizer, which makes all the sound. This program is called a server. It can run from within the interpreter or as a separate process. An internal server (one run within SuperCollider) has a small speed advantage over a separate process. Also, there are some things that can only be run from an internal server, such as an oscilloscope plug in which lets you view the waveforms produced by the server. However, if you crash the server, the interpreter will also crash and vice versa.

A server run as a separate process is called the localhost server. Because the process is separate, there is a stability advantage because a server crash does not also crash your interpreter or vice versa. You can run a server on a separate computer, if you’d like and even communicate with it via Rendezvous. The interpreter client and audio server communicate via a network protocol called OSC. The interpreter sends OSC messages to the server based on your programs and the server sends messages back, based on what it’s doing.

About this book

Explanatory text looks like all the text we have seen so far.

Code examples look like this.

Vocabulary words are in bold and are usually followed by a definition. They can also be found in the glossary.

Key points are summarized at the end of every section in a bulleted list.

The first part of this book is about the interpreter and the programming language it uses. The second part of this book is about the server and sound design.

Getting Started

First, you need a copy of SuperCollider 3, otherwise known as SC3. You can download it from http://sf.net/project/showfiles.php?group_id=54622. SC3 exists for Mac OSX, Windows and Linux. The Mac version is the most developed and the most stable and the version referred to by this book. However, aside from the appearance and the key-shortcuts, the Windows and Linux versions should be virtually the same.

There are some websites designed to help SuperCollider users, including the SC home page at http://www.audiosynth.com/, the SWIKI at http://swiki.hfbk-hamburg.de:8888/MusicTechnology/6, and the Electronic Life SC Forum at http://electroniclife.co.uk/scforum/index.php

Using SuperCollider

Once you have downloaded SuperCollider and installed it, double click on the icon. Three windows should open on your screen. A big text window called "Untitled" should print out some information and there should be two smaller windows below it called "localhost server" and "internal server." If there is an error in the Untitled window, the two server windows will not open. Try downloading a different build of SuperCollider or running it on a different machine.

The Untitled window is where text output goes. The other two windows control two different versions of the audio Server. The examples in this document use the localhost server. If you want to hear audio, you must boot the audio server, which you can do by pressing the "Boot" button. When you press the “Boot” button on the audio server, the interpreter starts up the server application. When the server is finished booting, it sends an OSC message to the interpreter saying that it booted. Then the interpreter changes the color of the word “localhost” to red and the “Boot” button changes to say “Quit.” You can also boot a server from within a program, as we will see shortly.

To run code, you highlight it with the mouse and then press the Enter key, NOT the return key. (The enter key may be located next to your arrows or in your number pad.) To stop code that is running, hit apple-period.

To get help, hit apple-shift-?. To get help on a specific topic, for instance on Synth, highlight the word Synth and hit apple-shift-?.

Your First Program

Open a new window, which you can do under the File menu or by typing apple-n.

Boot the localhost server. You can do this from within the interpreter. To do this, type in the new window:

 Sever.local.boot

Highlight the code you just typed with the mouse and then press the enter key. Then, after the server finishes booting, type:

 Event.default.play

Highlight the code you just typed with the mouse and then press the enter key. You should hear a single short A. If you do not hear a sound, make sure that you can hear other sounds from computer and the volume is turned up. Make sure the localhost server is booted (and not the internal server). If you are still having trouble, try downloading a different build of SuperCollider or asking somebody knowledgeable for help.

Chapter Summary

  • SuperCollider is a free tool for making music that can do DSP and algorithmic composition.
  • SuperCollider is a synthesis server and an interpreter than can run together or separately.
  • You must run a server (either internal, localhost or on another computer) to hear sound.
  • Highlight code and press enter to execute it.
  • Press apple-period to stop execution
  • Press apple-question to get help