Product SiteDocumentation Site

11.4.6. Scheduling the Tones

  1. The next step is to get these started consecutively, with 5-second pauses after each addition. For this we will use a TempoClock, and since this is the only thing that we're doing, we'll just use the default one called TempoClock.default. I don't feel like typing that, however, so we're going to define an alias variable: var t_c = TempoClock.default; You could put that in the main function, but I suggest putting it before the main function. This way, if we want to write another function later, then it can also access t_c.
  2. The default TempoClock has a default tempo of one beat per second (1 Hz). This will be good enough for us. If you wanted to change the tempo, remember that you can enter a metronome setting (which is "beats per minute") by dividing the metronome setting by 60. So a metronome's 120 beats per minute would be given to a new TempoClock as TempoClock.new( 120/60 ). Even though you could do that ahead of time and just write "2," inputting it as "120/60" makes it clearer what tempo you intend to set.
  3. You can schedule something on a TempoClock by using t_c.sched( x, f );, where f is a function to execute, and x is when it should be done, measured as the number of beats from now. So we can schedule our SinOsc like this:
    t_c.sched( 1, {{[ SinOsc.ar( freq:frequency1, mul:0.01 ), SinOsc.ar( freq:frequency1, mul:0.01 ) ]}.play;} );
  4. Schedule the rest, in intervals of five beats (which is five seconds). They will all be scheduled virtually instantaneously (that is, the computer will notice the slight delay between when each one is scheduled, but humans will not). I started at one beat from now, to insert a slight pause before the sound begins.
  5. If you've done this correctly, then we should get a build-up of ten pitches. But they never stop! This is going to take some more ingenuity to solve, because we can't just make a stereo array, play it, then throw it away. We need to hold onto the stereo array, so that we can stop it. The first step here is to store the stereo arrays in variables, and subsequently schedule them. You will end up with something like this:
    var sinosc1 = { [ SinOsc.ar( freq:frequency1, mul:0.01 ), SinOsc.ar( freq:frequency1, mul:0.01 ) ] };
    // the other nine...
    
       t_c.sched( 1, { sinosc1.play; } );
    // the other nine...
    
  6. It should still work, but we after all that cutting-and-pasting, we still haven't managed to turn off the SinOsc's. We need to "free" the object that was returned when we used the "play" function. We need to declare yet more variables: var so1, so2, so3, so4, so5, so6, so7, so8, so9, so0; should appear anywhere before the scheduler.
  7. Now adjust all the scheduling commands so they look like this: t_c.sched( 1, { so1 = sinosc1.play; } );
  8. Now you can add ten of these, after the existing scheduling commands: t_c.sched( 51, { so1.free; } );. Be sure to schedule each one for 51 beats, so that they all turn off simultaneously, 5 beats after the last pitch is added.
  9. It should work successfully. If it doesn't, then compare what you have to this, which does work:
    var t_c = TempoClock.default;
    
    {
       var frequency1 = 200 + 600.rand;
       var frequency2 = 200 + 600.rand;
       var frequency3 = 200 + 600.rand;
       var frequency4 = 200 + 600.rand;
       var frequency5 = 200 + 600.rand;
       var frequency6 = 200 + 600.rand;
       var frequency7 = 200 + 600.rand;
       var frequency8 = 200 + 600.rand;
       var frequency9 = 200 + 600.rand;
       var frequency0 = 200 + 600.rand;
    
       var sinosc1 = { [ SinOsc.ar( freq:frequency1, mul:0.01 ), SinOsc.ar( freq:frequency1, mul:0.01 ) ] };
       var sinosc2 = { [ SinOsc.ar( freq:frequency2, mul:0.01 ), SinOsc.ar( freq:frequency2, mul:0.01 ) ] };
       var sinosc3 = { [ SinOsc.ar( freq:frequency3, mul:0.01 ), SinOsc.ar( freq:frequency3, mul:0.01 ) ] };
       var sinosc4 = { [ SinOsc.ar( freq:frequency4, mul:0.01 ), SinOsc.ar( freq:frequency4, mul:0.01 ) ] };
       var sinosc5 = { [ SinOsc.ar( freq:frequency5, mul:0.01 ), SinOsc.ar( freq:frequency5, mul:0.01 ) ] };
       var sinosc6 = { [ SinOsc.ar( freq:frequency6, mul:0.01 ), SinOsc.ar( freq:frequency6, mul:0.01 ) ] };
       var sinosc7 = { [ SinOsc.ar( freq:frequency7, mul:0.01 ), SinOsc.ar( freq:frequency7, mul:0.01 ) ] };
       var sinosc8 = { [ SinOsc.ar( freq:frequency8, mul:0.01 ), SinOsc.ar( freq:frequency8, mul:0.01 ) ] };
       var sinosc9 = { [ SinOsc.ar( freq:frequency9, mul:0.01 ), SinOsc.ar( freq:frequency9, mul:0.01 ) ] };
       var sinosc0 = { [ SinOsc.ar( freq:frequency0, mul:0.01 ), SinOsc.ar( freq:frequency0, mul:0.01 ) ] };
    
       var so1, so2, so3, so4, so5, so6, so7, so8, so9, so0;
    
       t_c.sched( 1, { so1 = sinosc1.play; } );
       t_c.sched( 6, { so2 = sinosc2.play; } );
       t_c.sched( 11, { so3 = sinosc3.play; } );
       t_c.sched( 16, { so4 = sinosc4.play; } );
       t_c.sched( 21, { so5 = sinosc5.play; } );
       t_c.sched( 26, { so6 = sinosc6.play; } );
       t_c.sched( 31, { so7 = sinosc7.play; } );
       t_c.sched( 36, { so8 = sinosc8.play; } );
       t_c.sched( 41, { so9 = sinosc9.play; } );
       t_c.sched( 46, { so0 = sinosc0.play; } );
    
       t_c.sched( 51, { so1.free; } );
       t_c.sched( 51, { so2.free; } );
       t_c.sched( 51, { so3.free; } );
       t_c.sched( 51, { so4.free; } );
       t_c.sched( 51, { so5.free; } );
       t_c.sched( 51, { so6.free; } );
       t_c.sched( 51, { so7.free; } );
       t_c.sched( 51, { so8.free; } );
       t_c.sched( 51, { so9.free; } );
       t_c.sched( 51, { so0.free; } );
    
    }.value;