patest_sine_time.c (7176B)
1 /** @file patest_sine_time.c 2 @ingroup test_src 3 @brief Play a sine wave for several seconds, pausing in the middle. 4 Uses the Pa_GetStreamTime() call. 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 #include <stdio.h> 46 #include <math.h> 47 48 #include "portaudio.h" 49 #include "pa_util.h" 50 51 #define NUM_SECONDS (8) 52 #define SAMPLE_RATE (44100) 53 #define FRAMES_PER_BUFFER (64) 54 55 #ifndef M_PI 56 #define M_PI (3.14159265) 57 #endif 58 #define TWOPI (M_PI * 2.0) 59 60 #define TABLE_SIZE (200) 61 62 typedef struct 63 { 64 double left_phase; 65 double right_phase; 66 volatile PaTime outTime; 67 } 68 paTestData; 69 70 /* This routine will be called by the PortAudio engine when audio is needed. 71 ** It may called at interrupt level on some machines so don't do anything 72 ** that could mess up the system like calling malloc() or free(). 73 */ 74 static int patestCallback( const void *inputBuffer, void *outputBuffer, 75 unsigned long framesPerBuffer, 76 const PaStreamCallbackTimeInfo* timeInfo, 77 PaStreamCallbackFlags statusFlags, 78 void *userData ) 79 { 80 paTestData *data = (paTestData*)userData; 81 float *out = (float*)outputBuffer; 82 unsigned int i; 83 84 double left_phaseInc = 0.02; 85 double right_phaseInc = 0.06; 86 87 double left_phase = data->left_phase; 88 double right_phase = data->right_phase; 89 90 (void) statusFlags; /* Prevent unused variable warnings. */ 91 (void) inputBuffer; 92 93 data->outTime = timeInfo->outputBufferDacTime; 94 95 for( i=0; i<framesPerBuffer; i++ ) 96 { 97 left_phase += left_phaseInc; 98 if( left_phase > TWOPI ) left_phase -= TWOPI; 99 *out++ = (float) sin( left_phase ); 100 101 right_phase += right_phaseInc; 102 if( right_phase > TWOPI ) right_phase -= TWOPI; 103 *out++ = (float) sin( right_phase ); 104 } 105 106 data->left_phase = left_phase; 107 data->right_phase = right_phase; 108 109 return paContinue; 110 } 111 112 /*******************************************************************/ 113 static void ReportStreamTime( PaStream *stream, paTestData *data ); 114 static void ReportStreamTime( PaStream *stream, paTestData *data ) 115 { 116 PaTime streamTime, latency, outTime; 117 118 streamTime = Pa_GetStreamTime( stream ); 119 outTime = data->outTime; 120 if( outTime < 0.0 ) 121 { 122 printf("Stream time = %8.1f\n", streamTime ); 123 } 124 else 125 { 126 latency = outTime - streamTime; 127 printf("Stream time = %8.4f, outTime = %8.4f, latency = %8.4f\n", 128 streamTime, outTime, latency ); 129 } 130 fflush(stdout); 131 } 132 133 /*******************************************************************/ 134 int main(void); 135 int main(void) 136 { 137 PaStreamParameters outputParameters; 138 PaStream *stream; 139 PaError err; 140 paTestData data; 141 PaTime startTime; 142 143 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); 144 145 data.left_phase = data.right_phase = 0; 146 147 err = Pa_Initialize(); 148 if( err != paNoError ) goto error; 149 150 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 151 if (outputParameters.device == paNoDevice) { 152 fprintf(stderr,"Error: No default output device.\n"); 153 goto error; 154 } 155 outputParameters.channelCount = 2; /* stereo output */ 156 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ 157 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 158 outputParameters.hostApiSpecificStreamInfo = NULL; 159 160 err = Pa_OpenStream( 161 &stream, 162 NULL, /* no input */ 163 &outputParameters, 164 SAMPLE_RATE, 165 FRAMES_PER_BUFFER, 166 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 167 patestCallback, 168 &data ); 169 if( err != paNoError ) goto error; 170 171 /* Watch until sound is halfway finished. */ 172 printf("Play for %d seconds.\n", NUM_SECONDS/2 ); fflush(stdout); 173 174 data.outTime = -1.0; /* mark time for callback as undefined */ 175 err = Pa_StartStream( stream ); 176 if( err != paNoError ) goto error; 177 178 startTime = Pa_GetStreamTime( stream ); 179 180 do 181 { 182 ReportStreamTime( stream, &data ); 183 Pa_Sleep(100); 184 } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) ); 185 186 /* Stop sound for 2 seconds. */ 187 err = Pa_StopStream( stream ); 188 if( err != paNoError ) goto error; 189 190 printf("Pause for 2 seconds.\n"); fflush(stdout); 191 Pa_Sleep( 2000 ); 192 193 data.outTime = -1.0; /* mark time for callback as undefined */ 194 err = Pa_StartStream( stream ); 195 if( err != paNoError ) goto error; 196 197 startTime = Pa_GetStreamTime( stream ); 198 199 printf("Play until sound is finished.\n"); fflush(stdout); 200 do 201 { 202 ReportStreamTime( stream, &data ); 203 Pa_Sleep(100); 204 } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) ); 205 206 err = Pa_CloseStream( stream ); 207 if( err != paNoError ) goto error; 208 209 Pa_Terminate(); 210 printf("Test finished.\n"); 211 return err; 212 213 error: 214 Pa_Terminate(); 215 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 216 fprintf( stderr, "Error number: %d\n", err ); 217 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 218 return err; 219 }