Chris@4: /** @file patest_converters.c Chris@4: @ingroup test_src Chris@4: @brief Tests the converter functions in pa_converters.c Chris@4: @author Ross Bencina Chris@4: Chris@4: Link with pa_dither.c and pa_converters.c Chris@4: Chris@4: see http://www.portaudio.com/trac/wiki/V19ConvertersStatus for a discussion of this. Chris@4: */ Chris@4: /* Chris@4: * $Id: $ 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-2008 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: #include Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: Chris@4: #include "portaudio.h" Chris@4: #include "pa_converters.h" Chris@4: #include "pa_dither.h" Chris@4: #include "pa_types.h" Chris@4: #include "pa_endianness.h" Chris@4: Chris@4: #ifndef M_PI Chris@4: #define M_PI (3.14159265) Chris@4: #endif Chris@4: Chris@4: #define MAX_PER_CHANNEL_FRAME_COUNT (2048) Chris@4: #define MAX_CHANNEL_COUNT (8) Chris@4: Chris@4: Chris@4: #define SAMPLE_FORMAT_COUNT (6) Chris@4: Chris@4: static PaSampleFormat sampleFormats_[ SAMPLE_FORMAT_COUNT ] = Chris@4: { paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8 }; /* all standard PA sample formats */ Chris@4: Chris@4: static const char* sampleFormatNames_[SAMPLE_FORMAT_COUNT] = Chris@4: { "paFloat32", "paInt32", "paInt24", "paInt16", "paInt8", "paUInt8" }; Chris@4: Chris@4: Chris@4: static const char* abbreviatedSampleFormatNames_[SAMPLE_FORMAT_COUNT] = Chris@4: { "f32", "i32", "i24", "i16", " i8", "ui8" }; Chris@4: Chris@4: Chris@4: PaError My_Pa_GetSampleSize( PaSampleFormat format ); Chris@4: Chris@4: /* Chris@4: available flags are paClipOff and paDitherOff Chris@4: clipping is usually applied for float -> int conversions Chris@4: dither is usually applied for all downconversions (ie anything but 8bit->8bit conversions Chris@4: */ Chris@4: Chris@4: static int CanClip( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat ) Chris@4: { Chris@4: if( sourceFormat == paFloat32 && destinationFormat != sourceFormat ) Chris@4: return 1; Chris@4: else Chris@4: return 0; Chris@4: } Chris@4: Chris@4: static int CanDither( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat ) Chris@4: { Chris@4: if( sourceFormat < destinationFormat && sourceFormat != paInt8 ) Chris@4: return 1; Chris@4: else Chris@4: return 0; Chris@4: } Chris@4: Chris@4: static void GenerateOneCycleSineReference( double *out, int frameCount, int strideFrames ) Chris@4: { Chris@4: int i; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = sin( ((double)i/(double)frameCount) * 2. * M_PI ); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: static void GenerateOneCycleSine( PaSampleFormat format, void *buffer, int frameCount, int strideFrames ) Chris@4: { Chris@4: switch( format ){ Chris@4: Chris@4: case paFloat32: Chris@4: { Chris@4: int i; Chris@4: float *out = (float*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = (float).9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: break; Chris@4: case paInt32: Chris@4: { Chris@4: int i; Chris@4: PaInt32 *out = (PaInt32*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: break; Chris@4: case paInt24: Chris@4: { Chris@4: int i; Chris@4: unsigned char *out = (unsigned char*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: signed long temp = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF); Chris@4: Chris@4: #if defined(PA_LITTLE_ENDIAN) Chris@4: out[0] = (unsigned char)(temp >> 8) & 0xFF; Chris@4: out[1] = (unsigned char)(temp >> 16) & 0xFF; Chris@4: out[2] = (unsigned char)(temp >> 24) & 0xFF; Chris@4: #elif defined(PA_BIG_ENDIAN) Chris@4: out[0] = (unsigned char)(temp >> 24) & 0xFF; Chris@4: out[1] = (unsigned char)(temp >> 16) & 0xFF; Chris@4: out[2] = (unsigned char)(temp >> 8) & 0xFF; Chris@4: #endif Chris@4: out += 3; Chris@4: } Chris@4: } Chris@4: break; Chris@4: case paInt16: Chris@4: { Chris@4: int i; Chris@4: PaInt16 *out = (PaInt16*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = (PaInt16)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFF ); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: break; Chris@4: case paInt8: Chris@4: { Chris@4: int i; Chris@4: signed char *out = (signed char*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = (signed char)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7F ); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: break; Chris@4: case paUInt8: Chris@4: { Chris@4: int i; Chris@4: unsigned char *out = (unsigned char*)buffer; Chris@4: for( i=0; i < frameCount; ++i ){ Chris@4: *out = (unsigned char)( .5 * (1. + (.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ))) * 0xFF ); Chris@4: out += strideFrames; Chris@4: } Chris@4: } Chris@4: break; Chris@4: } Chris@4: } Chris@4: Chris@4: int TestNonZeroPresent( void *buffer, int size ) Chris@4: { Chris@4: char *p = (char*)buffer; Chris@4: int i; Chris@4: Chris@4: for( i=0; i < size; ++i ){ Chris@4: Chris@4: if( *p != 0 ) Chris@4: return 1; Chris@4: ++p; Chris@4: } Chris@4: Chris@4: return 0; Chris@4: } Chris@4: Chris@4: float MaximumAbsDifference( float* sourceBuffer, float* referenceBuffer, int count ) Chris@4: { Chris@4: float result = 0; Chris@4: float difference; Chris@4: while( count-- ){ Chris@4: difference = fabs( *sourceBuffer++ - *referenceBuffer++ ); Chris@4: if( difference > result ) Chris@4: result = difference; Chris@4: } Chris@4: Chris@4: return result; Chris@4: } Chris@4: Chris@4: int main( const char **argv, int argc ) Chris@4: { Chris@4: PaUtilTriangularDitherGenerator ditherState; Chris@4: PaUtilConverter *converter; Chris@4: void *destinationBuffer, *sourceBuffer; Chris@4: double *referenceBuffer; Chris@4: int sourceFormatIndex, destinationFormatIndex; Chris@4: PaSampleFormat sourceFormat, destinationFormat; Chris@4: PaStreamFlags flags; Chris@4: int passFailMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination] Chris@4: float noiseAmplitudeMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination] Chris@4: float amp; Chris@4: Chris@4: #define FLAG_COMBINATION_COUNT (4) Chris@4: PaStreamFlags flagCombinations[FLAG_COMBINATION_COUNT] = { paNoFlag, paClipOff, paDitherOff, paClipOff | paDitherOff }; Chris@4: const char *flagCombinationNames[FLAG_COMBINATION_COUNT] = { "paNoFlag", "paClipOff", "paDitherOff", "paClipOff | paDitherOff" }; Chris@4: int flagCombinationIndex; Chris@4: Chris@4: PaUtil_InitializeTriangularDitherState( &ditherState ); Chris@4: Chris@4: /* allocate more than enough space, we use sizeof(float) but we need to fit any 32 bit datum */ Chris@4: Chris@4: destinationBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); Chris@4: sourceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); Chris@4: referenceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); Chris@4: Chris@4: Chris@4: /* the first round of tests simply iterates through the buffer combinations testing Chris@4: that putting something in gives something out */ Chris@4: Chris@4: printf( "= Sine wave in, something out =\n" ); Chris@4: Chris@4: printf( "\n" ); Chris@4: Chris@4: GenerateOneCycleSine( paFloat32, referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 ); Chris@4: Chris@4: for( flagCombinationIndex = 0; flagCombinationIndex < FLAG_COMBINATION_COUNT; ++flagCombinationIndex ){ Chris@4: flags = flagCombinations[flagCombinationIndex]; Chris@4: Chris@4: printf( "\n" ); Chris@4: printf( "== flags = %s ==\n", flagCombinationNames[flagCombinationIndex] ); Chris@4: Chris@4: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ Chris@4: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ Chris@4: sourceFormat = sampleFormats_[sourceFormatIndex]; Chris@4: destinationFormat = sampleFormats_[destinationFormatIndex]; Chris@4: //printf( "%s -> %s ", sampleFormatNames_[ sourceFormatIndex ], sampleFormatNames_[ destinationFormatIndex ] ); Chris@4: Chris@4: converter = PaUtil_SelectConverter( sourceFormat, destinationFormat, flags ); Chris@4: Chris@4: /* source is a sinewave */ Chris@4: GenerateOneCycleSine( sourceFormat, sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 ); Chris@4: Chris@4: /* zero destination */ Chris@4: memset( destinationBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) ); Chris@4: Chris@4: (*converter)( destinationBuffer, 1, sourceBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState ); Chris@4: Chris@4: /* Chris@4: Other ways we could test this would be: Chris@4: - pass a constant, check for a constant (wouldn't work with dither) Chris@4: - pass alternating +/-, check for the same... Chris@4: */ Chris@4: if( TestNonZeroPresent( destinationBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) ) ){ Chris@4: //printf( "PASSED\n" ); Chris@4: passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 1; Chris@4: }else{ Chris@4: //printf( "FAILED\n" ); Chris@4: passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 0; Chris@4: } Chris@4: Chris@4: Chris@4: /* try to measure the noise floor (comparing output signal to a float32 sine wave) */ Chris@4: Chris@4: if( passFailMatrix[sourceFormatIndex][destinationFormatIndex] ){ Chris@4: Chris@4: /* convert destination back to paFloat32 into source */ Chris@4: converter = PaUtil_SelectConverter( destinationFormat, paFloat32, paNoFlag ); Chris@4: Chris@4: memset( sourceBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) ); Chris@4: (*converter)( sourceBuffer, 1, destinationBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState ); Chris@4: Chris@4: if( TestNonZeroPresent( sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) ) ){ Chris@4: Chris@4: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = MaximumAbsDifference( (float*)sourceBuffer, (float*)referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT ); Chris@4: Chris@4: }else{ Chris@4: /* can't test noise floor because there is no conversion from dest format to float available */ Chris@4: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed Chris@4: } Chris@4: }else{ Chris@4: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: printf( "\n" ); Chris@4: printf( "=== Output contains non-zero data ===\n" ); Chris@4: printf( "Key: . - pass, X - fail\n" ); Chris@4: printf( "{{{\n" ); // trac preformated text tag Chris@4: printf( "in| out: " ); Chris@4: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ Chris@4: printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] ); Chris@4: } Chris@4: printf( "\n" ); Chris@4: Chris@4: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ Chris@4: printf( "%s ", abbreviatedSampleFormatNames_[sourceFormatIndex] ); Chris@4: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ Chris@4: printf( " %s ", (passFailMatrix[sourceFormatIndex][destinationFormatIndex])? " ." : " X" ); Chris@4: } Chris@4: printf( "\n" ); Chris@4: } Chris@4: printf( "}}}\n" ); // trac preformated text tag Chris@4: Chris@4: printf( "\n" ); Chris@4: printf( "=== Combined dynamic range (src->dest->float32) ===\n" ); Chris@4: printf( "Key: Noise amplitude in dBfs, X - fail (either above failed or dest->float32 failed)\n" ); Chris@4: printf( "{{{\n" ); // trac preformated text tag Chris@4: printf( "in| out: " ); Chris@4: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ Chris@4: printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] ); Chris@4: } Chris@4: printf( "\n" ); Chris@4: Chris@4: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ Chris@4: printf( " %s ", abbreviatedSampleFormatNames_[sourceFormatIndex] ); Chris@4: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ Chris@4: amp = noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex]; Chris@4: if( amp < 0. ) Chris@4: printf( " X " ); Chris@4: else Chris@4: printf( " % 6.1f ", 20.*log10(amp) ); Chris@4: } Chris@4: printf( "\n" ); Chris@4: } Chris@4: printf( "}}}\n" ); // trac preformated text tag Chris@4: } Chris@4: Chris@4: Chris@4: free( destinationBuffer ); Chris@4: free( sourceBuffer ); Chris@4: free( referenceBuffer ); Chris@4: } Chris@4: Chris@4: // copied here for now otherwise we need to include the world just for this function. Chris@4: PaError My_Pa_GetSampleSize( PaSampleFormat format ) Chris@4: { Chris@4: int result; Chris@4: Chris@4: switch( format & ~paNonInterleaved ) Chris@4: { Chris@4: Chris@4: case paUInt8: Chris@4: case paInt8: Chris@4: result = 1; Chris@4: break; Chris@4: Chris@4: case paInt16: Chris@4: result = 2; Chris@4: break; Chris@4: Chris@4: case paInt24: Chris@4: result = 3; Chris@4: break; Chris@4: Chris@4: case paFloat32: Chris@4: case paInt32: Chris@4: result = 4; Chris@4: break; Chris@4: Chris@4: default: Chris@4: result = paSampleFormatNotSupported; Chris@4: break; Chris@4: } Chris@4: Chris@4: return (PaError) result; Chris@4: }