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