ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
799 stars 127 forks source link

FFT IFFT and xsynth.ck example #422

Open celestebetancur opened 8 months ago

celestebetancur commented 8 months ago

I am adding a working example of the "// really really bad cross synthesizer..." . Some little adjustments but I don't know why the signal that is being re-synthesized needs to be boosted by such a big factor. I tried the same example using SndBufs and an impulse response and the gain needs to be boosted by a factor of 1000.

//Using headphones is recommended // source one (mic) adc => FFT X => blackhole; FFT Y => blackhole; IFFT ifft => Gain amp => dac;

//to add some dry signal to the chain adc => Delay d => Gain dry => dac;

// source two (to be connected below) BlitSquare blt[6]; [ 40, 46, 52, 60, 64, 87] @=> int pitches[]; for( int i; i < blt.size(); i++ ) { blt[i] => Y; 20 => blt[i].harmonics; pitches[i] => Std.mtof => blt[i].freq; }

// This number will make a huge difference on the sound quality // Powers of 2 usually recommended (try 2, 4, 8, 16, 32 ...) 64 => int fftSizeScaler;

(128fftSizeScaler)::samp => d.max; (128fftSizeScaler)::samp => d.delay;

// The amount of the original signal you want to hear in the final mix 0.04 => dry.gain; // The gain of the convoluted (wet) signal 10 => amp.gain;

// set FFT size really big to get a better sound quality but it introduces a lot of delay (trade off) 128*fftSizeScaler => int FFT_SIZE; FFT_SIZE => X.size => Y.size;

// set window and window size Windowing.triangle(FFT_SIZE) => X.window => Y.window => ifft.window;

// use this to hold contents complex Z[FFT_SIZE/2];

// desired hop size FFT_SIZE / 4 => int HOP_SIZE;

// control loop while( true ) { // take ffts X.upchuck(); Y.upchuck();

// multiply in frequency domain
for(int i; i < FFT_SIZE/2; i++){
    //Math.sqrt((Y.cval(i)$polar).mag) * X.cval(i) => Z[i];
    // Simple element wise multiplication should work in freq domain:
    (X.cval(i) * (Y.cval(i))) * 4 => Z[i];
}

// take ifft
ifft.transform( Z );

// advance time
HOP_SIZE::samp => now;

}