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