annotate src/portaudio_20140130/test/patest_converters.c @ 167:bd3cc4d1df30

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