annotate src/portaudio_20140130/test/patest_stop.c @ 39:7ddb4fc30dac

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