| 1 |
/** @file patest_stop.c |
| 2 |
@brief Test different ways of stopping audio. |
| 3 |
|
| 4 |
Test the three ways of stopping audio: |
| 5 |
- calling Pa_StopStream(), |
| 6 |
- calling Pa_AbortStream(), |
| 7 |
- and returning a 1 from the callback function. |
| 8 |
|
| 9 |
A long latency is set up so that you can hear the difference. |
| 10 |
Then a simple 8 note sequence is repeated twice. |
| 11 |
The program will print what you should hear. |
| 12 |
|
| 13 |
@author Phil Burk <[email protected]> |
| 14 |
*/ |
| 15 |
/* |
| 16 |
* $Id: patest_stop.c,v 1.1 2006/06/10 21:30:57 dmazzoni Exp $ |
| 17 |
* |
| 18 |
* This program uses the PortAudio Portable Audio Library. |
| 19 |
* For more information see: http://www.portaudio.com |
| 20 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 21 |
* |
| 22 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 23 |
* a copy of this software and associated documentation files |
| 24 |
* (the "Software"), to deal in the Software without restriction, |
| 25 |
* including without limitation the rights to use, copy, modify, merge, |
| 26 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 27 |
* and to permit persons to whom the Software is furnished to do so, |
| 28 |
* subject to the following conditions: |
| 29 |
* |
| 30 |
* The above copyright notice and this permission notice shall be |
| 31 |
* included in all copies or substantial portions of the Software. |
| 32 |
* |
| 33 |
* Any person wishing to distribute modifications to the Software is |
| 34 |
* requested to send the modifications to the original developer so that |
| 35 |
* they can be incorporated into the canonical version. |
| 36 |
* |
| 37 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 38 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 39 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 40 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 41 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 42 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 43 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 44 |
* |
| 45 |
*/ |
| 46 |
#include <stdio.h> |
| 47 |
#include <math.h> |
| 48 |
#include "portaudio.h" |
| 49 |
|
| 50 |
#define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice()) |
| 51 |
#define SLEEP_DUR (200) |
| 52 |
#define SAMPLE_RATE (44100) |
| 53 |
#define FRAMES_PER_BUFFER (256) |
| 54 |
#define LATENCY_SECONDS (3.f) |
| 55 |
#define FRAMES_PER_NOTE (SAMPLE_RATE/2) |
| 56 |
#define MAX_REPEATS (2) |
| 57 |
#define FUNDAMENTAL (400.0f / SAMPLE_RATE) |
| 58 |
#define NOTE_0 (FUNDAMENTAL * 1.0f / 1.0f) |
| 59 |
#define NOTE_1 (FUNDAMENTAL * 5.0f / 4.0f) |
| 60 |
#define NOTE_2 (FUNDAMENTAL * 4.0f / 3.0f) |
| 61 |
#define NOTE_3 (FUNDAMENTAL * 3.0f / 2.0f) |
| 62 |
#define NOTE_4 (FUNDAMENTAL * 2.0f / 1.0f) |
| 63 |
#define MODE_FINISH (0) |
| 64 |
#define MODE_STOP (1) |
| 65 |
#define MODE_ABORT (2) |
| 66 |
#ifndef M_PI |
| 67 |
#define M_PI (3.14159265) |
| 68 |
#endif |
| 69 |
#define TABLE_SIZE (400) |
| 70 |
|
| 71 |
typedef struct |
| 72 |
{ |
| 73 |
float waveform[TABLE_SIZE + 1]; /* Add one for guard point for interpolation. */ |
| 74 |
float phase_increment; |
| 75 |
float phase; |
| 76 |
float *tune; |
| 77 |
int notesPerTune; |
| 78 |
int frameCounter; |
| 79 |
int noteCounter; |
| 80 |
int repeatCounter; |
| 81 |
PaTime outTime; |
| 82 |
int stopMode; |
| 83 |
int done; |
| 84 |
} |
| 85 |
paTestData; |
| 86 |
|
| 87 |
/************* Prototypes *****************************/ |
| 88 |
int TestStopMode( paTestData *data ); |
| 89 |
float LookupWaveform( paTestData *data, float phase ); |
| 90 |
|
| 91 |
/****************************************************** |
| 92 |
* Convert phase between 0.0 and 1.0 to waveform value |
| 93 |
* using linear interpolation. |
| 94 |
*/ |
| 95 |
float LookupWaveform( paTestData *data, float phase ) |
| 96 |
{ |
| 97 |
float fIndex = phase*TABLE_SIZE; |
| 98 |
int index = (int) fIndex; |
| 99 |
float fract = fIndex - index; |
| 100 |
float lo = data->waveform[index]; |
| 101 |
float hi = data->waveform[index+1]; |
| 102 |
float val = lo + fract*(hi-lo); |
| 103 |
return val; |
| 104 |
} |
| 105 |
|
| 106 |
/* This routine will be called by the PortAudio engine when audio is needed. |
| 107 |
** It may called at interrupt level on some machines so don't do anything |
| 108 |
** that could mess up the system like calling malloc() or free(). |
| 109 |
*/ |
| 110 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 111 |
unsigned long framesPerBuffer, |
| 112 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 113 |
PaStreamCallbackFlags statusFlags, |
| 114 |
void *userData ) |
| 115 |
{ |
| 116 |
paTestData *data = (paTestData*)userData; |
| 117 |
float *out = (float*)outputBuffer; |
| 118 |
float value; |
| 119 |
unsigned int i = 0; |
| 120 |
int finished = paContinue; |
| 121 |
|
| 122 |
(void) inputBuffer; /* Prevent unused variable warnings. */ |
| 123 |
(void) timeInfo; |
| 124 |
(void) statusFlags; |
| 125 |
|
| 126 |
|
| 127 |
/* data->outTime = outTime; */ |
| 128 |
|
| 129 |
if( !data->done ) |
| 130 |
{ |
| 131 |
for( i=0; i<framesPerBuffer; i++ ) |
| 132 |
{ |
| 133 |
/* Are we done with this note? */ |
| 134 |
if( data->frameCounter >= FRAMES_PER_NOTE ) |
| 135 |
{ |
| 136 |
data->noteCounter += 1; |
| 137 |
data->frameCounter = 0; |
| 138 |
/* Are we done with this tune? */ |
| 139 |
if( data->noteCounter >= data->notesPerTune ) |
| 140 |
{ |
| 141 |
data->noteCounter = 0; |
| 142 |
data->repeatCounter += 1; |
| 143 |
/* Are we totally done? */ |
| 144 |
if( data->repeatCounter >= MAX_REPEATS ) |
| 145 |
{ |
| 146 |
data->done = 1; |
| 147 |
if( data->stopMode == MODE_FINISH ) |
| 148 |
{ |
| 149 |
finished = paComplete; |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
data->phase_increment = data->tune[data->noteCounter]; |
| 155 |
} |
| 156 |
value = LookupWaveform(data, data->phase); |
| 157 |
*out++ = value; /* left */ |
| 158 |
*out++ = value; /* right */ |
| 159 |
data->phase += data->phase_increment; |
| 160 |
if( data->phase >= 1.0f ) data->phase -= 1.0f; |
| 161 |
|
| 162 |
data->frameCounter += 1; |
| 163 |
} |
| 164 |
} |
| 165 |
/* zero remainder of final buffer */ |
| 166 |
for( ; i<framesPerBuffer; i++ ) |
| 167 |
{ |
| 168 |
*out++ = 0; /* left */ |
| 169 |
*out++ = 0; /* right */ |
| 170 |
} |
| 171 |
return finished; |
| 172 |
} |
| 173 |
/*******************************************************************/ |
| 174 |
int main(void); |
| 175 |
int main(void) |
| 176 |
{ |
| 177 |
paTestData data; |
| 178 |
int i; |
| 179 |
float simpleTune[] = { NOTE_0, NOTE_1, NOTE_2, NOTE_3, NOTE_4, NOTE_3, NOTE_2, NOTE_1 }; |
| 180 |
|
| 181 |
printf("PortAudio Test: play song and test stopping. ask for %f seconds latency\n", LATENCY_SECONDS ); |
| 182 |
/* initialise sinusoidal wavetable */ |
| 183 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 184 |
{ |
| 185 |
data.waveform[i] = (float) ( |
| 186 |
(0.2 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )) + |
| 187 |
(0.2 * sin( ((double)(3*i)/(double)TABLE_SIZE) * M_PI * 2. )) + |
| 188 |
(0.1 * sin( ((double)(5*i)/(double)TABLE_SIZE) * M_PI * 2. )) |
| 189 |
); |
| 190 |
} |
| 191 |
data.waveform[TABLE_SIZE] = data.waveform[0]; /* Set guard point. */ |
| 192 |
data.tune = &simpleTune[0]; |
| 193 |
data.notesPerTune = sizeof(simpleTune) / sizeof(float); |
| 194 |
|
| 195 |
printf("Test MODE_FINISH - callback returns 1.\n"); |
| 196 |
printf("Should hear entire %d note tune repeated twice.\n", data.notesPerTune); |
| 197 |
data.stopMode = MODE_FINISH; |
| 198 |
if( TestStopMode( &data ) != paNoError ) |
| 199 |
{ |
| 200 |
printf("Test of MODE_FINISH failed!\n"); |
| 201 |
goto error; |
| 202 |
} |
| 203 |
|
| 204 |
printf("Test MODE_STOP - stop when song is done.\n"); |
| 205 |
printf("Should hear entire %d note tune repeated twice.\n", data.notesPerTune); |
| 206 |
data.stopMode = MODE_STOP; |
| 207 |
if( TestStopMode( &data ) != paNoError ) |
| 208 |
{ |
| 209 |
printf("Test of MODE_STOP failed!\n"); |
| 210 |
goto error; |
| 211 |
} |
| 212 |
|
| 213 |
printf("Test MODE_ABORT - abort immediately.\n"); |
| 214 |
printf("Should hear last repetition cut short by %f seconds.\n", LATENCY_SECONDS); |
| 215 |
data.stopMode = MODE_ABORT; |
| 216 |
if( TestStopMode( &data ) != paNoError ) |
| 217 |
{ |
| 218 |
printf("Test of MODE_ABORT failed!\n"); |
| 219 |
goto error; |
| 220 |
} |
| 221 |
|
| 222 |
return 0; |
| 223 |
|
| 224 |
error: |
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
int TestStopMode( paTestData *data ) |
| 229 |
{ |
| 230 |
PaStreamParameters outputParameters; |
| 231 |
PaStream *stream; |
| 232 |
PaError err; |
| 233 |
|
| 234 |
data->done = 0; |
| 235 |
data->phase = 0.0; |
| 236 |
data->frameCounter = 0; |
| 237 |
data->noteCounter = 0; |
| 238 |
data->repeatCounter = 0; |
| 239 |
data->phase_increment = data->tune[data->noteCounter]; |
| 240 |
|
| 241 |
err = Pa_Initialize(); |
| 242 |
if( err != paNoError ) goto error; |
| 243 |
|
| 244 |
outputParameters.device = OUTPUT_DEVICE; |
| 245 |
outputParameters.channelCount = 2; /* stereo output */ |
| 246 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ |
| 247 |
outputParameters.suggestedLatency = LATENCY_SECONDS; |
| 248 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 249 |
|
| 250 |
err = Pa_OpenStream( |
| 251 |
&stream, |
| 252 |
NULL, /* no input */ |
| 253 |
&outputParameters, |
| 254 |
SAMPLE_RATE, |
| 255 |
FRAMES_PER_BUFFER, /* frames per buffer */ |
| 256 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
| 257 |
patestCallback, |
| 258 |
data ); |
| 259 |
if( err != paNoError ) goto error; |
| 260 |
|
| 261 |
err = Pa_StartStream( stream ); |
| 262 |
if( err != paNoError ) goto error; |
| 263 |
|
| 264 |
if( data->stopMode == MODE_FINISH ) |
| 265 |
{ |
| 266 |
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) |
| 267 |
{ |
| 268 |
/*printf("outTime = %g, note# = %d, repeat# = %d\n", data->outTime, |
| 269 |
data->noteCounter, data->repeatCounter ); |
| 270 |
fflush(stdout); */ |
| 271 |
Pa_Sleep( SLEEP_DUR ); |
| 272 |
} |
| 273 |
if( err < 0 ) goto error; |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
while( data->repeatCounter < MAX_REPEATS ) |
| 278 |
{ |
| 279 |
/*printf("outTime = %g, note# = %d, repeat# = %d\n", data->outTime, |
| 280 |
data->noteCounter, data->repeatCounter ); |
| 281 |
fflush(stdout); */ |
| 282 |
Pa_Sleep( SLEEP_DUR ); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if( data->stopMode == MODE_ABORT ) |
| 287 |
{ |
| 288 |
printf("Call Pa_AbortStream()\n"); |
| 289 |
err = Pa_AbortStream( stream ); |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
printf("Call Pa_StopStream()\n"); |
| 294 |
err = Pa_StopStream( stream ); |
| 295 |
} |
| 296 |
if( err != paNoError ) goto error; |
| 297 |
|
| 298 |
printf("Call Pa_CloseStream()\n"); fflush(stdout); |
| 299 |
err = Pa_CloseStream( stream ); |
| 300 |
if( err != paNoError ) goto error; |
| 301 |
|
| 302 |
Pa_Terminate(); |
| 303 |
printf("Test finished.\n"); |
| 304 |
|
| 305 |
return err; |
| 306 |
|
| 307 |
error: |
| 308 |
Pa_Terminate(); |
| 309 |
fprintf( stderr, "An error occured while using the portaudio stream\n" ); |
| 310 |
fprintf( stderr, "Error number: %d\n", err ); |
| 311 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 312 |
return err; |
| 313 |
} |