Daniel B.
Daniel B.

Reputation: 1680

Control amplitude and release of Env using sample Buffer

I have a very short sample file, which plays via a Synth in the following loop.

I want the sample's amplitude and duration to be matched by the amplitude and release of the Env of a separate oscillator.

(Soon a live input will take the place of the sample file, if that helps clarify the path I'm taking.)

Issue #1

The following Env + SinOsc attempt has no effect:

(

s = Server.local;

b = Buffer.read(s, "/Users/freen/Desktop/a_rook_is_placed.wav");

SynthDef("aRookIsPlaced", { arg out=0, bufnum=0, rate=1, trigger=1, startPos=0, loop=0;
    var sampleBuf,
        env,
        amp,
        sig,
        bufRate = BufRateScale.kr(bufnum) * rate,
        bufStartPos = BufFrames.ir(bufnum) * startPos;

    sampleBuf = PlayBuf.ar(1, bufnum, bufRate, trigger, bufStartPos, loop);

    amp = Amplitude.kr(sampleBuf);
    env = EnvGen.kr(Env.perc(0.001, 4, amp), doneAction: 2);
    sig = SinOsc.ar(300, mul:env);

    Out.ar(out, sig ! 2);
}).add;

Routine({
    while ({ true }, {

        Synth(\aRookIsPlaced, [\out, 0, \bufnum, b.bufnum]);

        rrand(0.9, 1.7).wait;
    });
}).play(AppClock);

)

While a similar exercise using Pulse (no Env) works:

(

s = Server.local;

b = Buffer.read(s, "/Users/freen/Desktop/a_rook_is_placed.wav");

SynthDef("aRookIsPlaced", { arg out=0, bufnum=0, rate=1, trigger=1, startPos=0, loop=0;
    var sampleBuf,
        sig,
        bufRate = BufRateScale.kr(bufnum) * rate,
        bufStartPos = BufFrames.ir(bufnum) * startPos;

    sampleBuf = PlayBuf.ar(1, bufnum, bufRate, trigger, bufStartPos, loop);

    sig = Pulse.ar(90, 0.3, Amplitude.kr(sampleBuf));

    Out.ar(out, sig ! 2);
}).add;

Routine({
    while ({ true }, {

        Synth(\aRookIsPlaced, [\out, 0, \bufnum, b.bufnum]);

        rrand(0.9, 1.7).wait;
    });
}).play(AppClock);

)

Issue #2

I'm unsure how to access the buffer length, for use as Env's release.

Upvotes: 0

Views: 189

Answers (1)

ezra buchla
ezra buchla

Reputation: 364

Issue 1

The Env.perc level is fixed at creation time. (Documentation isn't super clear on this IMHO.) Instead, modulate the level of EnvGen.

Also, EnvGen should probably be audio rate.

Try env = EnvGen.ar(Env.perc(0.001, 4), levelScale:amp, doneAction: 2);

You will get a percussive envelope that is modulated by the sample amplitude; assuming that's what you're after.

Issue 2

Try BufDur.

Upvotes: 1

Related Questions