1
/** @file patest_many.c
3
@brief Start and stop the PortAudio Driver multiple times.
4
@author Phil Burk http://www.softsynth.com
7
* $Id: patest_many.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
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
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:
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
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.
34
* The text above constitutes the entire PortAudio license; however,
35
* the PortAudio community also makes the following non-binding requests:
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
47
#include "portaudio.h"
48
#define NUM_SECONDS (1)
49
#define SAMPLE_RATE (44100)
51
#define M_PI (3.14159265)
53
#define TABLE_SIZE (200)
56
short sine[TABLE_SIZE];
59
unsigned int sampsToGo;
62
PaError TestOnce( void );
63
static int patest1Callback( const void *inputBuffer, void *outputBuffer,
64
unsigned long framesPerBuffer,
65
const PaStreamCallbackTimeInfo* timeInfo,
66
PaStreamCallbackFlags statusFlags,
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().
73
static int patest1Callback( const void *inputBuffer, void *outputBuffer,
74
unsigned long framesPerBuffer,
75
const PaStreamCallbackTimeInfo* timeInfo,
76
PaStreamCallbackFlags statusFlags,
79
paTestData *data = (paTestData*)userData;
80
short *out = (short*)outputBuffer;
83
(void) inputBuffer; /* Prevent "unused variable" warnings. */
85
if( data->sampsToGo < framesPerBuffer )
89
for( i=0; i<data->sampsToGo; i++ )
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;
98
/* zero remainder of final buffer */
99
for( ; i<framesPerBuffer; i++ )
101
*out++ = 0; /* left */
102
*out++ = 0; /* right */
109
for( i=0; i<framesPerBuffer; i++ )
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;
118
data->sampsToGo -= framesPerBuffer;
122
/*******************************************************************/
130
printf("Loop %d times.\n", numLoops );
131
for( i=0; i<numLoops; i++ )
133
printf("Loop %d out of %d.\n", i+1, numLoops );
135
if( err < 0 ) return 0;
139
int main(int argc, char **argv);
140
int main(int argc, char **argv)
143
int i, numLoops = 10;
146
numLoops = atoi(argv[1]);
148
for( i=0; i<numLoops; i++ )
150
printf("Loop %d out of %d.\n", i+1, numLoops );
152
if( err < 0 ) return 1;
154
printf("Test complete.\n");
158
PaError TestOnce( void )
160
PaStreamParameters outputParameters;
166
/* initialise sinusoidal wavetable */
167
for( i=0; i<TABLE_SIZE; i++ )
169
data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
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;
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;
186
1024, /* frames per buffer */
187
paClipOff, /* we won't output out of range samples so don't bother clipping them */
190
if( err != paNoError ) goto error;
192
err = Pa_StartStream( stream );
193
if( err != paNoError ) goto error;
194
printf("Waiting for sound to finish.\n");
196
err = Pa_CloseStream( stream );
197
if( err != paNoError ) goto error;
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 ) );