// Assignment 1 Part 3 // William Chien // This program will generate repetition of beep tone. // Each repetition, the tone's amplitude is increased. // User will hear that the program is beeping louder and louder. // Basically, this program is mainly using C++ language. // C++ Modified version is provided by Eric Johnson #include #include #include #include "portaudio.h" //constants for producing sine wave tone #define NUM_SECONDS 3 #define SAMPLE_RATE 44100 #define BUFFER_SIZE 256 #define F 6.283185307179586 //F = 2pi #define FREQUENCY 1000 struct paSoundData { float amplitude; float frequency; float phase; unsigned long samples; }; //****************************************************************************** // 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; //Produce sinewave tone //Same ideas from Professor Dobrian's Pa_cosine example float A = data->amplitude; float phase = data->phase; float y; //Temporary output constant float G = F*data->frequency/SAMPLE_RATE; for( i=0; isamples++)+phase); //Sine wave equation *out++ = y; // left channel *out++ = y; // right channel } 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) { float n=1; int times; cout << "How many beeps do you want to genearate?.\n"; cout << "Each beep will become louder and louder.\n"; cin >> times; //Loops that increases the amplitude. for (n=1; n<=times ; n++) { PortAudioStream *stream; // Initialize our data for use by callback. data.amplitude = 0.5*n; data.frequency = FREQUENCY; data.phase = 0.; // Initialize library before making any other calls. PaError err = Pa_Initialize(); if( err != paNoError ) return error(err); // 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); err = Pa_StopStream( stream ); if( err != paNoError ) return error(err); err = Pa_CloseStream( stream ); if( err != paNoError ) return error(err); } Pa_Terminate(); cout << "Finished.\n"; return 0; }