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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_many.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_many.c
2
 
        @ingroup test_src
3
 
        @brief Start and stop the PortAudio Driver multiple times.
4
 
        @author Phil Burk  http://www.softsynth.com
5
 
*/
6
 
/*
7
 
 * $Id: patest_many.c,v 1.2 2006/09/23 18:42:52 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 <stdlib.h>
46
 
#include <math.h>
47
 
#include "portaudio.h"
48
 
#define NUM_SECONDS   (1)
49
 
#define SAMPLE_RATE   (44100)
50
 
#ifndef M_PI
51
 
#define M_PI  (3.14159265)
52
 
#endif
53
 
#define TABLE_SIZE   (200)
54
 
typedef struct
55
 
{
56
 
    short sine[TABLE_SIZE];
57
 
    int left_phase;
58
 
    int right_phase;
59
 
    unsigned int sampsToGo;
60
 
}
61
 
paTestData;
62
 
PaError TestOnce( void );
63
 
static int patest1Callback( const void *inputBuffer, void *outputBuffer,
64
 
                            unsigned long framesPerBuffer,
65
 
                            const PaStreamCallbackTimeInfo* timeInfo,
66
 
                            PaStreamCallbackFlags statusFlags,
67
 
                            void *userData );
68
 
 
69
 
/* This routine will be called by the PortAudio engine when audio is needed.
70
 
** It may 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 patest1Callback( const void *inputBuffer, void *outputBuffer,
74
 
                            unsigned long framesPerBuffer,
75
 
                            const PaStreamCallbackTimeInfo* timeInfo,
76
 
                            PaStreamCallbackFlags statusFlags,
77
 
                            void *userData )
78
 
{
79
 
    paTestData *data = (paTestData*)userData;
80
 
    short *out = (short*)outputBuffer;
81
 
    unsigned int i;
82
 
    int finished = 0;
83
 
    (void) inputBuffer; /* Prevent "unused variable" warnings. */
84
 
 
85
 
    if( data->sampsToGo < framesPerBuffer )
86
 
    {
87
 
        /* final buffer... */
88
 
 
89
 
        for( i=0; i<data->sampsToGo; i++ )
90
 
        {
91
 
            *out++ = data->sine[data->left_phase];  /* left */
92
 
            *out++ = data->sine[data->right_phase];  /* right */
93
 
            data->left_phase += 1;
94
 
            if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
95
 
            data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
96
 
            if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
97
 
        }
98
 
        /* zero remainder of final buffer */
99
 
        for( ; i<framesPerBuffer; i++ )
100
 
        {
101
 
            *out++ = 0; /* left */
102
 
            *out++ = 0; /* right */
103
 
        }
104
 
 
105
 
        finished = 1;
106
 
    }
107
 
    else
108
 
    {
109
 
        for( i=0; i<framesPerBuffer; i++ )
110
 
        {
111
 
            *out++ = data->sine[data->left_phase];  /* left */
112
 
            *out++ = data->sine[data->right_phase];  /* right */
113
 
            data->left_phase += 1;
114
 
            if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
115
 
            data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
116
 
            if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
117
 
        }
118
 
        data->sampsToGo -= framesPerBuffer;
119
 
    }
120
 
    return finished;
121
 
}
122
 
/*******************************************************************/
123
 
#ifdef MACINTOSH
124
 
int main(void);
125
 
int main(void)
126
 
{
127
 
    int i;
128
 
    PaError err;
129
 
    int numLoops = 10;
130
 
    printf("Loop %d times.\n", numLoops );
131
 
    for( i=0; i<numLoops; i++ )
132
 
    {
133
 
        printf("Loop %d out of %d.\n", i+1, numLoops );
134
 
        err = TestOnce();
135
 
        if( err < 0 ) return 0;
136
 
    }
137
 
}
138
 
#else
139
 
int main(int argc, char **argv);
140
 
int main(int argc, char **argv)
141
 
{
142
 
    PaError err;
143
 
    int i, numLoops = 10;
144
 
    if( argc > 1 )
145
 
    {
146
 
        numLoops = atoi(argv[1]);
147
 
    }
148
 
    for( i=0; i<numLoops; i++ )
149
 
    {
150
 
        printf("Loop %d out of %d.\n", i+1, numLoops );
151
 
        err = TestOnce();
152
 
        if( err < 0 ) return 1;
153
 
    }
154
 
    printf("Test complete.\n");
155
 
    return 0;
156
 
}
157
 
#endif
158
 
PaError TestOnce( void )
159
 
{
160
 
    PaStreamParameters outputParameters;
161
 
    PaStream *stream;
162
 
    PaError err;
163
 
    paTestData data;
164
 
    int i;
165
 
    int totalSamps;
166
 
    /* initialise sinusoidal wavetable */
167
 
    for( i=0; i<TABLE_SIZE; i++ )
168
 
    {
169
 
        data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
170
 
    }
171
 
    data.left_phase = data.right_phase = 0;
172
 
    data.sampsToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
173
 
    err = Pa_Initialize();
174
 
    if( err != paNoError ) goto error;
175
 
 
176
 
    outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
177
 
    outputParameters.channelCount = 2;                      /* stereo output */
178
 
    outputParameters.sampleFormat = paInt16;
179
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
180
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
181
 
    err = Pa_OpenStream(
182
 
              &stream,
183
 
              NULL,         /* no input */
184
 
              &outputParameters,
185
 
              SAMPLE_RATE,
186
 
              1024,         /* frames per buffer */
187
 
              paClipOff,    /* we won't output out of range samples so don't bother clipping them */
188
 
              patest1Callback,
189
 
              &data );
190
 
    if( err != paNoError ) goto error;
191
 
 
192
 
    err = Pa_StartStream( stream );
193
 
    if( err != paNoError ) goto error;
194
 
    printf("Waiting for sound to finish.\n");
195
 
    Pa_Sleep(1000);
196
 
    err = Pa_CloseStream( stream );
197
 
    if( err != paNoError ) goto error;
198
 
    Pa_Terminate();
199
 
    return paNoError;
200
 
error:
201
 
    Pa_Terminate();
202
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
203
 
    fprintf( stderr, "Error number: %d\n", err );
204
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
205
 
    return err;
206
 
}