// Assignment 1 // William Chien /* This is a program that is trying to play sound several times. Each time, the program will increase the sleeping time, so the sound will become longer and longer. */ // Basically, this program is mainly using C++ language. // C++ Modified version is provided by Eric Johnson #include #include #include #include "portaudio.h" const int NUM_SECONDS = 3; struct paSoundData { float left_phase; float right_phase; }; //****************************************************************************** // Callback Function (From PortAudio Website). // This routine is for Callback Funciton. static int paSoundCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData ) { // Cast data passed through stream to our structure. paSoundData *data = (paSoundData*)userData; float *out = (float*)outputBuffer; unsigned int i; (void) outTime; // Prevent unused variable warnings. (void) inputBuffer; for( i=0; ileft_phase; /* left */ *out++ = data->right_phase; /* right */ // Generate simple sawtooth phaser that ranges between -1.0 and 1.0. data->left_phase += 0.01f; // When signal reaches top, drop back down. if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f; // higher pitch so we can distinguish left and right. data->right_phase += 0.02f; if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f; } return 0; } //**************************************************************************** // Error handling function // replaces the goto label used in some of the examples int error (const int err) { Pa_Terminate(); cerr << "An error occured while using the portaudio stream\n"; cerr << "Error number: " << err << "\n"; cerr << "Error message: " << Pa_GetErrorText( err ) << endl; return err; } //******************************************************************* static paSoundData data; int main(void) { int n=1; int times; cout << "This program will repeat a sound.\n"; cout << "Each repetition, the sound will be played with longer sleep time.\n"; cout << "How many times you want to play?\n"; cin >> times; PortAudioStream *stream; // Initialize our data for use by callback. data.left_phase = data.right_phase = 0.0; // Initialize library before making any other calls. PaError err = Pa_Initialize(); if( err != paNoError ) return error(err); //loop for repetiting the tone //Each time, it will increase the sleeping time. for (n=1; n<=times ; n++) { // Open an audio I/O stream. err = Pa_OpenDefaultStream( &stream, 0, // no input channels 2, // stereo output paFloat32, // 32 bit floating point output 44100, // Sampling Rate 256, // frames per buffer 0, // number of buffers, if zero then use default minimum paSoundCallback, // specific callback &data ); // Pass data through Call back if( err != paNoError ) return error(err); err = Pa_StartStream( stream ); if( err != paNoError ) return error(err); // Sleep for several seconds. Pa_Sleep(NUM_SECONDS*100*n); err = Pa_StopStream( stream ); if( err != paNoError ) return error(err); err = Pa_CloseStream( stream ); if( err != paNoError ) return error(err); //Instead of just reopening the stream again, //add another sleep function to create silence between each tone. Pa_Sleep(NUM_SECONDS*100); } Pa_Terminate(); cout << "Finished.\n"; return err; }