int main() { //set the global sample rate Stk::setSampleRate(44100.0); Stk::showWarnings(true); int nFrames=100000; SineWave sine; RtWvOut *dac=0; try { // define and open the default realtime output device for one channel playback dac= new RtWvOut(1); } catch (StkError &) { exit(1); } sine.setFrequency(441.0); for (int i=0; i<nFrames; i++) { try { dac->tick(sine.tick()); } catch (StkError &) { goto cleanup; } } cleanup: delete dac; return 0; }
// This tick() function handles sample computation only. It will be // called automatically when the system needs a new buffer of audio // samples. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *dataPointer ) { SineWave *sine = (SineWave *) dataPointer; register StkFloat *samples = (StkFloat *) outputBuffer; for ( unsigned int i=0; i<nBufferFrames; i++ ) *samples++ = sine->tick(); return 0; }
int main( int argc, char **argv ) { StkFloat lengthseconds = 2; StkFloat lofreq = 10; StkFloat hifreq = 22000; StkFloat partialsperoctave = 1500.0; unsigned long numpartials = 0; unsigned int noisetype = WHITENOISE; std::string outfilename( "outfile.wav" ); // -(option parsing)-------------------------------------------------------- int optionfound = -1; unsigned char ppospecified = 1; while( 1 ) { int option_index = 0; // getopt_long stores the option index here. optionfound = getopt_long (argc, argv, "l:s:e:p:n:wko:", long_options, &option_index); // end of options if (optionfound == -1) break; switch (optionfound) { case 'l': lengthseconds = atof( optarg ); break; case 's': lofreq = atof( optarg ); break; case 'e': hifreq = atof( optarg ); break; case 'p': partialsperoctave = atof(optarg); ppospecified = 1; break; case 'n': numpartials = atol(optarg); ppospecified = 0; break; case 'w': noisetype = WHITENOISE; break; case 'k': noisetype = PINKNOISE; break; case 'o': outfilename = optarg; break; default: abort(); } }; unsigned long numsamples = static_cast<unsigned long>(lengthseconds * Stk::sampleRate()); // need to do this at the end since it depends on lofreq/hifreq and can be // overridden by --numpartials if (ppospecified) numpartials = static_cast<unsigned long>(partialsperoctave * numoctaves( lofreq, hifreq )); if ( lofreq >= hifreq ) { std::cout << "ERROR: low freq (" << lofreq << ") is not lower than high freq (" << hifreq << ")" << std::endl; exit(0); }; // -(main routine)---------------------------------------------------------- // final output StkFrames output(0.0, numsamples, 1); // sine ugen SineWave thissine; std::cout << "computing " << numpartials << " partials" << std::endl; for ( unsigned long i = 0; i < numpartials; i++ ) { // progress indicator std::cout << "." << std::flush; if ( i % 20 == 0 ) std::cout << " " << i << " " << std::flush; if ( noisetype == WHITENOISE ) { thissine.setFrequency( getlinrandfreq( lofreq, hifreq ) ); } else { // PINKNOISE thissine.setFrequency( getlograndfreq( lofreq, hifreq ) ); } // NB just changing the frequency in this way but using the same // SineWave ugen effectively randomises the starting phase. we don't // want all of the partials phase aligned at the start since that leads // to a big amplitude spike (throwing off the normalisation step). for ( unsigned long j = 0; j < numsamples; j++ ) { // NB we're assuming here that we're not going to overflow the // doubles. max I've seen is ~200.0, so we should be safe. output[j] += thissine.tick(); } } std::cout << "done computing" << std::endl; normalize( output ); std::cout << "done normalising" << std::endl; // write to file FileWrite outfile( outfilename, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 ); outfile.write( output ); outfile.close(); std::cout << "wrote to " << outfilename << std::endl; std::cout << "all done" << std::endl; }