To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / test / pa_minlat.c @ 164:9fa11135915a

History | View | Annotate | Download (6.85 KB)

1
/** @file pa_minlat.c
2
        @ingroup test_src
3
    @brief Experiment with different numbers of buffers to determine the
4
        minimum latency for a computer.
5
        @author Phil Burk  http://www.softsynth.com
6
*/
7
/*
8
 * $Id$
9
 *
10
 * This program uses the PortAudio Portable Audio Library.
11
 * For more information see: http://www.portaudio.com
12
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining
15
 * a copy of this software and associated documentation files
16
 * (the "Software"), to deal in the Software without restriction,
17
 * including without limitation the rights to use, copy, modify, merge,
18
 * publish, distribute, sublicense, and/or sell copies of the Software,
19
 * and to permit persons to whom the Software is furnished to do so,
20
 * subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be
23
 * included in all copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
 */
33

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

    
45
#include <stdio.h>
46
#include <stdlib.h>
47
#include <math.h>
48
#include <string.h>
49
#include "portaudio.h"
50

    
51
#ifndef M_PI
52
#define M_PI  (3.14159265)
53
#endif
54
#define TWOPI (M_PI * 2.0)
55

    
56
#define DEFAULT_BUFFER_SIZE   (32)
57

    
58
typedef struct
59
{
60
    double left_phase;
61
    double right_phase;
62
}
63
paTestData;
64

    
65
/* Very simple synthesis routine to generate two sine waves. */
66
static int paminlatCallback( const void *inputBuffer, void *outputBuffer,
67
                             unsigned long framesPerBuffer,
68
                             const PaStreamCallbackTimeInfo* timeInfo,
69
                             PaStreamCallbackFlags statusFlags,
70
                             void *userData )
71
{
72
    paTestData *data = (paTestData*)userData;
73
    float *out = (float*)outputBuffer;
74
    unsigned int i;
75
    double left_phaseInc = 0.02;
76
    double right_phaseInc = 0.06;
77

    
78
    double left_phase = data->left_phase;
79
    double right_phase = data->right_phase;
80

    
81
    for( i=0; i<framesPerBuffer; i++ )
82
    {
83
        left_phase += left_phaseInc;
84
        if( left_phase > TWOPI ) left_phase -= TWOPI;
85
        *out++ = (float) sin( left_phase );
86

    
87
        right_phase += right_phaseInc;
88
        if( right_phase > TWOPI ) right_phase -= TWOPI;
89
        *out++ = (float) sin( right_phase );
90
    }
91

    
92
    data->left_phase = left_phase;
93
    data->right_phase = right_phase;
94
    return 0;
95
}
96

    
97
int main( int argc, char **argv );
98
int main( int argc, char **argv )
99
{
100
    PaStreamParameters outputParameters;
101
    PaStream *stream;
102
    PaError err;
103
    paTestData data;
104
    int    go;
105
    int    outLatency = 0;
106
    int    minLatency = DEFAULT_BUFFER_SIZE * 2;
107
    int    framesPerBuffer;
108
    double sampleRate = 44100.0;
109
    char   str[256];
110
        char  *line;
111

    
112
    printf("pa_minlat - Determine minimum latency for your computer.\n");
113
    printf("  usage:         pa_minlat {userBufferSize}\n");
114
    printf("  for example:   pa_minlat 64\n");
115
    printf("Adjust your stereo until you hear a smooth tone in each speaker.\n");
116
    printf("Then try to find the smallest number of frames that still sounds smooth.\n");
117
    printf("Note that the sound will stop momentarily when you change the number of buffers.\n");
118

    
119
    /* Get bufferSize from command line. */
120
    framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE;
121
    printf("Frames per buffer = %d\n", framesPerBuffer );
122

    
123
    data.left_phase = data.right_phase = 0.0;
124

    
125
    err = Pa_Initialize();
126
    if( err != paNoError ) goto error;
127

    
128
    outLatency = sampleRate * 200.0 / 1000.0; /* 200 msec. */
129

    
130
    /* Try different numBuffers in a loop. */
131
    go = 1;
132
    while( go )
133
    {
134
        outputParameters.device                    = Pa_GetDefaultOutputDevice(); /* Default output device. */
135
        outputParameters.channelCount              = 2;                           /* Stereo output */
136
        outputParameters.sampleFormat              = paFloat32;                   /* 32 bit floating point output. */
137
        outputParameters.suggestedLatency          = (double)outLatency / sampleRate; /* In seconds. */
138
        outputParameters.hostApiSpecificStreamInfo = NULL;
139
        
140
        printf("Latency = %d frames = %6.1f msec.\n", outLatency, outputParameters.suggestedLatency * 1000.0 );
141

    
142
        err = Pa_OpenStream(
143
                  &stream,
144
                  NULL, /* no input */
145
                  &outputParameters,
146
                  sampleRate,
147
                  framesPerBuffer,
148
                  paClipOff,      /* we won't output out of range samples so don't bother clipping them */
149
                  paminlatCallback,
150
                  &data );
151
        if( err != paNoError ) goto error;
152
        if( stream == NULL ) goto error;
153

    
154
        /* Start audio. */
155
        err = Pa_StartStream( stream );
156
        if( err != paNoError ) goto error;
157

    
158
        /* Ask user for a new nlatency. */
159
        printf("\nMove windows around to see if the sound glitches.\n");
160
        printf("Latency now %d, enter new number of frames, or 'q' to quit: ", outLatency );
161
        line = fgets( str, 256, stdin );
162
                if( line == NULL )
163
                {
164
                        go = 0;
165
                }
166
                else
167
                {
168
                        {
169
                                /* Get rid of newline */
170
                                size_t l = strlen( str ) - 1;
171
                                if( str[ l ] == '\n')
172
                                        str[ l ] = '\0';
173
                        }
174
                        
175
                        
176
                        if( str[0] == 'q' ) go = 0;
177
                        else
178
                        {
179
                                outLatency = atol( str );
180
                                if( outLatency < minLatency )
181
                                {
182
                                        printf( "Latency below minimum of %d! Set to minimum!!!\n", minLatency );
183
                                        outLatency = minLatency;
184
                                }
185
                        }
186
                        
187
                }
188
                /* Stop sound until ENTER hit. */
189
        err = Pa_StopStream( stream );
190
        if( err != paNoError ) goto error;
191
        err = Pa_CloseStream( stream );
192
        if( err != paNoError ) goto error;
193
    }
194
    printf("A good setting for latency would be somewhat higher than\n");
195
    printf("the minimum latency that worked.\n");
196
    printf("PortAudio: Test finished.\n");
197
    Pa_Terminate();
198
    return 0;
199
error:
200
    Pa_Terminate();
201
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
202
    fprintf( stderr, "Error number: %d\n", err );
203
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
204
    return 1;
205
}