patest_underflow.c (5970B)
1 /** @file patest_underflow.c 2 @ingroup test_src 3 @brief Simulate an output buffer underflow condition. 4 Tests whether the stream can be stopped when underflowing buffers. 5 @author Ross Bencina <rossb@audiomulch.com> 6 @author Phil Burk <philburk@softsynth.com> 7 */ 8 /* 9 * $Id$ 10 * 11 * This program uses the PortAudio Portable Audio Library. 12 * For more information see: http://www.portaudio.com 13 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files 17 * (the "Software"), to deal in the Software without restriction, 18 * including without limitation the rights to use, copy, modify, merge, 19 * publish, distribute, sublicense, and/or sell copies of the Software, 20 * and to permit persons to whom the Software is furnished to do so, 21 * subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 29 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 31 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 */ 34 35 /* 36 * The text above constitutes the entire PortAudio license; however, 37 * the PortAudio community also makes the following non-binding requests: 38 * 39 * Any person wishing to distribute modifications to the Software is 40 * requested to send the modifications to the original developer so that 41 * they can be incorporated into the canonical version. It is also 42 * requested that these non-binding requests be included along with the 43 * license above. 44 */ 45 46 #include <stdio.h> 47 #include <math.h> 48 #include "portaudio.h" 49 50 #define NUM_SECONDS (20) 51 #define SAMPLE_RATE (44100) 52 #define FRAMES_PER_BUFFER (2048) 53 #define MSEC_PER_BUFFER ( (FRAMES_PER_BUFFER * 1000) / SAMPLE_RATE ) 54 55 #ifndef M_PI 56 #define M_PI (3.14159265) 57 #endif 58 59 #define TABLE_SIZE (200) 60 typedef struct 61 { 62 float sine[TABLE_SIZE]; 63 int left_phase; 64 int right_phase; 65 int sleepTime; 66 } 67 paTestData; 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 patestCallback( 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 float *out = (float*)outputBuffer; 81 unsigned long i; 82 int finished = 0; 83 (void) inputBuffer; /* Prevent unused variable warnings. */ 84 for( i=0; i<framesPerBuffer; i++ ) 85 { 86 *out++ = data->sine[data->left_phase]; /* left */ 87 *out++ = data->sine[data->right_phase]; /* right */ 88 data->left_phase += 1; 89 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; 90 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ 91 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; 92 } 93 94 /* Cause underflow to occur. */ 95 if( data->sleepTime > 0 ) Pa_Sleep( data->sleepTime ); 96 data->sleepTime += 1; 97 98 return finished; 99 } 100 101 /*******************************************************************/ 102 int main(void); 103 int main(void) 104 { 105 PaStreamParameters outputParameters; 106 PaStream *stream; 107 PaError err; 108 paTestData data; 109 int i; 110 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 111 /* initialise sinusoidal wavetable */ 112 for( i=0; i<TABLE_SIZE; i++ ) 113 { 114 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 115 } 116 data.left_phase = data.right_phase = data.sleepTime = 0; 117 err = Pa_Initialize(); 118 if( err != paNoError ) goto error; 119 120 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 121 if (outputParameters.device == paNoDevice) { 122 fprintf(stderr,"Error: No default output device.\n"); 123 goto error; 124 } 125 outputParameters.channelCount = 2; /* stereo output */ 126 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 127 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 128 outputParameters.hostApiSpecificStreamInfo = NULL; 129 err = Pa_OpenStream( 130 &stream, 131 NULL, /* no input */ 132 &outputParameters, 133 SAMPLE_RATE, 134 FRAMES_PER_BUFFER, 135 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 136 patestCallback, 137 &data ); 138 if( err != paNoError ) goto error; 139 err = Pa_StartStream( stream ); 140 if( err != paNoError ) goto error; 141 142 while( data.sleepTime < (2 * MSEC_PER_BUFFER) ) 143 { 144 printf("SleepTime = %d\n", data.sleepTime ); 145 Pa_Sleep( data.sleepTime ); 146 } 147 148 printf("Try to stop stream.\n"); 149 err = Pa_StopStream( stream ); 150 if( err != paNoError ) goto error; 151 err = Pa_CloseStream( stream ); 152 if( err != paNoError ) goto error; 153 Pa_Terminate(); 154 printf("Test finished.\n"); 155 return err; 156 error: 157 Pa_Terminate(); 158 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 159 fprintf( stderr, "Error number: %d\n", err ); 160 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 161 return err; 162 }