Wednesday, 1 February 2012

My First SynthDef

I just spent some time learning about SynthDef's, or Synth Definitions. Writing some code and executing it with .play; or .scope; is all well and good, but for a practical application not so much. My professor Nick Collins, who has some awesome Supercollider tutorials here (http://www.informatics.sussex.ac.uk/users/nc81/courses/cm1/sccourse.zip), describes a SynthDef as the recipe for a synth - once we have the recipe, we can use it many times, create multiple instances of the same Synth, and then manipulate those instances.

Here's the first, simplest SynthDef from the tutorials:

SynthDef(\sine, {Out.ar(0,SinOsc.ar(Rand(440,880),0,0.1))}).add;

This defines a synth (named \sine) that outputs a sine wave, that will take a random frequency value from between 440 and 880 each time it is instantiated. This randomisation is caused by using the Rand function for the frequency parameter of the SinOsc.

Once you execute that code and add (".add;") the SynthDef to the server, you can execute is thusly

Synth(\sine);

Much nicer than writing

{SinOsc.ar(440,0,0,1)}.play;

over and over.

Listening to a constant sine tone can get pretty boring though. I was pleased to discover that we can easily make things more interesting by adding arguments to the SynthDef - like frequency and amplitude arguments, for example.

SynthDef(\sine2,{arg freq = 220, amp = 0.5;
Out.ar(0,SinOsc.ar(freq,0,amp))}).add;
         
h=Synth(\sine2);
i=Synth(\sine2,[\freq,550]);
j=Synth(\sine2,[\freq,660, \amp,0.5]);

I'm really starting to appreciate the way things can plug into each other easily in Supercollider. Here, I initialise (you have to set default values) the arguments of frequency and amplitude at 220Hz and 0.5*. Then, we pass these arguments to the frequency and amplitude parameters of the SinOsc that produces the output. We can then instantiate them one at a time, with different frequency arguments each time. The sound gets richer as you add more.

Here's an example of me running the same SynthDef over and over, setting a different frequency argument each time.

http://soundcloud.com/samlovett/harmonic-synthdefs
Once you stop the SynthDef, use the code h.free; to stop the synth running.

Towards the end of the tutorial, there was an exercise to turn a patch you'd previously worked on into a SynthDef - so I did exactly that. Here's the square wave modulator we looked at before, turned into a SynthDef.


My First Synth Def

SynthDef(\squaremod, {Out.ar(0,
        {
            var modfreq, modindex;
           
            modfreq=MouseX.kr(1,440,'exponential');
            modindex=MouseY.kr(0.0,15,0);
           
            LFPulse.ar(
                SinOscFB.ar(modfreq,1,modfreq*modindex,120),0,0.5)*0.1
        })}).add;
       
Synth(\squaremod);

The fact that I can utilise all of that code (perhaps "all of" isn't quite right) just by typing Synth(\squaremod); makes me very happy.

Sunday, 22 January 2012

Week 1 (12/01/12)

Hi all, I'm Sam Lovett and I'm gonna use this blog to keep a learning journal for my Computer Music course at sussex uni, in which we'll be learning the music programming language Supercollider.

In class this week we looked at some basic synthesis techniques, how to initiate an oscillator etc, as well as control rates, and controlling modulation with the mouse, or with a line generator over a set time. I made this neat sub drop/white noise uplifter in class, sounds like some house to me!

{HPF.ar(WhiteNoise.ar(0.1),Line.kr(500,15000,7))
       +
       SinOsc.ar(Line.kr(160,80,7),0, 0.1)}.freqscope;

.kr = control, and the Line takes 3 parameters - the first two are the frequency range, and the third is time - so the Sine oscillator SinOsc drops from 160Hz to 80Hz over 7 seconds.

Here's the result:

http://soundcloud.com/samlovett/white-noise-uplifter-sub-drop

I like the logical way that plugins are nested inside each other - so the Line controlling the SinOsc is nested inside it.

As stated we also looked at modulation using the mouse. This is done by using the mouse to control 2 parameters of modulation, in this case frequency modulation. This is an example from Nick Collins Supercollider tutorials that I altered:

(
{
var modfreq, modindex;

modfreq= MouseX.kr(1,440, 'exponential');
modindex=MouseY.kr(0.0,10.0);

LFPulse.ar(SinOscFB.ar(modfreq,1,modfreq*modindex, 120),0,0.5)*0.1
}.scope
)


Here I modulate the a square wave (the LFPulse Ugen with a width of 0.5 to make it square) with a Sine oscilattor - as you can see the parameters of the SinOsc are the modulation frequency and modulation index. It is the modulation index that gives the sound the feeling of stepping over rates of modulation as you move the mouse across the screen.

Here's an example of what it sounds like:

http://soundcloud.com/samlovett/frequency-modulated-square

Nice. More geeking out next week.