annotate src/portaudio_20161030_catalina_patch/test/patest_converters.c @ 169:223a55898ab9 tip default

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