Debian Codegraph @

src/libout123/modules/portaudio.c

raw
1 /*
2 portaudio: audio output via PortAudio cross-platform audio API
3
4 copyright 2006-2020 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Nicholas J. Humfrey
7 */
8
9 /* Need usleep(). */
10 #define _DEFAULT_SOURCE
11 #define _BSD_SOURCE
12 #include "../out123_int.h"
13 #include <math.h>
14 #include <portaudio.h>
15
16 #ifdef WIN32
17 #include <windows.h>
18 #endif
19
20 /* Including the sfifo code locally, to avoid module linkage issues. */
21 #define SFIFO_STATIC
22 #include "../sfifo.c"
23
24 #include "../../common/debug.h"
25
26 #define SAMPLE_SIZE (2)
27 #define FRAMES_PER_BUFFER (256)
28 #define FIFO_DURATION (ao->device_buffer > 0. ? ao->device_buffer : 0.5f)
29
30
31 typedef struct {
32 PaStream *stream;
33 sfifo_t fifo;
34 int finished;
35 } mpg123_portaudio_t;
36
37 #ifdef PORTAUDIO18
38 #define PaTime PaTimestamp
39 #define Pa_IsStreamActive Pa_StreamActive
40 #endif
41
42 /* Some busy waiting. Proper stuff like semaphores might add
43 dependencies (POSIX) that the platform does not know. */
44 static void ms_sleep(int milliseconds)
45 {
46 #ifdef WIN32
47 Sleep(milliseconds);
48 #else
49 usleep(milliseconds*1000);
50 #endif
51 }
52
53 #ifdef PORTAUDIO18
54 static int paCallback( void *inputBuffer, void *outputBuffer,
55 unsigned long framesPerBuffer,
56 PaTime outTime, void *userData )
57 #else
58 static int paCallback(
59 const void *inputBuffer, void *outputBuffer,
60 unsigned long framesPerBuffer,
61 const PaStreamCallbackTimeInfo* timeInfo,
62 PaStreamCallbackFlags statusFlags,
63 void *userData )
64 #endif
65 {
66 out123_handle *ao = userData;
67 mpg123_portaudio_t *pa = (mpg123_portaudio_t*)ao->userptr;
68 unsigned long bytes = framesPerBuffer * SAMPLE_SIZE * ao->channels;
69 int bytes_avail;
70 int bytes_read;
71
72 while((bytes_avail=sfifo_used(&pa->fifo))<bytes && !pa->finished)
73 {
74 int ms = (bytes-bytes_avail)/ao->framesize*1000/ao->rate;
75 debug3("waiting for more input, %d ms missing (%i < %lu)"
76 , ms, bytes_avail, bytes);
77 ms_sleep(ms/10);
78 }
79 if(bytes_avail > bytes)
80 bytes_avail = bytes;
81 bytes_read = sfifo_read(&pa->fifo, outputBuffer, bytes_avail);
82 if(bytes_read != bytes_avail && !AOQUIET)
83 warning2("Error reading from the FIFO (wanted=%d, bytes_read=%d).\n"
84 , bytes_avail, bytes_read);
85 if(bytes_read < 0)
86 bytes_read = 0;
87 /* Ensure that any remaining space is filled with zero bytes. */
88 if(bytes_read >= 0 && bytes_read < bytes)
89 memset((char*)outputBuffer+bytes_read, 0, bytes-bytes_read);
90
91 debug1("callback successfully passed along %i B", bytes_read);
92 return 0;
93 }
94
95
96 static int open_portaudio(out123_handle *ao)
97 {
98 mpg123_portaudio_t *pa = (mpg123_portaudio_t*)ao->userptr;
99 PaError err;
100
101 pa->finished = 0;
102 /* Open an audio I/O stream. */
103 if (ao->rate > 0 && ao->channels >0 ) {
104
105 err = Pa_OpenDefaultStream(
106 &pa->stream,
107 0, /* no input channels */
108 ao->channels, /* number of output channels */
109 paInt16, /* signed 16-bit samples */
110 ao->rate, /* sample rate */
111 FRAMES_PER_BUFFER, /* frames per buffer */
112 #ifdef PORTAUDIO18
113 0, /* number of buffers, if zero then use default minimum */
114 #endif
115 paCallback, /* no callback - use blocking IO */
116 ao );
117
118 if( err != paNoError ) {
119 if(!AOQUIET)
120 error1( "Failed to open PortAudio default stream: %s"
121 , Pa_GetErrorText(err) );
122 return -1;
123 }
124
125 /* Initialise FIFO */
126 sfifo_init( &pa->fifo, ao->rate * FIFO_DURATION * SAMPLE_SIZE *ao->channels );
127
128 }
129
130 return(0);
131 }
132
133
134 static int get_formats_portaudio(out123_handle *ao)
135 {
136 /* Only implemented Signed 16-bit audio for now */
137 return MPG123_ENC_SIGNED_16;
138 }
139
140
141 static int write_portaudio(out123_handle *ao, unsigned char *buf, int len)
142 {
143 mpg123_portaudio_t *pa = (mpg123_portaudio_t*)ao->userptr;
144 PaError err;
145 int len_remain = len;
146
147 /* Some busy waiting, but feed what is possible. */
148 while(len_remain) /* Note: input len is multiple of framesize! */
149 {
150 int block = sfifo_space(&pa->fifo);
151 block -= block % ao->framesize;
152 debug1("space for writing: %i", block);
153 if(block > len_remain)
154 block = len_remain;
155 if(block)
156 {
157 sfifo_write(&pa->fifo, buf, block);
158 len_remain -= block;
159 buf += block;
160 /* Start stream if not ative and 50 % full.*/
161 if(sfifo_used(&pa->fifo) > (sfifo_size(&pa->fifo)/2))
162 {
163 pa->finished = 0;
164 err = Pa_IsStreamActive( pa->stream );
165 if (err == 0) {
166 err = Pa_StartStream( pa->stream );
167 if( err != paNoError ) {
168 if(!AOQUIET)
169 error1( "Failed to start PortAudio stream: %s"
170 , Pa_GetErrorText(err) );
171 return -1; /* triggering exit here is not good, better handle that somehow... */
172 }
173 else
174 debug("started stream");
175 } else if (err < 0)
176 {
177 if(!AOQUIET)
178 error1( "Failed to check state of PortAudio stream: %s"
179 , Pa_GetErrorText(err) );
180 return -1;
181 }
182 else
183 debug("stream already active");
184 }
185 }
186 if(len_remain)
187 {
188 debug1("Still need to write %d bytes, sleeping a bit.", len_remain);
189 ms_sleep(0.1*FIFO_DURATION*1000);
190 }
191 }
192
193 return len;
194 }
195
196 static int close_portaudio(out123_handle *ao)
197 {
198 mpg123_portaudio_t *pa = (mpg123_portaudio_t*)ao->userptr;
199 PaError err;
200 int stuff;
201
202 debug1("close_portaudio with %d", sfifo_used(&pa->fifo));
203 pa->finished = 1;
204 /* Wait at least until the FIFO is empty. */
205 while((stuff = sfifo_used(&pa->fifo))>0)
206 {
207 int ms = stuff/ao->framesize*1000/ao->rate;
208 debug1("still stuff for about %i ms there", ms);
209 ms_sleep(ms/2);
210 }
211
212 if (pa->stream) {
213 /* stop the stream if it is active */
214 if (Pa_IsStreamActive( pa->stream ) == 1) {
215 err = Pa_StopStream( pa->stream );
216 if( err != paNoError )
217 {
218 if(!AOQUIET)
219 error1( "Failed to stop PortAudio stream: %s"
220 , Pa_GetErrorText(err) );
221 return -1;
222 }
223 }
224
225 /* and then close the stream */
226 err = Pa_CloseStream( pa->stream );
227 if( err != paNoError )
228 {
229 if(!AOQUIET)
230 error1( "Failed to close PortAudio stream: %s"
231 , Pa_GetErrorText(err) );
232 return -1;
233 }
234
235 pa->stream = NULL;
236 }
237
238 /* and free memory used by fifo */
239 sfifo_close( &pa->fifo );
240
241 return 0;
242 }
243
244
245 static void flush_portaudio(out123_handle *ao)
246 {
247 mpg123_portaudio_t *pa = (mpg123_portaudio_t*)ao->userptr;
248 /*PaError err;*/
249
250 /* throw away contents of FIFO */
251 sfifo_flush( &pa->fifo );
252
253 /* and empty out PortAudio buffers */
254 /*err = */
255 Pa_AbortStream( pa->stream );
256 }
257
258
259 static void deinit_portaudio(out123_handle* ao)
260 {
261 /* Free up memory */
262 if (ao->userptr) {
263 free( ao->userptr );
264 ao->userptr = NULL;
265 }
266
267 /* Shut down PortAudio */
268 Pa_Terminate();
269 }
270
271
272 static int init_portaudio(out123_handle* ao)
273 {
274 int err = paNoError;
275 mpg123_portaudio_t *handle;
276
277 if (ao==NULL) return -1;
278
279 /* Set callbacks */
280 ao->open = open_portaudio;
281 ao->flush = flush_portaudio;
282 ao->write = write_portaudio;
283 ao->get_formats = get_formats_portaudio;
284 ao->close = close_portaudio;
285 ao->deinit = deinit_portaudio;
286
287 /* Initialise PortAudio */
288 err = Pa_Initialize();
289 if( err != paNoError )
290 {
291 if(!AOQUIET)
292 error1( "Failed to initialise PortAudio: %s"
293 , Pa_GetErrorText(err) );
294 return -1;
295 }
296
297 /* Allocate memory for handle */
298 ao->userptr = handle = malloc( sizeof(mpg123_portaudio_t) );
299 if (ao->userptr==NULL)
300 {
301 if(!AOQUIET)
302 error( "Failed to allocated memory for driver structure" );
303 return -1;
304 }
305 handle->finished = 0;
306 handle->stream = NULL;
307 memset(&handle->fifo, 0, sizeof(sfifo_t));
308
309 /* Success */
310 return 0;
311 }
312
313
314
315 /*
316 Module information data structure
317 */
318 mpg123_module_t mpg123_output_module_info = {
319 /* api_version */ MPG123_MODULE_API_VERSION,
320 /* name */ "portaudio",
321 /* description */ "Output audio using PortAudio",
322 /* revision */ "$Rev:$",
323 /* handle */ NULL,
324
325 /* init_output */ init_portaudio,
326 };
327