comparison src/audacious/output.c @ 2313:3149d4b1a9a9 trunk

[svn] - objective-make autodepend fixes - move all sourcecode into src/ and adjust Makefiles accordingly
author nenolod
date Fri, 12 Jan 2007 11:43:40 -0800
parents
children d88558b0de0a
comparison
equal deleted inserted replaced
2312:e1a5a66fb9cc 2313:3149d4b1a9a9
1 /* Audacious - Cross-platform multimedia player
2 * Copyright (C) 2005-2007 Audacious team
3 *
4 * Based on BMP:
5 * Copyright (C) 2003-2004 BMP development team.
6 *
7 * Based on XMMS:
8 * Copyright (C) 1998-2003 XMMS development team.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; under version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "output.h"
29 #include "iir.h"
30 #include "main.h"
31 #include "input.h"
32
33 #include "playlist.h"
34 #include "libaudacious/util.h"
35
36 OutputPluginData op_data = {
37 NULL,
38 NULL
39 };
40
41 OutputPluginState op_state = {
42 0,
43 0,
44 0
45 };
46
47 OutputPlugin psuedo_output_plugin = {
48 NULL,
49 NULL,
50 "XMMS reverse compatibility output plugin",
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 output_get_volume,
56 output_set_volume,
57 output_open_audio,
58 output_write_audio,
59 output_close_audio,
60
61 output_flush,
62 output_pause,
63 output_buffer_free,
64 output_buffer_playing,
65 get_output_time,
66 get_written_time,
67 NULL
68 };
69
70 OutputPlugin *
71 get_current_output_plugin(void)
72 {
73 return op_data.current_output_plugin;
74 }
75
76 void
77 set_current_output_plugin(gint i)
78 {
79 gint time;
80 gint pos;
81 gboolean playing;
82 OutputPlugin *op = get_current_output_plugin();
83
84 GList *node = g_list_nth(op_data.output_list, i);
85 if (!node) {
86 op_data.current_output_plugin = NULL;
87 return;
88 }
89
90 op_data.current_output_plugin = node->data;
91
92 playing = playback_get_playing();
93
94 if (playing == TRUE)
95 {
96 gint i = 99;
97 guint time, pos;
98 PlaylistEntry *entry;
99
100 /* don't stop yet, get the seek time and playlist position first */
101 pos = playlist_get_position(playlist_get_active());
102 time = op->output_time();
103
104 /* reset the audio system */
105 mainwin_stop_pushed();
106 op->close_audio();
107
108 g_usleep(300000);
109
110 /* wait for the playback thread to come online */
111 while (playback_get_playing())
112 g_message("waiting for audio system shutdown...");
113
114 /* wait for the playback thread to come online */
115 playlist_set_position(playlist_get_active(), pos);
116 entry = playlist_get_entry_to_play(playlist_get_active());
117 playback_play_file(entry);
118
119 while (!playback_get_playing())
120 {
121 gtk_main_iteration();
122 g_message("waiting for audio system startup...");
123 }
124
125 /* and signal a reseek */
126 if (playlist_get_current_length(playlist_get_active()) > -1 &&
127 time <= (playlist_get_current_length(playlist_get_active())))
128 {
129 gint i;
130
131 playback_seek(time / 1000);
132 }
133 }
134 }
135
136 GList *
137 get_output_list(void)
138 {
139 return op_data.output_list;
140 }
141
142 void
143 output_about(gint i)
144 {
145 OutputPlugin *out = g_list_nth(op_data.output_list, i)->data;
146 if (out && out->about)
147 out->about();
148 }
149
150 void
151 output_configure(gint i)
152 {
153 OutputPlugin *out = g_list_nth(op_data.output_list, i)->data;
154 if (out && out->configure)
155 out->configure();
156 }
157
158 void
159 output_get_volume(gint * l, gint * r)
160 {
161 *l = *r = -1;
162
163 if (!op_data.current_output_plugin)
164 return;
165
166 if (!op_data.current_output_plugin->get_volume)
167 return;
168
169 op_data.current_output_plugin->get_volume(l, r);
170 }
171
172 void
173 output_set_volume(gint l, gint r)
174 {
175 if (!op_data.current_output_plugin)
176 return;
177
178 if (!op_data.current_output_plugin->set_volume)
179 return;
180
181 op_data.current_output_plugin->set_volume(l, r);
182 }
183
184 void
185 output_set_eq(gboolean active, gfloat pre, gfloat * bands)
186 {
187 int i;
188 preamp[0] = 1.0 + 0.0932471 * pre + 0.00279033 * pre * pre;
189 preamp[1] = 1.0 + 0.0932471 * pre + 0.00279033 * pre * pre;
190
191 for (i = 0; i < 10; ++i)
192 {
193 set_gain(i, 0, 0.03 * bands[i] + 0.000999999 * bands[i] * bands[i]);
194 set_gain(i, 1, 0.03 * bands[i] + 0.000999999 * bands[i] * bands[i]);
195 }
196 }
197
198 /* this should be in BYTES, NOT gint16s */
199 static void
200 byteswap(size_t size,
201 gint16 * buf)
202 {
203 gint16 *it;
204 size &= ~1; /* must be multiple of 2 */
205 for (it = buf; it < buf + size / 2; ++it)
206 *(guint16 *) it = GUINT16_SWAP_LE_BE(*(guint16 *) it);
207 }
208
209 /* called by input plugin to peek at the output plugin's write progress */
210 gint
211 get_written_time(void)
212 {
213 OutputPlugin *op = get_current_output_plugin();
214
215 return op->written_time();
216 }
217
218 /* called by input plugin to peek at the output plugin's output progress */
219 gint
220 get_output_time(void)
221 {
222 OutputPlugin *op = get_current_output_plugin();
223
224 return op->output_time();
225 }
226
227 gint
228 output_open_audio(AFormat fmt, gint rate, gint nch)
229 {
230 gint ret;
231 OutputPlugin *op;
232
233 op = get_current_output_plugin();
234
235 if (op == NULL)
236 return -1;
237
238 /* Is our output port already open? */
239 if ((op_state.rate != 0 && op_state.nch != 0) &&
240 (op_state.rate == rate && op_state.nch == nch && op_state.fmt == fmt))
241 {
242 /* Yes, and it's the correct sampling rate. Reset the counter and go. */
243 op->flush(0);
244 return 1;
245 }
246 else if (op_state.rate != 0 && op_state.nch != 0)
247 op->close_audio();
248
249 ret = op->open_audio(fmt, rate, nch);
250
251 if (ret == 1) /* Success? */
252 {
253 op_state.fmt = fmt;
254 op_state.rate = rate;
255 op_state.nch = nch;
256 }
257
258 return ret;
259 }
260
261 void
262 output_write_audio(gpointer ptr, gint length)
263 {
264 OutputPlugin *op = get_current_output_plugin();
265
266 /* Sanity check. */
267 if (op == NULL)
268 return;
269
270 op->write_audio(ptr, length);
271 }
272
273 void
274 output_close_audio(void)
275 {
276 OutputPlugin *op = get_current_output_plugin();
277
278 /* Do not close if there are still songs to play and the user has
279 * not requested a stop. --nenolod
280 */
281 if (ip_data.stop == FALSE &&
282 (playlist_get_position_nolock(playlist_get_active()) < playlist_get_length_nolock(playlist_get_active()) - 1))
283 return;
284
285 /* Sanity check. */
286 if (op == NULL)
287 return;
288
289 op->close_audio();
290
291 /* Reset the op_state. */
292 op_state.fmt = op_state.rate = op_state.nch = 0;
293 }
294
295 void
296 output_flush(gint time)
297 {
298 OutputPlugin *op = get_current_output_plugin();
299
300 if (op == NULL)
301 return;
302
303 op->flush(time);
304 }
305
306 void
307 output_pause(gshort paused)
308 {
309 OutputPlugin *op = get_current_output_plugin();
310
311 if (op == NULL)
312 return;
313
314 op->pause(paused);
315 }
316
317 gint
318 output_buffer_free(void)
319 {
320 OutputPlugin *op = get_current_output_plugin();
321
322 if (op == NULL)
323 return 0;
324
325 return op->buffer_free();
326 }
327
328 gint
329 output_buffer_playing(void)
330 {
331 OutputPlugin *op = get_current_output_plugin();
332
333 if (op == NULL)
334 return 0;
335
336 return op->buffer_playing();
337 }
338
339 /* called by input plugin when data is ready */
340 void
341 produce_audio(gint time, /* position */
342 AFormat fmt, /* output format */
343 gint nch, /* channels */
344 gint length, /* length of sample */
345 gpointer ptr, /* data */
346 int *going /* 0 when time to stop */
347 )
348 {
349 static int init = 0;
350 int swapped = 0;
351 guint myorder = G_BYTE_ORDER == G_LITTLE_ENDIAN ? FMT_S16_LE : FMT_S16_BE;
352 int caneq = (fmt == FMT_S16_NE || fmt == myorder);
353 OutputPlugin *op = get_current_output_plugin();
354 int writeoffs;
355
356 if (!caneq && cfg.equalizer_active) { /* wrong byte order */
357 byteswap(length, ptr); /* so convert */
358 ++swapped;
359 ++caneq;
360 } /* can eq now, mark swapd */
361 else if (caneq && !cfg.equalizer_active) /* right order but no eq */
362 caneq = 0; /* so don't eq */
363
364 if (caneq) { /* if eq enab */
365 if (!init) { /* if first run */
366 init_iir(); /* then init eq */
367 ++init;
368 }
369
370 iir(&ptr, length, nch);
371
372 if (swapped) /* if was swapped */
373 byteswap(length, ptr); /* swap back for output */
374 }
375
376 /* do vis plugin(s) */
377 input_add_vis_pcm(time, fmt, nch, length, ptr);
378
379 writeoffs = 0;
380 while (writeoffs < length) {
381 int writable = length - writeoffs;
382
383 if (writable > 2048)
384 writable = 2048;
385
386 if (writable == 0)
387 return;
388
389 while (op->buffer_free() < writable) { /* wait output buf */
390 if (going && !*going) /* thread stopped? */
391 return; /* so finish */
392
393 if (ip_data.stop) /* has a stop been requested? */
394 return; /* yes, so finish */
395
396 g_usleep(10000); /* else sleep for retry */
397 }
398
399 if (ip_data.stop)
400 return;
401
402 if (going && !*going) /* thread stopped? */
403 return; /* so finish */
404
405 /* do output */
406 op->write_audio(((guint8 *) ptr) + writeoffs, writable);
407
408 writeoffs += writable;
409 }
410 }