4 * Audio mixer core. Combines all the active streams into a single sound
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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.
20 #include "aica/aica.h"
21 #include "aica/audio.h"
22 #include <glib/gmem.h>
28 extern struct audio_driver audio_null_driver;
29 extern struct audio_driver audio_osx_driver;
30 extern struct audio_driver audio_pulse_driver;
31 extern struct audio_driver audio_esd_driver;
32 extern struct audio_driver audio_alsa_driver;
34 audio_driver_t audio_driver_list[] = {
35 #ifdef HAVE_CORE_AUDIO
51 #define MS_PER_BUFFER 100
53 #define BUFFER_EMPTY 0
54 #define BUFFER_WRITING 1
58 audio_buffer_t output_buffers[NUM_BUFFERS];
61 uint32_t output_format;
63 uint32_t output_sample_size;
64 struct audio_channel channels[AUDIO_CHANNEL_COUNT];
67 audio_driver_t audio_driver = NULL;
69 #define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
74 * Preserve audio channel state only - don't bother saving the buffers
76 void audio_save_state( FILE *f )
78 fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
81 int audio_load_state( FILE *f )
83 int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
84 return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
87 audio_driver_t get_audio_driver_by_name( const char *name )
91 return audio_driver_list[0];
93 for( i=0; audio_driver_list[i] != NULL; i++ ) {
94 if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
95 return audio_driver_list[i];
102 void print_audio_drivers( FILE * out )
105 fprintf( out, "Available audio drivers:\n" );
106 for( i=0; audio_driver_list[i] != NULL; i++ ) {
107 fprintf( out, " %-8s %s\n", audio_driver_list[i]->name,
108 gettext(audio_driver_list[i]->description) );
112 audio_driver_t audio_init_driver( const char *preferred_driver )
114 audio_driver_t audio_driver = get_audio_driver_by_name(preferred_driver);
115 if( audio_driver == NULL ) {
116 ERROR( "Audio driver '%s' not found, aborting.", preferred_driver );
118 } else if( audio_set_driver( audio_driver ) == FALSE ) {
120 for( i=0; audio_driver_list[i] != NULL; i++ ) {
121 if( audio_driver_list[i] != audio_driver &&
122 audio_set_driver( audio_driver_list[i] ) ) {
123 ERROR( "Failed to initialize audio driver %s, falling back to %s",
124 audio_driver->name, audio_driver_list[i]->name );
125 return audio_driver_list[i];
128 ERROR( "Unable to intialize any audio driver, aborting." );
135 * Set the output driver, sample rate and format. Also initializes the
136 * output buffers, flushing any current data and reallocating as
139 gboolean audio_set_driver( audio_driver_t driver )
141 uint32_t bytes_per_sample = 1;
142 uint32_t samples_per_buffer;
145 if( audio_driver == NULL || driver != NULL ) {
147 driver = &audio_null_driver;
148 if( driver != audio_driver ) {
149 if( !driver->init() )
151 audio_driver = driver;
155 switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
157 bytes_per_sample = 1;
159 case AUDIO_FMT_16BIT:
160 bytes_per_sample = 2;
162 case AUDIO_FMT_FLOAT:
163 bytes_per_sample = 4;
167 if( driver->sample_format & AUDIO_FMT_STEREO )
168 bytes_per_sample <<= 1;
169 if( driver->sample_rate == audio.output_rate &&
170 bytes_per_sample == audio.output_sample_size )
172 samples_per_buffer = (driver->sample_rate * MS_PER_BUFFER / 1000);
173 for( i=0; i<NUM_BUFFERS; i++ ) {
174 if( audio.output_buffers[i] != NULL )
175 free(audio.output_buffers[i]);
176 audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
177 audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
178 audio.output_buffers[i]->posn = 0;
179 audio.output_buffers[i]->status = BUFFER_EMPTY;
181 audio.output_format = driver->sample_format;
182 audio.output_rate = driver->sample_rate;
183 audio.output_sample_size = bytes_per_sample;
184 audio.write_buffer = 0;
185 audio.read_buffer = 0;
191 * Mark the current write buffer as full and prepare the next buffer for
192 * writing. Returns the next buffer to write to.
193 * If all buffers are full, returns NULL.
195 audio_buffer_t audio_next_write_buffer( )
197 audio_buffer_t result = NULL;
198 audio_buffer_t current = audio.output_buffers[audio.write_buffer];
199 current->status = BUFFER_FULL;
200 if( audio.read_buffer == audio.write_buffer &&
201 audio_driver->process_buffer( current ) ) {
202 audio_next_read_buffer();
204 int next_buffer = NEXT_BUFFER();
205 result = audio.output_buffers[next_buffer];
206 if( result->status == BUFFER_FULL )
209 audio.write_buffer = next_buffer;
210 result->status = BUFFER_WRITING;
216 * Mark the current read buffer as empty and return the next buffer for
217 * reading. If there is no next buffer yet, returns NULL.
219 audio_buffer_t audio_next_read_buffer( )
221 audio_buffer_t current = audio.output_buffers[audio.read_buffer];
222 if( current->status == BUFFER_FULL ) {
223 // Current read buffer has data, which we've just emptied
224 current->status = BUFFER_EMPTY;
227 if( audio.read_buffer == NUM_BUFFERS )
228 audio.read_buffer = 0;
230 current = audio.output_buffers[audio.read_buffer];
231 if( current->status == BUFFER_FULL ) {
242 /*************************** ADPCM ***********************************/
245 * The following section borrows heavily from ffmpeg, which is
246 * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
247 * distributed under the GPL version 2 or later.
250 #define CLAMP_TO_SHORT(value) \
253 else if (value < -32768) \
256 static const int yamaha_indexscale[] = {
257 230, 230, 230, 230, 307, 409, 512, 614,
258 230, 230, 230, 230, 307, 409, 512, 614
261 static const int yamaha_difflookup[] = {
262 1, 3, 5, 7, 9, 11, 13, 15,
263 -1, -3, -5, -7, -9, -11, -13, -15
266 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c,
267 unsigned char nibble )
269 if( c->adpcm_step == 0 ) {
270 c->adpcm_predict = 0;
274 c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
275 CLAMP_TO_SHORT(c->adpcm_predict);
276 c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
277 c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
278 return c->adpcm_predict;
281 /*************************** Sample mixer *****************************/
284 * Mix a single output sample.
286 void audio_mix_samples( int num_samples )
289 int32_t result_buf[num_samples][2];
291 memset( &result_buf, 0, sizeof(result_buf) );
293 for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
294 audio_channel_t channel = &audio.channels[i];
295 if( channel->active ) {
297 int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
298 int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
299 switch( channel->sample_format ) {
300 case AUDIO_FMT_16BIT:
301 for( j=0; j<num_samples; j++ ) {
302 sample = ((int16_t *)(arm_mem + channel->start))[channel->posn];
303 result_buf[j][0] += sample * vol_left;
304 result_buf[j][1] += sample * vol_right;
306 channel->posn_left += channel->sample_rate;
307 while( channel->posn_left > audio.output_rate ) {
308 channel->posn_left -= audio.output_rate;
311 if( channel->posn == channel->end ) {
312 if( channel->loop ) {
313 channel->posn = channel->loop_start;
314 channel->loop = LOOP_LOOPED;
316 audio_stop_channel(i);
325 for( j=0; j<num_samples; j++ ) {
326 sample = ((int8_t *)(arm_mem + channel->start))[channel->posn] << 8;
327 result_buf[j][0] += sample * vol_left;
328 result_buf[j][1] += sample * vol_right;
330 channel->posn_left += channel->sample_rate;
331 while( channel->posn_left > audio.output_rate ) {
332 channel->posn_left -= audio.output_rate;
335 if( channel->posn == channel->end ) {
336 if( channel->loop ) {
337 channel->posn = channel->loop_start;
338 channel->loop = LOOP_LOOPED;
340 audio_stop_channel(i);
348 case AUDIO_FMT_ADPCM:
349 for( j=0; j<num_samples; j++ ) {
350 sample = (int16_t)channel->adpcm_predict;
351 result_buf[j][0] += sample * vol_left;
352 result_buf[j][1] += sample * vol_right;
353 channel->posn_left += channel->sample_rate;
354 while( channel->posn_left > audio.output_rate ) {
355 channel->posn_left -= audio.output_rate;
357 if( channel->posn == channel->end ) {
358 if( channel->loop ) {
359 channel->posn = channel->loop_start;
360 channel->loop = LOOP_LOOPED;
361 channel->adpcm_predict = 0;
362 channel->adpcm_step = 0;
364 audio_stop_channel(i);
369 uint8_t data = ((uint8_t *)(arm_mem + channel->start))[channel->posn>>1];
370 if( channel->posn&1 ) {
371 adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
373 adpcm_yamaha_decode_nibble( channel, data & 0x0F );
384 /* Down-render to the final output format */
385 audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
386 if( buf->status == BUFFER_FULL ) {
387 buf = audio_next_write_buffer();
388 if( buf == NULL ) { // no available space
393 switch( audio.output_format & AUDIO_FMT_SAMPLE_MASK ) {
394 case AUDIO_FMT_FLOAT: {
395 float scale = 1.0/SHRT_MAX;
396 float *data = (float *)&buf->data[buf->posn];
397 for( j=0; j<num_samples; j++ ) {
398 *data++ = scale * (result_buf[j][0] >> 6);
399 *data++ = scale * (result_buf[j][1] >> 6);
401 if( buf->posn == buf->length ) {
402 buf = audio_next_write_buffer();
406 data = (float *)&buf->data[0];
411 case AUDIO_FMT_16BIT: {
412 int16_t *data = (int16_t *)&buf->data[buf->posn];
413 for( j=0; j < num_samples; j++ ) {
414 *data++ = (int16_t)(result_buf[j][0] >> 6);
415 *data++ = (int16_t)(result_buf[j][1] >> 6);
417 if( buf->posn == buf->length ) {
418 buf = audio_next_write_buffer();
420 // All buffers are full
423 data = (int16_t *)&buf->data[0];
428 case AUDIO_FMT_8BIT: {
429 int8_t *data = (int8_t *)&buf->data[buf->posn];
430 for( j=0; j < num_samples; j++ ) {
431 *data++ = (int8_t)(result_buf[j][0] >> 16);
432 *data++ = (int8_t)(result_buf[j][1] >> 16);
434 if( buf->posn == buf->length ) {
435 buf = audio_next_write_buffer();
437 // All buffers are full
440 buf = audio.output_buffers[audio.write_buffer];
441 data = (int8_t *)&buf->data[0];
449 /********************** Internal AICA calls ***************************/
451 audio_channel_t audio_get_channel( int channel )
453 return &audio.channels[channel];
456 void audio_start_stop_channel( int channel, gboolean start )
458 if( audio.channels[channel].active ) {
460 audio_stop_channel(channel);
463 audio_start_channel(channel);
467 void audio_stop_channel( int channel )
469 audio.channels[channel].active = FALSE;
473 void audio_start_channel( int channel )
475 audio.channels[channel].posn = 0;
476 audio.channels[channel].posn_left = 0;
477 audio.channels[channel].active = TRUE;
478 if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
479 audio.channels[channel].adpcm_step = 0;
480 audio.channels[channel].adpcm_predict = 0;
481 uint8_t data = ((uint8_t *)(arm_mem + audio.channels[channel].start))[0];
482 adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
.