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