1 /******************************************************************************
2 * audio_output.c : audio output thread
4 ******************************************************************************/
8 * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
10 * - Faire les optimisations dans les fonctions threads :
11 * = Stocker les "petits calculs" dans des variables au lieu de les refaire
13 * = Utiliser des tables pour les gros calculs
14 * - Faire une structure différente pour intf/adec fifo
15 * - Rajouter des pthread_cond_signal ?
19 /******************************************************************************
21 ******************************************************************************/
25 #include <sys/soundcard.h>
26 #include <stdio.h> /* "intf_msg.h" */
27 #include <stdlib.h> /* calloc(), malloc(), free() */
31 #include "mtime.h" /* mtime_t, mdate(), msleep() */
33 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
35 #include "audio_output.h"
36 #include "audio_dsp.h"
38 /******************************************************************************
40 ******************************************************************************/
42 /* Creating as much aout_Thread functions as configurations is one solution,
43 * examining the different cases in the Thread loop of an unique function is
44 * another. I chose the first solution. */
45 void aout_Thread_S8_Mono ( aout_thread_t * p_aout );
46 void aout_Thread_U8_Mono ( aout_thread_t * p_aout );
47 void aout_Thread_S16_Mono ( aout_thread_t * p_aout );
48 void aout_Thread_U16_Mono ( aout_thread_t * p_aout );
49 void aout_Thread_S8_Stereo ( aout_thread_t * p_aout );
50 void aout_Thread_U8_Stereo ( aout_thread_t * p_aout );
51 void aout_Thread_S16_Stereo ( aout_thread_t * p_aout );
52 void aout_Thread_U16_Stereo ( aout_thread_t * p_aout );
54 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
55 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo );
57 /******************************************************************************
59 ******************************************************************************/
60 int aout_Open( aout_thread_t * p_aout )
62 if ( aout_dspOpen( &p_aout->dsp ) )
67 if ( aout_dspReset( &p_aout->dsp ) )
69 aout_dspClose( &p_aout->dsp );
73 if ( aout_dspSetFormat( &p_aout->dsp ) )
75 aout_dspClose( &p_aout->dsp );
79 if ( aout_dspSetChannels( &p_aout->dsp ) )
81 aout_dspClose( &p_aout->dsp );
85 if ( aout_dspSetRate( &p_aout->dsp ) )
87 aout_dspClose( &p_aout->dsp );
91 intf_DbgMsg("aout debug: audio device (%s) opened (format=%i, stereo=%i, rate=%li)\n",
92 p_aout->dsp.psz_device,
94 p_aout->dsp.b_stereo, p_aout->dsp.l_rate);
99 /******************************************************************************
101 ******************************************************************************/
102 int aout_SpawnThread( aout_thread_t * p_aout )
106 s64 s64_numerator, s64_denominator;
107 void * aout_thread = NULL;
109 intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
111 /* We want the audio output thread to live */
114 /* Initialize the fifos lock */
115 pthread_mutex_init( &p_aout->fifos_lock, NULL );
116 /* Initialize audio fifos : set all fifos as empty and initialize locks */
117 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
119 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
120 pthread_mutex_init( &p_aout->fifo[i_fifo].data_lock, NULL );
121 pthread_cond_init( &p_aout->fifo[i_fifo].data_wait, NULL );
124 /* Compute the size (in audio units) of the audio output buffer. Although
125 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
126 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
127 p_aout->l_units = (long)( ((s64)p_aout->dsp.l_rate * AOUT_BUFFER_DURATION) / 1000000 );
129 /* Make aout_thread point to the right thread function, and compute the
130 * byte size of the audio output buffer */
131 switch ( p_aout->dsp.b_stereo )
133 /* Audio output is mono */
135 switch ( p_aout->dsp.i_format )
138 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
139 aout_thread = (void *)aout_Thread_U8_Mono;
143 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
144 aout_thread = (void *)aout_Thread_S8_Mono;
149 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
150 aout_thread = (void *)aout_Thread_U16_Mono;
155 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
156 aout_thread = (void *)aout_Thread_S16_Mono;
160 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
161 p_aout->dsp.i_format);
166 /* Audio output is stereo */
168 switch ( p_aout->dsp.i_format )
171 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
172 aout_thread = (void *)aout_Thread_U8_Stereo;
176 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
177 aout_thread = (void *)aout_Thread_S8_Stereo;
182 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
183 aout_thread = (void *)aout_Thread_U16_Stereo;
188 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
189 aout_thread = (void *)aout_Thread_S16_Stereo;
193 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
194 p_aout->dsp.i_format);
200 intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
201 p_aout->dsp.b_stereo + 1);
205 /* Allocate the memory needed by the audio output buffers, and set to zero
206 * the s32 buffer's memory */
207 if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
209 intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
212 if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << p_aout->dsp.b_stereo)) == NULL )
214 intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
215 free( p_aout->buffer );
219 /* Initialize the incremental structure that is used to work out the date
220 * of the first audio unit in the output buffer */
221 s64_numerator = (s64)p_aout->l_units * 1000000;
222 s64_denominator = (s64)p_aout->dsp.l_rate;
224 p_aout->date_increment.l_remainder = -(long)s64_denominator;
226 p_aout->date_increment.l_euclidean_integer = 0;
227 while ( s64_numerator >= s64_denominator )
229 p_aout->date_increment.l_euclidean_integer++;
230 s64_numerator -= s64_denominator;
233 p_aout->date_increment.l_euclidean_remainder = (long)s64_numerator;
235 p_aout->date_increment.l_euclidean_denominator = (long)s64_denominator;
237 /* Before launching the thread, we try to predict the date of the first
238 * audio unit in the first output buffer */
239 p_aout->date = mdate();
241 /* Launch the thread */
242 if ( pthread_create( &p_aout->thread_id, NULL, aout_thread, p_aout ) )
244 intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
245 free( p_aout->buffer );
246 free( p_aout->s32_buffer );
250 intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
254 /******************************************************************************
256 ******************************************************************************/
257 void aout_CancelThread( aout_thread_t * p_aout )
259 intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
261 /* Ask thread to kill itself and wait until it's done */
263 pthread_join( p_aout->thread_id, NULL );
265 /* Free the allocated memory */
266 free( p_aout->buffer );
267 free( p_aout->s32_buffer );
270 /******************************************************************************
272 ******************************************************************************/
273 void aout_Close( aout_thread_t * p_aout )
275 aout_dspClose( &p_aout->dsp );
276 intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->dsp.psz_device);
279 /******************************************************************************
281 ******************************************************************************/
282 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
286 /* Take the fifos lock */
287 pthread_mutex_lock( &p_aout->fifos_lock );
289 /* Looking for a free fifo structure */
290 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
292 if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
297 if ( i_fifo == AOUT_MAX_FIFOS )
299 intf_ErrMsg("aout error: no empty fifo available\n");
300 pthread_mutex_unlock( &p_aout->fifos_lock );
304 /* Initialize the new fifo structure */
305 switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
307 case AOUT_INTF_MONO_FIFO:
308 case AOUT_INTF_STEREO_FIFO:
309 p_aout->fifo[i_fifo].b_die = 0;
311 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
312 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
314 p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
316 p_aout->fifo[i_fifo].l_unit = 0;
317 InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->dsp.l_rate );
318 p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
321 case AOUT_ADEC_MONO_FIFO:
322 case AOUT_ADEC_STEREO_FIFO:
325 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
326 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
328 /* Allocate the memory needed to store the audio frames. As the
329 * fifo is a rotative fifo, we must be able to find out whether the
330 * fifo is full or empty, that's why we must in fact allocate memory
331 * for (AOUT_FIFO_SIZE+1) audio frames. */
332 if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*AOUT_FRAME_SIZE )) == NULL )
334 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
335 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
336 pthread_mutex_unlock( &p_aout->fifos_lock );
340 /* Allocate the memory needed to store the dates of the frames */
341 if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
343 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
344 free( p_aout->fifo[i_fifo].buffer );
345 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
346 pthread_mutex_unlock( &p_aout->fifos_lock );
350 /* Set the fifo's buffer as empty (the first frame that is to be
351 * played is also the first frame that is not to be played) */
352 p_aout->fifo[i_fifo].l_start_frame = 0;
353 /* p_aout->fifo[i_fifo].l_next_frame = 0; */
354 p_aout->fifo[i_fifo].l_end_frame = 0;
356 /* Waiting for the audio decoder to compute enough frames to work
357 * out the fifo's current rate (as soon as the decoder has decoded
358 * enough frames, the members of the fifo structure that are not
359 * initialized now will be calculated) */
360 p_aout->fifo[i_fifo].l_unit = 0; /* !! */
361 p_aout->fifo[i_fifo].b_start_frame = 0;
362 p_aout->fifo[i_fifo].b_next_frame = 0;
366 intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
367 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
368 pthread_mutex_unlock( &p_aout->fifos_lock );
372 /* Release the fifos lock */
373 pthread_mutex_unlock( &p_aout->fifos_lock );
375 /* Return the pointer to the fifo structure */
376 intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
377 return( &p_aout->fifo[i_fifo] );
380 /******************************************************************************
382 ******************************************************************************/
383 void aout_DestroyFifo( aout_fifo_t * p_fifo )
385 intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
389 /* Here are the local macros */
391 #define S32_TO_S16( sample ) \
394 #define UPDATE_INCREMENT( increment, integer ) \
395 if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 ) \
397 (integer) += (increment).l_euclidean_integer + 1; \
398 (increment).l_remainder -= (increment).l_euclidean_denominator; \
402 (integer) += (increment).l_euclidean_integer; \
405 /* Following functions are local */
407 /******************************************************************************
408 * InitializeIncrement
409 ******************************************************************************/
410 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
412 p_increment->l_remainder = -l_denominator;
414 p_increment->l_euclidean_integer = 0;
415 while ( l_numerator >= l_denominator )
417 p_increment->l_euclidean_integer++;
418 l_numerator -= l_denominator;
421 p_increment->l_euclidean_remainder = l_numerator;
423 p_increment->l_euclidean_denominator = l_denominator;
426 /******************************************************************************
428 ******************************************************************************/
429 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
431 long l_units, l_rate;
433 /* We take the lock */
434 pthread_mutex_lock( &p_fifo->data_lock );
436 /* Are we looking for a dated start frame ? */
437 if ( !p_fifo->b_start_frame )
439 while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
441 if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
443 p_fifo->b_start_frame = 1;
444 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
447 p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
449 if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
451 pthread_mutex_unlock( &p_fifo->data_lock );
456 /* We are looking for the next dated frame */
457 while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
459 if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
461 p_fifo->b_next_frame = 1;
464 p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
466 if ( p_fifo->l_next_frame == p_fifo->l_end_frame )
468 if ( (((p_fifo->l_end_frame + 1) - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) == 0 )
470 p_fifo->l_start_frame = 0;
471 p_fifo->b_start_frame = 0;
472 /* p_fifo->l_next_frame = 0; */
473 /* p_fifo->b_next_frame = 0; */
474 p_fifo->l_end_frame = 0;
476 pthread_mutex_unlock( &p_fifo->data_lock );
480 l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE)
481 * (AOUT_FRAME_SIZE >> p_fifo->b_stereo);
483 l_rate = (long)( ((mtime_t)l_units * 1000000)
484 / (p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame]) );
486 InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->dsp.l_rate );
488 p_fifo->l_units = (((l_units - (p_fifo->l_unit -
489 (p_fifo->l_start_frame * (AOUT_FRAME_SIZE >> p_fifo->b_stereo))))
490 * p_aout->dsp.l_rate) / l_rate) + 1;
492 /* We release the lock before leaving */
493 pthread_mutex_unlock( &p_fifo->data_lock );
497 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
501 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
505 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
509 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
513 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
517 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
521 long l_buffer, l_buffer_limit;
523 intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
525 /* As the s32_buffer was created with calloc(), we don't have to set this
526 * memory to zero and we can immediately jump into the thread's loop */
527 while ( !p_aout->b_die )
529 pthread_mutex_lock( &p_aout->fifos_lock );
530 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
532 switch ( p_aout->fifo[i_fifo].i_type )
534 case AOUT_EMPTY_FIFO:
537 case AOUT_INTF_MONO_FIFO:
538 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
541 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
543 p_aout->s32_buffer[l_buffer++] +=
544 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
545 p_aout->s32_buffer[l_buffer++] +=
546 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
547 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
549 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
554 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
556 p_aout->s32_buffer[l_buffer++] +=
557 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
558 p_aout->s32_buffer[l_buffer++] +=
559 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
560 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
562 free( p_aout->fifo[i_fifo].buffer ); /* !! */
563 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
564 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
568 case AOUT_INTF_STEREO_FIFO:
569 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
572 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
574 p_aout->s32_buffer[l_buffer++] +=
575 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
576 p_aout->s32_buffer[l_buffer++] +=
577 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
578 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
580 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
585 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo */
587 p_aout->s32_buffer[l_buffer++] +=
588 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
589 p_aout->s32_buffer[l_buffer++] +=
590 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
591 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
593 free( p_aout->fifo[i_fifo].buffer ); /* !! */
594 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
595 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
599 case AOUT_ADEC_MONO_FIFO:
600 l_units = p_aout->l_units;
602 while ( l_units > 0 )
604 if ( !p_aout->fifo[i_fifo].b_next_frame )
606 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]) )
612 if ( p_aout->fifo[i_fifo].l_units > l_units )
614 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
615 while ( l_buffer < l_buffer_limit )
617 p_aout->s32_buffer[l_buffer++] +=
618 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
619 p_aout->s32_buffer[l_buffer++] +=
620 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
622 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
623 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
624 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0)) )
626 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
627 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0));
630 p_aout->fifo[i_fifo].l_units -= l_units;
635 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
636 /* p_aout->dsp.b_stereo == 1 */
637 while ( l_buffer < l_buffer_limit )
639 p_aout->s32_buffer[l_buffer++] +=
640 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
641 p_aout->s32_buffer[l_buffer++] +=
642 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
644 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
645 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
646 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0)) )
648 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
649 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0));
652 l_units -= p_aout->fifo[i_fifo].l_units;
654 pthread_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
655 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
656 pthread_cond_signal( &p_aout->fifo[i_fifo].data_wait );
657 pthread_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
659 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
660 p_aout->fifo[i_fifo].l_next_frame += 1;
661 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
662 p_aout->fifo[i_fifo].b_next_frame = 0;
667 case AOUT_ADEC_STEREO_FIFO:
668 l_units = p_aout->l_units;
670 while ( l_units > 0 )
672 if ( !p_aout->fifo[i_fifo].b_next_frame )
674 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]) )
680 if ( p_aout->fifo[i_fifo].l_units > l_units )
682 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
683 while ( l_buffer < l_buffer_limit )
685 p_aout->s32_buffer[l_buffer++] +=
686 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
687 p_aout->s32_buffer[l_buffer++] +=
688 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
690 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
691 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
692 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1)) )
694 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
695 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1));
698 p_aout->fifo[i_fifo].l_units -= l_units;
703 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
704 /* p_aout->dsp.b_stereo == 1 */
705 while ( l_buffer < l_buffer_limit )
707 p_aout->s32_buffer[l_buffer++] +=
708 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
709 p_aout->s32_buffer[l_buffer++] +=
710 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
712 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
713 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
714 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1)) )
716 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
717 ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1));
720 l_units -= p_aout->fifo[i_fifo].l_units;
722 pthread_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
723 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
724 pthread_cond_signal( &p_aout->fifo[i_fifo].data_wait );
725 pthread_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
727 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
728 p_aout->fifo[i_fifo].l_next_frame += 1;
729 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
730 p_aout->fifo[i_fifo].b_next_frame = 0;
736 intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
740 pthread_mutex_unlock( &p_aout->fifos_lock );
742 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
744 for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
746 ((s16 *)p_aout->buffer)[l_buffer] = S32_TO_S16( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
747 p_aout->s32_buffer[l_buffer] = 0;
750 aout_dspGetBufInfo( &p_aout->dsp );
751 if ( p_aout->dsp.buf_info.fragments == p_aout->dsp.buf_info.fragstotal ) /* ?? */
753 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
754 p_aout->date = mdate();
756 else if ( p_aout->dsp.buf_info.bytes >=
757 ((p_aout->dsp.buf_info.fragsize * p_aout->dsp.buf_info.fragstotal) -
758 (sizeof(s16) * l_buffer_limit)) )
760 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
764 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
765 msleep( p_aout->date_increment.l_euclidean_integer );
767 UPDATE_INCREMENT( p_aout->date_increment, p_aout->date )
770 pthread_mutex_lock( &p_aout->fifos_lock );
771 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
773 switch ( p_aout->fifo[i_fifo].i_type )
775 case AOUT_EMPTY_FIFO:
778 case AOUT_INTF_MONO_FIFO:
779 case AOUT_INTF_STEREO_FIFO:
780 free( p_aout->fifo[i_fifo].buffer ); /* !! */
781 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
782 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
785 case AOUT_ADEC_MONO_FIFO:
786 case AOUT_ADEC_STEREO_FIFO:
787 free( p_aout->fifo[i_fifo].buffer );
788 free( p_aout->fifo[i_fifo].date );
789 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
790 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
797 pthread_mutex_unlock( &p_aout->fifos_lock );
800 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
804 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )