summaryrefslogtreecommitdiff
path: root/3rdparty/portaudio/test/patest_dsound_find_best_latency_params.c
blob: 1c074ab16cf458f136fe79d4d6e232a04236243f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * $Id: $
 * Portable Audio I/O Library
 * Windows DirectSound low level buffer user guided parameters search
 *
 * Copyright (c) 2010-2011 Ross Bencina
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * The text above constitutes the entire PortAudio license; however,
 * the PortAudio community also makes the following non-binding requests:
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version. It is also
 * requested that these non-binding requests be included along with the
 * license above.
 */

#include <stdio.h>
#include <time.h>
#include <math.h>

#define  _WIN32_WINNT 0x0501 /* for GetNativeSystemInfo */
#include <windows.h>
//#include <mmsystem.h>   /* required when using pa_win_wmme.h */

#include <conio.h>      /* for _getch */


#include "portaudio.h"
#include "pa_win_ds.h"


#define DEFAULT_SAMPLE_RATE             (44100.)

#ifndef M_PI
#define M_PI  (3.14159265)
#endif

#define TABLE_SIZE              (2048)

#define CHANNEL_COUNT           (2)


/*******************************************************************/
/* functions to query and print Windows version information */

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

LPFN_ISWOW64PROCESS fnIsWow64Process;

static BOOL IsWow64()
{
    BOOL bIsWow64 = FALSE;

    //IsWow64Process is not available on all supported versions of Windows.
    //Use GetModuleHandle to get a handle to the DLL that contains the function
    //and GetProcAddress to get a pointer to the function if available.

    fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
        GetModuleHandle(TEXT("kernel32")),"IsWow64Process" );

    if(NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
        {
            //handle error
        }
    }
    return bIsWow64;
}

static void printWindowsVersionInfo( FILE *fp )
{
    OSVERSIONINFOEX osVersionInfoEx;
    SYSTEM_INFO systemInfo;
    const char *osName = "Unknown";
    const char *osProductType = "";
    const char *processorArchitecture = "Unknown";

    memset( &osVersionInfoEx, 0, sizeof(OSVERSIONINFOEX) );
    osVersionInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx( &osVersionInfoEx );


    if( osVersionInfoEx.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ){
        switch( osVersionInfoEx.dwMinorVersion ){
            case 0: osName = "Windows 95"; break;
            case 10: osName = "Windows 98"; break;  // could also be 98SE (I've seen code discriminate based
                                                    // on osInfo.Version.Revision.ToString() == "2222A")
            case 90: osName = "Windows Me"; break;
        }
    }else if( osVersionInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT ){
        switch( osVersionInfoEx.dwMajorVersion ){
            case 3: osName = "Windows NT 3.51"; break;
            case 4: osName = "Windows NT 4.0"; break;
            case 5: switch( osVersionInfoEx.dwMinorVersion ){
                        case 0: osName = "Windows 2000"; break;
                        case 1: osName = "Windows XP"; break;
                        case 2:
                            if( osVersionInfoEx.wSuiteMask & 0x00008000 /*VER_SUITE_WH_SERVER*/ ){
                                osName = "Windows Home Server";
                            }else{
                                if( osVersionInfoEx.wProductType == VER_NT_WORKSTATION ){
                                    osName = "Windows XP Professional x64 Edition (?)";
                                }else{
                                    if( GetSystemMetrics(/*SM_SERVERR2*/89) == 0 )
                                        osName = "Windows Server 2003";
                                    else
                                        osName = "Windows Server 2003 R2";
                                }
                            }break;
                    }break;
            case 6:switch( osVersionInfoEx.dwMinorVersion ){
                        case 0:
                            if( osVersionInfoEx.wProductType == VER_NT_WORKSTATION )
                                osName = "Windows Vista";
                            else
                                osName = "Windows Server 2008";
                            break;
                        case 1:
                            if( osVersionInfoEx.wProductType == VER_NT_WORKSTATION )
                                osName = "Windows 7";
                            else
                                osName = "Windows Server 2008 R2";
                            break;
                    }break;
        }
    }

    if(osVersionInfoEx.dwMajorVersion == 4)
    {
        if(osVersionInfoEx.wProductType == VER_NT_WORKSTATION)
            osProductType = "Workstation";
        else if(osVersionInfoEx.wProductType == VER_NT_SERVER)
            osProductType = "Server";
    }
    else if(osVersionInfoEx.dwMajorVersion == 5)
    {
        if(osVersionInfoEx.wProductType == VER_NT_WORKSTATION)
        {
            if((osVersionInfoEx.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
                osProductType = "Home Edition"; // Windows XP Home Edition
            else
                osProductType = "Professional"; // Windows XP / Windows 2000 Professional
        }
        else if(osVersionInfoEx.wProductType == VER_NT_SERVER)
        {
            if(osVersionInfoEx.dwMinorVersion == 0)
            {
                if((osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
                    osProductType = "Datacenter Server"; // Windows 2000 Datacenter Server
                else if((osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
                    osProductType = "Advanced Server"; // Windows 2000 Advanced Server
                else
                    osProductType = "Server"; // Windows 2000 Server
            }
        }
        else
        {
            if((osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
                osProductType = "Datacenter Edition"; // Windows Server 2003 Datacenter Edition
            else if((osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
                osProductType = "Enterprise Edition"; // Windows Server 2003 Enterprise Edition
            else if((osVersionInfoEx.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
                osProductType = "Web Edition"; // Windows Server 2003 Web Edition
            else
                osProductType = "Standard Edition"; // Windows Server 2003 Standard Edition
        }
    }

    memset( &systemInfo, 0, sizeof(SYSTEM_INFO) );
    GetNativeSystemInfo( &systemInfo );

    if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL )
        processorArchitecture = "x86";
    else if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 )
        processorArchitecture = "x64";
    else if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
        processorArchitecture = "Itanium";


    fprintf( fp, "OS name and edition: %s %s\n", osName, osProductType );
    fprintf( fp, "OS version: %d.%d.%d %S\n",
                osVersionInfoEx.dwMajorVersion, osVersionInfoEx.dwMinorVersion,
                osVersionInfoEx.dwBuildNumber, osVersionInfoEx.szCSDVersion );
    fprintf( fp, "Processor architecture: %s\n", processorArchitecture );
    fprintf( fp, "WoW64 process: %s\n", IsWow64() ? "Yes" : "No" );
}

static void printTimeAndDate( FILE *fp )
{
    struct tm *local;
    time_t t;

    t = time(NULL);
    local = localtime(&t);
    fprintf(fp, "Local time and date: %s", asctime(local));
    local = gmtime(&t);
    fprintf(fp, "UTC time and date: %s", asctime(local));
}

/*******************************************************************/

typedef struct
{
    float sine[TABLE_SIZE];
    double phase;
    double phaseIncrement;
    volatile int fadeIn;
    volatile int fadeOut;
    double amp;
}
paTestData;

static paTestData data;

/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int patestCallback( const void *inputBuffer, void *outputBuffer,
                            unsigned long framesPerBuffer,
                            const PaStreamCallbackTimeInfo* timeInfo,
                            PaStreamCallbackFlags statusFlags,
                            void *userData )
{
    paTestData *data = (paTestData*)userData;
    float *out = (float*)outputBuffer;
    unsigned long i,j;

    (void) timeInfo; /* Prevent unused variable warnings. */
    (void) statusFlags;
    (void) inputBuffer;

    for( i=0; i<framesPerBuffer; i++ )
    {
        float x = data->sine[(int)data->phase];
        data->phase += data->phaseIncrement;
        if( data->phase >= TABLE_SIZE ){
            data->phase -= TABLE_SIZE;
        }

        x *= data->amp;
        if( data->fadeIn ){
            data->amp += .001;
            if( data->amp >= 1. )
                data->fadeIn = 0;
        }else if( data->fadeOut ){
            if( data->amp > 0 )
                data->amp -= .001;
        }

        for( j = 0; j < CHANNEL_COUNT; ++j ){
            *out++ = x;
        }
    }

    if( data->amp > 0 )
        return paContinue;
    else
        return paComplete;
}


#define YES     1
#define NO      0


static int playUntilKeyPress( int deviceIndex, float sampleRate,
                             int framesPerUserBuffer, int framesPerDSoundBuffer )
{
    PaStreamParameters outputParameters;
    PaWinDirectSoundStreamInfo directSoundStreamInfo;
    PaStream *stream;
    PaError err;
    int c;

    outputParameters.device = deviceIndex;
    outputParameters.channelCount = CHANNEL_COUNT;
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point processing */
    outputParameters.suggestedLatency = 0; /*Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;*/
    outputParameters.hostApiSpecificStreamInfo = NULL;

    directSoundStreamInfo.size = sizeof(PaWinDirectSoundStreamInfo);
    directSoundStreamInfo.hostApiType = paDirectSound;
    directSoundStreamInfo.version = 2;
    directSoundStreamInfo.flags = paWinDirectSoundUseLowLevelLatencyParameters;
    directSoundStreamInfo.framesPerBuffer = framesPerDSoundBuffer;
    outputParameters.hostApiSpecificStreamInfo = &directSoundStreamInfo;

    err = Pa_OpenStream(
              &stream,
              NULL, /* no input */
              &outputParameters,
              sampleRate,
              framesPerUserBuffer,
              paClipOff | paPrimeOutputBuffersUsingStreamCallback,      /* we won't output out of range samples so don't bother clipping them */
              patestCallback,
              &data );
    if( err != paNoError ) goto error;

    data.amp = 0;
    data.fadeIn = 1;
    data.fadeOut = 0;
    data.phase = 0;
    data.phaseIncrement = 15 + ((rand()%100) / 10); // randomise pitch

    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;


    do{
        printf( "Trying buffer size %d.\nIf it sounds smooth (without clicks or glitches) press 'y', if it sounds bad press 'n' ('q' to quit)\n", framesPerDSoundBuffer );
        c = tolower(_getch());
        if( c == 'q' ){
            Pa_Terminate();
            exit(0);
        }
    }while( c != 'y' && c != 'n' );

    data.fadeOut = 1;
    while( Pa_IsStreamActive(stream) == 1 )
        Pa_Sleep( 100 );

    err = Pa_StopStream( stream );
    if( err != paNoError ) goto error;

    err = Pa_CloseStream( stream );
    if( err != paNoError ) goto error;

    return (c == 'y') ? YES : NO;

error:
    return err;
}

/*******************************************************************/
static void usage( int dsoundHostApiIndex )
{
    int i;

    fprintf( stderr, "PortAudio DirectSound output latency user guided test\n" );
    fprintf( stderr, "Usage: x.exe dsound-device-index [sampleRate]\n" );
    fprintf( stderr, "Invalid device index. Use one of these:\n" );
    for( i=0; i < Pa_GetDeviceCount(); ++i ){

        if( Pa_GetDeviceInfo(i)->hostApi == dsoundHostApiIndex && Pa_GetDeviceInfo(i)->maxOutputChannels > 0  )
            fprintf( stderr, "%d (%s)\n", i, Pa_GetDeviceInfo(i)->name );
    }
    Pa_Terminate();
    exit(-1);
}

/*
    ideas:
        o- could be testing with 80% CPU load
        o- could test with different channel counts
*/

int main(int argc, char* argv[])
{
    PaError err;
    int i;
    int deviceIndex;
    int dsoundBufferSize, smallestWorkingBufferSize;
    int smallestWorkingBufferingLatencyFrames;
    int min, max, mid;
    int testResult;
    FILE *resultsFp;
    int dsoundHostApiIndex;
    const PaHostApiInfo *dsoundHostApiInfo;
    double sampleRate = DEFAULT_SAMPLE_RATE;

    err = Pa_Initialize();
    if( err != paNoError ) goto error;

    dsoundHostApiIndex = Pa_HostApiTypeIdToHostApiIndex( paDirectSound );
    dsoundHostApiInfo = Pa_GetHostApiInfo( dsoundHostApiIndex );

    if( argc > 3 )
        usage(dsoundHostApiIndex);

    deviceIndex = dsoundHostApiInfo->defaultOutputDevice;
    if( argc >= 2 ){
        deviceIndex = -1;
        if( sscanf( argv[1], "%d", &deviceIndex ) != 1 )
            usage(dsoundHostApiInfo);
        if( deviceIndex < 0 || deviceIndex >= Pa_GetDeviceCount() || Pa_GetDeviceInfo(deviceIndex)->hostApi != dsoundHostApiIndex ){
            usage(dsoundHostApiInfo);
        }
    }

    printf( "Using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name );

    if( argc >= 3 ){
        if( sscanf( argv[2], "%lf", &sampleRate ) != 1 )
            usage(dsoundHostApiIndex);
    }

    printf( "Testing with sample rate %f.\n", (float)sampleRate );



    /* initialise sinusoidal wavetable */
    for( i=0; i<TABLE_SIZE; i++ )
    {
        data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
    }

    data.phase = 0;

    resultsFp = fopen( "results.txt", "at" );
    fprintf( resultsFp, "*** DirectSound smallest working output buffer sizes\n" );

    printTimeAndDate( resultsFp );
    printWindowsVersionInfo( resultsFp );

    fprintf( resultsFp, "audio device: %s\n", Pa_GetDeviceInfo( deviceIndex )->name );
    fflush( resultsFp );

    fprintf( resultsFp, "Sample rate: %f\n", (float)sampleRate );
    fprintf( resultsFp, "Smallest working buffer size (frames), Smallest working buffering latency (frames), Smallest working buffering latency (Seconds)\n" );


    /*
        Binary search after Niklaus Wirth
        from http://en.wikipedia.org/wiki/Binary_search_algorithm#The_algorithm
     */
    min = 1;
    max = (int)(sampleRate * .3);   /* we assume that this size works 300ms */
    smallestWorkingBufferSize = 0;

    do{
        mid = min + ((max - min) / 2);

        dsoundBufferSize = mid;
        testResult = playUntilKeyPress( deviceIndex, sampleRate, 0, dsoundBufferSize );

        if( testResult == YES ){
            max = mid - 1;
            smallestWorkingBufferSize = dsoundBufferSize;
        }else{
            min = mid + 1;
        }

    }while( (min <= max) && (testResult == YES || testResult == NO) );

    smallestWorkingBufferingLatencyFrames = smallestWorkingBufferSize; /* not strictly true, but we're using an unspecified callback size, so kind of */

    printf( "Smallest working buffer size is: %d\n", smallestWorkingBufferSize );
    printf( "Corresponding to buffering latency of %d frames, or %f seconds.\n", smallestWorkingBufferingLatencyFrames, smallestWorkingBufferingLatencyFrames / sampleRate );

    fprintf( resultsFp, "%d, %d, %f\n", smallestWorkingBufferSize, smallestWorkingBufferingLatencyFrames, smallestWorkingBufferingLatencyFrames / sampleRate );
    fflush( resultsFp );


    /* power of 2 test. iterate to the smallest power of two that works */

    smallestWorkingBufferSize = 0;
    dsoundBufferSize = 64;

    do{
        testResult = playUntilKeyPress( deviceIndex, sampleRate, 0, dsoundBufferSize );

        if( testResult == YES ){
            smallestWorkingBufferSize = dsoundBufferSize;
        }else{
            dsoundBufferSize *= 2;
        }

    }while( (dsoundBufferSize <= (int)(sampleRate * .3)) && testResult == NO );

    smallestWorkingBufferingLatencyFrames = smallestWorkingBufferSize; /* not strictly true, but we're using an unspecified callback size, so kind of */

    fprintf( resultsFp, "%d, %d, %f\n", smallestWorkingBufferSize, smallestWorkingBufferingLatencyFrames, smallestWorkingBufferingLatencyFrames / sampleRate );
    fflush( resultsFp );


    fprintf( resultsFp, "###\n" );
    fclose( resultsFp );

    Pa_Terminate();
    printf("Test finished.\n");

    return err;
error:
    Pa_Terminate();
    fprintf( stderr, "An error occurred while using the PortAudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return err;
}