Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 989:7baf5ecd8e98
prev934:3acd3b3ee6d1
next1024:c67f2d61ab97
author nkeynes
date Sat Feb 28 06:26:48 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Add SDL audio driver, thanks to Wahrhaft!
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Audio mixer core. Combines all the active streams into a single sound
     5  * buffer for output. 
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     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.
    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  */
    20 #include "aica/aica.h"
    21 #include "aica/audio.h"
    22 #include <glib/gmem.h>
    23 #include "dream.h"
    24 #include <assert.h>
    25 #include <string.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;
    33 extern struct audio_driver audio_sdl_driver;
    35 audio_driver_t audio_driver_list[] = {
    36 #ifdef HAVE_CORE_AUDIO
    37         &audio_osx_driver,
    38 #endif
    39 #ifdef HAVE_SDL
    40         &audio_sdl_driver,
    41 #endif
    42 #ifdef HAVE_PULSE
    43         &audio_pulse_driver,
    44 #endif
    45 #ifdef HAVE_ESOUND
    46         &audio_esd_driver,
    47 #endif
    48 #ifdef HAVE_ALSA
    49         &audio_alsa_driver,
    50 #endif
    51         &audio_null_driver,
    52         NULL };
    54 #define NUM_BUFFERS 3
    55 #define MS_PER_BUFFER 100
    57 #define BUFFER_EMPTY   0
    58 #define BUFFER_WRITING 1
    59 #define BUFFER_FULL    2
    61 struct audio_state {
    62     audio_buffer_t output_buffers[NUM_BUFFERS];
    63     int write_buffer;
    64     int read_buffer;
    65     uint32_t output_format;
    66     uint32_t output_rate;
    67     uint32_t output_sample_size;
    68     struct audio_channel channels[AUDIO_CHANNEL_COUNT];
    69 } audio;
    71 audio_driver_t audio_driver = NULL;
    73 #define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
    75 /**
    76  * Preserve audio channel state only - don't bother saving the buffers
    77  */
    78 void audio_save_state( FILE *f )
    79 {
    80     fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    81 }
    83 int audio_load_state( FILE *f )
    84 {
    85     int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    86     return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
    87 }
    89 audio_driver_t get_audio_driver_by_name( const char *name )
    90 {
    91     int i;
    92     if( name == NULL ) {
    93         return audio_driver_list[0];
    94     }
    95     for( i=0; audio_driver_list[i] != NULL; i++ ) {
    96         if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
    97             return audio_driver_list[i];
    98         }
    99     }
   101     return NULL;
   102 }
   104 void print_audio_drivers( FILE * out )
   105 {
   106     int i;
   107     fprintf( out, "Available audio drivers:\n" );
   108     for( i=0; audio_driver_list[i] != NULL; i++ ) {
   109         fprintf( out, "  %-8s %s\n", audio_driver_list[i]->name,
   110                 gettext(audio_driver_list[i]->description) );
   111     }
   112 }
   114 audio_driver_t audio_init_driver( const char *preferred_driver )
   115 {
   116     audio_driver_t audio_driver = get_audio_driver_by_name(preferred_driver);
   117     if( audio_driver == NULL ) {
   118         ERROR( "Audio driver '%s' not found, aborting.", preferred_driver );
   119         exit(2);
   120     } else if( audio_set_driver( audio_driver ) == FALSE ) {
   121         int i;
   122         for( i=0; audio_driver_list[i] != NULL; i++ ) {
   123             if( audio_driver_list[i] != audio_driver &&
   124                 audio_set_driver( audio_driver_list[i] ) ) {
   125                 ERROR( "Failed to initialize audio driver %s, falling back to %s", 
   126                        audio_driver->name, audio_driver_list[i]->name );
   127                 return audio_driver_list[i];
   128             }
   129         }
   130         ERROR( "Unable to intialize any audio driver, aborting." );
   131         exit(2);
   132     }
   133     return audio_driver;
   134 }
   136 /**
   137  * Set the output driver, sample rate and format. Also initializes the 
   138  * output buffers, flushing any current data and reallocating as 
   139  * necessary.
   140  */
   141 gboolean audio_set_driver( audio_driver_t driver )
   142 {
   143     uint32_t bytes_per_sample = 1;
   144     uint32_t samples_per_buffer;
   145     int i;
   147     if( audio_driver == NULL || driver != NULL ) {
   148         if( driver == NULL  )
   149             driver = &audio_null_driver;
   150         if( driver != audio_driver ) {	
   151             if( !driver->init() )
   152                 return FALSE;
   153             audio_driver = driver;
   154         }
   155     }
   157     switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
   158     case AUDIO_FMT_8BIT:
   159         bytes_per_sample = 1;
   160         break;
   161     case AUDIO_FMT_16BIT:
   162         bytes_per_sample = 2;
   163         break;
   164     case AUDIO_FMT_FLOAT:
   165         bytes_per_sample = 4;
   166         break;
   167     }
   169     if( driver->sample_format & AUDIO_FMT_STEREO )
   170         bytes_per_sample <<= 1;
   171     if( driver->sample_rate == audio.output_rate &&
   172             bytes_per_sample == audio.output_sample_size )
   173         return TRUE;
   174     samples_per_buffer = (driver->sample_rate * MS_PER_BUFFER / 1000);
   175     for( i=0; i<NUM_BUFFERS; i++ ) {
   176         if( audio.output_buffers[i] != NULL )
   177             free(audio.output_buffers[i]);
   178         audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
   179         audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
   180         audio.output_buffers[i]->posn = 0;
   181         audio.output_buffers[i]->status = BUFFER_EMPTY;
   182     }
   183     audio.output_format = driver->sample_format;
   184     audio.output_rate = driver->sample_rate;
   185     audio.output_sample_size = bytes_per_sample;
   186     audio.write_buffer = 0;
   187     audio.read_buffer = 0;
   189     return TRUE;
   190 }
   192 /**
   193  * Mark the current write buffer as full and prepare the next buffer for
   194  * writing. Returns the next buffer to write to.
   195  * If all buffers are full, returns NULL.
   196  */
   197 audio_buffer_t audio_next_write_buffer( )
   198 {
   199     audio_buffer_t result = NULL;
   200     audio_buffer_t current = audio.output_buffers[audio.write_buffer];
   201     current->status = BUFFER_FULL;
   202     if( audio.read_buffer == audio.write_buffer &&
   203             audio_driver->process_buffer( current ) ) {
   204         audio_next_read_buffer();
   205     }
   206     int next_buffer = NEXT_BUFFER();
   207     result = audio.output_buffers[next_buffer];
   208     if( result->status == BUFFER_FULL )
   209         return NULL;
   210     else {
   211         audio.write_buffer = next_buffer;
   212         result->status = BUFFER_WRITING;
   213         return result;
   214     }
   215 }
   217 /**
   218  * Mark the current read buffer as empty and return the next buffer for
   219  * reading. If there is no next buffer yet, returns NULL.
   220  */
   221 audio_buffer_t audio_next_read_buffer( )
   222 {
   223     audio_buffer_t current = audio.output_buffers[audio.read_buffer];
   224     if( current->status == BUFFER_FULL ) {
   225         // Current read buffer has data, which we've just emptied
   226         current->status = BUFFER_EMPTY;
   227         current->posn = 0;
   228         audio.read_buffer++;
   229         if( audio.read_buffer == NUM_BUFFERS )
   230             audio.read_buffer = 0;
   232         current = audio.output_buffers[audio.read_buffer];
   233         if( current->status == BUFFER_FULL ) {
   234             current->posn = 0;
   235             return current;
   236         }
   237         else return NULL;
   238     } else {
   239         return NULL;
   240     }
   242 }
   244 /*************************** ADPCM ***********************************/
   246 /**
   247  * The following section borrows heavily from ffmpeg, which is
   248  * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
   249  * distributed under the GPL version 2 or later.
   250  */
   252 #define CLAMP_TO_SHORT(value) \
   253     if (value > 32767) \
   254     value = 32767; \
   255     else if (value < -32768) \
   256     value = -32768; \
   258 static const int yamaha_indexscale[] = {
   259         230, 230, 230, 230, 307, 409, 512, 614,
   260         230, 230, 230, 230, 307, 409, 512, 614
   261 };
   263 static const int yamaha_difflookup[] = {
   264         1, 3, 5, 7, 9, 11, 13, 15,
   265         -1, -3, -5, -7, -9, -11, -13, -15
   266 };
   268 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
   269                                                 unsigned char nibble )
   270 {
   271     if( c->adpcm_step == 0 ) {
   272         c->adpcm_predict = 0;
   273         c->adpcm_step = 127;
   274     }
   276     c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
   277     CLAMP_TO_SHORT(c->adpcm_predict);
   278     c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
   279     c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
   280     return c->adpcm_predict;
   281 }
   283 /*************************** Sample mixer *****************************/
   285 /**
   286  * Mix a single output sample.
   287  */
   288 void audio_mix_samples( int num_samples )
   289 {
   290     int i, j;
   291     int32_t result_buf[num_samples][2];
   293     memset( &result_buf, 0, sizeof(result_buf) );
   295     for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
   296         audio_channel_t channel = &audio.channels[i];
   297         if( channel->active ) {
   298             int32_t sample;
   299             int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
   300             int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
   301             switch( channel->sample_format ) {
   302             case AUDIO_FMT_16BIT:
   303                 for( j=0; j<num_samples; j++ ) {
   304                     sample = ((int16_t *)(aica_main_ram + channel->start))[channel->posn];
   305                     result_buf[j][0] += sample * vol_left;
   306                     result_buf[j][1] += sample * vol_right;
   308                     channel->posn_left += channel->sample_rate;
   309                     while( channel->posn_left > audio.output_rate ) {
   310                         channel->posn_left -= audio.output_rate;
   311                         channel->posn++;
   313                         if( channel->posn == channel->end ) {
   314                             if( channel->loop ) {
   315                                 channel->posn = channel->loop_start;
   316                                 channel->loop = LOOP_LOOPED;
   317                             } else {
   318                                 audio_stop_channel(i);
   319                                 j = num_samples;
   320                                 break;
   321                             }
   322                         }
   323                     }
   324                 }
   325                 break;
   326             case AUDIO_FMT_8BIT:
   327                 for( j=0; j<num_samples; j++ ) {
   328                     sample = ((int8_t *)(aica_main_ram + channel->start))[channel->posn] << 8;
   329                     result_buf[j][0] += sample * vol_left;
   330                     result_buf[j][1] += sample * vol_right;
   332                     channel->posn_left += channel->sample_rate;
   333                     while( channel->posn_left > audio.output_rate ) {
   334                         channel->posn_left -= audio.output_rate;
   335                         channel->posn++;
   337                         if( channel->posn == channel->end ) {
   338                             if( channel->loop ) {
   339                                 channel->posn = channel->loop_start;
   340                                 channel->loop = LOOP_LOOPED;
   341                             } else {
   342                                 audio_stop_channel(i);
   343                                 j = num_samples;
   344                                 break;
   345                             }
   346                         }
   347                     }
   348                 }
   349                 break;
   350             case AUDIO_FMT_ADPCM:
   351                 for( j=0; j<num_samples; j++ ) {
   352                     sample = (int16_t)channel->adpcm_predict;
   353                     result_buf[j][0] += sample * vol_left;
   354                     result_buf[j][1] += sample * vol_right;
   355                     channel->posn_left += channel->sample_rate;
   356                     while( channel->posn_left > audio.output_rate ) {
   357                         channel->posn_left -= audio.output_rate;
   358                         channel->posn++;
   359                         if( channel->posn == channel->end ) {
   360                             if( channel->loop ) {
   361                                 channel->posn = channel->loop_start;
   362                                 channel->loop = LOOP_LOOPED;
   363                                 channel->adpcm_predict = 0;
   364                                 channel->adpcm_step = 0;
   365                             } else {
   366                                 audio_stop_channel(i);
   367                                 j = num_samples;
   368                                 break;
   369                             }
   370                         }
   371                         uint8_t data = ((uint8_t *)(aica_main_ram + channel->start))[channel->posn>>1];
   372                         if( channel->posn&1 ) {
   373                             adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
   374                         } else {
   375                             adpcm_yamaha_decode_nibble( channel, data & 0x0F );
   376                         }
   377                     }
   378                 }
   379                 break;
   380             default:
   381                 break;
   382             }
   383         }
   384     }
   386     /* Down-render to the final output format */
   387     audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
   388     if( buf->status == BUFFER_FULL ) {
   389         buf = audio_next_write_buffer();
   390         if( buf == NULL ) { // no available space
   391             return;
   392         }
   393     }
   395     switch( audio.output_format & AUDIO_FMT_SAMPLE_MASK ) {
   396     case AUDIO_FMT_FLOAT: {
   397         float scale = 1.0/SHRT_MAX;
   398         float *data = (float *)&buf->data[buf->posn];
   399         for( j=0; j<num_samples; j++ ) {
   400             *data++ = scale * (result_buf[j][0] >> 6);
   401             *data++ = scale * (result_buf[j][1] >> 6);
   402             buf->posn += 8;
   403             if( buf->posn == buf->length ) {
   404                 buf = audio_next_write_buffer();
   405                 if( buf == NULL ) {
   406                     break;
   407                 }
   408                 data = (float *)&buf->data[0];
   409             }
   410         }
   411         break;
   412     }
   413     case AUDIO_FMT_16BIT: {
   414         int16_t *data = (int16_t *)&buf->data[buf->posn];
   415         for( j=0; j < num_samples; j++ ) {
   416             *data++ = (int16_t)(result_buf[j][0] >> 6);
   417             *data++ = (int16_t)(result_buf[j][1] >> 6);	
   418             buf->posn += 4;
   419             if( buf->posn == buf->length ) {
   420                 buf = audio_next_write_buffer();
   421                 if( buf == NULL ) {
   422                     // All buffers are full
   423                     break;
   424                 }
   425                 data = (int16_t *)&buf->data[0];
   426             }
   427         }
   428         break;
   429     }
   430     case AUDIO_FMT_8BIT: {
   431         int8_t *data = (int8_t *)&buf->data[buf->posn];
   432         for( j=0; j < num_samples; j++ ) {
   433             *data++ = (int8_t)(result_buf[j][0] >> 16);
   434             *data++ = (int8_t)(result_buf[j][1] >> 16);	
   435             buf->posn += 2;
   436             if( buf->posn == buf->length ) {
   437                 buf = audio_next_write_buffer();
   438                 if( buf == NULL ) {
   439                     // All buffers are full
   440                     break;
   441                 }
   442                 buf = audio.output_buffers[audio.write_buffer];
   443                 data = (int8_t *)&buf->data[0];
   444             }
   445         }
   446         break;
   447     }
   448     }
   449 }
   451 /********************** Internal AICA calls ***************************/
   453 audio_channel_t audio_get_channel( int channel ) 
   454 {
   455     return &audio.channels[channel];
   456 }
   458 void audio_start_stop_channel( int channel, gboolean start )
   459 {
   460     if( audio.channels[channel].active ) {
   461         if( !start ) {
   462             audio_stop_channel(channel);
   463         }
   464     } else if( start ) {
   465         audio_start_channel(channel);
   466     }
   467 }
   469 void audio_stop_channel( int channel ) 
   470 {
   471     audio.channels[channel].active = FALSE;
   472 }
   475 void audio_start_channel( int channel )
   476 {
   477     audio.channels[channel].posn = 0;
   478     audio.channels[channel].posn_left = 0;
   479     audio.channels[channel].active = TRUE;
   480     if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
   481         audio.channels[channel].adpcm_step = 0;
   482         audio.channels[channel].adpcm_predict = 0;
   483         uint8_t data = ((uint8_t *)(aica_main_ram + audio.channels[channel].start))[0];
   484         adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
   485     }
   486 }
.