1 /** @file patest_prime.c
2 @brief Test stream priming mode.
3 @author Ross Bencina http://www.audiomulch.com/~rossb
7 * $Id: patest_prime.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
9 * This program uses the PortAudio Portable Audio Library.
10 * For more information see: http://www.portaudio.com
11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include "portaudio.h"
44 #define SAMPLE_RATE (44100)
45 #define SAMPLE_PERIOD (1.0/44100.0)
46 #define FRAMES_PER_BUFFER (256)
47 #define BEEP_DURATION (400)
48 #define IDLE_DURATION (SAMPLE_RATE*2) /* 2 seconds */
49 #define SLEEP_MSEC (50)
51 #define STATE_BKG_IDLE (0)
52 #define STATE_BKG_BEEPING (1)
64 static void InitializeTestData( paTestData *testData )
66 testData->leftPhase = 0;
67 testData->rightPhase = 0;
68 testData->state = STATE_BKG_BEEPING;
69 testData->beepCountdown = BEEP_DURATION;
70 testData->idleCountdown = IDLE_DURATION;
73 /* This routine will be called by the PortAudio engine when audio is needed.
74 ** It may called at interrupt level on some machines so don't do anything
75 ** that could mess up the system like calling malloc() or free().
77 static int patestCallback( const void *inputBuffer, void *outputBuffer,
78 unsigned long framesPerBuffer,
79 const PaStreamCallbackTimeInfo *timeInfo,
80 PaStreamCallbackFlags statusFlags, void *userData )
82 /* Cast data passed through stream to our structure. */
83 paTestData *data = (paTestData*)userData;
84 float *out = (float*)outputBuffer;
86 int result = paContinue;
88 /* supress unused parameter warnings */
93 for( i=0; i<framesPerBuffer; i++ )
98 *out++ = 0.0; /* left */
99 *out++ = 0.0; /* right */
100 --data->idleCountdown;
102 if( data->idleCountdown <= 0 ) result = paComplete;
105 case STATE_BKG_BEEPING:
106 if( data->beepCountdown <= 0 )
108 data->state = STATE_BKG_IDLE;
109 *out++ = 0.0; /* left */
110 *out++ = 0.0; /* right */
114 /* Play sawtooth wave. */
115 *out++ = data->leftPhase; /* left */
116 *out++ = data->rightPhase; /* right */
117 /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
118 data->leftPhase += 0.01f;
119 /* When signal reaches top, drop back down. */
120 if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f;
121 /* higher pitch so we can distinguish left and right. */
122 data->rightPhase += 0.03f;
123 if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f;
125 --data->beepCountdown;
133 /*******************************************************************/
134 static PaError DoTest( int flags )
139 PaStreamParameters outputParameters;
141 InitializeTestData( &data );
143 outputParameters.device = Pa_GetDefaultOutputDevice();
144 outputParameters.channelCount = 2;
145 outputParameters.hostApiSpecificStreamInfo = NULL;
146 outputParameters.sampleFormat = paFloat32;
147 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
149 /* Open an audio I/O stream. */
155 FRAMES_PER_BUFFER, /* frames per buffer */
156 paClipOff | flags, /* we won't output out of range samples so don't bother clipping them */
159 if( err != paNoError ) goto error;
162 err = Pa_StartStream( stream );
163 if( err != paNoError ) goto error;
165 printf("hear \"BEEP\"\n" );
168 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC);
169 if( err < 0 ) goto error;
171 err = Pa_StopStream( stream );
172 if( err != paNoError ) goto error;
174 err = Pa_CloseStream( stream );
175 if( err != paNoError ) goto error;
182 /*******************************************************************/
186 PaError err = paNoError;
189 /* Initialize library before making any other calls. */
190 err = Pa_Initialize();
191 if( err != paNoError ) goto error;
193 printf("PortAudio Test: Testing stream playback with no priming.\n");
194 printf("PortAudio Test: you should see BEEP before you hear it.\n");
195 printf("BEEP %d times.\n", NUM_BEEPS );
197 for( i=0; i< NUM_BEEPS; ++i )
200 if( err != paNoError )
204 printf("PortAudio Test: Testing stream playback with priming.\n");
205 printf("PortAudio Test: you should see BEEP around the same time you hear it.\n");
206 for( i=0; i< NUM_BEEPS; ++i )
208 err = DoTest( paPrimeOutputBuffersUsingStreamCallback );
209 if( err != paNoError )
213 printf("Test finished.\n");
219 fprintf( stderr, "An error occured while using the portaudio stream\n" );
220 fprintf( stderr, "Error number: %d\n", err );
221 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );