cannam@162: /** @file paqa_devs.c cannam@162: @ingroup qa_src cannam@162: @brief Self Testing Quality Assurance app for PortAudio cannam@162: Try to open each device and run through all the cannam@162: possible configurations. This test does not verify cannam@162: that the configuration works well. It just verifies cannam@162: that it does not crash. It requires a human to listen to cannam@162: the outputs. cannam@162: cannam@162: @author Phil Burk http://www.softsynth.com cannam@162: cannam@162: Pieter adapted to V19 API. Test now relies heavily on cannam@162: Pa_IsFormatSupported(). Uses same 'standard' sample rates cannam@162: as in test pa_devs.c. cannam@162: */ cannam@162: /* cannam@162: * $Id$ cannam@162: * cannam@162: * This program uses the PortAudio Portable Audio Library. cannam@162: * For more information see: http://www.portaudio.com cannam@162: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk cannam@162: * cannam@162: * Permission is hereby granted, free of charge, to any person obtaining cannam@162: * a copy of this software and associated documentation files cannam@162: * (the "Software"), to deal in the Software without restriction, cannam@162: * including without limitation the rights to use, copy, modify, merge, cannam@162: * publish, distribute, sublicense, and/or sell copies of the Software, cannam@162: * and to permit persons to whom the Software is furnished to do so, cannam@162: * subject to the following conditions: cannam@162: * cannam@162: * The above copyright notice and this permission notice shall be cannam@162: * included in all copies or substantial portions of the Software. cannam@162: * cannam@162: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@162: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@162: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. cannam@162: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR cannam@162: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@162: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@162: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@162: */ cannam@162: cannam@162: /* cannam@162: * The text above constitutes the entire PortAudio license; however, cannam@162: * the PortAudio community also makes the following non-binding requests: cannam@162: * cannam@162: * Any person wishing to distribute modifications to the Software is cannam@162: * requested to send the modifications to the original developer so that cannam@162: * they can be incorporated into the canonical version. It is also cannam@162: * requested that these non-binding requests be included along with the cannam@162: * license above. cannam@162: */ cannam@162: cannam@162: #include cannam@162: #include cannam@162: #include "portaudio.h" cannam@162: #include "pa_trace.h" cannam@162: cannam@162: /****************************************** Definitions ***********/ cannam@162: #define MODE_INPUT (0) cannam@162: #define MODE_OUTPUT (1) cannam@162: #define MAX_TEST_CHANNELS 4 cannam@162: cannam@162: typedef struct PaQaData cannam@162: { cannam@162: unsigned long framesLeft; cannam@162: int numChannels; cannam@162: int bytesPerSample; cannam@162: int mode; cannam@162: short sawPhase; cannam@162: PaSampleFormat format; cannam@162: } cannam@162: PaQaData; cannam@162: cannam@162: /****************************************** Prototypes ***********/ cannam@162: static void TestDevices( int mode ); cannam@162: static void TestFormats( int mode, PaDeviceIndex deviceID, double sampleRate, cannam@162: int numChannels ); cannam@162: static int TestAdvance( int mode, PaDeviceIndex deviceID, double sampleRate, cannam@162: int numChannels, PaSampleFormat format ); cannam@162: static int QaCallback( const void *inputBuffer, void *outputBuffer, cannam@162: unsigned long framesPerBuffer, cannam@162: const PaStreamCallbackTimeInfo* timeInfo, cannam@162: PaStreamCallbackFlags statusFlags, cannam@162: void *userData ); cannam@162: cannam@162: /****************************************** Globals ***********/ cannam@162: static int gNumPassed = 0; cannam@162: static int gNumFailed = 0; cannam@162: cannam@162: /****************************************** Macros ***********/ cannam@162: /* Print ERROR if it fails. Tally success or failure. */ cannam@162: /* Odd do-while wrapper seems to be needed for some compilers. */ cannam@162: #define EXPECT(_exp) \ cannam@162: do \ cannam@162: { \ cannam@162: if ((_exp)) {\ cannam@162: /* printf("SUCCESS for %s\n", #_exp ); */ \ cannam@162: gNumPassed++; \ cannam@162: } \ cannam@162: else { \ cannam@162: printf("ERROR - 0x%x - %s for %s\n", result, \ cannam@162: ((result == 0) ? "-" : Pa_GetErrorText(result)), \ cannam@162: #_exp ); \ cannam@162: gNumFailed++; \ cannam@162: goto error; \ cannam@162: } \ cannam@162: } while(0) cannam@162: cannam@162: /*******************************************************************/ cannam@162: /* This routine will be called by the PortAudio engine when audio is needed. cannam@162: ** It may be called at interrupt level on some machines so don't do anything cannam@162: ** that could mess up the system like calling malloc() or free(). cannam@162: */ cannam@162: static int QaCallback( const void *inputBuffer, void *outputBuffer, cannam@162: unsigned long framesPerBuffer, cannam@162: const PaStreamCallbackTimeInfo* timeInfo, cannam@162: PaStreamCallbackFlags statusFlags, cannam@162: void *userData ) cannam@162: { cannam@162: unsigned long i; cannam@162: short phase; cannam@162: PaQaData *data = (PaQaData *) userData; cannam@162: (void) inputBuffer; cannam@162: cannam@162: /* Play simple sawtooth wave. */ cannam@162: if( data->mode == MODE_OUTPUT ) cannam@162: { cannam@162: phase = data->sawPhase; cannam@162: switch( data->format ) cannam@162: { cannam@162: case paFloat32: cannam@162: { cannam@162: float *out = (float *) outputBuffer; cannam@162: for( i=0; inumChannels == 2 ) cannam@162: { cannam@162: *out++ = (float) (phase * (1.0 / 32768.0)); cannam@162: } cannam@162: } cannam@162: } cannam@162: break; cannam@162: cannam@162: case paInt32: cannam@162: { cannam@162: int *out = (int *) outputBuffer; cannam@162: for( i=0; inumChannels == 2 ) cannam@162: { cannam@162: *out++ = ((int) phase ) << 16; cannam@162: } cannam@162: } cannam@162: } cannam@162: break; cannam@162: cannam@162: case paInt16: cannam@162: { cannam@162: short *out = (short *) outputBuffer; cannam@162: for( i=0; inumChannels == 2 ) cannam@162: { cannam@162: *out++ = phase; cannam@162: } cannam@162: } cannam@162: } cannam@162: break; cannam@162: cannam@162: default: cannam@162: { cannam@162: unsigned char *out = (unsigned char *) outputBuffer; cannam@162: unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample; cannam@162: for( i=0; isawPhase = phase; cannam@162: } cannam@162: /* Are we through yet? */ cannam@162: if( data->framesLeft > framesPerBuffer ) cannam@162: { cannam@162: PaUtil_AddTraceMessage("QaCallback: running. framesLeft", data->framesLeft ); cannam@162: data->framesLeft -= framesPerBuffer; cannam@162: return 0; cannam@162: } cannam@162: else cannam@162: { cannam@162: PaUtil_AddTraceMessage("QaCallback: DONE! framesLeft", data->framesLeft ); cannam@162: data->framesLeft = 0; cannam@162: return 1; cannam@162: } cannam@162: } cannam@162: cannam@162: /*******************************************************************/ cannam@162: int main(void); cannam@162: int main(void) cannam@162: { cannam@162: PaError result; cannam@162: EXPECT( ((result=Pa_Initialize()) == 0) ); cannam@162: printf("Test OUTPUT ---------------\n"); cannam@162: TestDevices( MODE_OUTPUT ); cannam@162: printf("Test INPUT ---------------\n"); cannam@162: TestDevices( MODE_INPUT ); cannam@162: error: cannam@162: Pa_Terminate(); cannam@162: printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed ); cannam@162: return (gNumFailed > 0) ? 1 : 0; cannam@162: } cannam@162: cannam@162: /******************************************************************* cannam@162: * Try each output device, through its full range of capabilities. */ cannam@162: static void TestDevices( int mode ) cannam@162: { cannam@162: int id, jc, i; cannam@162: int maxChannels; cannam@162: const PaDeviceInfo *pdi; cannam@162: static double standardSampleRates[] = { 8000.0, 9600.0, 11025.0, 12000.0, cannam@162: 16000.0, 22050.0, 24000.0, cannam@162: 32000.0, 44100.0, 48000.0, cannam@162: 88200.0, 96000.0, cannam@162: -1.0 }; /* Negative terminated list. */ cannam@162: int numDevices = Pa_GetDeviceCount(); cannam@162: for( id=0; idmaxInputChannels : pdi->maxOutputChannels); cannam@162: if( maxChannels > MAX_TEST_CHANNELS ) cannam@162: maxChannels = MAX_TEST_CHANNELS; cannam@162: cannam@162: for( jc=1; jc<=maxChannels; jc++ ) cannam@162: { cannam@162: printf("\n========================================================================\n"); cannam@162: printf(" Device = %s\n", pdi->name ); cannam@162: printf("========================================================================\n"); cannam@162: /* Try each standard sample rate. */ cannam@162: for( i=0; standardSampleRates[i] > 0; i++ ) cannam@162: { cannam@162: TestFormats( mode, (PaDeviceIndex)id, standardSampleRates[i], jc ); cannam@162: } cannam@162: } cannam@162: } cannam@162: } cannam@162: cannam@162: /*******************************************************************/ cannam@162: static void TestFormats( int mode, PaDeviceIndex deviceID, double sampleRate, cannam@162: int numChannels ) cannam@162: { cannam@162: TestAdvance( mode, deviceID, sampleRate, numChannels, paFloat32 ); cannam@162: TestAdvance( mode, deviceID, sampleRate, numChannels, paInt16 ); cannam@162: TestAdvance( mode, deviceID, sampleRate, numChannels, paInt32 ); cannam@162: /* TestAdvance( mode, deviceID, sampleRate, numChannels, paInt24 ); */ cannam@162: } cannam@162: cannam@162: /*******************************************************************/ cannam@162: static int TestAdvance( int mode, PaDeviceIndex deviceID, double sampleRate, cannam@162: int numChannels, PaSampleFormat format ) cannam@162: { cannam@162: PaStreamParameters inputParameters, outputParameters, *ipp, *opp; cannam@162: PaStream *stream = NULL; cannam@162: PaError result = paNoError; cannam@162: PaQaData myData; cannam@162: #define FRAMES_PER_BUFFER (64) cannam@162: const int kNumSeconds = 100; cannam@162: cannam@162: /* Setup data for synthesis thread. */ cannam@162: myData.framesLeft = (unsigned long) (sampleRate * kNumSeconds); cannam@162: myData.numChannels = numChannels; cannam@162: myData.mode = mode; cannam@162: myData.format = format; cannam@162: switch( format ) cannam@162: { cannam@162: case paFloat32: cannam@162: case paInt32: cannam@162: case paInt24: cannam@162: myData.bytesPerSample = 4; cannam@162: break; cannam@162: /* case paPackedInt24: cannam@162: myData.bytesPerSample = 3; cannam@162: break; */ cannam@162: default: cannam@162: myData.bytesPerSample = 2; cannam@162: break; cannam@162: } cannam@162: cannam@162: if( mode == MODE_INPUT ) cannam@162: { cannam@162: inputParameters.device = deviceID; cannam@162: inputParameters.channelCount = numChannels; cannam@162: inputParameters.sampleFormat = format; cannam@162: inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; cannam@162: inputParameters.hostApiSpecificStreamInfo = NULL; cannam@162: ipp = &inputParameters; cannam@162: } cannam@162: else cannam@162: { cannam@162: ipp = NULL; cannam@162: } cannam@162: cannam@162: if( mode == MODE_OUTPUT ) cannam@162: { cannam@162: outputParameters.device = deviceID; cannam@162: outputParameters.channelCount = numChannels; cannam@162: outputParameters.sampleFormat = format; cannam@162: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; cannam@162: outputParameters.hostApiSpecificStreamInfo = NULL; cannam@162: opp = &outputParameters; cannam@162: } cannam@162: else cannam@162: { cannam@162: opp = NULL; cannam@162: } cannam@162: cannam@162: if(paFormatIsSupported == Pa_IsFormatSupported( ipp, opp, sampleRate )) cannam@162: { cannam@162: printf("------ TestAdvance: %s, device = %d, rate = %g, numChannels = %d, format = %lu -------\n", cannam@162: ( mode == MODE_INPUT ) ? "INPUT" : "OUTPUT", cannam@162: deviceID, sampleRate, numChannels, (unsigned long)format); cannam@162: EXPECT( ((result = Pa_OpenStream( &stream, cannam@162: ipp, cannam@162: opp, cannam@162: sampleRate, cannam@162: FRAMES_PER_BUFFER, cannam@162: paClipOff, /* we won't output out of range samples so don't bother clipping them */ cannam@162: QaCallback, cannam@162: &myData ) ) == 0) ); cannam@162: if( stream ) cannam@162: { cannam@162: PaTime oldStamp, newStamp; cannam@162: unsigned long oldFrames; cannam@162: int minDelay = ( mode == MODE_INPUT ) ? 1000 : 400; cannam@162: /* Was: cannam@162: int minNumBuffers = Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate ); cannam@162: int msec = (int) ((minNumBuffers * 3 * 1000.0 * FRAMES_PER_BUFFER) / sampleRate); cannam@162: */ cannam@162: int msec = (int)( 3.0 * cannam@162: (( mode == MODE_INPUT ) ? inputParameters.suggestedLatency : outputParameters.suggestedLatency )); cannam@162: if( msec < minDelay ) msec = minDelay; cannam@162: printf("msec = %d\n", msec); /**/ cannam@162: EXPECT( ((result=Pa_StartStream( stream )) == 0) ); cannam@162: /* Check to make sure PortAudio is advancing timeStamp. */ cannam@162: oldStamp = Pa_GetStreamTime(stream); cannam@162: Pa_Sleep(msec); cannam@162: newStamp = Pa_GetStreamTime(stream); cannam@162: printf("oldStamp = %9.6f, newStamp = %9.6f\n", oldStamp, newStamp ); /**/ cannam@162: EXPECT( (oldStamp < newStamp) ); cannam@162: /* Check to make sure callback is decrementing framesLeft. */ cannam@162: oldFrames = myData.framesLeft; cannam@162: Pa_Sleep(msec); cannam@162: printf("oldFrames = %lu, myData.framesLeft = %lu\n", oldFrames, myData.framesLeft ); /**/ cannam@162: EXPECT( (oldFrames > myData.framesLeft) ); cannam@162: EXPECT( ((result=Pa_CloseStream( stream )) == 0) ); cannam@162: stream = NULL; cannam@162: } cannam@162: } cannam@162: return 0; cannam@162: error: cannam@162: if( stream != NULL ) Pa_CloseStream( stream ); cannam@162: return -1; cannam@162: }