cannam@89: /** @file patest_converters.c cannam@89: @ingroup test_src cannam@89: @brief Tests the converter functions in pa_converters.c cannam@89: @author Ross Bencina cannam@89: cannam@89: Link with pa_dither.c and pa_converters.c cannam@89: cannam@89: see http://www.portaudio.com/trac/wiki/V19ConvertersStatus for a discussion of this. cannam@89: */ cannam@89: /* cannam@89: * $Id: $ cannam@89: * cannam@89: * This program uses the PortAudio Portable Audio Library. cannam@89: * For more information see: http://www.portaudio.com/ cannam@89: * Copyright (c) 1999-2008 Ross Bencina and Phil Burk cannam@89: * cannam@89: * Permission is hereby granted, free of charge, to any person obtaining cannam@89: * a copy of this software and associated documentation files cannam@89: * (the "Software"), to deal in the Software without restriction, cannam@89: * including without limitation the rights to use, copy, modify, merge, cannam@89: * publish, distribute, sublicense, and/or sell copies of the Software, cannam@89: * and to permit persons to whom the Software is furnished to do so, cannam@89: * subject to the following conditions: cannam@89: * cannam@89: * The above copyright notice and this permission notice shall be cannam@89: * included in all copies or substantial portions of the Software. cannam@89: * cannam@89: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@89: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@89: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. cannam@89: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR cannam@89: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@89: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@89: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@89: */ cannam@89: cannam@89: /* cannam@89: * The text above constitutes the entire PortAudio license; however, cannam@89: * the PortAudio community also makes the following non-binding requests: cannam@89: * cannam@89: * Any person wishing to distribute modifications to the Software is cannam@89: * requested to send the modifications to the original developer so that cannam@89: * they can be incorporated into the canonical version. It is also cannam@89: * requested that these non-binding requests be included along with the cannam@89: * license above. cannam@89: */ cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: cannam@89: #include "portaudio.h" cannam@89: #include "pa_converters.h" cannam@89: #include "pa_dither.h" cannam@89: #include "pa_types.h" cannam@89: #include "pa_endianness.h" cannam@89: cannam@89: #ifndef M_PI cannam@89: #define M_PI (3.14159265) cannam@89: #endif cannam@89: cannam@89: #define MAX_PER_CHANNEL_FRAME_COUNT (2048) cannam@89: #define MAX_CHANNEL_COUNT (8) cannam@89: cannam@89: cannam@89: #define SAMPLE_FORMAT_COUNT (6) cannam@89: cannam@89: static PaSampleFormat sampleFormats_[ SAMPLE_FORMAT_COUNT ] = cannam@89: { paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8 }; /* all standard PA sample formats */ cannam@89: cannam@89: static const char* sampleFormatNames_[SAMPLE_FORMAT_COUNT] = cannam@89: { "paFloat32", "paInt32", "paInt24", "paInt16", "paInt8", "paUInt8" }; cannam@89: cannam@89: cannam@89: static const char* abbreviatedSampleFormatNames_[SAMPLE_FORMAT_COUNT] = cannam@89: { "f32", "i32", "i24", "i16", " i8", "ui8" }; cannam@89: cannam@89: cannam@89: PaError My_Pa_GetSampleSize( PaSampleFormat format ); cannam@89: cannam@89: /* cannam@89: available flags are paClipOff and paDitherOff cannam@89: clipping is usually applied for float -> int conversions cannam@89: dither is usually applied for all downconversions (ie anything but 8bit->8bit conversions cannam@89: */ cannam@89: cannam@89: static int CanClip( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat ) cannam@89: { cannam@89: if( sourceFormat == paFloat32 && destinationFormat != sourceFormat ) cannam@89: return 1; cannam@89: else cannam@89: return 0; cannam@89: } cannam@89: cannam@89: static int CanDither( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat ) cannam@89: { cannam@89: if( sourceFormat < destinationFormat && sourceFormat != paInt8 ) cannam@89: return 1; cannam@89: else cannam@89: return 0; cannam@89: } cannam@89: cannam@89: static void GenerateOneCycleSineReference( double *out, int frameCount, int strideFrames ) cannam@89: { cannam@89: int i; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = sin( ((double)i/(double)frameCount) * 2. * M_PI ); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: cannam@89: cannam@89: static void GenerateOneCycleSine( PaSampleFormat format, void *buffer, int frameCount, int strideFrames ) cannam@89: { cannam@89: switch( format ){ cannam@89: cannam@89: case paFloat32: cannam@89: { cannam@89: int i; cannam@89: float *out = (float*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = (float).9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: break; cannam@89: case paInt32: cannam@89: { cannam@89: int i; cannam@89: PaInt32 *out = (PaInt32*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: break; cannam@89: case paInt24: cannam@89: { cannam@89: int i; cannam@89: unsigned char *out = (unsigned char*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: signed long temp = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF); cannam@89: cannam@89: #if defined(PA_LITTLE_ENDIAN) cannam@89: out[0] = (unsigned char)(temp >> 8) & 0xFF; cannam@89: out[1] = (unsigned char)(temp >> 16) & 0xFF; cannam@89: out[2] = (unsigned char)(temp >> 24) & 0xFF; cannam@89: #elif defined(PA_BIG_ENDIAN) cannam@89: out[0] = (unsigned char)(temp >> 24) & 0xFF; cannam@89: out[1] = (unsigned char)(temp >> 16) & 0xFF; cannam@89: out[2] = (unsigned char)(temp >> 8) & 0xFF; cannam@89: #endif cannam@89: out += 3; cannam@89: } cannam@89: } cannam@89: break; cannam@89: case paInt16: cannam@89: { cannam@89: int i; cannam@89: PaInt16 *out = (PaInt16*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = (PaInt16)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFF ); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: break; cannam@89: case paInt8: cannam@89: { cannam@89: int i; cannam@89: signed char *out = (signed char*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = (signed char)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7F ); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: break; cannam@89: case paUInt8: cannam@89: { cannam@89: int i; cannam@89: unsigned char *out = (unsigned char*)buffer; cannam@89: for( i=0; i < frameCount; ++i ){ cannam@89: *out = (unsigned char)( .5 * (1. + (.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ))) * 0xFF ); cannam@89: out += strideFrames; cannam@89: } cannam@89: } cannam@89: break; cannam@89: } cannam@89: } cannam@89: cannam@89: int TestNonZeroPresent( void *buffer, int size ) cannam@89: { cannam@89: char *p = (char*)buffer; cannam@89: int i; cannam@89: cannam@89: for( i=0; i < size; ++i ){ cannam@89: cannam@89: if( *p != 0 ) cannam@89: return 1; cannam@89: ++p; cannam@89: } cannam@89: cannam@89: return 0; cannam@89: } cannam@89: cannam@89: float MaximumAbsDifference( float* sourceBuffer, float* referenceBuffer, int count ) cannam@89: { cannam@89: float result = 0; cannam@89: float difference; cannam@89: while( count-- ){ cannam@89: difference = fabs( *sourceBuffer++ - *referenceBuffer++ ); cannam@89: if( difference > result ) cannam@89: result = difference; cannam@89: } cannam@89: cannam@89: return result; cannam@89: } cannam@89: cannam@89: int main( const char **argv, int argc ) cannam@89: { cannam@89: PaUtilTriangularDitherGenerator ditherState; cannam@89: PaUtilConverter *converter; cannam@89: void *destinationBuffer, *sourceBuffer; cannam@89: double *referenceBuffer; cannam@89: int sourceFormatIndex, destinationFormatIndex; cannam@89: PaSampleFormat sourceFormat, destinationFormat; cannam@89: PaStreamFlags flags; cannam@89: int passFailMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination] cannam@89: float noiseAmplitudeMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination] cannam@89: float amp; cannam@89: cannam@89: #define FLAG_COMBINATION_COUNT (4) cannam@89: PaStreamFlags flagCombinations[FLAG_COMBINATION_COUNT] = { paNoFlag, paClipOff, paDitherOff, paClipOff | paDitherOff }; cannam@89: const char *flagCombinationNames[FLAG_COMBINATION_COUNT] = { "paNoFlag", "paClipOff", "paDitherOff", "paClipOff | paDitherOff" }; cannam@89: int flagCombinationIndex; cannam@89: cannam@89: PaUtil_InitializeTriangularDitherState( &ditherState ); cannam@89: cannam@89: /* allocate more than enough space, we use sizeof(float) but we need to fit any 32 bit datum */ cannam@89: cannam@89: destinationBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); cannam@89: sourceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); cannam@89: referenceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) ); cannam@89: cannam@89: cannam@89: /* the first round of tests simply iterates through the buffer combinations testing cannam@89: that putting something in gives something out */ cannam@89: cannam@89: printf( "= Sine wave in, something out =\n" ); cannam@89: cannam@89: printf( "\n" ); cannam@89: cannam@89: GenerateOneCycleSine( paFloat32, referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 ); cannam@89: cannam@89: for( flagCombinationIndex = 0; flagCombinationIndex < FLAG_COMBINATION_COUNT; ++flagCombinationIndex ){ cannam@89: flags = flagCombinations[flagCombinationIndex]; cannam@89: cannam@89: printf( "\n" ); cannam@89: printf( "== flags = %s ==\n", flagCombinationNames[flagCombinationIndex] ); cannam@89: cannam@89: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ cannam@89: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ cannam@89: sourceFormat = sampleFormats_[sourceFormatIndex]; cannam@89: destinationFormat = sampleFormats_[destinationFormatIndex]; cannam@89: //printf( "%s -> %s ", sampleFormatNames_[ sourceFormatIndex ], sampleFormatNames_[ destinationFormatIndex ] ); cannam@89: cannam@89: converter = PaUtil_SelectConverter( sourceFormat, destinationFormat, flags ); cannam@89: cannam@89: /* source is a sinewave */ cannam@89: GenerateOneCycleSine( sourceFormat, sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 ); cannam@89: cannam@89: /* zero destination */ cannam@89: memset( destinationBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) ); cannam@89: cannam@89: (*converter)( destinationBuffer, 1, sourceBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState ); cannam@89: cannam@89: /* cannam@89: Other ways we could test this would be: cannam@89: - pass a constant, check for a constant (wouldn't work with dither) cannam@89: - pass alternating +/-, check for the same... cannam@89: */ cannam@89: if( TestNonZeroPresent( destinationBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) ) ){ cannam@89: //printf( "PASSED\n" ); cannam@89: passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 1; cannam@89: }else{ cannam@89: //printf( "FAILED\n" ); cannam@89: passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 0; cannam@89: } cannam@89: cannam@89: cannam@89: /* try to measure the noise floor (comparing output signal to a float32 sine wave) */ cannam@89: cannam@89: if( passFailMatrix[sourceFormatIndex][destinationFormatIndex] ){ cannam@89: cannam@89: /* convert destination back to paFloat32 into source */ cannam@89: converter = PaUtil_SelectConverter( destinationFormat, paFloat32, paNoFlag ); cannam@89: cannam@89: memset( sourceBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) ); cannam@89: (*converter)( sourceBuffer, 1, destinationBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState ); cannam@89: cannam@89: if( TestNonZeroPresent( sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) ) ){ cannam@89: cannam@89: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = MaximumAbsDifference( (float*)sourceBuffer, (float*)referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT ); cannam@89: cannam@89: }else{ cannam@89: /* can't test noise floor because there is no conversion from dest format to float available */ cannam@89: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed cannam@89: } cannam@89: }else{ cannam@89: noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed cannam@89: } cannam@89: } cannam@89: } cannam@89: cannam@89: printf( "\n" ); cannam@89: printf( "=== Output contains non-zero data ===\n" ); cannam@89: printf( "Key: . - pass, X - fail\n" ); cannam@89: printf( "{{{\n" ); // trac preformated text tag cannam@89: printf( "in| out: " ); cannam@89: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ cannam@89: printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] ); cannam@89: } cannam@89: printf( "\n" ); cannam@89: cannam@89: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ cannam@89: printf( "%s ", abbreviatedSampleFormatNames_[sourceFormatIndex] ); cannam@89: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ cannam@89: printf( " %s ", (passFailMatrix[sourceFormatIndex][destinationFormatIndex])? " ." : " X" ); cannam@89: } cannam@89: printf( "\n" ); cannam@89: } cannam@89: printf( "}}}\n" ); // trac preformated text tag cannam@89: cannam@89: printf( "\n" ); cannam@89: printf( "=== Combined dynamic range (src->dest->float32) ===\n" ); cannam@89: printf( "Key: Noise amplitude in dBfs, X - fail (either above failed or dest->float32 failed)\n" ); cannam@89: printf( "{{{\n" ); // trac preformated text tag cannam@89: printf( "in| out: " ); cannam@89: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ cannam@89: printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] ); cannam@89: } cannam@89: printf( "\n" ); cannam@89: cannam@89: for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){ cannam@89: printf( " %s ", abbreviatedSampleFormatNames_[sourceFormatIndex] ); cannam@89: for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){ cannam@89: amp = noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex]; cannam@89: if( amp < 0. ) cannam@89: printf( " X " ); cannam@89: else cannam@89: printf( " % 6.1f ", 20.*log10(amp) ); cannam@89: } cannam@89: printf( "\n" ); cannam@89: } cannam@89: printf( "}}}\n" ); // trac preformated text tag cannam@89: } cannam@89: cannam@89: cannam@89: free( destinationBuffer ); cannam@89: free( sourceBuffer ); cannam@89: free( referenceBuffer ); cannam@89: } cannam@89: cannam@89: // copied here for now otherwise we need to include the world just for this function. cannam@89: PaError My_Pa_GetSampleSize( PaSampleFormat format ) cannam@89: { cannam@89: int result; cannam@89: cannam@89: switch( format & ~paNonInterleaved ) cannam@89: { cannam@89: cannam@89: case paUInt8: cannam@89: case paInt8: cannam@89: result = 1; cannam@89: break; cannam@89: cannam@89: case paInt16: cannam@89: result = 2; cannam@89: break; cannam@89: cannam@89: case paInt24: cannam@89: result = 3; cannam@89: break; cannam@89: cannam@89: case paFloat32: cannam@89: case paInt32: cannam@89: result = 4; cannam@89: break; cannam@89: cannam@89: default: cannam@89: result = paSampleFormatNotSupported; cannam@89: break; cannam@89: } cannam@89: cannam@89: return (PaError) result; cannam@89: }