Chris@4: /** @file paqa_errs.c Chris@4: @ingroup qa_src Chris@4: @brief Self Testing Quality Assurance app for PortAudio Chris@4: Do lots of bad things to test error reporting. Chris@4: @author Phil Burk http://www.softsynth.com Chris@4: Pieter Suurmond adapted to V19 API. Chris@4: */ Chris@4: /* Chris@4: * $Id: paqa_errs.c 1756 2011-09-08 06:09:29Z philburk $ Chris@4: * Chris@4: * This program uses the PortAudio Portable Audio Library. Chris@4: * For more information see: http://www.portaudio.com Chris@4: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk Chris@4: * Chris@4: * Permission is hereby granted, free of charge, to any person obtaining Chris@4: * a copy of this software and associated documentation files Chris@4: * (the "Software"), to deal in the Software without restriction, Chris@4: * including without limitation the rights to use, copy, modify, merge, Chris@4: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@4: * and to permit persons to whom the Software is furnished to do so, Chris@4: * subject to the following conditions: Chris@4: * Chris@4: * The above copyright notice and this permission notice shall be Chris@4: * included in all copies or substantial portions of the Software. Chris@4: * Chris@4: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@4: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@4: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@4: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@4: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@4: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@4: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@4: */ Chris@4: Chris@4: /* Chris@4: * The text above constitutes the entire PortAudio license; however, Chris@4: * the PortAudio community also makes the following non-binding requests: Chris@4: * Chris@4: * Any person wishing to distribute modifications to the Software is Chris@4: * requested to send the modifications to the original developer so that Chris@4: * they can be incorporated into the canonical version. It is also Chris@4: * requested that these non-binding requests be included along with the Chris@4: * license above. Chris@4: */ Chris@4: Chris@4: #include Chris@4: #include Chris@4: Chris@4: #include "portaudio.h" Chris@4: Chris@4: /*--------- Definitions ---------*/ Chris@4: #define MODE_INPUT (0) Chris@4: #define MODE_OUTPUT (1) Chris@4: #define FRAMES_PER_BUFFER (64) Chris@4: #define SAMPLE_RATE (44100.0) Chris@4: Chris@4: typedef struct PaQaData Chris@4: { Chris@4: unsigned long framesLeft; Chris@4: int numChannels; Chris@4: int bytesPerSample; Chris@4: int mode; Chris@4: } Chris@4: PaQaData; Chris@4: Chris@4: static int gNumPassed = 0; /* Two globals */ Chris@4: static int gNumFailed = 0; Chris@4: Chris@4: /*------------------- Macros ------------------------------*/ Chris@4: /* Print ERROR if it fails. Tally success or failure. Odd */ Chris@4: /* do-while wrapper seems to be needed for some compilers. */ Chris@4: Chris@4: #define EXPECT(_exp) \ Chris@4: do \ Chris@4: { \ Chris@4: if ((_exp)) {\ Chris@4: gNumPassed++; \ Chris@4: } \ Chris@4: else { \ Chris@4: printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \ Chris@4: gNumFailed++; \ Chris@4: goto error; \ Chris@4: } \ Chris@4: } while(0) Chris@4: Chris@4: #define HOPEFOR(_exp) \ Chris@4: do \ Chris@4: { \ Chris@4: if ((_exp)) {\ Chris@4: gNumPassed++; \ Chris@4: } \ Chris@4: else { \ Chris@4: printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \ Chris@4: gNumFailed++; \ Chris@4: } \ Chris@4: } while(0) Chris@4: Chris@4: /*-------------------------------------------------------------------------*/ Chris@4: /* This routine will be called by the PortAudio engine when audio is needed. Chris@4: It may be called at interrupt level on some machines so don't do anything Chris@4: that could mess up the system like calling malloc() or free(). Chris@4: */ Chris@4: static int QaCallback( const void* inputBuffer, Chris@4: void* outputBuffer, Chris@4: unsigned long framesPerBuffer, Chris@4: const PaStreamCallbackTimeInfo* timeInfo, Chris@4: PaStreamCallbackFlags statusFlags, Chris@4: void* userData ) Chris@4: { Chris@4: unsigned long i; Chris@4: unsigned char* out = (unsigned char *) outputBuffer; Chris@4: PaQaData* data = (PaQaData *) userData; Chris@4: Chris@4: (void)inputBuffer; /* Prevent "unused variable" warnings. */ Chris@4: Chris@4: /* Zero out buffer so we don't hear terrible noise. */ Chris@4: if( data->mode == MODE_OUTPUT ) Chris@4: { Chris@4: unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample; Chris@4: for( i=0; iframesLeft > framesPerBuffer ) Chris@4: { Chris@4: data->framesLeft -= framesPerBuffer; Chris@4: return 0; Chris@4: } Chris@4: else Chris@4: { Chris@4: data->framesLeft = 0; Chris@4: return 1; Chris@4: } Chris@4: } Chris@4: Chris@4: static PaDeviceIndex FindInputOnlyDevice(void) Chris@4: { Chris@4: PaDeviceIndex result = Pa_GetDefaultInputDevice(); Chris@4: if( result != paNoDevice && Pa_GetDeviceInfo(result)->maxOutputChannels == 0 ) Chris@4: return result; Chris@4: Chris@4: for( result = 0; result < Pa_GetDeviceCount(); ++result ) Chris@4: { Chris@4: if( Pa_GetDeviceInfo(result)->maxOutputChannels == 0 ) Chris@4: return result; Chris@4: } Chris@4: Chris@4: return paNoDevice; Chris@4: } Chris@4: Chris@4: static PaDeviceIndex FindOutputOnlyDevice(void) Chris@4: { Chris@4: PaDeviceIndex result = Pa_GetDefaultOutputDevice(); Chris@4: if( result != paNoDevice && Pa_GetDeviceInfo(result)->maxInputChannels == 0 ) Chris@4: return result; Chris@4: Chris@4: for( result = 0; result < Pa_GetDeviceCount(); ++result ) Chris@4: { Chris@4: if( Pa_GetDeviceInfo(result)->maxInputChannels == 0 ) Chris@4: return result; Chris@4: } Chris@4: Chris@4: return paNoDevice; Chris@4: } Chris@4: Chris@4: /*-------------------------------------------------------------------------------------------------*/ Chris@4: static int TestBadOpens( void ) Chris@4: { Chris@4: PaStream* stream = NULL; Chris@4: PaError result; Chris@4: PaQaData myData; Chris@4: PaStreamParameters ipp, opp; Chris@4: const PaDeviceInfo* info = NULL; Chris@4: Chris@4: Chris@4: /* Setup data for synthesis thread. */ Chris@4: myData.framesLeft = (unsigned long) (SAMPLE_RATE * 100); /* 100 seconds */ Chris@4: myData.numChannels = 1; Chris@4: myData.mode = MODE_OUTPUT; Chris@4: Chris@4: /*----------------------------- No devices specified: */ Chris@4: ipp.device = opp.device = paNoDevice; Chris@4: ipp.channelCount = opp.channelCount = 0; /* Also no channels. */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: /* Take the low latency of the default device for all subsequent tests. */ Chris@4: info = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice()); Chris@4: ipp.suggestedLatency = info ? info->defaultLowInputLatency : 0.100; Chris@4: info = Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice()); Chris@4: opp.suggestedLatency = info ? info->defaultLowOutputLatency : 0.100; Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: /*----------------------------- No devices specified #2: */ Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, NULL, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: /*----------------------------- Out of range input device specified: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = Pa_GetDeviceCount(); /* And no output device, and no channels. */ Chris@4: opp.channelCount = 0; opp.device = paNoDevice; Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: /*----------------------------- Out of range output device specified: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no channels. */ Chris@4: opp.channelCount = 0; opp.device = Pa_GetDeviceCount(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: if (Pa_GetDefaultInputDevice() != paNoDevice) { Chris@4: /*----------------------------- Zero input channels: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = Pa_GetDefaultInputDevice(); Chris@4: opp.channelCount = 0; opp.device = paNoDevice; /* And no output device, and no output channels. */ Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidChannelCount)); Chris@4: } Chris@4: Chris@4: if (Pa_GetDefaultOutputDevice() != paNoDevice) { Chris@4: /*----------------------------- Zero output channels: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no input channels. */ Chris@4: opp.channelCount = 0; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidChannelCount)); Chris@4: } Chris@4: /*----------------------------- Nonzero input and output channels but no output device: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 2; ipp.device = Pa_GetDefaultInputDevice(); /* Both stereo. */ Chris@4: opp.channelCount = 2; opp.device = paNoDevice; Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: /*----------------------------- Nonzero input and output channels but no input device: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 2; ipp.device = paNoDevice; Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidDevice)); Chris@4: Chris@4: if (Pa_GetDefaultOutputDevice() != paNoDevice) { Chris@4: /*----------------------------- NULL stream pointer: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; /* Output is more likely than input. */ Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); /* Only 2 output channels. */ Chris@4: HOPEFOR(((result = Pa_OpenStream(NULL, &ipp, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paBadStreamPtr)); Chris@4: Chris@4: /*----------------------------- Low sample rate: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: 1.0, FRAMES_PER_BUFFER, /* 1 cycle per second (1 Hz) is too low. */ Chris@4: paClipOff, QaCallback, &myData )) == paInvalidSampleRate)); Chris@4: Chris@4: /*----------------------------- High sample rate: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: 10000000.0, FRAMES_PER_BUFFER, /* 10^6 cycles per second (10 MHz) is too high. */ Chris@4: paClipOff, QaCallback, &myData )) == paInvalidSampleRate)); Chris@4: Chris@4: /*----------------------------- NULL callback: */ Chris@4: /* NULL callback is valid in V19 -- it means use blocking read/write stream Chris@4: Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, Chris@4: NULL, Chris@4: &myData )) == paNullCallback)); Chris@4: */ Chris@4: Chris@4: /*----------------------------- Bad flag: */ Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; Chris@4: opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: 255, /* Is 8 maybe legal V19 API? */ Chris@4: QaCallback, &myData )) == paInvalidFlag)); Chris@4: } Chris@4: Chris@4: /*----------------------------- using input device as output device: */ Chris@4: if( FindInputOnlyDevice() != paNoDevice ) Chris@4: { Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no channels. */ Chris@4: opp.channelCount = 2; opp.device = FindInputOnlyDevice(); Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidChannelCount)); Chris@4: } Chris@4: Chris@4: /*----------------------------- using output device as input device: */ Chris@4: if( FindOutputOnlyDevice() != paNoDevice ) Chris@4: { Chris@4: ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL; Chris@4: ipp.sampleFormat = opp.sampleFormat = paFloat32; Chris@4: ipp.channelCount = 2; ipp.device = FindOutputOnlyDevice(); Chris@4: opp.channelCount = 0; opp.device = paNoDevice; /* And no output device, and no channels. */ Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL, Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paInvalidChannelCount)); Chris@4: Chris@4: } Chris@4: Chris@4: if( stream != NULL ) Pa_CloseStream( stream ); Chris@4: return result; Chris@4: } Chris@4: Chris@4: /*-----------------------------------------------------------------------------------------*/ Chris@4: static int TestBadActions( void ) Chris@4: { Chris@4: PaStream* stream = NULL; Chris@4: const PaDeviceInfo* deviceInfo = NULL; Chris@4: PaError result = 0; Chris@4: PaQaData myData; Chris@4: PaStreamParameters opp; Chris@4: const PaDeviceInfo* info = NULL; Chris@4: Chris@4: /* Setup data for synthesis thread. */ Chris@4: myData.framesLeft = (unsigned long)(SAMPLE_RATE * 100); /* 100 seconds */ Chris@4: myData.numChannels = 1; Chris@4: myData.mode = MODE_OUTPUT; Chris@4: Chris@4: opp.device = Pa_GetDefaultOutputDevice(); /* Default output. */ Chris@4: opp.channelCount = 2; /* Stereo output. */ Chris@4: opp.hostApiSpecificStreamInfo = NULL; Chris@4: opp.sampleFormat = paFloat32; Chris@4: info = Pa_GetDeviceInfo(opp.device); Chris@4: opp.suggestedLatency = info ? info->defaultLowOutputLatency : 0.100; Chris@4: Chris@4: if (opp.device != paNoDevice) { Chris@4: HOPEFOR(((result = Pa_OpenStream(&stream, NULL, /* Take NULL as input parame- */ Chris@4: &opp, /* ters, meaning try only output. */ Chris@4: SAMPLE_RATE, FRAMES_PER_BUFFER, Chris@4: paClipOff, QaCallback, &myData )) == paNoError)); Chris@4: } Chris@4: Chris@4: HOPEFOR(((deviceInfo = Pa_GetDeviceInfo(paNoDevice)) == NULL)); Chris@4: HOPEFOR(((deviceInfo = Pa_GetDeviceInfo(87654)) == NULL)); Chris@4: HOPEFOR(((result = Pa_StartStream(NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_StopStream(NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_IsStreamStopped(NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_IsStreamActive(NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_CloseStream(NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_SetStreamFinishedCallback(NULL, NULL)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = !Pa_GetStreamInfo(NULL)))); Chris@4: HOPEFOR(((result = Pa_GetStreamTime(NULL)) == 0.0)); Chris@4: HOPEFOR(((result = Pa_GetStreamCpuLoad(NULL)) == 0.0)); Chris@4: HOPEFOR(((result = Pa_ReadStream(NULL, NULL, 0)) == paBadStreamPtr)); Chris@4: HOPEFOR(((result = Pa_WriteStream(NULL, NULL, 0)) == paBadStreamPtr)); Chris@4: Chris@4: /** @todo test Pa_GetStreamReadAvailable and Pa_GetStreamWriteAvailable */ Chris@4: Chris@4: if (stream != NULL) Pa_CloseStream(stream); Chris@4: return result; Chris@4: } Chris@4: Chris@4: /*---------------------------------------------------------------------*/ Chris@4: int main(void); Chris@4: int main(void) Chris@4: { Chris@4: PaError result; Chris@4: Chris@4: EXPECT(((result = Pa_Initialize()) == paNoError)); Chris@4: TestBadOpens(); Chris@4: TestBadActions(); Chris@4: error: Chris@4: Pa_Terminate(); Chris@4: printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed); Chris@4: return 0; Chris@4: }