1
/** @file patest_prime.c
3
@brief Test stream priming mode.
4
@author Ross Bencina http://www.audiomulch.com/~rossb
8
* $Id: patest_prime.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
10
* This program uses the PortAudio Portable Audio Library.
11
* For more information see: http://www.portaudio.com
12
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14
* Permission is hereby granted, free of charge, to any person obtaining
15
* a copy of this software and associated documentation files
16
* (the "Software"), to deal in the Software without restriction,
17
* including without limitation the rights to use, copy, modify, merge,
18
* publish, distribute, sublicense, and/or sell copies of the Software,
19
* and to permit persons to whom the Software is furnished to do so,
20
* subject to the following conditions:
22
* The above copyright notice and this permission notice shall be
23
* included in all copies or substantial portions of the Software.
25
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
* The text above constitutes the entire PortAudio license; however,
36
* the PortAudio community also makes the following non-binding requests:
38
* Any person wishing to distribute modifications to the Software is
39
* requested to send the modifications to the original developer so that
40
* they can be incorporated into the canonical version. It is also
41
* requested that these non-binding requests be included along with the
47
#include "portaudio.h"
51
#define SAMPLE_RATE (44100)
52
#define SAMPLE_PERIOD (1.0/44100.0)
53
#define FRAMES_PER_BUFFER (256)
54
#define BEEP_DURATION (400)
55
#define IDLE_DURATION (SAMPLE_RATE*2) /* 2 seconds */
56
#define SLEEP_MSEC (50)
58
#define STATE_BKG_IDLE (0)
59
#define STATE_BKG_BEEPING (1)
71
static void InitializeTestData( paTestData *testData )
73
testData->leftPhase = 0;
74
testData->rightPhase = 0;
75
testData->state = STATE_BKG_BEEPING;
76
testData->beepCountdown = BEEP_DURATION;
77
testData->idleCountdown = IDLE_DURATION;
80
/* This routine will be called by the PortAudio engine when audio is needed.
81
** It may called at interrupt level on some machines so don't do anything
82
** that could mess up the system like calling malloc() or free().
84
static int patestCallback( const void *inputBuffer, void *outputBuffer,
85
unsigned long framesPerBuffer,
86
const PaStreamCallbackTimeInfo *timeInfo,
87
PaStreamCallbackFlags statusFlags, void *userData )
89
/* Cast data passed through stream to our structure. */
90
paTestData *data = (paTestData*)userData;
91
float *out = (float*)outputBuffer;
93
int result = paContinue;
95
/* supress unused parameter warnings */
100
for( i=0; i<framesPerBuffer; i++ )
102
switch( data->state )
105
*out++ = 0.0; /* left */
106
*out++ = 0.0; /* right */
107
--data->idleCountdown;
109
if( data->idleCountdown <= 0 ) result = paComplete;
112
case STATE_BKG_BEEPING:
113
if( data->beepCountdown <= 0 )
115
data->state = STATE_BKG_IDLE;
116
*out++ = 0.0; /* left */
117
*out++ = 0.0; /* right */
121
/* Play sawtooth wave. */
122
*out++ = data->leftPhase; /* left */
123
*out++ = data->rightPhase; /* right */
124
/* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
125
data->leftPhase += 0.01f;
126
/* When signal reaches top, drop back down. */
127
if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f;
128
/* higher pitch so we can distinguish left and right. */
129
data->rightPhase += 0.03f;
130
if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f;
132
--data->beepCountdown;
140
/*******************************************************************/
141
static PaError DoTest( int flags )
146
PaStreamParameters outputParameters;
148
InitializeTestData( &data );
150
outputParameters.device = Pa_GetDefaultOutputDevice();
151
outputParameters.channelCount = 2;
152
outputParameters.hostApiSpecificStreamInfo = NULL;
153
outputParameters.sampleFormat = paFloat32;
154
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
156
/* Open an audio I/O stream. */
162
FRAMES_PER_BUFFER, /* frames per buffer */
163
paClipOff | flags, /* we won't output out of range samples so don't bother clipping them */
166
if( err != paNoError ) goto error;
169
err = Pa_StartStream( stream );
170
if( err != paNoError ) goto error;
172
printf("hear \"BEEP\"\n" );
175
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC);
176
if( err < 0 ) goto error;
178
err = Pa_StopStream( stream );
179
if( err != paNoError ) goto error;
181
err = Pa_CloseStream( stream );
182
if( err != paNoError ) goto error;
189
/*******************************************************************/
193
PaError err = paNoError;
196
/* Initialize library before making any other calls. */
197
err = Pa_Initialize();
198
if( err != paNoError ) goto error;
200
printf("PortAudio Test: Testing stream playback with no priming.\n");
201
printf("PortAudio Test: you should see BEEP before you hear it.\n");
202
printf("BEEP %d times.\n", NUM_BEEPS );
204
for( i=0; i< NUM_BEEPS; ++i )
207
if( err != paNoError )
211
printf("PortAudio Test: Testing stream playback with priming.\n");
212
printf("PortAudio Test: you should see BEEP around the same time you hear it.\n");
213
for( i=0; i< NUM_BEEPS; ++i )
215
err = DoTest( paPrimeOutputBuffersUsingStreamCallback );
216
if( err != paNoError )
220
printf("Test finished.\n");
226
fprintf( stderr, "An error occured while using the portaudio stream\n" );
227
fprintf( stderr, "Error number: %d\n", err );
228
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );