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"
27 audio_driver_t audio_driver_list[] = {
41 #define MS_PER_BUFFER 100
43 #define BUFFER_EMPTY 0
44 #define BUFFER_WRITING 1
48 audio_buffer_t output_buffers[NUM_BUFFERS];
51 uint32_t output_format;
53 uint32_t output_sample_size;
54 struct audio_channel channels[AUDIO_CHANNEL_COUNT];
57 audio_driver_t audio_driver = NULL;
59 #define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
64 * Preserve audio channel state only - don't bother saving the buffers
66 void audio_save_state( FILE *f )
68 fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
71 int audio_load_state( FILE *f )
73 int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
74 return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
77 audio_driver_t get_audio_driver_by_name( const char *name )
81 return audio_driver_list[0];
83 for( i=0; audio_driver_list[i] != NULL; i++ ) {
84 if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
85 return audio_driver_list[i];
93 * Set the output driver, sample rate and format. Also initializes the
94 * output buffers, flushing any current data and reallocating as
97 gboolean audio_set_driver( audio_driver_t driver,
98 uint32_t samplerate, int format )
100 uint32_t bytes_per_sample = 1;
101 uint32_t samples_per_buffer;
104 if( audio_driver == NULL || driver != NULL ) {
106 driver = &audio_null_driver;
107 if( driver != audio_driver ) {
108 if( !driver->set_output_format( samplerate, format ) )
110 audio_driver = driver;
114 if( format & AUDIO_FMT_16BIT )
115 bytes_per_sample = 2;
116 if( format & AUDIO_FMT_STEREO )
117 bytes_per_sample <<= 1;
118 if( samplerate == audio.output_rate &&
119 bytes_per_sample == audio.output_sample_size )
121 samples_per_buffer = (samplerate * MS_PER_BUFFER / 1000);
122 for( i=0; i<NUM_BUFFERS; i++ ) {
123 if( audio.output_buffers[i] != NULL )
124 free(audio.output_buffers[i]);
125 audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
126 audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
127 audio.output_buffers[i]->posn = 0;
128 audio.output_buffers[i]->status = BUFFER_EMPTY;
130 audio.output_format = format;
131 audio.output_rate = samplerate;
132 audio.output_sample_size = bytes_per_sample;
133 audio.write_buffer = 0;
134 audio.read_buffer = 0;
140 * Mark the current write buffer as full and prepare the next buffer for
141 * writing. Returns the next buffer to write to.
142 * If all buffers are full, returns NULL.
144 audio_buffer_t audio_next_write_buffer( )
146 audio_buffer_t result = NULL;
147 audio_buffer_t current = audio.output_buffers[audio.write_buffer];
148 current->status = BUFFER_FULL;
149 if( audio.read_buffer == audio.write_buffer &&
150 audio_driver->process_buffer( current ) ) {
151 audio_next_read_buffer();
153 audio.write_buffer = NEXT_BUFFER();
154 result = audio.output_buffers[audio.write_buffer];
155 if( result->status == BUFFER_FULL )
158 result->status = BUFFER_WRITING;
164 * Mark the current read buffer as empty and return the next buffer for
165 * reading. If there is no next buffer yet, returns NULL.
167 audio_buffer_t audio_next_read_buffer( )
169 audio_buffer_t current = audio.output_buffers[audio.read_buffer];
170 assert( current->status == BUFFER_FULL );
171 current->status = BUFFER_EMPTY;
174 if( audio.read_buffer == NUM_BUFFERS )
175 audio.read_buffer = 0;
177 current = audio.output_buffers[audio.read_buffer];
178 if( current->status == BUFFER_FULL )
183 /*************************** ADPCM ***********************************/
186 * The following section borrows heavily from ffmpeg, which is
187 * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
188 * distributed under the GPL version 2 or later.
191 #define CLAMP_TO_SHORT(value) \
194 else if (value < -32768) \
197 static const int yamaha_indexscale[] = {
198 230, 230, 230, 230, 307, 409, 512, 614,
199 230, 230, 230, 230, 307, 409, 512, 614
202 static const int yamaha_difflookup[] = {
203 1, 3, 5, 7, 9, 11, 13, 15,
204 -1, -3, -5, -7, -9, -11, -13, -15
207 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c,
208 unsigned char nibble )
210 if( c->adpcm_step == 0 ) {
211 c->adpcm_predict = 0;
215 c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
216 CLAMP_TO_SHORT(c->adpcm_predict);
217 c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
218 c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
219 return c->adpcm_predict;
222 /*************************** Sample mixer *****************************/
225 * Mix a single output sample.
227 void audio_mix_samples( int num_samples )
230 int32_t result_buf[num_samples][2];
232 memset( &result_buf, 0, sizeof(result_buf) );
234 for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
235 audio_channel_t channel = &audio.channels[i];
236 if( channel->active ) {
238 int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
239 int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
240 switch( channel->sample_format ) {
241 case AUDIO_FMT_16BIT:
242 for( j=0; j<num_samples; j++ ) {
243 sample = ((int16_t *)(arm_mem + channel->start))[channel->posn];
244 result_buf[j][0] += sample * vol_left;
245 result_buf[j][1] += sample * vol_right;
247 channel->posn_left += channel->sample_rate;
248 while( channel->posn_left > audio.output_rate ) {
249 channel->posn_left -= audio.output_rate;
252 if( channel->posn == channel->end ) {
253 if( channel->loop ) {
254 channel->posn = channel->loop_start;
255 channel->loop = LOOP_LOOPED;
257 audio_stop_channel(i);
266 for( j=0; j<num_samples; j++ ) {
267 sample = ((int8_t *)(arm_mem + channel->start))[channel->posn] << 8;
268 result_buf[j][0] += sample * vol_left;
269 result_buf[j][1] += sample * vol_right;
271 channel->posn_left += channel->sample_rate;
272 while( channel->posn_left > audio.output_rate ) {
273 channel->posn_left -= audio.output_rate;
276 if( channel->posn == channel->end ) {
277 if( channel->loop ) {
278 channel->posn = channel->loop_start;
279 channel->loop = LOOP_LOOPED;
281 audio_stop_channel(i);
289 case AUDIO_FMT_ADPCM:
290 for( j=0; j<num_samples; j++ ) {
291 sample = (int16_t)channel->adpcm_predict;
292 result_buf[j][0] += sample * vol_left;
293 result_buf[j][1] += sample * vol_right;
294 channel->posn_left += channel->sample_rate;
295 while( channel->posn_left > audio.output_rate ) {
296 channel->posn_left -= audio.output_rate;
298 if( channel->posn == channel->end ) {
299 if( channel->loop ) {
300 channel->posn = channel->loop_start;
301 channel->loop = LOOP_LOOPED;
302 channel->adpcm_predict = 0;
303 channel->adpcm_step = 0;
305 audio_stop_channel(i);
310 uint8_t data = ((uint8_t *)(arm_mem + channel->start))[channel->posn>>1];
311 if( channel->posn&1 ) {
312 adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
314 adpcm_yamaha_decode_nibble( channel, data & 0x0F );
325 /* Down-render to the final output format */
327 if( audio.output_format & AUDIO_FMT_16BIT ) {
328 audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
329 uint16_t *data = (uint16_t *)&buf->data[buf->posn];
330 for( j=0; j < num_samples; j++ ) {
331 *data++ = (int16_t)(result_buf[j][0] >> 6);
332 *data++ = (int16_t)(result_buf[j][1] >> 6);
334 if( buf->posn == buf->length ) {
335 audio_next_write_buffer();
336 buf = audio.output_buffers[audio.write_buffer];
337 data = (uint16_t *)&buf->data[0];
341 audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
342 uint8_t *data = (uint8_t *)&buf->data[buf->posn];
343 for( j=0; j < num_samples; j++ ) {
344 *data++ = (uint8_t)(result_buf[j][0] >> 16);
345 *data++ = (uint8_t)(result_buf[j][1] >> 16);
347 if( buf->posn == buf->length ) {
348 audio_next_write_buffer();
349 buf = audio.output_buffers[audio.write_buffer];
350 data = (uint8_t *)&buf->data[0];
356 /********************** Internal AICA calls ***************************/
358 audio_channel_t audio_get_channel( int channel )
360 return &audio.channels[channel];
363 void audio_start_stop_channel( int channel, gboolean start )
365 if( audio.channels[channel].active ) {
367 audio_stop_channel(channel);
370 audio_start_channel(channel);
374 void audio_stop_channel( int channel )
376 audio.channels[channel].active = FALSE;
380 void audio_start_channel( int channel )
382 audio.channels[channel].posn = 0;
383 audio.channels[channel].posn_left = 0;
384 audio.channels[channel].active = TRUE;
385 if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
386 audio.channels[channel].adpcm_step = 0;
387 audio.channels[channel].adpcm_predict = 0;
388 uint8_t data = ((uint8_t *)(arm_mem + audio.channels[channel].start))[0];
389 adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
.