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