Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_underflow.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 <[email protected]>
5 @author Phil Burk <[email protected]>
6 */
7 /*
8 * $Id: patest_underflow.c,v 1.1 2006/06/10 21:30:57 dmazzoni Exp $
9 *
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
13 *
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:
21 *
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
24 *
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.
28 *
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.
36 *
37 */
38 #include <stdio.h>
39 #include <math.h>
40 #include "portaudio.h"
41
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 )
46
47 #ifndef M_PI
48 #define M_PI (3.14159265)
49 #endif
50
51 #define TABLE_SIZE (200)
52 typedef struct
53 {
54 float sine[TABLE_SIZE];
55 int left_phase;
56 int right_phase;
57 int sleepTime;
58 }
59 paTestData;
60
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().
64 */
65 static int patestCallback( const void *inputBuffer, void *outputBuffer,
66 unsigned long framesPerBuffer,
67 const PaStreamCallbackTimeInfo* timeInfo,
68 PaStreamCallbackFlags statusFlags,
69 void *userData )
70 {
71 paTestData *data = (paTestData*)userData;
72 float *out = (float*)outputBuffer;
73 unsigned long i;
74 int finished = 0;
75 (void) inputBuffer; /* Prevent unused variable warnings. */
76 for( i=0; i<framesPerBuffer; i++ )
77 {
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;
84 }
85
86 /* Cause underflow to occur. */
87 if( data->sleepTime > 0 ) Pa_Sleep( data->sleepTime );
88 data->sleepTime += 1;
89
90 return finished;
91 }
92
93 /*******************************************************************/
94 int main(void);
95 int main(void)
96 {
97 PaStreamParameters outputParameters;
98 PaStream *stream;
99 PaError err;
100 paTestData data;
101 int i;
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++ )
105 {
106 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
107 }
108 data.left_phase = data.right_phase = data.sleepTime = 0;
109 err = Pa_Initialize();
110 if( err != paNoError ) goto error;
111
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;
117 err = Pa_OpenStream(
118 &stream,
119 NULL, /* no input */
120 &outputParameters,
121 SAMPLE_RATE,
122 FRAMES_PER_BUFFER,
123 paClipOff, /* we won't output out of range samples so don't bother clipping them */
124 patestCallback,
125 &data );
126 if( err != paNoError ) goto error;
127 err = Pa_StartStream( stream );
128 if( err != paNoError ) goto error;
129
130 while( data.sleepTime < (2 * MSEC_PER_BUFFER) )
131 {
132 printf("SleepTime = %d\n", data.sleepTime );
133 Pa_Sleep( data.sleepTime );
134 }
135
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;
141 Pa_Terminate();
142 printf("Test finished.\n");
143 return err;
144 error:
145 Pa_Terminate();
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 ) );
149 return err;
150 }