Switch cases with sample

Hi I’m trying to create 5 states/cases.

Every case runs 15 min (check!)
Every case starts with a sample that plays for 5 seconds (FAIL)
Every case then plays a Synth before going over to the next case. (check!)

I’ve tried something I thought would work. But it doesn’t.

How do I get to play the sample for the time it takes wait a few second and then start playing the “composition” or synthdef in the case?

s.boot;
// load samples
var b1 = Buffer.read(s, "/Users/location/Supercollider/actone.mp3");
var b2 = Buffer.read(s, "/Users/location/Supercollider/acttwo.mp3");
var b3 = Buffer.read(s, "/Users/location/Supercollider/actthree.mp3");
var b4 = Buffer.read(s, "/Users/location/Supercollider/actfour.mp3");
var b5 = Buffer.read(s, "/Users/location/Supercollider/actfive.mp3");




(
Different synthdefs to use in the cases
)

(
var switchTime = 60; // time in seconds between cases
var caseIndex = 0;
var cases = Array.fill(5, {});
var currentSynth; // Variable to keep track of the currently playing synth
var sampleDuration = 5;

// Assign actions to each case, ensuring to free the currentSynth before playing a new one
cases[0] = {
	b1.play; // play sample for case 1 //------> this doesn't work
    sampleDuration.wait; // wait
    currentSynth !? _.free; // Free the current synth if it exists
    "Case 1: synth 1".postln;
        currentSynth = Synth(\synth1); // play synth1

};

cases[1] = {
    currentSynth !? _.free;
    "Case 2: Simple FM".postln;
    currentSynth = Synth(\simpleFM, [\carrier, 330, \modIndex, 2]);
};

cases[2] = {
    currentSynth !? _.free;
    "Case 3: Noise Burst".postln;
    currentSynth = Synth(\noiseBurst);
};

cases[3] = {
    currentSynth !? _.free;
    "Case 4: Pulsating Sound".postln;
    currentSynth = Synth(\pulsating, [\freq, 220]);
};

cases[4] = {
    currentSynth !? _.free;
    "Case 5: Saw Tone".postln;
    currentSynth = Synth(\sawTone, [\freq, 550]);
};

// Cycle through cases
fork {
    loop {
        cases[caseIndex].value();
        caseIndex = (caseIndex + 1) % cases.size;
        switchTime.wait;
    };
};
)

UPDATE FORGOT THE POSTWINDOW
The post window!

ERROR: Variable 'b1' not defined.
  in interpreted text
  line 10 char 3:

  	b1.play; // play sample for case 1

Read thy post window:

s.boot;

b = Buffer.read(s, "/home/dlm/Music/23-0902-piano-chords.mp3");

File '/home/dlm/Music/23-0902-piano-chords.mp3' could not be opened: Format not recognised.

Try wav, aiff or flac.

hjh

1 Like

I have to amend this: I’d assumed that “we don’t support mp3” but it seems that isn’t always true – in some cases, we might be able to load an mp3!

It turns out that libsndfile can be compiled with mp3 support – but it isn’t, on my machine (probably because I get libsndfile from Ubuntu packages, and the package has probably been built without mp3). I’m not sure about our binary releases.

In any case, the original example here looks correct, and the most likely problem is failure to read the files, and mp3 may or may not be the problem.

hjh

1 Like

This seems to be true for many releases. Now, it’s just an option to allow it or not.

Changed the samples to wav, and now it works!
And made the sample var en global variable.

1 Like

Cases[0] stops when cases[1] starts.

But when I work with a routine (with bind, yield etcc)
The routine doesn’t stop when cases[2] start.

I’ve tried a lot of things, and I’m a bit lost in my own process.

Going to start over, but if there is anybody who has a hint where to look or how to fix this.
I’m all ears!

(
var switchTime = 40; // time in seconds between cases
var caseIndex = 0;
var cases = Array.fill(5, {});
var currentSynth; // Variable to keep track of the currently playing synth
var sampleDuration = 5;



// Assign actions to each case, ensuring to free the currentSynth before playing a new one
cases[0] = {
	Routine.run({
	3.wait;
    currentSynth !? _.free; // Free the current synth if it exists
	currentSynth = Synth(\sampleOne, [\bufnum, ~b1.bufnum]); // play sample first
	3.wait;
	currentSynth.free;
	"Case 1: Breakcore".postln;
	currentSynth = Synth(\breakcore); // after sample play case composition

	});

};

cases[1] = {
    Routine.run({
        10.wait;
        currentSynth !? _.free;
        currentSynth = Synth(\sampleTwo, [\bufnum, ~b2.bufnum]); // play sample first
        6.wait;
        currentSynth.free;
        "Case 2: Metal Drone".postln;
{
        // Define a variable to store the routine
        var case2Routine;

        // Start the routine and assign it to the variable
        case2Routine = Routine({
            var s, root;
            s = Server.default;

            root = 50;

                s.bind { Synth.tail(nil, \fx) };
                (0..2).normalize.linlin(0, 1, -0.7, 0.7).do { |pan, i|
                    s.bind { Synth(\guitar, [pan: pan]) };
                };
                20.0.wait;
                2.do {
                    s.bind { Synth(\bass, [freq: root * -2.midiratio]) };
                    8.0.wait;
                    s.bind { Synth(\bass, [freq: root * -8.midiratio]) };
                    4.0.wait;
                };
                inf.do {
                    s.bind { Synth(\bass, [freq: root* -6.midiratio]) };
                    3.0.wait;

                    s.bind { Synth(\bass, [freq: root * -12.midiratio]) };
                    6.0.wait;
                };


            // Wait for the switch time before stopping the routine
            switchTime.wait;
			// Stop the routine when the switch time is reached
            case2Routine.stop; // Stop the routine
            // Stop the routine when the switch time is reached
            s.bind {
                Synth(\guitar).free;
                Synth(\bass).free;
                Synth(\fx).free;
            };
	}).play; // Execute the function block immediately
	}});
};

There is no instance of cases[2] in your code.