// title: Example of morphing (interpolating) between two Signals/Wavetables // author: totalgee // description: // This is in response to a question on sc-users, with subject: [Envelope as signal](http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Envelope-as-signal-td7603680.html), about how to morph between two different Envelopes-as-wavetables... // // I tried to do one with double-buffering and cross-fading between the two buffers using XFade2.ar, but it gave me a sort of "pulsing" effect. You even get this when doing XFade2 between two versions of the same signal! But, fortunately, single-buffering seems to work fine. The changes to any given sample in the Signal/Wavetable are small at any iteration, so you don't hear any noise or clicking. (you would if you had too few steps in the interpolation, but even with 20-50 steps, it seems noise-free) // code: ( // Set things up, and start the Synth s.waitForBoot { SynthDef(\oscplayer,{ arg out = 0, bufnum; var signal; signal = Osc.ar(bufnum, MouseX.kr(60, 300)); Out.ar(out, signal ! 2) }).add; z = { Env([0] ++ rrand(-0.5, 0.5!5) ++ 0, [0.1, 0.2, 0.05, 0.3, 0.4, 0.1].scramble, \sin).asSignal(512) }; a = z ! 2; // a[0] is the start waveform, a[1] is the target to morph to... a.plot; c = a[0].asWavetable; b.free; b = Buffer.alloc(s, c.size); // 1024: twice the size of the 512-element signal! s.sync; b.setn(0, c); x = Synth(\oscplayer, [\bufnum, b]); }; ) ( // Do interpolation from one wave to another fork { var fadeTime = 5; // seconds var stepSize = 0.025; // seconds var numSteps = fadeTime / stepSize; "Fading...".postln; numSteps.do { arg i; var interp = i / (numSteps - 1); var interpSignal = a[0] * (1 - interp) + (a[1] * interp); b.setn(0, interpSignal.asWavetable); (fadeTime/numSteps).wait; }; "...done!".postln; }; ) ( // Generate a new target buffer, then run the fork above again to morph to new wave! a[0] = a[1]; a[1] = z.value; a.plot; ) x.free; b.free;