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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_prime.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_prime.c
2
 
        @ingroup test_src
3
 
        @brief Test stream priming mode.
4
 
        @author Ross Bencina http://www.audiomulch.com/~rossb
5
 
*/
6
 
 
7
 
/*
8
 
 * $Id: patest_prime.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
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 <math.h>
47
 
#include "portaudio.h"
48
 
#include "pa_util.h"
49
 
 
50
 
#define NUM_BEEPS           (3)
51
 
#define SAMPLE_RATE         (44100)
52
 
#define SAMPLE_PERIOD       (1.0/44100.0)
53
 
#define FRAMES_PER_BUFFER   (256)
54
 
#define BEEP_DURATION       (400)
55
 
#define IDLE_DURATION       (SAMPLE_RATE*2)      /* 2 seconds */
56
 
#define SLEEP_MSEC          (50)
57
 
 
58
 
#define STATE_BKG_IDLE      (0)
59
 
#define STATE_BKG_BEEPING   (1)
60
 
 
61
 
typedef struct
62
 
{
63
 
    float        leftPhase;
64
 
    float        rightPhase;
65
 
    int          state;
66
 
    int          beepCountdown;
67
 
    int          idleCountdown;
68
 
}
69
 
paTestData;
70
 
 
71
 
static void InitializeTestData( paTestData *testData )
72
 
{
73
 
    testData->leftPhase = 0;
74
 
    testData->rightPhase = 0;
75
 
    testData->state = STATE_BKG_BEEPING;
76
 
    testData->beepCountdown = BEEP_DURATION;
77
 
    testData->idleCountdown = IDLE_DURATION;
78
 
}
79
 
 
80
 
/* This routine will be called by the PortAudio engine when audio is needed.
81
 
** It may called at interrupt level on some machines so don't do anything
82
 
** that could mess up the system like calling malloc() or free().
83
 
*/
84
 
static int patestCallback( const void *inputBuffer, void *outputBuffer,
85
 
                           unsigned long framesPerBuffer,
86
 
                                       const PaStreamCallbackTimeInfo *timeInfo,
87
 
                                       PaStreamCallbackFlags statusFlags, void *userData )
88
 
{
89
 
    /* Cast data passed through stream to our structure. */
90
 
    paTestData *data = (paTestData*)userData;
91
 
    float *out = (float*)outputBuffer;
92
 
    unsigned int i;
93
 
    int result = paContinue;
94
 
 
95
 
    /* supress unused parameter warnings */
96
 
    (void) inputBuffer;
97
 
    (void) timeInfo;
98
 
    (void) statusFlags;
99
 
 
100
 
    for( i=0; i<framesPerBuffer; i++ )
101
 
    {
102
 
        switch( data->state )
103
 
        {
104
 
        case STATE_BKG_IDLE:
105
 
            *out++ = 0.0;  /* left */
106
 
            *out++ = 0.0;  /* right */
107
 
            --data->idleCountdown;
108
 
            
109
 
            if( data->idleCountdown <= 0 ) result = paComplete;
110
 
            break;
111
 
 
112
 
        case STATE_BKG_BEEPING:
113
 
            if( data->beepCountdown <= 0 )
114
 
            {
115
 
                data->state = STATE_BKG_IDLE;
116
 
                *out++ = 0.0;  /* left */
117
 
                *out++ = 0.0;  /* right */
118
 
            }
119
 
            else
120
 
            {
121
 
                /* Play sawtooth wave. */
122
 
                *out++ = data->leftPhase;  /* left */
123
 
                *out++ = data->rightPhase;  /* right */
124
 
                /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
125
 
                data->leftPhase += 0.01f;
126
 
                /* When signal reaches top, drop back down. */
127
 
                if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f;
128
 
                /* higher pitch so we can distinguish left and right. */
129
 
                data->rightPhase += 0.03f;
130
 
                if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f;
131
 
            }
132
 
            --data->beepCountdown;
133
 
            break;
134
 
        }
135
 
    }
136
 
    
137
 
    return result;
138
 
}
139
 
 
140
 
/*******************************************************************/
141
 
static PaError DoTest( int flags )
142
 
{
143
 
    PaStream *stream;
144
 
    PaError    err;
145
 
    paTestData data;
146
 
    PaStreamParameters outputParameters;
147
 
 
148
 
    InitializeTestData( &data );       
149
 
 
150
 
    outputParameters.device = Pa_GetDefaultOutputDevice();
151
 
    outputParameters.channelCount = 2;
152
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
153
 
    outputParameters.sampleFormat = paFloat32;
154
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
155
 
 
156
 
    /* Open an audio I/O stream. */
157
 
    err = Pa_OpenStream(
158
 
        &stream,
159
 
        NULL,                         /* no input */
160
 
        &outputParameters,
161
 
        SAMPLE_RATE,
162
 
        FRAMES_PER_BUFFER,            /* frames per buffer */
163
 
        paClipOff | flags,      /* we won't output out of range samples so don't bother clipping them */
164
 
        patestCallback,
165
 
        &data );
166
 
    if( err != paNoError ) goto error;
167
 
 
168
 
 
169
 
    err = Pa_StartStream( stream );
170
 
    if( err != paNoError ) goto error;
171
 
 
172
 
    printf("hear \"BEEP\"\n" );
173
 
    fflush(stdout);
174
 
 
175
 
    while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC);
176
 
    if( err < 0 ) goto error;
177
 
 
178
 
    err = Pa_StopStream( stream );
179
 
    if( err != paNoError ) goto error;
180
 
 
181
 
    err = Pa_CloseStream( stream );
182
 
    if( err != paNoError ) goto error;
183
 
 
184
 
    return err;
185
 
error:
186
 
    return err;
187
 
}
188
 
 
189
 
/*******************************************************************/
190
 
int main(void);
191
 
int main(void)
192
 
{
193
 
    PaError    err = paNoError;
194
 
    int        i;
195
 
 
196
 
    /* Initialize library before making any other calls. */
197
 
    err = Pa_Initialize();
198
 
    if( err != paNoError ) goto error;
199
 
    
200
 
    printf("PortAudio Test: Testing stream playback with no priming.\n");
201
 
    printf("PortAudio Test: you should see BEEP before you hear it.\n");
202
 
    printf("BEEP %d times.\n", NUM_BEEPS );
203
 
 
204
 
    for( i=0; i< NUM_BEEPS; ++i )
205
 
    {
206
 
        err = DoTest( 0 );
207
 
        if( err != paNoError )
208
 
            goto error;
209
 
    }
210
 
 
211
 
    printf("PortAudio Test: Testing stream playback with priming.\n");
212
 
    printf("PortAudio Test: you should see BEEP around the same time you hear it.\n");
213
 
    for( i=0; i< NUM_BEEPS; ++i )
214
 
    {
215
 
        err = DoTest( paPrimeOutputBuffersUsingStreamCallback );
216
 
        if( err != paNoError )
217
 
            goto error;
218
 
    }
219
 
 
220
 
    printf("Test finished.\n");
221
 
 
222
 
    Pa_Terminate();
223
 
    return err;
224
 
error:
225
 
    Pa_Terminate();
226
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
227
 
    fprintf( stderr, "Error number: %d\n", err );
228
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
229
 
    return err;
230
 
}