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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-17 02:36:41 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517023641-5yjqt9434cbcb6ph
Tags: 1.3.2-4
* debian/patches:
   - desktop_file.patch: fixed broken Category entry
* debian/audacity.mime:
   - added entry for application/x-audacity-project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file patest_callbackstop.c
 
2
        @ingroup test_src
 
3
        @brief Test the paComplete callback result code.
 
4
        @author Ross Bencina <rossb@audiomulch.com>
 
5
*/
 
6
/*
 
7
 * $Id: patest_callbackstop.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
 
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
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
25
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
26
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
27
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 
28
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 
29
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
30
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
31
 */
 
32
 
 
33
/*
 
34
 * The text above constitutes the entire PortAudio license; however, 
 
35
 * the PortAudio community also makes the following non-binding requests:
 
36
 *
 
37
 * Any person wishing to distribute modifications to the Software is
 
38
 * requested to send the modifications to the original developer so that
 
39
 * they can be incorporated into the canonical version. It is also 
 
40
 * requested that these non-binding requests be included along with the 
 
41
 * license above.
 
42
 */
 
43
 
 
44
#include <stdio.h>
 
45
#include <math.h>
 
46
#include "portaudio.h"
 
47
 
 
48
#define NUM_SECONDS   (5)
 
49
#define NUM_LOOPS     (4)
 
50
#define SAMPLE_RATE   (44100)
 
51
#define FRAMES_PER_BUFFER  (67)
 
52
 
 
53
#ifndef M_PI
 
54
#define M_PI  (3.14159265)
 
55
#endif
 
56
 
 
57
#define TABLE_SIZE   (200)
 
58
typedef struct
 
59
{
 
60
    float sine[TABLE_SIZE];
 
61
    int phase;
 
62
    unsigned long generatedFramesCount;
 
63
    volatile int callbackReturnedPaComplete;
 
64
    volatile int callbackInvokedAfterReturningPaComplete;
 
65
}
 
66
TestData;
 
67
 
 
68
/*
 
69
   This routine will be called by the PortAudio stream when audio is needed.
 
70
   It may be called at interrupt level on some machines so don't do anything
 
71
   that could mess up the system like calling malloc() or free().
 
72
*/
 
73
static int TestCallback( const void *input, void *output,
 
74
                            unsigned long frameCount,
 
75
                            const PaStreamCallbackTimeInfo* timeInfo,
 
76
                            PaStreamCallbackFlags statusFlags,
 
77
                            void *userData )
 
78
{
 
79
    TestData *data = (TestData*)userData;
 
80
    float *out = (float*)output;
 
81
    unsigned long i;
 
82
    float x;
 
83
 
 
84
    (void) input;       /* Prevent unused variable warnings. */
 
85
    (void) timeInfo;
 
86
    (void) statusFlags;
 
87
 
 
88
    
 
89
    if( data->callbackReturnedPaComplete )
 
90
        data->callbackInvokedAfterReturningPaComplete = 1;
 
91
 
 
92
    for( i=0; i<frameCount; i++ )
 
93
    {
 
94
        /* generate tone */
 
95
        
 
96
        x = data->sine[ data->phase++ ];
 
97
        if( data->phase >= TABLE_SIZE )
 
98
            data->phase -= TABLE_SIZE;
 
99
        
 
100
        *out++ = x;  /* left */
 
101
        *out++ = x;  /* right */
 
102
    }
 
103
 
 
104
    data->generatedFramesCount += frameCount;
 
105
    if( data->generatedFramesCount >= (NUM_SECONDS * SAMPLE_RATE) )
 
106
    {
 
107
        data->callbackReturnedPaComplete = 1;
 
108
        return paComplete;
 
109
    }
 
110
    else
 
111
    {
 
112
        return paContinue;
 
113
    }
 
114
}
 
115
 
 
116
/*----------------------------------------------------------------------------*/
 
117
int main(void);
 
118
int main(void)
 
119
{
 
120
    PaStreamParameters outputParameters;
 
121
    PaStream *stream;
 
122
    PaError err;
 
123
    TestData data;
 
124
    int i, j;
 
125
 
 
126
    
 
127
    printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
 
128
            SAMPLE_RATE, FRAMES_PER_BUFFER );
 
129
    
 
130
    /* initialise sinusoidal wavetable */
 
131
    for( i=0; i<TABLE_SIZE; i++ )
 
132
    {
 
133
        data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
 
134
    }
 
135
    
 
136
    err = Pa_Initialize();
 
137
    if( err != paNoError ) goto error;
 
138
 
 
139
    outputParameters.device                    = Pa_GetDefaultOutputDevice();
 
140
    outputParameters.channelCount              = 2;               /* stereo output */
 
141
    outputParameters.sampleFormat              = paFloat32;       /* 32 bit floating point output */
 
142
    outputParameters.suggestedLatency          = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
 
143
    outputParameters.hostApiSpecificStreamInfo = NULL;
 
144
 
 
145
    err = Pa_OpenStream(
 
146
              &stream,
 
147
              NULL, /* no input */
 
148
              &outputParameters,
 
149
              SAMPLE_RATE,
 
150
              FRAMES_PER_BUFFER,
 
151
              paClipOff,      /* output will be in-range, so no need to clip */
 
152
              TestCallback,
 
153
              &data );
 
154
    if( err != paNoError ) goto error;
 
155
 
 
156
    printf("Repeating test %d times.\n", NUM_LOOPS );
 
157
    
 
158
    for( i=0; i < NUM_LOOPS; ++i )
 
159
    {
 
160
        data.phase = 0;
 
161
        data.generatedFramesCount = 0;
 
162
        data.callbackReturnedPaComplete = 0;
 
163
        data.callbackInvokedAfterReturningPaComplete = 0;
 
164
 
 
165
        err = Pa_StartStream( stream );
 
166
        if( err != paNoError ) goto error;
 
167
 
 
168
        printf("Play for %d seconds.\n", NUM_SECONDS );
 
169
 
 
170
        /* wait for the callback to complete generating NUM_SECONDS of tone */
 
171
 
 
172
        do
 
173
        {
 
174
            Pa_Sleep( 500 );
 
175
        }
 
176
        while( !data.callbackReturnedPaComplete );
 
177
 
 
178
        printf( "Callback returned paComplete.\n" );
 
179
        printf( "Waiting for buffers to finish playing...\n" );
 
180
 
 
181
        /* wait for stream to become inactive,
 
182
           or for a timeout of approximately NUM_SECONDS
 
183
         */
 
184
     
 
185
        j = 0;
 
186
        while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 )
 
187
        {
 
188
            printf(".\n" );
 
189
            Pa_Sleep( 500 );
 
190
            ++j;
 
191
        }
 
192
 
 
193
        if( err < 0 )
 
194
        {
 
195
            goto error;
 
196
        }
 
197
        else if( err == 1 )
 
198
        {
 
199
            printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" );
 
200
        }
 
201
        else
 
202
        {
 
203
            printf("Buffers finished.\n" );
 
204
        }
 
205
 
 
206
        if( data.callbackInvokedAfterReturningPaComplete )
 
207
        {
 
208
            printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" );
 
209
        }
 
210
 
 
211
 
 
212
        err = Pa_StopStream( stream );
 
213
        if( err != paNoError ) goto error;
 
214
    }
 
215
 
 
216
    err = Pa_CloseStream( stream );
 
217
    if( err != paNoError ) goto error;
 
218
 
 
219
    Pa_Terminate();
 
220
    printf("Test finished.\n");
 
221
    
 
222
    return err;
 
223
error:
 
224
    Pa_Terminate();
 
225
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
 
226
    fprintf( stderr, "Error number: %d\n", err );
 
227
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
 
228
    return err;
 
229
}