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