~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_stop_playout.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** @file patest_stop_playout.c
2
 
        @ingroup test_src
3
 
        @brief Test whether all queued samples are played when Pa_StopStream()
4
 
            is used with a callback or read/write stream, or when the callback
5
 
            returns paComplete.
6
 
        @author Ross Bencina <rossb@audiomulch.com>
7
 
*/
8
 
/*
9
 
 * $Id: patest_stop_playout.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
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
 
#include <stdio.h>
46
 
#include <math.h>
47
 
#include "portaudio.h"
48
 
 
49
 
#define SAMPLE_RATE         (44100)
50
 
#define FRAMES_PER_BUFFER   (1024)
51
 
 
52
 
#define TONE_SECONDS        (1)      /* long tone */
53
 
#define TONE_FADE_SECONDS   (.04)    /* fades at start and end of long tone */
54
 
#define GAP_SECONDS         (.25)     /* gap between long tone and blip */
55
 
#define BLIP_SECONDS        (.035)   /* short blip */
56
 
 
57
 
#define NUM_REPEATS         (3)
58
 
 
59
 
#ifndef M_PI
60
 
#define M_PI (3.14159265)
61
 
#endif
62
 
 
63
 
#define TABLE_SIZE          (2048)
64
 
typedef struct
65
 
{
66
 
    float sine[TABLE_SIZE+1];
67
 
 
68
 
    int repeatCount;
69
 
    
70
 
    double phase;
71
 
    double lowIncrement, highIncrement;
72
 
    
73
 
    int gap1Length, toneLength, toneFadesLength, gap2Length, blipLength;
74
 
    int gap1Countdown, toneCountdown, gap2Countdown, blipCountdown;
75
 
}
76
 
TestData;
77
 
 
78
 
 
79
 
static void RetriggerTestSignalGenerator( TestData *data )
80
 
{
81
 
    data->phase = 0.;
82
 
    data->gap1Countdown = data->gap1Length;
83
 
    data->toneCountdown = data->toneLength;
84
 
    data->gap2Countdown = data->gap2Length;
85
 
    data->blipCountdown = data->blipLength;
86
 
}
87
 
 
88
 
 
89
 
static void ResetTestSignalGenerator( TestData *data )
90
 
{
91
 
    data->repeatCount = 0;
92
 
    RetriggerTestSignalGenerator( data );
93
 
}
94
 
 
95
 
 
96
 
static void InitTestSignalGenerator( TestData *data )
97
 
{
98
 
    int signalLengthModBufferLength, i;
99
 
 
100
 
    /* initialise sinusoidal wavetable */
101
 
    for( i=0; i<TABLE_SIZE; i++ )
102
 
    {
103
 
        data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
104
 
    }
105
 
    data->sine[TABLE_SIZE] = data->sine[0]; /* guard point for linear interpolation */
106
 
 
107
 
 
108
 
    
109
 
    data->lowIncrement = (330. / SAMPLE_RATE) * TABLE_SIZE;
110
 
    data->highIncrement = (1760. / SAMPLE_RATE) * TABLE_SIZE;
111
 
 
112
 
    data->gap1Length = GAP_SECONDS * SAMPLE_RATE;
113
 
    data->toneLength = TONE_SECONDS * SAMPLE_RATE;
114
 
    data->toneFadesLength = TONE_FADE_SECONDS * SAMPLE_RATE;
115
 
    data->gap2Length = GAP_SECONDS * SAMPLE_RATE;
116
 
    data->blipLength = BLIP_SECONDS * SAMPLE_RATE;
117
 
 
118
 
    /* adjust signal length to be a multiple of the buffer length */
119
 
    signalLengthModBufferLength = (data->gap1Length + data->toneLength + data->gap2Length + data->blipLength) % FRAMES_PER_BUFFER;
120
 
    if( signalLengthModBufferLength > 0 )
121
 
        data->toneLength += signalLengthModBufferLength;
122
 
 
123
 
    ResetTestSignalGenerator( data );
124
 
}
125
 
 
126
 
 
127
 
#define MIN( a, b ) (((a)<(b))?(a):(b))
128
 
 
129
 
static void GenerateTestSignal( TestData *data, float *stereo, int frameCount )
130
 
{
131
 
    int framesGenerated = 0;
132
 
    float output;
133
 
    long index;
134
 
    float fraction;
135
 
    int count, i;
136
 
 
137
 
    while( framesGenerated < frameCount && data->repeatCount < NUM_REPEATS )
138
 
    {
139
 
        if( framesGenerated < frameCount && data->gap1Countdown > 0 ){
140
 
            count = MIN( frameCount - framesGenerated, data->gap1Countdown );
141
 
            for( i=0; i < count; ++i )
142
 
            {
143
 
                *stereo++ = 0.f;
144
 
                *stereo++ = 0.f;
145
 
            }
146
 
 
147
 
            data->gap1Countdown -= count;
148
 
            framesGenerated += count;
149
 
        }
150
 
    
151
 
        if( framesGenerated < frameCount && data->toneCountdown > 0 ){
152
 
            count = MIN( frameCount - framesGenerated, data->toneCountdown );
153
 
            for( i=0; i < count; ++i )
154
 
            {
155
 
                /* tone with data->lowIncrement phase increment */
156
 
                index = (long)data->phase;
157
 
                fraction = data->phase - index;
158
 
                output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
159
 
 
160
 
                data->phase += data->lowIncrement;
161
 
                while( data->phase >= TABLE_SIZE )
162
 
                    data->phase -= TABLE_SIZE;
163
 
 
164
 
                /* apply fade to ends */
165
 
 
166
 
                if( data->toneCountdown < data->toneFadesLength )
167
 
                {
168
 
                    /* cosine-bell fade out at end */
169
 
                    output *= (-cos(((float)data->toneCountdown / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
170
 
                }
171
 
                else if( data->toneCountdown > data->toneLength - data->toneFadesLength ) 
172
 
                {
173
 
                    /* cosine-bell fade in at start */
174
 
                    output *= (cos(((float)(data->toneCountdown - (data->toneLength - data->toneFadesLength)) / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
175
 
                }
176
 
 
177
 
                output *= .5; /* play tone half as loud as blip */
178
 
            
179
 
                *stereo++ = output;
180
 
                *stereo++ = output;
181
 
 
182
 
                data->toneCountdown--;
183
 
            }                         
184
 
 
185
 
            framesGenerated += count;
186
 
        }
187
 
 
188
 
        if( framesGenerated < frameCount && data->gap2Countdown > 0 ){
189
 
            count = MIN( frameCount - framesGenerated, data->gap2Countdown );
190
 
            for( i=0; i < count; ++i )
191
 
            {
192
 
                *stereo++ = 0.f;
193
 
                *stereo++ = 0.f;
194
 
            }
195
 
 
196
 
            data->gap2Countdown -= count;
197
 
            framesGenerated += count;
198
 
        }
199
 
 
200
 
        if( framesGenerated < frameCount && data->blipCountdown > 0 ){
201
 
            count = MIN( frameCount - framesGenerated, data->blipCountdown );
202
 
            for( i=0; i < count; ++i )
203
 
            {
204
 
                /* tone with data->highIncrement phase increment */
205
 
                index = (long)data->phase;
206
 
                fraction = data->phase - index;
207
 
                output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
208
 
 
209
 
                data->phase += data->highIncrement;
210
 
                while( data->phase >= TABLE_SIZE )
211
 
                    data->phase -= TABLE_SIZE;
212
 
 
213
 
                /* cosine-bell envelope over whole blip */
214
 
                output *= (-cos( ((float)data->blipCountdown / (float)data->blipLength) * 2. * M_PI) + 1.) * .5;
215
 
                
216
 
                *stereo++ = output;
217
 
                *stereo++ = output;
218
 
 
219
 
                data->blipCountdown--;
220
 
            }
221
 
 
222
 
            framesGenerated += count;
223
 
        }
224
 
 
225
 
 
226
 
        if( data->blipCountdown == 0 )
227
 
        {
228
 
            RetriggerTestSignalGenerator( data );
229
 
            data->repeatCount++;
230
 
        }        
231
 
    }
232
 
 
233
 
    if( framesGenerated < frameCount )
234
 
    {
235
 
        count = frameCount - framesGenerated;
236
 
        for( i=0; i < count; ++i )
237
 
        {
238
 
            *stereo++ = 0.f;
239
 
            *stereo++ = 0.f;
240
 
        }
241
 
    }
242
 
}
243
 
 
244
 
 
245
 
static int IsTestSignalFinished( TestData *data )
246
 
{
247
 
    if( data->repeatCount >= NUM_REPEATS )
248
 
        return 1;
249
 
    else
250
 
        return 0;
251
 
}
252
 
 
253
 
 
254
 
static int TestCallback1( const void *inputBuffer, void *outputBuffer,
255
 
                            unsigned long frameCount,
256
 
                            const PaStreamCallbackTimeInfo* timeInfo,
257
 
                            PaStreamCallbackFlags statusFlags,
258
 
                            void *userData )
259
 
{
260
 
    (void) inputBuffer; /* Prevent unused variable warnings. */
261
 
    (void) timeInfo;
262
 
    (void) statusFlags;
263
 
 
264
 
    GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
265
 
 
266
 
    if( IsTestSignalFinished( (TestData*)userData ) )
267
 
        return paComplete;
268
 
    else
269
 
        return paContinue;
270
 
}
271
 
 
272
 
 
273
 
volatile int testCallback2Finished = 0;
274
 
 
275
 
static int TestCallback2( const void *inputBuffer, void *outputBuffer,
276
 
                            unsigned long frameCount,
277
 
                            const PaStreamCallbackTimeInfo* timeInfo,
278
 
                            PaStreamCallbackFlags statusFlags,
279
 
                            void *userData )
280
 
{
281
 
    (void) inputBuffer; /* Prevent unused variable warnings. */
282
 
    (void) timeInfo;
283
 
    (void) statusFlags;
284
 
 
285
 
    GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
286
 
 
287
 
    if( IsTestSignalFinished( (TestData*)userData ) )
288
 
        testCallback2Finished = 1;
289
 
   
290
 
    return paContinue;
291
 
}
292
 
 
293
 
/*******************************************************************/
294
 
int main(void);
295
 
int main(void)
296
 
{
297
 
    PaStreamParameters outputParameters;
298
 
    PaStream *stream;
299
 
    PaError err;
300
 
    TestData data;
301
 
    float writeBuffer[ FRAMES_PER_BUFFER * 2 ];
302
 
    
303
 
    printf("PortAudio Test: check that stopping stream plays out all queued samples. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
304
 
 
305
 
    InitTestSignalGenerator( &data );
306
 
    
307
 
    err = Pa_Initialize();
308
 
    if( err != paNoError ) goto error;
309
 
 
310
 
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
311
 
    outputParameters.channelCount = 2;       /* stereo output */
312
 
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
313
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
314
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
315
 
 
316
 
/* test paComplete ---------------------------------------------------------- */
317
 
 
318
 
    ResetTestSignalGenerator( &data );
319
 
 
320
 
    err = Pa_OpenStream(
321
 
              &stream,
322
 
              NULL, /* no input */
323
 
              &outputParameters,
324
 
              SAMPLE_RATE,
325
 
              FRAMES_PER_BUFFER,
326
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
327
 
              TestCallback1,
328
 
              &data );
329
 
    if( err != paNoError ) goto error;
330
 
 
331
 
    err = Pa_StartStream( stream );
332
 
    if( err != paNoError ) goto error;
333
 
 
334
 
 
335
 
    printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS );
336
 
    printf("If final blip is not intact, callback+paComplete implementation may be faulty.\n\n" );
337
 
 
338
 
    while( (err = Pa_IsStreamActive( stream )) == 1 )
339
 
        Pa_Sleep( 5 );
340
 
 
341
 
    if( err != 0 ) goto error;
342
 
 
343
 
    
344
 
    err = Pa_StopStream( stream );
345
 
    if( err != paNoError ) goto error;
346
 
 
347
 
    err = Pa_CloseStream( stream );
348
 
    if( err != paNoError ) goto error;
349
 
 
350
 
    Pa_Sleep( 500 );
351
 
 
352
 
/* test Pa_StopStream() with callback --------------------------------------- */
353
 
 
354
 
    ResetTestSignalGenerator( &data );
355
 
 
356
 
    testCallback2Finished = 0;
357
 
    
358
 
    err = Pa_OpenStream(
359
 
              &stream,
360
 
              NULL, /* no input */
361
 
              &outputParameters,
362
 
              SAMPLE_RATE,
363
 
              FRAMES_PER_BUFFER,
364
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
365
 
              TestCallback2,
366
 
              &data );
367
 
    if( err != paNoError ) goto error;
368
 
 
369
 
    err = Pa_StartStream( stream );
370
 
    if( err != paNoError ) goto error;
371
 
 
372
 
 
373
 
    printf("\nPlaying 'tone-blip' %d times using callback, stops by calling Pa_StopStream.\n", NUM_REPEATS );
374
 
    printf("If final blip is not intact, callback+Pa_StopStream implementation may be faulty.\n\n" );
375
 
 
376
 
    /* note that polling a volatile flag is not a good way to synchronise with
377
 
        the callback, but it's the best we can do portably. */
378
 
    while( !testCallback2Finished )
379
 
        Pa_Sleep( 2 );
380
 
 
381
 
    err = Pa_StopStream( stream );
382
 
    if( err != paNoError ) goto error;
383
 
    
384
 
 
385
 
    err = Pa_CloseStream( stream );
386
 
    if( err != paNoError ) goto error;
387
 
 
388
 
    Pa_Sleep( 500 );
389
 
 
390
 
/* test Pa_StopStream() with Pa_WriteStream --------------------------------- */
391
 
 
392
 
    ResetTestSignalGenerator( &data );
393
 
 
394
 
    err = Pa_OpenStream(
395
 
              &stream,
396
 
              NULL, /* no input */
397
 
              &outputParameters,
398
 
              SAMPLE_RATE,
399
 
              FRAMES_PER_BUFFER,
400
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
401
 
              NULL, /* no callback, use blocking API */
402
 
              NULL ); /* no callback, so no callback userData */
403
 
    if( err != paNoError ) goto error;
404
 
 
405
 
    err = Pa_StartStream( stream );
406
 
    if( err != paNoError ) goto error;
407
 
 
408
 
 
409
 
    printf("\nPlaying 'tone-blip' %d times using Pa_WriteStream, stops by calling Pa_StopStream.\n", NUM_REPEATS );
410
 
    printf("If final blip is not intact, Pa_WriteStream+Pa_StopStream implementation may be faulty.\n\n" );
411
 
 
412
 
    do{
413
 
        GenerateTestSignal( &data, writeBuffer, FRAMES_PER_BUFFER );
414
 
        err = Pa_WriteStream( stream, writeBuffer, FRAMES_PER_BUFFER );
415
 
        if( err != paNoError ) goto error;
416
 
        
417
 
    }while( !IsTestSignalFinished( &data ) );
418
 
 
419
 
    err = Pa_StopStream( stream );
420
 
    if( err != paNoError ) goto error;
421
 
    
422
 
 
423
 
    err = Pa_CloseStream( stream );
424
 
    if( err != paNoError ) goto error;
425
 
 
426
 
/* -------------------------------------------------------------------------- */
427
 
    
428
 
    Pa_Terminate();
429
 
    printf("Test finished.\n");
430
 
    
431
 
    return err;
432
 
    
433
 
error:
434
 
    Pa_Terminate();
435
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
436
 
    fprintf( stderr, "Error number: %d\n", err );
437
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
438
 
    return err;
439
 
}