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