Debian Codegraph @

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

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