From: rossb Date: Fri, 4 Mar 2011 05:17:47 +0000 (+0000) Subject: changes to get qa building on Windows with msvc. had to move variable declarations... X-Git-Tag: pa_stable_v19_20110326_r1647~11 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=cedaf416060fe7e767e5f2154d4b456b94e21818;p=portaudio changes to get qa building on Windows with msvc. had to move variable declarations to the top of functions (ie ANSI C89) --- diff --git a/qa/loopback/src/audio_analyzer.c b/qa/loopback/src/audio_analyzer.c index a57feb0..f027f63 100644 --- a/qa/loopback/src/audio_analyzer.c +++ b/qa/loopback/src/audio_analyzer.c @@ -114,7 +114,7 @@ void PaQa_GenerateCrackDISABLED( float *buffer, int numSamples, int stride ) int PaQa_InitializeRecording( PaQaRecording *recording, int maxFrames, int frameRate ) { int numBytes = maxFrames * sizeof(float); - recording->buffer = malloc(numBytes); + recording->buffer = (float*)malloc(numBytes); QA_ASSERT_TRUE( "Allocate recording buffer.", (recording->buffer != NULL) ); recording->maxFrames = maxFrames; recording->sampleRate = frameRate; recording->numFrames = 0; @@ -138,12 +138,15 @@ void PaQa_TerminateRecording( PaQaRecording *recording ) int PaQa_WriteRecording( PaQaRecording *recording, float *buffer, int numFrames, int stride ) { int i; - int framesToWrite = numFrames; + int framesToWrite; + float *data = &recording->buffer[recording->numFrames]; + + framesToWrite = numFrames; if ((framesToWrite + recording->numFrames) > recording->maxFrames) { framesToWrite = recording->maxFrames - recording->numFrames; } - float *data = &recording->buffer[recording->numFrames]; + for( i=0; ibuffer[recording->numFrames]; + + framesToRecord = numFrames; if ((framesToRecord + recording->numFrames) > recording->maxFrames) { framesToRecord = recording->maxFrames - recording->numFrames; } - float *data = &recording->buffer[recording->numFrames]; + for( i=0; ibuffer[recording->numFrames]; + + framesToRecord = numFrames; if ((framesToRecord + recording->numFrames) > recording->maxFrames) { framesToRecord = recording->maxFrames - recording->numFrames; } - float *data = &recording->buffer[recording->numFrames]; + for( i=0; inumFrames; + float *buffer = &recording->buffer[0]; + result = Audio_WAV_OpenWriter( &writer, filename, recording->sampleRate, samplesPerFrame ); if( result < 0 ) goto error; - int numLeft = recording->numFrames; - float *buffer = &recording->buffer[0]; - while( numLeft > 0 ) { int i; @@ -291,15 +300,16 @@ double PaQa_CorrelateSine( PaQaRecording *recording, double frequency, double fr int startFrame, int numFrames, double *phasePtr ) { double magnitude = 0.0; - QA_ASSERT_TRUE( "startFrame out of bounds", (startFrame < recording->numFrames) ); - QA_ASSERT_TRUE( "numFrames out of bounds", ((startFrame+numFrames) <= recording->numFrames) ); - - int numLeft = numFrames; + int numLeft = numFrames; double phase = 0.0; double phaseIncrement = 2.0 * MATH_PI * frequency / frameRate; double sinAccumulator = 0.0; double cosAccumulator = 0.0; float *data = &recording->buffer[startFrame]; + + QA_ASSERT_TRUE( "startFrame out of bounds", (startFrame < recording->numFrames) ); + QA_ASSERT_TRUE( "numFrames out of bounds", ((startFrame+numFrames) <= recording->numFrames) ); + while( numLeft > 0 ) { double sample = (double) *data++; @@ -340,12 +350,14 @@ void PaQa_FilterRecording( PaQaRecording *input, PaQaRecording *output, BiquadFi double PaQa_FindFirstMatch( PaQaRecording *recording, float *buffer, int numFrames, double tolerance ) { int ic,is; - QA_ASSERT_TRUE( "numFrames out of bounds", (numFrames < recording->numFrames) ); // How many buffers will fit in the recording? int maxCorrelations = recording->numFrames - numFrames; double maxSum = 0.0; int peakIndex = -1; double location = -1.0; + + QA_ASSERT_TRUE( "numFrames out of bounds", (numFrames < recording->numFrames) ); + for( ic=0; icnumFrames) ); - double recordedArea = PaQa_MeasureArea( &recording->buffer[startAt], numFrames, 1 ); - double bufferArea = PaQa_MeasureArea( buffer, numFrames, 1 ); - if( bufferArea == 0.0 ) return 100000000.0; - return recordedArea / bufferArea; + { + double recordedArea = PaQa_MeasureArea( &recording->buffer[startAt], numFrames, 1 ); + double bufferArea = PaQa_MeasureArea( buffer, numFrames, 1 ); + if( bufferArea == 0.0 ) return 100000000.0; + return recordedArea / bufferArea; + } error: return -1.0; } @@ -455,9 +469,8 @@ int PaQa_MeasureLatency( PaQaRecording *recording, PaQaTestTone *testTone, PaQaA PaQa_SetupSineGenerator( &generator, testTone->frequency, testTone->amplitude, testTone->sampleRate ); PaQa_EraseBuffer( buffer, cycleSize, testTone->samplesPerFrame ); PaQa_MixSine( &generator, buffer, cycleSize, testTone->samplesPerFrame ); - - double tolerance = 0.1; - analysisResult->latency = PaQa_FindFirstMatch( recording, buffer, cycleSize, tolerance ); + + analysisResult->latency = PaQa_FindFirstMatch( recording, buffer, cycleSize, /* tolerance= */ 0.1 ); QA_ASSERT_TRUE( "Could not find the start of the signal.", (analysisResult->latency >= 0) ); analysisResult->amplitudeRatio = PaQa_CompareAmplitudes( recording, analysisResult->latency, buffer, cycleSize ); return 0; @@ -472,21 +485,23 @@ error: void PaQa_FadeInRecording( PaQaRecording *recording, int startFrame, int count ) { int is; - assert( startFrame >= 0 ); - assert( count > 0 ); double phase = 0.5 * MATH_PI; // Advance a quarter wave double phaseIncrement = 0.25 * 2.0 * MATH_PI / count; + assert( startFrame >= 0 ); + assert( count > 0 ); + for( is=0; isbuffer[ is + startFrame ]; float y = x * w; //printf("FADE %d : w=%f, x=%f, y=%f\n", is, w, x, y ); recording->buffer[ is + startFrame ] = y; + + phase += phaseIncrement; } } @@ -496,7 +511,11 @@ void PaQa_FadeInRecording( PaQaRecording *recording, int startFrame, int count ) */ int PaQa_DetectPop( PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult ) { + int result = 0; int i; + double maxAmplitude; + int maxPosition; + PaQaRecording notchOutput = { 0 }; BiquadFilter notchFilter; @@ -508,7 +527,7 @@ int PaQa_DetectPop( PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalys analysisResult->popPosition = -1; analysisResult->popAmplitude = 0.0; - int result = PaQa_InitializeRecording( ¬chOutput, recording->numFrames, frameRate ); + result = PaQa_InitializeRecording( ¬chOutput, recording->numFrames, frameRate ); QA_ASSERT_EQUALS( "PaQa_InitializeRecording failed", 0, result ); result = PaQa_InitializeRecording( &hipassOutput, recording->numFrames, frameRate ); @@ -530,8 +549,8 @@ int PaQa_DetectPop( PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalys //QA_ASSERT_EQUALS( "PaQa_SaveRecordingToWaveFile failed", 0, result ); // Scan remaining signal looking for peak. - double maxAmplitude = 0.0; - int maxPosition = -1; + maxAmplitude = 0.0; + maxPosition = -1; for( i=0; isampleRate / testTone->frequency; int cycleSize = (int) (period + 0.5); - // Scan recording starting with first cycle, looking for phase errors. - analysisResult->numDroppedFrames = 0.0; - analysisResult->numAddedFrames = 0.0; - analysisResult->droppedFramesPosition = -1.0; - analysisResult->addedFramesPosition = -1.0; - double maxAddedFrames = 0.0; double maxDroppedFrames = 0.0; @@ -580,6 +593,12 @@ int PaQa_DetectPhaseError( PaQaRecording *recording, PaQaTestTone *testTone, PaQ int loopCount = 0; int skip = cycleSize; int windowSize = cycleSize; + + // Scan recording starting with first cycle, looking for phase errors. + analysisResult->numDroppedFrames = 0.0; + analysisResult->numAddedFrames = 0.0; + analysisResult->droppedFramesPosition = -1.0; + analysisResult->addedFramesPosition = -1.0; for( i=analysisResult->latency; i<(recording->numFrames - windowSize); i += skip ) { @@ -636,10 +655,11 @@ int PaQa_DetectPhaseError( PaQaRecording *recording, PaQaTestTone *testTone, PaQ /*==========================================================================================*/ int PaQa_AnalyseRecording( PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult ) { + int result = 0; + memset( analysisResult, 0, sizeof(PaQaAnalysisResult) ); - - int result = PaQa_MeasureLatency( recording, testTone, analysisResult ); - QA_ASSERT_EQUALS( "latency measurement", 0, result ); + result = PaQa_MeasureLatency( recording, testTone, analysisResult ); + QA_ASSERT_EQUALS( "latency measurement", 0, result ); if( (analysisResult->latency >= 0) && (analysisResult->amplitudeRatio > 0.1) ) { diff --git a/qa/loopback/src/paqa.c b/qa/loopback/src/paqa.c index 29f9f6c..3fed6b2 100644 --- a/qa/loopback/src/paqa.c +++ b/qa/loopback/src/paqa.c @@ -40,6 +40,8 @@ #include #include #include +#include + #include "portaudio.h" #include "qa_tools.h" @@ -641,7 +643,11 @@ static int PaQa_SaveTestResultToWaveFile( UserOptions *userOptions, PaQaRecordin if( userOptions->saveBadWaves ) { char filename[256]; +#ifdef WIN32 + _snprintf( filename, sizeof(filename), "%s\\paloopback_%d.wav", userOptions->waveFilePath, userOptions->waveFileCount++ ); +#else snprintf( filename, sizeof(filename), "%s/paloopback_%d.wav", userOptions->waveFilePath, userOptions->waveFileCount++ ); +#endif printf( "\"%s\", ", filename ); return PaQa_SaveRecordingToWaveFile( recording, filename ); } @@ -759,12 +765,14 @@ static int PaQa_SingleLoopBackTest( UserOptions *userOptions, TestParameters *te if( i==0 ) { + double latencyMSec; + printf( "%4d-%4d | ", loopbackContext.minFramesPerBuffer, loopbackContext.maxFramesPerBuffer ); - double latencyMSec = 1000.0 * analysisResult.latency / testParams->sampleRate; + latencyMSec = 1000.0 * analysisResult.latency / testParams->sampleRate; printf("%7.2f | ", latencyMSec ); } @@ -885,17 +893,12 @@ static int PaQa_AnalyzeLoopbackConnection( UserOptions *userOptions, PaDeviceInd int iRate; int iSize; int iFormat; - int totalBadChannels = 0; int savedValue; TestParameters testParams; - const PaDeviceInfo *inputDeviceInfo; - const PaDeviceInfo *outputDeviceInfo; - inputDeviceInfo = Pa_GetDeviceInfo( inputDevice ); - outputDeviceInfo = Pa_GetDeviceInfo( outputDevice ); - - printf( "=============== Analysing Loopback %d to %d ====================\n", outputDevice, inputDevice ); - printf( " Devices: %s => %s\n", outputDeviceInfo->name, inputDeviceInfo->name); - + const PaDeviceInfo *inputDeviceInfo = Pa_GetDeviceInfo( inputDevice ); + const PaDeviceInfo *outputDeviceInfo = Pa_GetDeviceInfo( outputDevice ); + int totalBadChannels = 0; + // test half duplex first because it is more likely to work. int flagSettings[] = { PAQA_FLAG_TWO_STREAMS, 0 }; int numFlagSettings = (sizeof(flagSettings)/sizeof(int)); @@ -911,6 +914,9 @@ static int PaQa_AnalyzeLoopbackConnection( UserOptions *userOptions, PaDeviceInd const char *sampleFormatNames[] = { "paUInt8", "paInt8", "paInt16", "paInt32" }; int numSampleFormats = (sizeof(sampleFormats)/sizeof(PaSampleFormat)); + printf( "=============== Analysing Loopback %d to %d ====================\n", outputDevice, inputDevice ); + printf( " Devices: %s => %s\n", outputDeviceInfo->name, inputDeviceInfo->name); + PaQa_SetDefaultTestParameters( &testParams, inputDevice, outputDevice ); PaQa_OverrideTestParameters( &testParams, userOptions ); @@ -932,11 +938,13 @@ static int PaQa_AnalyzeLoopbackConnection( UserOptions *userOptions, PaDeviceInd savedValue = testParams.sampleRate; for( iRate=0; iRate minAmplitude) && (magRight > minAmplitude)); // Check for backwards cable. - if( !loopbackConnected ) + loopbackIsConnected = ((magLeft > minAmplitude) && (magRight > minAmplitude)); + + if( !loopbackIsConnected ) { double magLeftReverse = PaQa_CorrelateSine( &loopbackContext.recordings[0], loopbackContext.generators[1].frequency, @@ -1146,7 +1161,7 @@ int PaQa_CheckForLoopBack( UserOptions *userOptions, PaDeviceIndex inputDevice, if( PaQa_CheckForClippedLoopback( &loopbackContext ) ) { // Clipped so don't use this loopback. - loopbackConnected = 0; + loopbackIsConnected = 0; } err = PaQa_MeasureBackgroundNoise( &loopbackContext, &rms ); @@ -1154,17 +1169,17 @@ int PaQa_CheckForLoopBack( UserOptions *userOptions, PaDeviceIndex inputDevice, if( err ) { printf("ERROR - Could not measure background noise on this input!\n"); - loopbackConnected = 0; + loopbackIsConnected = 0; } else if( rms > MAX_BACKGROUND_NOISE_RMS ) { printf("ERROR - There is too much background noise on this input!\n"); - loopbackConnected = 0; + loopbackIsConnected = 0; } } PaQa_TeardownLoopbackContext( &loopbackContext ); - return loopbackConnected; + return loopbackIsConnected; error: PaQa_TeardownLoopbackContext( &loopbackContext ); @@ -1248,7 +1263,7 @@ int TestSampleFormatConversion( void ) const char charInput[] = { 127, 64, -64, -128 }; const unsigned char ucharInput[] = { 255, 128+64, 64, 0 }; const short shortInput[] = { 32767, 32768/2, -32768/2, -32768 }; - const int intInput[] = { 2147483647, 2147483647/2, -2147483648/2, -2147483648 }; + const int intInput[] = { 2147483647, 2147483647/2, -1073741824 /*-2147483648/2 doesn't work in msvc*/, -2147483648 }; float floatOutput[4]; short shortOutput[4]; @@ -1341,10 +1356,12 @@ void usage( const char *name ) /*******************************************************************/ int main( int argc, char **argv ) { + int i; UserOptions userOptions; - int i; int result = 0; int justMath = 0; + char *executableName = argv[0]; + printf("PortAudio LoopBack Test built " __DATE__ " at " __TIME__ "\n"); memset(&userOptions, 0, sizeof(userOptions)); @@ -1357,7 +1374,6 @@ int main( int argc, char **argv ) userOptions.waveFilePath = "."; // Process arguments. Skip name of executable. - char *name = argv[0]; i = 1; while( i> 8; + int value = (*data++) >> 8; float fval = (float) (value / ((double) 0x00800000)); *output++ = fval; } diff --git a/qa/loopback/src/test_audio_analyzer.c b/qa/loopback/src/test_audio_analyzer.c index 5ba01f7..eb523a9 100644 --- a/qa/loopback/src/test_audio_analyzer.c +++ b/qa/loopback/src/test_audio_analyzer.c @@ -63,18 +63,21 @@ static int TestSingleMonoTone( void ) double sampleRate = 44100.0; int maxFrames = ((int)sampleRate) * 1; int samplesPerFrame = 1; - + int stride = 1; + int done = 0; + double freq = 234.5; double amp = 0.5; + double mag1, mag2; + // Setup a sine oscillator. PaQa_SetupSineGenerator( &generator, freq, amp, sampleRate ); result = PaQa_InitializeRecording( &recording, maxFrames, (int) sampleRate ); QA_ASSERT_EQUALS( "PaQa_InitializeRecording failed", 0, result ); - - int stride = 1; - int done = 0; + + done = 0; while (!done) { PaQa_EraseBuffer( buffer, FRAMES_PER_BLOCK, samplesPerFrame ); @@ -82,10 +85,10 @@ static int TestSingleMonoTone( void ) done = PaQa_WriteRecording( &recording, buffer, FRAMES_PER_BLOCK, samplesPerFrame ); } - double mag1 = PaQa_CorrelateSine( &recording, freq, sampleRate, 0, recording.numFrames, NULL ); + mag1 = PaQa_CorrelateSine( &recording, freq, sampleRate, 0, recording.numFrames, NULL ); QA_ASSERT_CLOSE( "exact frequency match", amp, mag1, 0.01 ); - double mag2 = PaQa_CorrelateSine( &recording, freq * 1.23, sampleRate, 0, recording.numFrames, NULL ); + mag2 = PaQa_CorrelateSine( &recording, freq * 1.23, sampleRate, 0, recording.numFrames, NULL ); QA_ASSERT_CLOSE( "wrong frequency", 0.0, mag2, 0.01 ); PaQa_TerminateRecording( &recording ); @@ -117,6 +120,11 @@ static int TestMixedMonoTones( void ) double baseFreq = 234.5; double amp = 0.1; + double mag2; + + int stride = samplesPerFrame; + int done = 0; + // Setup a sine oscillator. for( i=0; ifrequency, testTone->amplitude, testTone->sampleRate ); + int frameCounter = testTone->startDelay; int stride = 1; // Record some initial silence. int done = PaQa_WriteSilence( recording, testTone->startDelay ); - int frameCounter = testTone->startDelay; + // Setup a sine oscillator. + PaQa_SetupSineGenerator( &generator, testTone->frequency, testTone->amplitude, testTone->sampleRate ); + while (!done) { int framesThisLoop = BUFFER_SIZE; @@ -225,13 +232,13 @@ static void MakeRecordingWithPop( PaQaRecording *recording, PaQaTestTone *testTo #define BUFFER_SIZE 512 float buffer[BUFFER_SIZE]; - // Setup a sine oscillator. - PaQa_SetupSineGenerator( &generator, testTone->frequency, testTone->amplitude, testTone->sampleRate ); - int stride = 1; // Record some initial silence. int done = PaQa_WriteSilence( recording, testTone->startDelay ); + // Setup a sine oscillator. + PaQa_SetupSineGenerator( &generator, testTone->frequency, testTone->amplitude, testTone->sampleRate ); + // Generate recording with good phase. while (!done) { @@ -260,20 +267,19 @@ static void MakeRecordingWithPop( PaQaRecording *recording, PaQaTestTone *testTo */ static int TestDetectSinglePhaseError( double sampleRate, int cycleSize, int latencyFrames, int glitchPosition, int framesAdded ) { - int result = 0; PaQaRecording recording; PaQaTestTone testTone; PaQaAnalysisResult analysisResult = { 0.0 }; - + int framesDropped = 0; + int maxFrames = ((int)sampleRate) * 2; + testTone.samplesPerFrame = 1; testTone.sampleRate = sampleRate; testTone.frequency = sampleRate / cycleSize; testTone.amplitude = 0.5; testTone.startDelay = latencyFrames; - int maxFrames = ((int)testTone.sampleRate) * 2; - result = PaQa_InitializeRecording( &recording, maxFrames, (int) sampleRate ); QA_ASSERT_EQUALS( "PaQa_InitializeRecording failed", 0, result ); @@ -281,7 +287,6 @@ static int TestDetectSinglePhaseError( double sampleRate, int cycleSize, int lat PaQa_AnalyseRecording( &recording, &testTone, &analysisResult ); - int framesDropped = 0; if( framesAdded < 0 ) { framesDropped = -framesAdded; @@ -350,20 +355,18 @@ static int TestDetectPhaseErrors( void ) */ static int TestDetectSinglePop( double sampleRate, int cycleSize, int latencyFrames, int popPosition, int popWidth, double popAmplitude ) { - int result = 0; PaQaRecording recording; PaQaTestTone testTone; PaQaAnalysisResult analysisResult = { 0.0 }; - + int maxFrames = ((int)sampleRate) * 2; + testTone.samplesPerFrame = 1; testTone.sampleRate = sampleRate; testTone.frequency = sampleRate / cycleSize; testTone.amplitude = 0.5; testTone.startDelay = latencyFrames; - int maxFrames = ((int)testTone.sampleRate) * 2; - result = PaQa_InitializeRecording( &recording, maxFrames, (int) sampleRate ); QA_ASSERT_EQUALS( "PaQa_InitializeRecording failed", 0, result ); @@ -478,12 +481,13 @@ void PaQa_FillWithSine( PaQaRecording *recording, double sampleRate, double freq PaQaSineGenerator generator; float buffer[FRAMES_PER_BLOCK]; int samplesPerFrame = 1; - + int stride = 1; + int done = 0; + // Setup a sine oscillator. PaQa_SetupSineGenerator( &generator, freq, amp, sampleRate ); - int stride = 1; - int done = 0; + done = 0; while (!done) { PaQa_EraseBuffer( buffer, FRAMES_PER_BLOCK, samplesPerFrame ); @@ -510,6 +514,8 @@ static int TestNotchFilter( void ) double freq = 234.5; double amp = 0.5; + double mag1, mag2, mag3; + result = PaQa_InitializeRecording( &original, maxFrames, (int) sampleRate ); QA_ASSERT_EQUALS( "PaQa_InitializeRecording failed", 0, result ); @@ -518,7 +524,7 @@ static int TestNotchFilter( void ) //result = PaQa_SaveRecordingToWaveFile( &original, "original.wav" ); //QA_ASSERT_EQUALS( "PaQa_SaveRecordingToWaveFile failed", 0, result ); - double mag1 = PaQa_CorrelateSine( &original, freq, sampleRate, 0, original.numFrames, NULL ); + mag1 = PaQa_CorrelateSine( &original, freq, sampleRate, 0, original.numFrames, NULL ); QA_ASSERT_CLOSE( "exact frequency match", amp, mag1, 0.01 ); // Filter with exact frequency. @@ -530,7 +536,7 @@ static int TestNotchFilter( void ) result = PaQa_SaveRecordingToWaveFile( &filtered, "filtered1.wav" ); QA_ASSERT_EQUALS( "PaQa_SaveRecordingToWaveFile failed", 0, result ); - double mag2 = PaQa_CorrelateSine( &filtered, freq, sampleRate, 0, filtered.numFrames, NULL ); + mag2 = PaQa_CorrelateSine( &filtered, freq, sampleRate, 0, filtered.numFrames, NULL ); QA_ASSERT_CLOSE( "should eliminate tone", 0.0, mag2, 0.01 ); // Filter with mismatched frequency. @@ -540,7 +546,7 @@ static int TestNotchFilter( void ) //result = PaQa_SaveRecordingToWaveFile( &filtered, "badfiltered.wav" ); //QA_ASSERT_EQUALS( "PaQa_SaveRecordingToWaveFile failed", 0, result ); - double mag3 = PaQa_CorrelateSine( &filtered, freq, sampleRate, 0, filtered.numFrames, NULL ); + mag3 = PaQa_CorrelateSine( &filtered, freq, sampleRate, 0, filtered.numFrames, NULL ); QA_ASSERT_CLOSE( "should eliminate tone", amp*0.26, mag3, 0.01 );