// ----------------------------------------------------------------- // To enable searching for optimal delay loop length, insert // this code at the beginning of the ChucK script. // ----------------------------------------------------------------- spork ~ keepIncrementingDelayAndPrintingRMS(); 0.001::ms => dur searchStepSize; // If you enable search, step size. // . // . // . // main sound cancellation code goes here // . // . // . // ========================= SEARCH CODE START ==================== // (if you don't need to automatically search for the best delay, // you can ignore this code.) // Call using: spork ~ keepIncrementingDelayAndPrintingRMS(); // (This line is below, right above the main program loop.) // ----------------------------------------------------------------- // Here we set up the analyzer so we can measure how much sound // is not being cancelled out. If it's high, we should search // for a better delay. // ----------------------------------------------------------------- dac => FFT fft =^ RMS rms => blackhole; 1024 => fft.size; Windowing.hann(1024) => fft.window; // ----------------------------------------------------------------- // This function is started up by the "spork" command below, and // increments the delay while measuring RMS (total sound energy). // We want the lowest RMS possible. // ----------------------------------------------------------------- fun void keepIncrementingDelayAndPrintingRMS() { while (true) { delayLength + searchStepSize => delayLength; delayLength => delay.max => delay.delay; <<< " " >>>; <<< delayLength / 1::ms >>>; 2::second => now; 0 => float sum; for (0 => int x; x < 4; x++) { rms.upchuck() @=> UAnaBlob blob; sum + blob.fval(0) => sum; // <<< blob.fval(0) >>>; fft.size()::samp => now; 500::ms => now; } <<< "average" >>>; <<< sum / 4.0 >>>; } } // ========================= SEARCH CODE END ====================