1
/** @file patest_underflow.c
2
@brief Simulate an output buffer underflow condition.
3
Tests whether the stream can be stopped when underflowing buffers.
4
@author Ross Bencina <rossb@audiomulch.com>
5
@author Phil Burk <philburk@softsynth.com>
8
* $Id: patest_underflow.c,v 1.1 2006/06/10 21:30:57 dmazzoni Exp $
10
* This program uses the PortAudio Portable Audio Library.
11
* For more information see: http://www.audiomulch.com/portaudio/
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
* Any person wishing to distribute modifications to the Software is
26
* requested to send the modifications to the original developer so that
27
* they can be incorporated into the canonical version.
29
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
33
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40
#include "portaudio.h"
42
#define NUM_SECONDS (20)
43
#define SAMPLE_RATE (44100)
44
#define FRAMES_PER_BUFFER (2048)
45
#define MSEC_PER_BUFFER ( (FRAMES_PER_BUFFER * 1000) / SAMPLE_RATE )
48
#define M_PI (3.14159265)
51
#define TABLE_SIZE (200)
54
float sine[TABLE_SIZE];
61
/* This routine will be called by the PortAudio engine when audio is needed.
62
** It may called at interrupt level on some machines so don't do anything
63
** that could mess up the system like calling malloc() or free().
65
static int patestCallback( const void *inputBuffer, void *outputBuffer,
66
unsigned long framesPerBuffer,
67
const PaStreamCallbackTimeInfo* timeInfo,
68
PaStreamCallbackFlags statusFlags,
71
paTestData *data = (paTestData*)userData;
72
float *out = (float*)outputBuffer;
75
(void) inputBuffer; /* Prevent unused variable warnings. */
76
for( i=0; i<framesPerBuffer; i++ )
78
*out++ = data->sine[data->left_phase]; /* left */
79
*out++ = data->sine[data->right_phase]; /* right */
80
data->left_phase += 1;
81
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
82
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
83
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
86
/* Cause underflow to occur. */
87
if( data->sleepTime > 0 ) Pa_Sleep( data->sleepTime );
93
/*******************************************************************/
97
PaStreamParameters outputParameters;
102
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
103
/* initialise sinusoidal wavetable */
104
for( i=0; i<TABLE_SIZE; i++ )
106
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
108
data.left_phase = data.right_phase = data.sleepTime = 0;
109
err = Pa_Initialize();
110
if( err != paNoError ) goto error;
112
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
113
outputParameters.channelCount = 2; /* stereo output */
114
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
115
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
116
outputParameters.hostApiSpecificStreamInfo = NULL;
123
paClipOff, /* we won't output out of range samples so don't bother clipping them */
126
if( err != paNoError ) goto error;
127
err = Pa_StartStream( stream );
128
if( err != paNoError ) goto error;
130
while( data.sleepTime < (2 * MSEC_PER_BUFFER) )
132
printf("SleepTime = %d\n", data.sleepTime );
133
Pa_Sleep( data.sleepTime );
136
printf("Try to stop stream.\n");
137
err = Pa_StopStream( stream );
138
if( err != paNoError ) goto error;
139
err = Pa_CloseStream( stream );
140
if( err != paNoError ) goto error;
142
printf("Test finished.\n");
146
fprintf( stderr, "An error occured while using the portaudio stream\n" );
147
fprintf( stderr, "Error number: %d\n", err );
148
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );