#include #include #include #pragma comment(lib, "winmm.lib") #pragma comment(lib, "dsound.lib") #include #include /*#include #include */ #include //#include //#include #include /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ #define SAMPLE_RATE (44100) #define NUM_SECONDS (5) #define NUM_CHANNELS (2) /* #define DITHER_FLAG (paDitherOff) */ #define DITHER_FLAG (0) /**/ #define FRAMES_PER_BUFFER (1024) /* Select sample format. */ #if 1 #define PA_SAMPLE_TYPE paFloat32 typedef float SAMPLE; #define SAMPLE_SILENCE (0.0f) #elif 0 #define PA_SAMPLE_TYPE paInt16 typedef short SAMPLE; #define SAMPLE_SILENCE (0) #elif 0 #define PA_SAMPLE_TYPE paInt8 typedef char SAMPLE; #define SAMPLE_SILENCE (0) #else #define PA_SAMPLE_TYPE paUInt8 typedef unsigned char SAMPLE; #define SAMPLE_SILENCE (128) #endif typedef struct { int frameIndex; /* Index into sample array. */ int maxFrameIndex; SAMPLE *recordedSamples; } paTestData; /* This routine will be called by the PortAudio engine when audio is needed. ** It may be called at interrupt level on some machines so don't do anything ** that could mess up the system like calling malloc() or free(). */ static int recordCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData ) { paTestData *data = (paTestData*)userData; SAMPLE *rptr = (SAMPLE*)inputBuffer; SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; long framesToRecord; long i; int finished; unsigned long framesLeft = data->maxFrameIndex - data->frameIndex; int samplesToRecord; (void) outputBuffer; /* Prevent unused variable warnings. */ (void) outTime; if( framesLeft < framesPerBuffer ) { framesToRecord = framesLeft; finished = 1; } else { framesToRecord = framesPerBuffer; finished = 0; } samplesToRecord = framesToRecord * NUM_CHANNELS; if( inputBuffer == NULL ) { for( i=0; iframeIndex += framesToRecord; return finished; } /* This routine will be called by the PortAudio engine when audio is needed. ** It may be called at interrupt level on some machines so don't do anything ** that could mess up the system like calling malloc() or free(). */ static int playCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData ) { paTestData *data = (paTestData*)userData; SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; SAMPLE *wptr = (SAMPLE*)outputBuffer; unsigned int i; int finished; unsigned int framesLeft = data->maxFrameIndex - data->frameIndex; (void) inputBuffer; /* Prevent unused variable warnings. */ (void) outTime; int framesToPlay, samplesToPlay, samplesPerBuffer; if( framesLeft < framesPerBuffer ) { framesToPlay = framesLeft; finished = 1; } else { framesToPlay = framesPerBuffer; finished = 0; } samplesToPlay = framesToPlay * NUM_CHANNELS; samplesPerBuffer = framesPerBuffer * NUM_CHANNELS; for( i=0; iframeIndex += framesToPlay; return finished; } /*******************************************************************/ int main(void); int main(void) { //Biquad foo(fmodeBP, 1000, .03, 44100); CFxRbjFilter foo; CFxRbjFilter foo2; foo.calc_filter_coeffs(3, 1000, 44100, 30, 1, 0); foo2.calc_filter_coeffs(3, 1000, 44100, 30, 1, 0); PortAudioStream *stream; PortAudioStream *stream2; PaError err; PaError err2; paTestData data; paTestData data2; int i; int i2; int totalFrames; int totalFrames2; int numSamples; int numSamples2; int numBytes; int numBytes2; SAMPLE max, average, val; SAMPLE max2, average2, val2; printf("patest_record.c\n"); fflush(stdout); data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ data2.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; data.frameIndex = 0; data2.frameIndex = 0; numSamples = totalFrames * NUM_CHANNELS; numSamples2 = totalFrames2 * NUM_CHANNELS; numBytes = numSamples * sizeof(SAMPLE); numBytes2 = numSamples2 * sizeof(SAMPLE); data.recordedSamples = (SAMPLE *) malloc( numBytes ); data2.recordedSamples = (SAMPLE *) malloc( numBytes2 ); if( data.recordedSamples == NULL ) { printf("Could not allocate record array for 1.\n"); exit(1); } if( data2.recordedSamples == NULL ) { printf("Could not allocate record array for 2.\n"); exit(1); } for( i=0; iname ); printf("Max Inputs = %d", pdi->maxInputChannels ); printf(", Max Outputs = %d\n", pdi->maxOutputChannels ); if( pdi->numSampleRates == -1 ) { printf("Sample Rate Range = %f to %f\n", pdi->sampleRates[0], pdi->sampleRates[1] ); } else { printf("Sample Rates ="); int j; for( j=0; jnumSampleRates; j++ ) { printf(" %8.2f,", pdi->sampleRates[j] ); } printf("\n"); } printf("Native Sample Formats = "); if( pdi->nativeSampleFormats & paInt8 ) printf("paInt8, "); if( pdi->nativeSampleFormats & paUInt8 ) printf("paUInt8, "); if( pdi->nativeSampleFormats & paInt16 ) printf("paInt16, "); if( pdi->nativeSampleFormats & paInt32 ) printf("paInt32, "); if( pdi->nativeSampleFormats & paFloat32 ) printf("paFloat32, "); if( pdi->nativeSampleFormats & paInt24 ) printf("paInt24, "); if( pdi->nativeSampleFormats & paPackedInt24 ) printf("paPackedInt24, "); printf("\n"); while( Pa_StreamActive( stream ) ) { Pa_Sleep(1000); printf("index = %d\n", data.frameIndex ); fflush(stdout); printf("index = %d\n", data2.frameIndex ); fflush(stdout); } err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; err2 = Pa_CloseStream( stream2 ); if( err2 != paNoError ) goto error; /* Measure maximum peak amplitude. */ max = 0; average = 0; printf("Processing through bandpass...\n"); fflush(stdout); float myvar; float myvar2; for( i=0; i max ) { max = val; } average += val; //printf ("floats: %f\n", val);fflush(stdout); myvar=foo.filter(data.recordedSamples[i],.999,2000,44100);/* &data.recordedSamples[i]); */ //myvar=foo.filtera(myvar); //printf ("floats: %f\n", myvar);fflush(stdout); data.recordedSamples[i]=myvar; myvar2=foo2.filter(data2.recordedSamples[i],.999,2000,44100); data2.recordedSamples[i]=myvar2; } average = average / numSamples; if( PA_SAMPLE_TYPE == paFloat32 ) /* This should be done at compile-time with "#if" ?? */ { /* MIPS-compiler warns at the int-version below. */ printf("sample max amplitude = %f\n", max ); printf("sample average = %f\n", average ); } else { printf("sample max amplitude = %d\n", max ); /* <-- This IS compiled anyhow. */ printf("sample average = %d\n", average ); } /* Write recorded data to a file. */ #if 0 { FILE *fid; fid = fopen("recorded.raw", "wb"); if( fid == NULL ) { printf("Could not open file."); } else { fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid ); fclose( fid ); printf("Wrote data to 'recorded.raw'\n"); } } #endif /* Playback recorded data. -------------------------------------------- */ data.frameIndex = 0; data2.frameIndex = 0; printf("Begin playback of device 1.\n"); fflush(stdout); err = Pa_OpenStream( &stream, paNoDevice, 0, /* NO input */ PA_SAMPLE_TYPE, NULL, Pa_GetDefaultOutputDeviceID(), NUM_CHANNELS, PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ playCallback, &data ); if( err != paNoError ) goto error; if( stream ) { err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Waiting for playback to finish.\n"); fflush(stdout); while( Pa_StreamActive( stream ) ) Pa_Sleep(100); err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; printf("Done.\n"); fflush(stdout); } free( data.recordedSamples ); err = Pa_OpenStream( &stream2, paNoDevice, 0, /* NO input */ PA_SAMPLE_TYPE, NULL, Pa_GetDefaultOutputDeviceID(), NUM_CHANNELS, PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ playCallback, &data2 ); if( err != paNoError ) goto error; if( stream2 ) { err = Pa_StartStream( stream2 ); if( err != paNoError ) goto error; printf("Waiting for playback to finish.\n"); fflush(stdout); while( Pa_StreamActive( stream2 ) ) Pa_Sleep(100); err = Pa_CloseStream( stream2 ); if( err != paNoError ) goto error; printf("Done.\n"); fflush(stdout); } free( data2.recordedSamples ); Pa_Terminate(); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return -1; }