From e763432f49d5963ab4d63d383fe3ce205ca5b650 Mon Sep 17 00:00:00 2001 From: philburk Date: Thu, 21 Mar 2002 00:58:45 +0000 Subject: [PATCH] Minor tweaks while debugging pa_mac_core.c --- pa_tests/debug_multi_out.c | 1 + pa_tests/debug_sine.c | 2 +- pa_tests/paqa_errs.c | 25 +++++++++++++ pa_tests/patest_dither.c | 1 + pa_tests/patest_latency.c | 3 +- pa_tests/patest_maxsines.c | 17 +++++---- pa_tests/patest_record.c | 53 +++++++++++++++++++--------- pa_tests/patest_sine_formats.c | 13 +++---- pa_tests/patest_sine_time.c | 64 +++++++++++++++++++++++++++------- 9 files changed, 133 insertions(+), 46 deletions(-) diff --git a/pa_tests/debug_multi_out.c b/pa_tests/debug_multi_out.c index 1d211e1..d230833 100644 --- a/pa_tests/debug_multi_out.c +++ b/pa_tests/debug_multi_out.c @@ -125,6 +125,7 @@ int main(void) if( err != paNoError ) goto error; printf("Hit ENTER to stop sound.\n"); + fflush(stdout); getchar(); err = Pa_StopStream( stream ); diff --git a/pa_tests/debug_sine.c b/pa_tests/debug_sine.c index b79405d..86610d3 100644 --- a/pa_tests/debug_sine.c +++ b/pa_tests/debug_sine.c @@ -43,7 +43,7 @@ #define SLEEP_DUR (800) #define SAMPLE_RATE (44100) #define FRAMES_PER_BUFFER (256) -#if 1 +#if 0 #define MIN_LATENCY_MSEC (200) #define NUM_BUFFERS ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000)) #else diff --git a/pa_tests/paqa_errs.c b/pa_tests/paqa_errs.c index 37e75d7..e5ffc62 100644 --- a/pa_tests/paqa_errs.c +++ b/pa_tests/paqa_errs.c @@ -270,6 +270,31 @@ static int TestBadOpens( void ) QaCallback, &myData ) ) == paInvalidFlag) ); + +#if 0 /* FIXME - this is legal for some implementations. */ + HOPEFOR( ( /* Use input device as output device. */ + (result = Pa_OpenStream( + &stream, + paNoDevice, 0, paFloat32, NULL, + Pa_GetDefaultInputDeviceID(), 2, paFloat32, NULL, + SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS, + paClipOff, + QaCallback, + &myData ) + ) == paInvalidDeviceId) ); + + HOPEFOR( ( /* Use output device as input device. */ + (result = Pa_OpenStream( + &stream, + Pa_GetDefaultOutputDeviceID(), 2, paFloat32, NULL, + paNoDevice, 0, paFloat32, NULL, + SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS, + paClipOff, + QaCallback, + &myData ) + ) == paInvalidDeviceId) ); +#endif + if( stream != NULL ) Pa_CloseStream( stream ); return result; } diff --git a/pa_tests/patest_dither.c b/pa_tests/patest_dither.c index 8878161..1aa0d38 100644 --- a/pa_tests/patest_dither.c +++ b/pa_tests/patest_dither.c @@ -2,6 +2,7 @@ * $Id$ * patest_dither.c * Attempt to hear difference between dithered and non-dithered signal. + * This only has an effect if the native format is 16 bit. * * Author: Phil Burk http://www.softsynth.com * diff --git a/pa_tests/patest_latency.c b/pa_tests/patest_latency.c index 3096c8c..66820bc 100644 --- a/pa_tests/patest_latency.c +++ b/pa_tests/patest_latency.c @@ -115,7 +115,7 @@ int main(void) /* initialise sinusoidal wavetable */ for( i=0; iphases[j] = phase; } - - *out++ = (float) (output / data->numSines); + outSample = (float) (output / data->numSines); + *out++ = outSample; /* Left */ + *out++ = outSample; /* Right */ } return finished; } @@ -111,16 +113,16 @@ int main(void) if( err != paNoError ) goto error; err = Pa_OpenStream( &stream, - paNoDevice,/* default input device */ + paNoDevice, 0, /* no input */ - paFloat32, /* 32 bit floating point input */ + paFloat32, NULL, Pa_GetDefaultOutputDeviceID(), /* default output device */ - 1, /* mono output */ + 2, /* stereo output */ paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE, - FRAMES_PER_BUFFER, /* 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 */ patestCallback, @@ -136,8 +138,9 @@ int main(void) load = Pa_GetCPULoad( stream ); printf("numSines = %d, CPU load = %f\n", data.numSines, load ); + fflush( stdout ); } - while( load < 0.8 ); + while( (load < 0.8) && (data.numSines < MAX_SINES) ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; diff --git a/pa_tests/patest_record.c b/pa_tests/patest_record.c index 47ccec1..11d8791 100644 --- a/pa_tests/patest_record.c +++ b/pa_tests/patest_record.c @@ -42,24 +42,34 @@ /* #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) /**/ /* Select sample format. */ #if 0 #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; - int samplesPerFrame; SAMPLE *recordedSamples; } paTestData; @@ -73,7 +83,7 @@ static int recordCallback( void *inputBuffer, void *outputBuffer, { paTestData *data = (paTestData*)userData; SAMPLE *rptr = (SAMPLE*)inputBuffer; - SAMPLE *wptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame]; + SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; long framesToCalc; long i; int finished; @@ -96,8 +106,8 @@ static int recordCallback( void *inputBuffer, void *outputBuffer, { for( i=0; iframeIndex += framesToCalc; @@ -121,7 +131,7 @@ static int playCallback( void *inputBuffer, void *outputBuffer, PaTimestamp outTime, void *userData ) { paTestData *data = (paTestData*)userData; - SAMPLE *rptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame]; + SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; SAMPLE *wptr = (SAMPLE*)outputBuffer; unsigned int i; int finished; @@ -135,12 +145,12 @@ static int playCallback( void *inputBuffer, void *outputBuffer, for( i=0; iframeIndex += framesLeft; finished = 1; @@ -150,7 +160,7 @@ static int playCallback( void *inputBuffer, void *outputBuffer, for( i=0; iframeIndex += framesPerBuffer; finished = 0; @@ -174,8 +184,7 @@ int main(void) data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ data.frameIndex = 0; - data.samplesPerFrame = 2; - numSamples = totalFrames * data.samplesPerFrame; + numSamples = totalFrames * NUM_CHANNELS; numBytes = numSamples * sizeof(SAMPLE); data.recordedSamples = (SAMPLE *) malloc( numBytes ); @@ -193,7 +202,7 @@ int main(void) err = Pa_OpenStream( &stream, Pa_GetDefaultInputDeviceID(), - data.samplesPerFrame, /* stereo input */ + NUM_CHANNELS, /* stereo input */ PA_SAMPLE_TYPE, NULL, paNoDevice, @@ -203,7 +212,7 @@ int main(void) SAMPLE_RATE, 1024, /* 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 */ + 0, //paDitherOff, /* flags */ recordCallback, &data ); if( err != paNoError ) goto error; @@ -234,10 +243,20 @@ int main(void) } average += val; } - printf("sample max amplitude = %d\n", max ); + average = average / numSamples; - printf("sample average = %d\n", average ); + if( PA_SAMPLE_TYPE == paFloat32 ) + { + printf("sample max amplitude = %f\n", max ); + printf("sample average = %f\n", average ); + } + else + { + printf("sample max amplitude = %d\n", max ); + printf("sample average = %d\n", average ); + } + /* Write recorded data to a file. */ #if 0 { @@ -249,7 +268,7 @@ int main(void) } else { - fwrite( data.recordedSamples, data.samplesPerFrame * sizeof(SAMPLE), totalFrames, fid ); + fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid ); fclose( fid ); printf("Wrote data to 'recorded.raw'\n"); } @@ -266,7 +285,7 @@ int main(void) PA_SAMPLE_TYPE, NULL, Pa_GetDefaultOutputDeviceID(), - data.samplesPerFrame, /* stereo output */ + NUM_CHANNELS, /* stereo output */ PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, diff --git a/pa_tests/patest_sine_formats.c b/pa_tests/patest_sine_formats.c index 09abb7b..ea92603 100644 --- a/pa_tests/patest_sine_formats.c +++ b/pa_tests/patest_sine_formats.c @@ -40,7 +40,7 @@ #define NUM_SECONDS (20) #define SAMPLE_RATE (44100) -#define FRAMES_PER_BUFFER (256) +#define FRAMES_PER_BUFFER (512) #define LEFT_FREQ (SAMPLE_RATE/256.0) /* So we hit 1.0 */ #define RIGHT_FREQ (500.0) #define AMPLITUDE (1.0) @@ -57,25 +57,22 @@ typedef unsigned char SAMPLE_t; #define SAMPLE_ZERO (0x80) #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x))) #define FORMAT_NAME "Unsigned 8 Bit" -#endif -#if TEST_INT8 +#elif TEST_INT8 #define TEST_FORMAT paInt8 typedef char SAMPLE_t; #define SAMPLE_ZERO (0) #define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x))) #define FORMAT_NAME "Signed 8 Bit" -#endif -#if TEST_INT16 +#elif TEST_INT16 #define TEST_FORMAT paInt16 typedef short SAMPLE_t; #define SAMPLE_ZERO (0) -#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767.0 * (x))) +#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x))) #define FORMAT_NAME "Signed 16 Bit" -#endif -#if TEST_FLOAT32 +#elif TEST_FLOAT32 #define TEST_FORMAT paFloat32 typedef float SAMPLE_t; #define SAMPLE_ZERO (0.0) diff --git a/pa_tests/patest_sine_time.c b/pa_tests/patest_sine_time.c index 9b9cf3b..9c3f0af 100644 --- a/pa_tests/patest_sine_time.c +++ b/pa_tests/patest_sine_time.c @@ -43,16 +43,18 @@ #define NUM_SECONDS (8) #define SAMPLE_RATE (44100) #define FRAMES_PER_BUFFER (64) +#define NUM_BUFFERS (0) #ifndef M_PI #define M_PI (3.14159265) #endif #define TABLE_SIZE (200) typedef struct { - float sine[TABLE_SIZE]; - int left_phase; - int right_phase; - int framesToGo; + float sine[TABLE_SIZE]; + int left_phase; + int right_phase; + int framesToGo; + volatile PaTimestamp outTime; } paTestData; /* This routine will be called by the PortAudio engine when audio is needed. @@ -71,6 +73,8 @@ static int patestCallback( void *inputBuffer, void *outputBuffer, (void) outTime; /* Prevent unused variable warnings. */ (void) inputBuffer; + data->outTime = outTime; + if( data->framesToGo < framesPerBuffer ) { framesToCalc = data->framesToGo; @@ -100,23 +104,44 @@ static int patestCallback( void *inputBuffer, void *outputBuffer, } return finished; } +/*******************************************************************/ +static void ReportStreamTime( PortAudioStream *stream, paTestData *data ); +static void ReportStreamTime( PortAudioStream *stream, paTestData *data ) +{ + PaTimestamp streamTime, latency, outTime; + + streamTime = Pa_StreamTime( stream ); + outTime = data->outTime; + if( outTime < 0.0 ) + { + printf("Stream time = %8.1f\n", streamTime ); + } + else + { + latency = outTime - streamTime; + printf("Stream time = %8.1f, outTime = %8.1f, latency = %8.1f\n", + streamTime, outTime, latency ); + } + fflush(stdout); +} + /*******************************************************************/ int main(void); int main(void) { PortAudioStream *stream; PaError err; - paTestData data; + paTestData DATA; int i; int totalSamps; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); /* initialise sinusoidal wavetable */ for( i=0; i