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