annotate src/portaudio/test/patest_converters.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents e13257ea84a4
children
rev   line source
Chris@4 1 /** @file patest_converters.c
Chris@4 2 @ingroup test_src
Chris@4 3 @brief Tests the converter functions in pa_converters.c
Chris@4 4 @author Ross Bencina <rossb@audiomulch.com>
Chris@4 5
Chris@4 6 Link with pa_dither.c and pa_converters.c
Chris@4 7
Chris@4 8 see http://www.portaudio.com/trac/wiki/V19ConvertersStatus for a discussion of this.
Chris@4 9 */
Chris@4 10 /*
Chris@4 11 * $Id: $
Chris@4 12 *
Chris@4 13 * This program uses the PortAudio Portable Audio Library.
Chris@4 14 * For more information see: http://www.portaudio.com/
Chris@4 15 * Copyright (c) 1999-2008 Ross Bencina and Phil Burk
Chris@4 16 *
Chris@4 17 * Permission is hereby granted, free of charge, to any person obtaining
Chris@4 18 * a copy of this software and associated documentation files
Chris@4 19 * (the "Software"), to deal in the Software without restriction,
Chris@4 20 * including without limitation the rights to use, copy, modify, merge,
Chris@4 21 * publish, distribute, sublicense, and/or sell copies of the Software,
Chris@4 22 * and to permit persons to whom the Software is furnished to do so,
Chris@4 23 * subject to the following conditions:
Chris@4 24 *
Chris@4 25 * The above copyright notice and this permission notice shall be
Chris@4 26 * included in all copies or substantial portions of the Software.
Chris@4 27 *
Chris@4 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@4 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@4 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chris@4 31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
Chris@4 32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@4 33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@4 34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@4 35 */
Chris@4 36
Chris@4 37 /*
Chris@4 38 * The text above constitutes the entire PortAudio license; however,
Chris@4 39 * the PortAudio community also makes the following non-binding requests:
Chris@4 40 *
Chris@4 41 * Any person wishing to distribute modifications to the Software is
Chris@4 42 * requested to send the modifications to the original developer so that
Chris@4 43 * they can be incorporated into the canonical version. It is also
Chris@4 44 * requested that these non-binding requests be included along with the
Chris@4 45 * license above.
Chris@4 46 */
Chris@4 47 #include <stdio.h>
Chris@4 48 #include <stdlib.h>
Chris@4 49 #include <string.h>
Chris@4 50 #include <math.h>
Chris@4 51
Chris@4 52 #include "portaudio.h"
Chris@4 53 #include "pa_converters.h"
Chris@4 54 #include "pa_dither.h"
Chris@4 55 #include "pa_types.h"
Chris@4 56 #include "pa_endianness.h"
Chris@4 57
Chris@4 58 #ifndef M_PI
Chris@4 59 #define M_PI (3.14159265)
Chris@4 60 #endif
Chris@4 61
Chris@4 62 #define MAX_PER_CHANNEL_FRAME_COUNT (2048)
Chris@4 63 #define MAX_CHANNEL_COUNT (8)
Chris@4 64
Chris@4 65
Chris@4 66 #define SAMPLE_FORMAT_COUNT (6)
Chris@4 67
Chris@4 68 static PaSampleFormat sampleFormats_[ SAMPLE_FORMAT_COUNT ] =
Chris@4 69 { paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8 }; /* all standard PA sample formats */
Chris@4 70
Chris@4 71 static const char* sampleFormatNames_[SAMPLE_FORMAT_COUNT] =
Chris@4 72 { "paFloat32", "paInt32", "paInt24", "paInt16", "paInt8", "paUInt8" };
Chris@4 73
Chris@4 74
Chris@4 75 static const char* abbreviatedSampleFormatNames_[SAMPLE_FORMAT_COUNT] =
Chris@4 76 { "f32", "i32", "i24", "i16", " i8", "ui8" };
Chris@4 77
Chris@4 78
Chris@4 79 PaError My_Pa_GetSampleSize( PaSampleFormat format );
Chris@4 80
Chris@4 81 /*
Chris@4 82 available flags are paClipOff and paDitherOff
Chris@4 83 clipping is usually applied for float -> int conversions
Chris@4 84 dither is usually applied for all downconversions (ie anything but 8bit->8bit conversions
Chris@4 85 */
Chris@4 86
Chris@4 87 static int CanClip( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat )
Chris@4 88 {
Chris@4 89 if( sourceFormat == paFloat32 && destinationFormat != sourceFormat )
Chris@4 90 return 1;
Chris@4 91 else
Chris@4 92 return 0;
Chris@4 93 }
Chris@4 94
Chris@4 95 static int CanDither( PaSampleFormat sourceFormat, PaSampleFormat destinationFormat )
Chris@4 96 {
Chris@4 97 if( sourceFormat < destinationFormat && sourceFormat != paInt8 )
Chris@4 98 return 1;
Chris@4 99 else
Chris@4 100 return 0;
Chris@4 101 }
Chris@4 102
Chris@4 103 static void GenerateOneCycleSineReference( double *out, int frameCount, int strideFrames )
Chris@4 104 {
Chris@4 105 int i;
Chris@4 106 for( i=0; i < frameCount; ++i ){
Chris@4 107 *out = sin( ((double)i/(double)frameCount) * 2. * M_PI );
Chris@4 108 out += strideFrames;
Chris@4 109 }
Chris@4 110 }
Chris@4 111
Chris@4 112
Chris@4 113 static void GenerateOneCycleSine( PaSampleFormat format, void *buffer, int frameCount, int strideFrames )
Chris@4 114 {
Chris@4 115 switch( format ){
Chris@4 116
Chris@4 117 case paFloat32:
Chris@4 118 {
Chris@4 119 int i;
Chris@4 120 float *out = (float*)buffer;
Chris@4 121 for( i=0; i < frameCount; ++i ){
Chris@4 122 *out = (float).9 * sin( ((double)i/(double)frameCount) * 2. * M_PI );
Chris@4 123 out += strideFrames;
Chris@4 124 }
Chris@4 125 }
Chris@4 126 break;
Chris@4 127 case paInt32:
Chris@4 128 {
Chris@4 129 int i;
Chris@4 130 PaInt32 *out = (PaInt32*)buffer;
Chris@4 131 for( i=0; i < frameCount; ++i ){
Chris@4 132 *out = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF);
Chris@4 133 out += strideFrames;
Chris@4 134 }
Chris@4 135 }
Chris@4 136 break;
Chris@4 137 case paInt24:
Chris@4 138 {
Chris@4 139 int i;
Chris@4 140 unsigned char *out = (unsigned char*)buffer;
Chris@4 141 for( i=0; i < frameCount; ++i ){
Chris@4 142 signed long temp = (PaInt32)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFFFFFF);
Chris@4 143
Chris@4 144 #if defined(PA_LITTLE_ENDIAN)
Chris@4 145 out[0] = (unsigned char)(temp >> 8) & 0xFF;
Chris@4 146 out[1] = (unsigned char)(temp >> 16) & 0xFF;
Chris@4 147 out[2] = (unsigned char)(temp >> 24) & 0xFF;
Chris@4 148 #elif defined(PA_BIG_ENDIAN)
Chris@4 149 out[0] = (unsigned char)(temp >> 24) & 0xFF;
Chris@4 150 out[1] = (unsigned char)(temp >> 16) & 0xFF;
Chris@4 151 out[2] = (unsigned char)(temp >> 8) & 0xFF;
Chris@4 152 #endif
Chris@4 153 out += 3;
Chris@4 154 }
Chris@4 155 }
Chris@4 156 break;
Chris@4 157 case paInt16:
Chris@4 158 {
Chris@4 159 int i;
Chris@4 160 PaInt16 *out = (PaInt16*)buffer;
Chris@4 161 for( i=0; i < frameCount; ++i ){
Chris@4 162 *out = (PaInt16)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7FFF );
Chris@4 163 out += strideFrames;
Chris@4 164 }
Chris@4 165 }
Chris@4 166 break;
Chris@4 167 case paInt8:
Chris@4 168 {
Chris@4 169 int i;
Chris@4 170 signed char *out = (signed char*)buffer;
Chris@4 171 for( i=0; i < frameCount; ++i ){
Chris@4 172 *out = (signed char)(.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ) * 0x7F );
Chris@4 173 out += strideFrames;
Chris@4 174 }
Chris@4 175 }
Chris@4 176 break;
Chris@4 177 case paUInt8:
Chris@4 178 {
Chris@4 179 int i;
Chris@4 180 unsigned char *out = (unsigned char*)buffer;
Chris@4 181 for( i=0; i < frameCount; ++i ){
Chris@4 182 *out = (unsigned char)( .5 * (1. + (.9 * sin( ((double)i/(double)frameCount) * 2. * M_PI ))) * 0xFF );
Chris@4 183 out += strideFrames;
Chris@4 184 }
Chris@4 185 }
Chris@4 186 break;
Chris@4 187 }
Chris@4 188 }
Chris@4 189
Chris@4 190 int TestNonZeroPresent( void *buffer, int size )
Chris@4 191 {
Chris@4 192 char *p = (char*)buffer;
Chris@4 193 int i;
Chris@4 194
Chris@4 195 for( i=0; i < size; ++i ){
Chris@4 196
Chris@4 197 if( *p != 0 )
Chris@4 198 return 1;
Chris@4 199 ++p;
Chris@4 200 }
Chris@4 201
Chris@4 202 return 0;
Chris@4 203 }
Chris@4 204
Chris@4 205 float MaximumAbsDifference( float* sourceBuffer, float* referenceBuffer, int count )
Chris@4 206 {
Chris@4 207 float result = 0;
Chris@4 208 float difference;
Chris@4 209 while( count-- ){
Chris@4 210 difference = fabs( *sourceBuffer++ - *referenceBuffer++ );
Chris@4 211 if( difference > result )
Chris@4 212 result = difference;
Chris@4 213 }
Chris@4 214
Chris@4 215 return result;
Chris@4 216 }
Chris@4 217
Chris@4 218 int main( const char **argv, int argc )
Chris@4 219 {
Chris@4 220 PaUtilTriangularDitherGenerator ditherState;
Chris@4 221 PaUtilConverter *converter;
Chris@4 222 void *destinationBuffer, *sourceBuffer;
Chris@4 223 double *referenceBuffer;
Chris@4 224 int sourceFormatIndex, destinationFormatIndex;
Chris@4 225 PaSampleFormat sourceFormat, destinationFormat;
Chris@4 226 PaStreamFlags flags;
Chris@4 227 int passFailMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination]
Chris@4 228 float noiseAmplitudeMatrix[SAMPLE_FORMAT_COUNT][SAMPLE_FORMAT_COUNT]; // [source][destination]
Chris@4 229 float amp;
Chris@4 230
Chris@4 231 #define FLAG_COMBINATION_COUNT (4)
Chris@4 232 PaStreamFlags flagCombinations[FLAG_COMBINATION_COUNT] = { paNoFlag, paClipOff, paDitherOff, paClipOff | paDitherOff };
Chris@4 233 const char *flagCombinationNames[FLAG_COMBINATION_COUNT] = { "paNoFlag", "paClipOff", "paDitherOff", "paClipOff | paDitherOff" };
Chris@4 234 int flagCombinationIndex;
Chris@4 235
Chris@4 236 PaUtil_InitializeTriangularDitherState( &ditherState );
Chris@4 237
Chris@4 238 /* allocate more than enough space, we use sizeof(float) but we need to fit any 32 bit datum */
Chris@4 239
Chris@4 240 destinationBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) );
Chris@4 241 sourceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) );
Chris@4 242 referenceBuffer = (void*)malloc( MAX_PER_CHANNEL_FRAME_COUNT * MAX_CHANNEL_COUNT * sizeof(float) );
Chris@4 243
Chris@4 244
Chris@4 245 /* the first round of tests simply iterates through the buffer combinations testing
Chris@4 246 that putting something in gives something out */
Chris@4 247
Chris@4 248 printf( "= Sine wave in, something out =\n" );
Chris@4 249
Chris@4 250 printf( "\n" );
Chris@4 251
Chris@4 252 GenerateOneCycleSine( paFloat32, referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 );
Chris@4 253
Chris@4 254 for( flagCombinationIndex = 0; flagCombinationIndex < FLAG_COMBINATION_COUNT; ++flagCombinationIndex ){
Chris@4 255 flags = flagCombinations[flagCombinationIndex];
Chris@4 256
Chris@4 257 printf( "\n" );
Chris@4 258 printf( "== flags = %s ==\n", flagCombinationNames[flagCombinationIndex] );
Chris@4 259
Chris@4 260 for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){
Chris@4 261 for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){
Chris@4 262 sourceFormat = sampleFormats_[sourceFormatIndex];
Chris@4 263 destinationFormat = sampleFormats_[destinationFormatIndex];
Chris@4 264 //printf( "%s -> %s ", sampleFormatNames_[ sourceFormatIndex ], sampleFormatNames_[ destinationFormatIndex ] );
Chris@4 265
Chris@4 266 converter = PaUtil_SelectConverter( sourceFormat, destinationFormat, flags );
Chris@4 267
Chris@4 268 /* source is a sinewave */
Chris@4 269 GenerateOneCycleSine( sourceFormat, sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT, 1 );
Chris@4 270
Chris@4 271 /* zero destination */
Chris@4 272 memset( destinationBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) );
Chris@4 273
Chris@4 274 (*converter)( destinationBuffer, 1, sourceBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState );
Chris@4 275
Chris@4 276 /*
Chris@4 277 Other ways we could test this would be:
Chris@4 278 - pass a constant, check for a constant (wouldn't work with dither)
Chris@4 279 - pass alternating +/-, check for the same...
Chris@4 280 */
Chris@4 281 if( TestNonZeroPresent( destinationBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( destinationFormat ) ) ){
Chris@4 282 //printf( "PASSED\n" );
Chris@4 283 passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 1;
Chris@4 284 }else{
Chris@4 285 //printf( "FAILED\n" );
Chris@4 286 passFailMatrix[sourceFormatIndex][destinationFormatIndex] = 0;
Chris@4 287 }
Chris@4 288
Chris@4 289
Chris@4 290 /* try to measure the noise floor (comparing output signal to a float32 sine wave) */
Chris@4 291
Chris@4 292 if( passFailMatrix[sourceFormatIndex][destinationFormatIndex] ){
Chris@4 293
Chris@4 294 /* convert destination back to paFloat32 into source */
Chris@4 295 converter = PaUtil_SelectConverter( destinationFormat, paFloat32, paNoFlag );
Chris@4 296
Chris@4 297 memset( sourceBuffer, 0, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) );
Chris@4 298 (*converter)( sourceBuffer, 1, destinationBuffer, 1, MAX_PER_CHANNEL_FRAME_COUNT, &ditherState );
Chris@4 299
Chris@4 300 if( TestNonZeroPresent( sourceBuffer, MAX_PER_CHANNEL_FRAME_COUNT * My_Pa_GetSampleSize( paFloat32 ) ) ){
Chris@4 301
Chris@4 302 noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = MaximumAbsDifference( (float*)sourceBuffer, (float*)referenceBuffer, MAX_PER_CHANNEL_FRAME_COUNT );
Chris@4 303
Chris@4 304 }else{
Chris@4 305 /* can't test noise floor because there is no conversion from dest format to float available */
Chris@4 306 noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed
Chris@4 307 }
Chris@4 308 }else{
Chris@4 309 noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex] = -1; // mark as failed
Chris@4 310 }
Chris@4 311 }
Chris@4 312 }
Chris@4 313
Chris@4 314 printf( "\n" );
Chris@4 315 printf( "=== Output contains non-zero data ===\n" );
Chris@4 316 printf( "Key: . - pass, X - fail\n" );
Chris@4 317 printf( "{{{\n" ); // trac preformated text tag
Chris@4 318 printf( "in| out: " );
Chris@4 319 for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){
Chris@4 320 printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] );
Chris@4 321 }
Chris@4 322 printf( "\n" );
Chris@4 323
Chris@4 324 for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){
Chris@4 325 printf( "%s ", abbreviatedSampleFormatNames_[sourceFormatIndex] );
Chris@4 326 for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){
Chris@4 327 printf( " %s ", (passFailMatrix[sourceFormatIndex][destinationFormatIndex])? " ." : " X" );
Chris@4 328 }
Chris@4 329 printf( "\n" );
Chris@4 330 }
Chris@4 331 printf( "}}}\n" ); // trac preformated text tag
Chris@4 332
Chris@4 333 printf( "\n" );
Chris@4 334 printf( "=== Combined dynamic range (src->dest->float32) ===\n" );
Chris@4 335 printf( "Key: Noise amplitude in dBfs, X - fail (either above failed or dest->float32 failed)\n" );
Chris@4 336 printf( "{{{\n" ); // trac preformated text tag
Chris@4 337 printf( "in| out: " );
Chris@4 338 for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){
Chris@4 339 printf( " %s ", abbreviatedSampleFormatNames_[destinationFormatIndex] );
Chris@4 340 }
Chris@4 341 printf( "\n" );
Chris@4 342
Chris@4 343 for( sourceFormatIndex = 0; sourceFormatIndex < SAMPLE_FORMAT_COUNT; ++sourceFormatIndex ){
Chris@4 344 printf( " %s ", abbreviatedSampleFormatNames_[sourceFormatIndex] );
Chris@4 345 for( destinationFormatIndex = 0; destinationFormatIndex < SAMPLE_FORMAT_COUNT; ++destinationFormatIndex ){
Chris@4 346 amp = noiseAmplitudeMatrix[sourceFormatIndex][destinationFormatIndex];
Chris@4 347 if( amp < 0. )
Chris@4 348 printf( " X " );
Chris@4 349 else
Chris@4 350 printf( " % 6.1f ", 20.*log10(amp) );
Chris@4 351 }
Chris@4 352 printf( "\n" );
Chris@4 353 }
Chris@4 354 printf( "}}}\n" ); // trac preformated text tag
Chris@4 355 }
Chris@4 356
Chris@4 357
Chris@4 358 free( destinationBuffer );
Chris@4 359 free( sourceBuffer );
Chris@4 360 free( referenceBuffer );
Chris@4 361 }
Chris@4 362
Chris@4 363 // copied here for now otherwise we need to include the world just for this function.
Chris@4 364 PaError My_Pa_GetSampleSize( PaSampleFormat format )
Chris@4 365 {
Chris@4 366 int result;
Chris@4 367
Chris@4 368 switch( format & ~paNonInterleaved )
Chris@4 369 {
Chris@4 370
Chris@4 371 case paUInt8:
Chris@4 372 case paInt8:
Chris@4 373 result = 1;
Chris@4 374 break;
Chris@4 375
Chris@4 376 case paInt16:
Chris@4 377 result = 2;
Chris@4 378 break;
Chris@4 379
Chris@4 380 case paInt24:
Chris@4 381 result = 3;
Chris@4 382 break;
Chris@4 383
Chris@4 384 case paFloat32:
Chris@4 385 case paInt32:
Chris@4 386 result = 4;
Chris@4 387 break;
Chris@4 388
Chris@4 389 default:
Chris@4 390 result = paSampleFormatNotSupported;
Chris@4 391 break;
Chris@4 392 }
Chris@4 393
Chris@4 394 return (PaError) result;
Chris@4 395 }