title: Control amplitude and release of Env using sample Buffer
body: <p>I have a very short sample file, which plays via a Synth in the following loop.</p>
  
  <p><em>I want the sample's amplitude and duration to be matched by the amplitude and release of the Env of a separate oscillator.</em></p>
  
  <p>(Soon a live input will take the place of the sample file, if that helps clarify the path I'm taking.)</p>
  
  <p><strong>Issue #1</strong></p>
  
  <p><em>The following Env + SinOsc attempt <strong>has no effect:</em></strong></p>
  
  <pre class="lang-rb prettyprint-override"><code>(
  
  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);
  
  )
  </code></pre>
  
  <p><em>While a similar exercise using Pulse (no Env) <strong>works:</em></strong></p>
  
  <pre class="lang-rb prettyprint-override"><code>(
  
  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);
  
  )
  </code></pre>
  
  <p><strong>Issue #2</strong></p>
  
  <p>I'm unsure how to access the buffer length, for use as Env's release.</p>