| 1 |
/** @file patest_many.c |
| 2 |
@brief Start and stop the PortAudio Driver multiple times. |
| 3 |
@author Phil Burk http://www.softsynth.com |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: patest_many.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 7 |
* |
| 8 |
* This program uses the PortAudio Portable Audio Library. |
| 9 |
* For more information see: http://www.portaudio.com |
| 10 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 11 |
* |
| 12 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 13 |
* a copy of this software and associated documentation files |
| 14 |
* (the "Software"), to deal in the Software without restriction, |
| 15 |
* including without limitation the rights to use, copy, modify, merge, |
| 16 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 |
* and to permit persons to whom the Software is furnished to do so, |
| 18 |
* subject to the following conditions: |
| 19 |
* |
| 20 |
* The above copyright notice and this permission notice shall be |
| 21 |
* included in all copies or substantial portions of the Software. |
| 22 |
* |
| 23 |
* Any person wishing to distribute modifications to the Software is |
| 24 |
* requested to send the modifications to the original developer so that |
| 25 |
* they can be incorporated into the canonical version. |
| 26 |
* |
| 27 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 30 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 31 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 |
* |
| 35 |
*/ |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <math.h> |
| 39 |
#include "portaudio.h" |
| 40 |
#define NUM_SECONDS (1) |
| 41 |
#define SAMPLE_RATE (44100) |
| 42 |
#ifndef M_PI |
| 43 |
#define M_PI (3.14159265) |
| 44 |
#endif |
| 45 |
#define TABLE_SIZE (200) |
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
short sine[TABLE_SIZE]; |
| 49 |
int left_phase; |
| 50 |
int right_phase; |
| 51 |
unsigned int sampsToGo; |
| 52 |
} |
| 53 |
paTestData; |
| 54 |
PaError TestOnce( void ); |
| 55 |
static int patest1Callback( const void *inputBuffer, void *outputBuffer, |
| 56 |
unsigned long framesPerBuffer, |
| 57 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 58 |
PaStreamCallbackFlags statusFlags, |
| 59 |
void *userData ); |
| 60 |
|
| 61 |
/* This routine will be called by the PortAudio engine when audio is needed. |
| 62 |
** It may called at interrupt level on some machines so don't do anything |
| 63 |
** that could mess up the system like calling malloc() or free(). |
| 64 |
*/ |
| 65 |
static int patest1Callback( const void *inputBuffer, void *outputBuffer, |
| 66 |
unsigned long framesPerBuffer, |
| 67 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 68 |
PaStreamCallbackFlags statusFlags, |
| 69 |
void *userData ) |
| 70 |
{ |
| 71 |
paTestData *data = (paTestData*)userData; |
| 72 |
short *out = (short*)outputBuffer; |
| 73 |
unsigned int i; |
| 74 |
int finished = 0; |
| 75 |
(void) inputBuffer; /* Prevent "unused variable" warnings. */ |
| 76 |
|
| 77 |
if( data->sampsToGo < framesPerBuffer ) |
| 78 |
{ |
| 79 |
/* final buffer... */ |
| 80 |
|
| 81 |
for( i=0; i<data->sampsToGo; i++ ) |
| 82 |
{ |
| 83 |
*out++ = data->sine[data->left_phase]; /* left */ |
| 84 |
*out++ = data->sine[data->right_phase]; /* right */ |
| 85 |
data->left_phase += 1; |
| 86 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 87 |
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ |
| 88 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 89 |
} |
| 90 |
/* zero remainder of final buffer */ |
| 91 |
for( ; i<framesPerBuffer; i++ ) |
| 92 |
{ |
| 93 |
*out++ = 0; /* left */ |
| 94 |
*out++ = 0; /* right */ |
| 95 |
} |
| 96 |
|
| 97 |
finished = 1; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
for( i=0; i<framesPerBuffer; i++ ) |
| 102 |
{ |
| 103 |
*out++ = data->sine[data->left_phase]; /* left */ |
| 104 |
*out++ = data->sine[data->right_phase]; /* right */ |
| 105 |
data->left_phase += 1; |
| 106 |
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; |
| 107 |
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ |
| 108 |
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; |
| 109 |
} |
| 110 |
data->sampsToGo -= framesPerBuffer; |
| 111 |
} |
| 112 |
return finished; |
| 113 |
} |
| 114 |
/*******************************************************************/ |
| 115 |
#ifdef MACINTOSH |
| 116 |
int main(void); |
| 117 |
int main(void) |
| 118 |
{ |
| 119 |
int i; |
| 120 |
PaError err; |
| 121 |
int numLoops = 10; |
| 122 |
printf("Loop %d times.\n", numLoops ); |
| 123 |
for( i=0; i<numLoops; i++ ) |
| 124 |
{ |
| 125 |
printf("Loop %d out of %d.\n", i+1, numLoops ); |
| 126 |
err = TestOnce(); |
| 127 |
if( err < 0 ) return 0; |
| 128 |
} |
| 129 |
} |
| 130 |
#else |
| 131 |
int main(int argc, char **argv); |
| 132 |
int main(int argc, char **argv) |
| 133 |
{ |
| 134 |
PaError err; |
| 135 |
int i, numLoops = 10; |
| 136 |
if( argc > 1 ) |
| 137 |
{ |
| 138 |
numLoops = atoi(argv[1]); |
| 139 |
} |
| 140 |
for( i=0; i<numLoops; i++ ) |
| 141 |
{ |
| 142 |
printf("Loop %d out of %d.\n", i+1, numLoops ); |
| 143 |
err = TestOnce(); |
| 144 |
if( err < 0 ) return 1; |
| 145 |
} |
| 146 |
printf("Test complete.\n"); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
#endif |
| 150 |
PaError TestOnce( void ) |
| 151 |
{ |
| 152 |
PaStreamParameters outputParameters; |
| 153 |
PaStream *stream; |
| 154 |
PaError err; |
| 155 |
paTestData data; |
| 156 |
int i; |
| 157 |
int totalSamps; |
| 158 |
/* initialise sinusoidal wavetable */ |
| 159 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 160 |
{ |
| 161 |
data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); |
| 162 |
} |
| 163 |
data.left_phase = data.right_phase = 0; |
| 164 |
data.sampsToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */ |
| 165 |
err = Pa_Initialize(); |
| 166 |
if( err != paNoError ) goto error; |
| 167 |
|
| 168 |
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ |
| 169 |
outputParameters.channelCount = 2; /* stereo output */ |
| 170 |
outputParameters.sampleFormat = paInt16; |
| 171 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 172 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 173 |
err = Pa_OpenStream( |
| 174 |
&stream, |
| 175 |
NULL, /* no input */ |
| 176 |
&outputParameters, |
| 177 |
SAMPLE_RATE, |
| 178 |
1024, /* frames per buffer */ |
| 179 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
| 180 |
patest1Callback, |
| 181 |
&data ); |
| 182 |
if( err != paNoError ) goto error; |
| 183 |
|
| 184 |
err = Pa_StartStream( stream ); |
| 185 |
if( err != paNoError ) goto error; |
| 186 |
printf("Waiting for sound to finish.\n"); |
| 187 |
Pa_Sleep(1000); |
| 188 |
err = Pa_CloseStream( stream ); |
| 189 |
if( err != paNoError ) goto error; |
| 190 |
Pa_Terminate(); |
| 191 |
return paNoError; |
| 192 |
error: |
| 193 |
Pa_Terminate(); |
| 194 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 195 |
fprintf( stderr, "Error number: %d\n", err ); |
| 196 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 197 |
return err; |
| 198 |
} |