// am.cpp STK example ring modulation program using blocking audio output.
//
// compile with: g++ -std=c++11 -Istk/include/ -Lstk/src/ -D__MACOSX_CORE__ am.cpp -lstk -lpthread -framework CoreAudio -framework CoreMIDI -framework CoreFoundation
#include "SineWave.h"
#include "Envelope.h"
#include "RtWvOut.h"
using namespace stk;
int main( int argc, char *argv[] )
{
if ( argc != 2 ) {
std::cout << "usage: " << argv[0] << " duration" << std::endl;
std::cout << " where 'duration' is in seconds.\n" << std::endl;
exit(0);
}
int nSeconds = atof( argv[1] ); // convert ascii to float
if ( nSeconds <= 0.0 ) {
std::cout << "Duration must be greater than zero!\n" << std::endl;
exit(0);
}
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
Stk::showWarnings( true );
int nFrames = (int) nSeconds * Stk::sampleRate();
StkFloat temp;
try {
SineWave carrier;
SineWave modulator;
Envelope env;
// Define and open the default realtime output device for one-channel playback
RtWvOut dac( 1 );
carrier.setFrequency( 440.0 );
// Use envelope to sweep the modulator frequency from 4 to 100 Hz
// over duration of the program.
env.keyOn(); // sets target to 1.0
env.setTime( nFrames / Stk::sampleRate() );
// Single-sample computations
for ( int i=0; i<nFrames; i++ ) {
modulator.setFrequency( 4.0 + 96.0*env.tick() ); // 4 to 100 Hz
temp = 0.4 * (1.0 + modulator.tick()) * carrier.tick();
dac.tick( temp );
}
}
catch ( StkError & ) {
exit( 1 );
}
return 0;
}
| ©2004-2025 McGill University. All Rights Reserved. Maintained by Gary P. Scavone. |