Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 779:a60e47313e7b
prev759:f16975739abc
next934:3acd3b3ee6d1
author nkeynes
date Mon Jul 28 10:10:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change If the requested audio driver won't initialize, try all the others in order (worst case, null driver)
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;
    34 audio_driver_t audio_driver_list[] = {
    35 #ifdef HAVE_CORE_AUDIO
    36         &audio_osx_driver,
    37 #endif
    38 #ifdef HAVE_PULSE
    39         &audio_pulse_driver,
    40 #endif
    41 #ifdef HAVE_ESOUND
    42         &audio_esd_driver,
    43 #endif
    44 #ifdef HAVE_ALSA
    45         &audio_alsa_driver,
    46 #endif
    47         &audio_null_driver,
    48         NULL };
    50 #define NUM_BUFFERS 3
    51 #define MS_PER_BUFFER 100
    53 #define BUFFER_EMPTY   0
    54 #define BUFFER_WRITING 1
    55 #define BUFFER_FULL    2
    57 struct audio_state {
    58     audio_buffer_t output_buffers[NUM_BUFFERS];
    59     int write_buffer;
    60     int read_buffer;
    61     uint32_t output_format;
    62     uint32_t output_rate;
    63     uint32_t output_sample_size;
    64     struct audio_channel channels[AUDIO_CHANNEL_COUNT];
    65 } audio;
    67 audio_driver_t audio_driver = NULL;
    69 #define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
    71 extern char *arm_mem;
    73 /**
    74  * Preserve audio channel state only - don't bother saving the buffers
    75  */
    76 void audio_save_state( FILE *f )
    77 {
    78     fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    79 }
    81 int audio_load_state( FILE *f )
    82 {
    83     int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    84     return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
    85 }
    87 audio_driver_t get_audio_driver_by_name( const char *name )
    88 {
    89     int i;
    90     if( name == NULL ) {
    91         return audio_driver_list[0];
    92     }
    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];
    96         }
    97     }
    99     return NULL;
   100 }
   102 void print_audio_drivers( FILE * out )
   103 {
   104     int i;
   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) );
   109     }
   110 }
   112 audio_driver_t audio_init_driver( const char *preferred_driver )
   113 {
   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 );
   117         exit(2);
   118     } else if( audio_set_driver( audio_driver ) == FALSE ) {
   119         int i;
   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];
   126             }
   127         }
   128         ERROR( "Unable to intialize any audio driver, aborting." );
   129         exit(2);
   130     }
   131     return audio_driver;
   132 }
   134 /**
   135  * Set the output driver, sample rate and format. Also initializes the 
   136  * output buffers, flushing any current data and reallocating as 
   137  * necessary.
   138  */
   139 gboolean audio_set_driver( audio_driver_t driver )
   140 {
   141     uint32_t bytes_per_sample = 1;
   142     uint32_t samples_per_buffer;
   143     int i;
   145     if( audio_driver == NULL || driver != NULL ) {
   146         if( driver == NULL  )
   147             driver = &audio_null_driver;
   148         if( driver != audio_driver ) {	
   149             if( !driver->init() )
   150                 return FALSE;
   151             audio_driver = driver;
   152         }
   153     }
   155     switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
   156     case AUDIO_FMT_8BIT:
   157         bytes_per_sample = 1;
   158         break;
   159     case AUDIO_FMT_16BIT:
   160         bytes_per_sample = 2;
   161         break;
   162     case AUDIO_FMT_FLOAT:
   163         bytes_per_sample = 4;
   164         break;
   165     }
   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 )
   171         return TRUE;
   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;
   180     }
   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;
   187     return TRUE;
   188 }
   190 /**
   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.
   194  */
   195 audio_buffer_t audio_next_write_buffer( )
   196 {
   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();
   203     }
   204     int next_buffer = NEXT_BUFFER();
   205     result = audio.output_buffers[next_buffer];
   206     if( result->status == BUFFER_FULL )
   207         return NULL;
   208     else {
   209         audio.write_buffer = next_buffer;
   210         result->status = BUFFER_WRITING;
   211         return result;
   212     }
   213 }
   215 /**
   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.
   218  */
   219 audio_buffer_t audio_next_read_buffer( )
   220 {
   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;
   225         current->posn = 0;
   226         audio.read_buffer++;
   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 ) {
   232             current->posn = 0;
   233             return current;
   234         }
   235         else return NULL;
   236     } else {
   237         return NULL;
   238     }
   240 }
   242 /*************************** ADPCM ***********************************/
   244 /**
   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.
   248  */
   250 #define CLAMP_TO_SHORT(value) \
   251     if (value > 32767) \
   252     value = 32767; \
   253     else if (value < -32768) \
   254     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
   259 };
   261 static const int yamaha_difflookup[] = {
   262         1, 3, 5, 7, 9, 11, 13, 15,
   263         -1, -3, -5, -7, -9, -11, -13, -15
   264 };
   266 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
   267                                                 unsigned char nibble )
   268 {
   269     if( c->adpcm_step == 0 ) {
   270         c->adpcm_predict = 0;
   271         c->adpcm_step = 127;
   272     }
   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;
   279 }
   281 /*************************** Sample mixer *****************************/
   283 /**
   284  * Mix a single output sample.
   285  */
   286 void audio_mix_samples( int num_samples )
   287 {
   288     int i, j;
   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 ) {
   296             int32_t sample;
   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;
   309                         channel->posn++;
   311                         if( channel->posn == channel->end ) {
   312                             if( channel->loop ) {
   313                                 channel->posn = channel->loop_start;
   314                                 channel->loop = LOOP_LOOPED;
   315                             } else {
   316                                 audio_stop_channel(i);
   317                                 j = num_samples;
   318                                 break;
   319                             }
   320                         }
   321                     }
   322                 }
   323                 break;
   324             case AUDIO_FMT_8BIT:
   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;
   333                         channel->posn++;
   335                         if( channel->posn == channel->end ) {
   336                             if( channel->loop ) {
   337                                 channel->posn = channel->loop_start;
   338                                 channel->loop = LOOP_LOOPED;
   339                             } else {
   340                                 audio_stop_channel(i);
   341                                 j = num_samples;
   342                                 break;
   343                             }
   344                         }
   345                     }
   346                 }
   347                 break;
   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;
   356                         channel->posn++;
   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;
   363                             } else {
   364                                 audio_stop_channel(i);
   365                                 j = num_samples;
   366                                 break;
   367                             }
   368                         }
   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 );
   372                         } else {
   373                             adpcm_yamaha_decode_nibble( channel, data & 0x0F );
   374                         }
   375                     }
   376                 }
   377                 break;
   378             default:
   379                 break;
   380             }
   381         }
   382     }
   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
   389             return;
   390         }
   391     }
   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);
   400             buf->posn += 8;
   401             if( buf->posn == buf->length ) {
   402                 buf = audio_next_write_buffer();
   403                 if( buf == NULL ) {
   404                     break;
   405                 }
   406                 data = (float *)&buf->data[0];
   407             }
   408         }
   409         break;
   410     }
   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);	
   416             buf->posn += 4;
   417             if( buf->posn == buf->length ) {
   418                 buf = audio_next_write_buffer();
   419                 if( buf == NULL ) {
   420                     // All buffers are full
   421                     break;
   422                 }
   423                 data = (int16_t *)&buf->data[0];
   424             }
   425         }
   426         break;
   427     }
   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);	
   433             buf->posn += 2;
   434             if( buf->posn == buf->length ) {
   435                 buf = audio_next_write_buffer();
   436                 if( buf == NULL ) {
   437                     // All buffers are full
   438                     break;
   439                 }
   440                 buf = audio.output_buffers[audio.write_buffer];
   441                 data = (int8_t *)&buf->data[0];
   442             }
   443         }
   444         break;
   445     }
   446     }
   447 }
   449 /********************** Internal AICA calls ***************************/
   451 audio_channel_t audio_get_channel( int channel ) 
   452 {
   453     return &audio.channels[channel];
   454 }
   456 void audio_start_stop_channel( int channel, gboolean start )
   457 {
   458     if( audio.channels[channel].active ) {
   459         if( !start ) {
   460             audio_stop_channel(channel);
   461         }
   462     } else if( start ) {
   463         audio_start_channel(channel);
   464     }
   465 }
   467 void audio_stop_channel( int channel ) 
   468 {
   469     audio.channels[channel].active = FALSE;
   470 }
   473 void audio_start_channel( int channel )
   474 {
   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 );
   483     }
   484 }
.