annotate src/portaudio_20140130/test/patest_converters.c @ 81:7029a4916348

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