root/portaudio/branches/V18.1/pa_tests/pa_minlat.c

Revision 161, 6.4 KB (checked in by philburk, 8 years ago)

Use odd table size so it doesn't seem to work when it actually fails.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 * paminlat.c
4 * Experiment with different numbers of buffers to determine the
5 * minimum latency for a computer.
6 *
7 * Author: Phil Burk  http://www.softsynth.com
8 *
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37#include <stdio.h>
38#include <stdlib.h>
39#include <math.h>
40#include "portaudio.h"
41
42#ifndef M_PI
43#define M_PI  (3.14159265)
44#endif
45#define TWOPI (M_PI * 2.0)
46
47#define DEFAULT_BUFFER_SIZE   (64)
48
49typedef struct
50{
51    double left_phase;
52    double right_phase;
53}
54paTestData;
55
56/* Very simple synthesis routine to generate two sine waves. */
57static int paminlatCallback( void *inputBuffer, void *outputBuffer,
58                             unsigned long framesPerBuffer,
59                             PaTimestamp outTime, void *userData )
60{
61    paTestData *data = (paTestData*)userData;
62    float *out = (float*)outputBuffer;
63    unsigned int i;
64    double left_phaseInc = 0.02;
65    double right_phaseInc = 0.06;
66
67    double left_phase = data->left_phase;
68    double right_phase = data->right_phase;
69
70    for( i=0; i<framesPerBuffer; i++ )
71    {
72        left_phase += left_phaseInc;
73        if( left_phase > TWOPI ) left_phase -= TWOPI;
74        *out++ = (float) sin( left_phase );
75
76        right_phase += right_phaseInc;
77        if( right_phase > TWOPI ) right_phase -= TWOPI;
78        *out++ = (float) sin( right_phase );
79    }
80
81    data->left_phase = left_phase;
82    data->right_phase = right_phase;
83    return 0;
84}
85void main( int argc, char **argv );
86void main( int argc, char **argv )
87{
88    PortAudioStream *stream;
89    PaError err;
90    paTestData data;
91    int    go;
92    int    numBuffers = 0;
93    int    minBuffers = 0;
94    int    framesPerBuffer;
95    double sampleRate = 44100.0;
96    char   str[256];
97    printf("paminlat - Determine minimum latency for your computer.\n");
98    printf("  usage:         paminlat {framesPerBuffer}\n");
99    printf("  for example:   paminlat 256\n");
100    printf("Adjust your stereo until you hear a smooth tone in each speaker.\n");
101    printf("Then try to find the smallest number of buffers that still sounds smooth.\n");
102    printf("Note that the sound will stop momentarily when you change the number of buffers.\n");
103    /* Get bufferSize from command line. */
104    framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE;
105    printf("Frames per buffer = %d\n", framesPerBuffer );
106
107    data.left_phase = data.right_phase = 0.0;
108    err = Pa_Initialize();
109    if( err != paNoError ) goto error;
110    /* Ask PortAudio for the recommended minimum number of buffers. */
111    numBuffers = minBuffers = Pa_GetMinNumBuffers( framesPerBuffer, sampleRate );
112    printf("NumBuffers set to %d based on a call to Pa_GetMinNumBuffers()\n", numBuffers );
113    /* Try different numBuffers in a loop. */
114    go = 1;
115    while( go )
116    {
117
118        printf("Latency = framesPerBuffer * numBuffers = %d * %d = %d frames = %d msecs.\n",
119               framesPerBuffer, numBuffers, framesPerBuffer*numBuffers,
120               (int)((1000 * framesPerBuffer * numBuffers) / sampleRate) );
121        err = Pa_OpenStream(
122                  &stream,
123                  paNoDevice,
124                  0,              /* no input */
125                  paFloat32,  /* 32 bit floating point input */
126                  NULL,
127                  Pa_GetDefaultOutputDeviceID(), /* default output device */
128                  2,              /* stereo output */
129                  paFloat32,      /* 32 bit floating point output */
130                  NULL,
131                  sampleRate,
132                  framesPerBuffer,
133                  numBuffers,     /* number of buffers */
134                  paClipOff,      /* we won't output out of range samples so don't bother clipping them */
135                  paminlatCallback,
136                  &data );
137        if( err != paNoError ) goto error;
138        if( stream == NULL ) goto error;
139        /* Start audio. */
140        err = Pa_StartStream( stream );
141        if( err != paNoError ) goto error;
142        /* Ask user for a new number of buffers. */
143        printf("\nMove windows around to see if the sound glitches.\n");
144        printf("NumBuffers currently %d, enter new number, or 'q' to quit: ", numBuffers );
145        gets( str );
146        if( str[0] == 'q' ) go = 0;
147        else
148        {
149            numBuffers = atol( str );
150            if( numBuffers < minBuffers )
151            {
152                printf( "numBuffers below minimum of %d! Set to minimum!!!\n", minBuffers );
153                numBuffers = minBuffers;
154            }
155        }
156        /* Stop sound until ENTER hit. */
157        err = Pa_StopStream( stream );
158        if( err != paNoError ) goto error;
159        err = Pa_CloseStream( stream );
160        if( err != paNoError ) goto error;
161    }
162    printf("A good setting for latency would be somewhat higher than\n");
163    printf("the minimum latency that worked.\n");
164    printf("PortAudio: Test finished.\n");
165    Pa_Terminate();
166    return;
167error:
168    Pa_Terminate();
169    fprintf( stderr, "An error occured while using the portaudio stream\n" );
170    fprintf( stderr, "Error number: %d\n", err );
171    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
172}
Note: See TracBrowser for help on using the browser.