Skip to content

Conversation

celestebetancur
Copy link
Contributor

This new example works as expected

@gewang
Copy link
Member

gewang commented Jul 23, 2025

thank you for this fix -- @AndrewAday and I further tweaked the code as combination of your fix and the original (but removing an misplaced Math.sqrt() and also swapping X and Y, as they are not meant to be commutative. Here is the code (I tried committing to this branch but couldn't figure out how, so we added the final code directly to main and also credited @celestebetancur for the fix in the program as in the release notes. Thank you!

// name: xsynth.ck
// desc: a FFT-based cross-synthesizer
//       => input X modulates a second input Y,
//          carried out in the frequency domain
//
// authors: Ge Wang
//          Rebecca Fiebrink
//          Celeste Betancur
//          Andrew Zhu Aday
//
// date: 2007 initial (really bad) version
//       2025 made less bad

// input x (mic)
adc => FFT X => blackhole;
// source two (to be connected below)
FFT Y => blackhole;
// synthesis
IFFT ifft => dac;

// input y
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;
}

// set FFT size
1024 => X.size => Y.size => int FFT_SIZE;
// desired hop size
FFT_SIZE / 4 => int HOP_SIZE;
// set window and window size
Windowing.hann(FFT_SIZE) => X.window;
Windowing.hann(FFT_SIZE) => Y.window;
Windowing.hann(FFT_SIZE) => ifft.window;
// use this to hold contents
complex Z[FFT_SIZE/2];

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

    // multiply in frequency domain
    // NOTE: using input X's magnitude to modulate input Y's signal
    //       X and Y are NOT commutative
    for( int i; i < Z.size(); i++ )
        (X.cval(i)$polar).mag * Y.cval(i) => Z[i];

    // take ifft
    ifft.transform( Z );
    
    // advance time
    HOP_SIZE::samp => now;
}

@gewang gewang closed this Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants