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; }
//-------------------------------------------------------------- void testApp::setup(){ ofSoundStreamSetup(2, 0, this, 44100, 256, 4); // create a new oscillator which we'll use for the actual audio signal SineWave tone = SineWave(); // create a sine wave we'll use for some vibrato SineWave vibratoOsc = SineWave(); vibratoOsc.freq(1); // you can use the regular arithmatic operators on Generators and their subclasses (SineWave extends Generator) float basePitch = 400; Generator frequency = basePitch + (vibratoOsc * basePitch * 0.01); // plug that frequency generator into the frequency slot of the main audio-producing sine wave tone.freq(frequency); ControlGenerator envelopeTrigger = synth.addParameter("trigger"); Generator env = ADSR().attack(0.01).decay(0.5).sustain(0).release(0).trigger(envelopeTrigger); // set the synth's final output generator synth.setOutputGen( tone * env ); }
int main() { // Set the global sample rate before creating class instances. Stk::setSampleRate( 44100.0 ); SineWave sine; RtAudio dac; // Figure out how many bytes in an StkFloat and setup the RtAudio stream. RtAudio::StreamParameters parameters; parameters.deviceId = dac.getDefaultOutputDevice(); parameters.deviceId = 3; parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; try { dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine ); } catch ( RtAudioError &error ) { error.printMessage(); goto cleanup; } // configuration of oscilator sine.setFrequency(440.0); // start the main real time loop try { dac.startStream(); } catch ( RtAudioError &error ) { error.printMessage(); goto cleanup; } // USER interface // Block waiting here. char keyhit; std::cout << "\nPlaying ... press <enter> to quit.\n"; std::cin.get( keyhit ); // SYSTEM shutdown // Shut down the output stream. try { dac.closeStream(); } catch ( RtAudioError &error ) { error.printMessage(); } cleanup: 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[]) { SineWave sine; float* tempSize; enum{ARG_NAME = 0, ARG_FREQ, ARG_AMP, ARG_DUR, NUM_ARGS}; if (argc != NUM_ARGS || atoi(argv[ARG_FREQ]) < 1 || atof(argv[ARG_AMP]) > 1 || atof(argv[ARG_DUR]) <= 0 ) { cout << "Give: \n" "- frequency(> 1) \n" << "- amplitude(<= 1) \n" << "- duration(> 0) \n" << " default sampleRate = 44100" << endl; } else { sine.setFrequency(atoi(argv[ARG_FREQ])); sine.setAmplitude(atof(argv[ARG_AMP])); sine.setSampleRate(); sine.setDuration(atof(argv[ARG_DUR])); tempSize = sine.generate(); for (int i = 0; i < sine.setSampleRate() * sine.setDuration(atof(argv[ARG_DUR])); i++) { cout << tempSize[i] << endl; } delete[] tempSize; } 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; }
int main() { // Set the global sample rate before creating class instances. Stk::setSampleRate( 44100.0 ); SineWave sine; RtAudio dac; // Figure out how many bytes in an StkFloat and setup the RtAudio stream. RtAudio::StreamParameters parameters; parameters.deviceId = dac.getDefaultOutputDevice(); parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; try { dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine ); } catch ( RtError &error ) { error.printMessage(); goto cleanup; } double f = 440.0; double twelveRoot2 = 1.0594630943592952645618252949463; sine.setFrequency(f); try { dac.startStream(); } catch ( RtError &error ) { error.printMessage(); goto cleanup; } // Block waiting here. int keyhit = 0; std::cout << "\nPlaying ... press <esc> to quit.\n"; while (keyhit != 32 && keyhit != 27) { keyhit = _getch(); if (tolower(keyhit) == 'a') { f = 220.0; sine.setFrequency(f); } else if (tolower(keyhit) == 'g') { f /= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'h') { f *= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'f') { for (int i = 0; i < 2; ++i) f /= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'j') { for (int i = 0; i < 2; ++i) f *= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'd') { for (int i = 0; i < 3; ++i) f /= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'k') { for (int i = 0; i < 3; ++i) f *= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 's') { for (int i = 0; i < 4; ++i) f /= twelveRoot2; sine.setFrequency(f); } else if (tolower(keyhit) == 'l') { for (int i = 0; i < 4; ++i) f *= twelveRoot2; sine.setFrequency(f); } else { std::cout << "Freq: " << f << std::endl; } } // Shut down the output stream. try { dac.closeStream(); } catch ( RtError &error ) { error.printMessage(); } cleanup: return 0; }