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 / patest_out_underflow.c @ 162:d43aab368df9

History | View | Annotate | Download (8.13 KB)

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

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

    
46
#include <stdio.h>
47
#include <math.h>
48
#include "portaudio.h"
49

    
50
#define MAX_SINES     (1000)
51
#define MAX_LOAD      (1.2)
52
#define SAMPLE_RATE   (44100)
53
#define FRAMES_PER_BUFFER  (512)
54
#ifndef M_PI
55
#define M_PI  (3.14159265)
56
#endif
57
#define TWOPI (M_PI * 2.0)
58

    
59
typedef struct paTestData
60
{
61
    int sineCount;
62
    double phases[MAX_SINES];
63
    int countUnderflows;
64
    int outputUnderflowCount;
65
}
66
paTestData;
67

    
68
/* This routine will be called by the PortAudio engine when audio is needed.
69
** It may called at interrupt level on some machines so don't do anything
70
** that could mess up the system like calling malloc() or free().
71
*/
72
static int patestCallback( const void *inputBuffer, void *outputBuffer,
73
                           unsigned long framesPerBuffer,
74
                           const PaStreamCallbackTimeInfo* timeInfo,
75
                           PaStreamCallbackFlags statusFlags,
76
                           void *userData )
77
{
78
    paTestData *data = (paTestData*)userData;
79
    float *out = (float*)outputBuffer;
80
    unsigned long i;
81
    int j;
82
    int finished = paContinue;
83
    (void) timeInfo;    /* Prevent unused variable warning. */
84
    (void) inputBuffer; /* Prevent unused variable warning. */
85

    
86

    
87
    if( data->countUnderflows && (statusFlags & paOutputUnderflow) )
88
    {
89
        data->outputUnderflowCount++;
90
    }
91
    for( i=0; i<framesPerBuffer; i++ )
92
    {
93
        float output = 0.0;
94
        double phaseInc = 0.02;
95
        double phase;
96

    
97
        for( j=0; j<data->sineCount; j++ )
98
        {
99
            /* Advance phase of next oscillator. */
100
            phase = data->phases[j];
101
            phase += phaseInc;
102
            if( phase > TWOPI ) phase -= TWOPI;
103

    
104
            phaseInc *= 1.02;
105
            if( phaseInc > 0.5 ) phaseInc *= 0.5;
106

    
107
            /* This is not a very efficient way to calc sines. */
108
            output += (float) sin( phase );
109
            data->phases[j] = phase;
110
        }
111
        *out++ = (float) (output / data->sineCount);
112
    }
113

    
114
    return finished;
115
}
116

    
117
/*******************************************************************/
118
int main(void);
119
int main(void)
120
{
121
    PaStreamParameters outputParameters;
122
    PaStream *stream;
123
    PaError err;
124
    int safeSineCount, stressedSineCount;
125
    int sineCount;
126
    int safeUnderflowCount, stressedUnderflowCount;
127
    paTestData data = {0};
128
    double load;
129
    double suggestedLatency;
130

    
131

    
132
    printf("PortAudio Test: output sine waves, count underflows. SR = %d, BufSize = %d. MAX_LOAD = %f\n",
133
        SAMPLE_RATE, FRAMES_PER_BUFFER, (float)MAX_LOAD );
134

    
135
    err = Pa_Initialize();
136
    if( err != paNoError ) goto error;
137
    
138
    outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
139
    if (outputParameters.device == paNoDevice) {
140
      fprintf(stderr,"Error: No default output device.\n");
141
      goto error;
142
    }
143
    outputParameters.channelCount = 1;                      /* mono output */
144
    outputParameters.sampleFormat = paFloat32;              /* 32 bit floating point output */
145
    suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
146
    outputParameters.suggestedLatency = suggestedLatency;
147
    outputParameters.hostApiSpecificStreamInfo = NULL;
148

    
149
    err = Pa_OpenStream(
150
              &stream,
151
              NULL,         /* no input */
152
              &outputParameters,
153
              SAMPLE_RATE,
154
              FRAMES_PER_BUFFER,
155
              paClipOff,    /* we won't output out of range samples so don't bother clipping them */
156
              patestCallback,
157
              &data );    
158
    if( err != paNoError ) goto error;
159
    err = Pa_StartStream( stream );
160
    if( err != paNoError ) goto error;
161

    
162
    printf("Establishing load conditions...\n" );
163

    
164
    /* Determine number of sines required to get to 50% */
165
    do
166
    {        
167
        Pa_Sleep( 100 );
168

    
169
        load = Pa_GetStreamCpuLoad( stream );
170
        printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
171
                
172
        if( load < 0.3 )
173
        {
174
            data.sineCount += 10;
175
        }
176
        else if( load < 0.4 )
177
        {
178
            data.sineCount += 2;
179
        }
180
        else
181
        {
182
            data.sineCount += 1;
183
        }
184
    }
185
    while( load < 0.5 && data.sineCount < (MAX_SINES-1));
186

    
187
    safeSineCount = data.sineCount;
188

    
189
    /* Calculate target stress value then ramp up to that level*/
190
    stressedSineCount = (int) (2.0 * data.sineCount * MAX_LOAD );
191
    if( stressedSineCount > MAX_SINES )
192
        stressedSineCount = MAX_SINES;
193
    sineCount = data.sineCount;
194
    for( ; sineCount < stressedSineCount; sineCount+=4 )
195
    {
196
        data.sineCount = sineCount;
197
        Pa_Sleep( 100 );
198
        load = Pa_GetStreamCpuLoad( stream );
199
        printf("STRESSING: sineCount = %d, CPU load = %f\n", sineCount, load );
200
    }
201
    
202
    printf("Counting underflows for 2 seconds.\n");
203
    data.countUnderflows = 1;
204
    Pa_Sleep( 2000 );
205

    
206
    stressedUnderflowCount = data.outputUnderflowCount;
207

    
208
    data.countUnderflows = 0;
209
    data.sineCount = safeSineCount;
210

    
211
    printf("Resuming safe load...\n");
212
    Pa_Sleep( 1500 );
213
    data.outputUnderflowCount = 0;
214
    Pa_Sleep( 1500 );
215
    load = Pa_GetStreamCpuLoad( stream );
216
    printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
217

    
218
    printf("Counting underflows for 5 seconds.\n");
219
    data.countUnderflows = 1;
220
    Pa_Sleep( 5000 );
221

    
222
    safeUnderflowCount = data.outputUnderflowCount;
223
    
224
    printf("Stop stream.\n");
225
    err = Pa_StopStream( stream );
226
    if( err != paNoError ) goto error;
227
    
228
    err = Pa_CloseStream( stream );
229
    if( err != paNoError ) goto error;
230
    
231
    Pa_Terminate();
232

    
233
    printf("suggestedLatency = %f\n", suggestedLatency);
234

    
235
    // Report pass or fail
236
    if( stressedUnderflowCount == 0 )
237
        printf("Test FAILED, no output underflows detected under stress.\n");
238
    else
239
        printf("Test %s, %d expected output underflows detected under stress, "
240
               "%d unexpected underflows detected under safe load.\n",
241
               (safeUnderflowCount == 0) ? "PASSED" : "FAILED",
242
               stressedUnderflowCount, safeUnderflowCount );
243

    
244
    return err;
245
error:
246
    Pa_Terminate();
247
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
248
    fprintf( stderr, "Error number: %d\n", err );
249
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
250
    return err;
251
}