Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 934:3acd3b3ee6d1
prev779:a60e47313e7b
next989:7baf5ecd8e98
author nkeynes
date Tue Jan 13 11:56:28 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Merge lxdream-mem branch back to trunk
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 /**
    72  * Preserve audio channel state only - don't bother saving the buffers
    73  */
    74 void audio_save_state( FILE *f )
    75 {
    76     fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    77 }
    79 int audio_load_state( FILE *f )
    80 {
    81     int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
    82     return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
    83 }
    85 audio_driver_t get_audio_driver_by_name( const char *name )
    86 {
    87     int i;
    88     if( name == NULL ) {
    89         return audio_driver_list[0];
    90     }
    91     for( i=0; audio_driver_list[i] != NULL; i++ ) {
    92         if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
    93             return audio_driver_list[i];
    94         }
    95     }
    97     return NULL;
    98 }
   100 void print_audio_drivers( FILE * out )
   101 {
   102     int i;
   103     fprintf( out, "Available audio drivers:\n" );
   104     for( i=0; audio_driver_list[i] != NULL; i++ ) {
   105         fprintf( out, "  %-8s %s\n", audio_driver_list[i]->name,
   106                 gettext(audio_driver_list[i]->description) );
   107     }
   108 }
   110 audio_driver_t audio_init_driver( const char *preferred_driver )
   111 {
   112     audio_driver_t audio_driver = get_audio_driver_by_name(preferred_driver);
   113     if( audio_driver == NULL ) {
   114         ERROR( "Audio driver '%s' not found, aborting.", preferred_driver );
   115         exit(2);
   116     } else if( audio_set_driver( audio_driver ) == FALSE ) {
   117         int i;
   118         for( i=0; audio_driver_list[i] != NULL; i++ ) {
   119             if( audio_driver_list[i] != audio_driver &&
   120                 audio_set_driver( audio_driver_list[i] ) ) {
   121                 ERROR( "Failed to initialize audio driver %s, falling back to %s", 
   122                        audio_driver->name, audio_driver_list[i]->name );
   123                 return audio_driver_list[i];
   124             }
   125         }
   126         ERROR( "Unable to intialize any audio driver, aborting." );
   127         exit(2);
   128     }
   129     return audio_driver;
   130 }
   132 /**
   133  * Set the output driver, sample rate and format. Also initializes the 
   134  * output buffers, flushing any current data and reallocating as 
   135  * necessary.
   136  */
   137 gboolean audio_set_driver( audio_driver_t driver )
   138 {
   139     uint32_t bytes_per_sample = 1;
   140     uint32_t samples_per_buffer;
   141     int i;
   143     if( audio_driver == NULL || driver != NULL ) {
   144         if( driver == NULL  )
   145             driver = &audio_null_driver;
   146         if( driver != audio_driver ) {	
   147             if( !driver->init() )
   148                 return FALSE;
   149             audio_driver = driver;
   150         }
   151     }
   153     switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
   154     case AUDIO_FMT_8BIT:
   155         bytes_per_sample = 1;
   156         break;
   157     case AUDIO_FMT_16BIT:
   158         bytes_per_sample = 2;
   159         break;
   160     case AUDIO_FMT_FLOAT:
   161         bytes_per_sample = 4;
   162         break;
   163     }
   165     if( driver->sample_format & AUDIO_FMT_STEREO )
   166         bytes_per_sample <<= 1;
   167     if( driver->sample_rate == audio.output_rate &&
   168             bytes_per_sample == audio.output_sample_size )
   169         return TRUE;
   170     samples_per_buffer = (driver->sample_rate * MS_PER_BUFFER / 1000);
   171     for( i=0; i<NUM_BUFFERS; i++ ) {
   172         if( audio.output_buffers[i] != NULL )
   173             free(audio.output_buffers[i]);
   174         audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
   175         audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
   176         audio.output_buffers[i]->posn = 0;
   177         audio.output_buffers[i]->status = BUFFER_EMPTY;
   178     }
   179     audio.output_format = driver->sample_format;
   180     audio.output_rate = driver->sample_rate;
   181     audio.output_sample_size = bytes_per_sample;
   182     audio.write_buffer = 0;
   183     audio.read_buffer = 0;
   185     return TRUE;
   186 }
   188 /**
   189  * Mark the current write buffer as full and prepare the next buffer for
   190  * writing. Returns the next buffer to write to.
   191  * If all buffers are full, returns NULL.
   192  */
   193 audio_buffer_t audio_next_write_buffer( )
   194 {
   195     audio_buffer_t result = NULL;
   196     audio_buffer_t current = audio.output_buffers[audio.write_buffer];
   197     current->status = BUFFER_FULL;
   198     if( audio.read_buffer == audio.write_buffer &&
   199             audio_driver->process_buffer( current ) ) {
   200         audio_next_read_buffer();
   201     }
   202     int next_buffer = NEXT_BUFFER();
   203     result = audio.output_buffers[next_buffer];
   204     if( result->status == BUFFER_FULL )
   205         return NULL;
   206     else {
   207         audio.write_buffer = next_buffer;
   208         result->status = BUFFER_WRITING;
   209         return result;
   210     }
   211 }
   213 /**
   214  * Mark the current read buffer as empty and return the next buffer for
   215  * reading. If there is no next buffer yet, returns NULL.
   216  */
   217 audio_buffer_t audio_next_read_buffer( )
   218 {
   219     audio_buffer_t current = audio.output_buffers[audio.read_buffer];
   220     if( current->status == BUFFER_FULL ) {
   221         // Current read buffer has data, which we've just emptied
   222         current->status = BUFFER_EMPTY;
   223         current->posn = 0;
   224         audio.read_buffer++;
   225         if( audio.read_buffer == NUM_BUFFERS )
   226             audio.read_buffer = 0;
   228         current = audio.output_buffers[audio.read_buffer];
   229         if( current->status == BUFFER_FULL ) {
   230             current->posn = 0;
   231             return current;
   232         }
   233         else return NULL;
   234     } else {
   235         return NULL;
   236     }
   238 }
   240 /*************************** ADPCM ***********************************/
   242 /**
   243  * The following section borrows heavily from ffmpeg, which is
   244  * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
   245  * distributed under the GPL version 2 or later.
   246  */
   248 #define CLAMP_TO_SHORT(value) \
   249     if (value > 32767) \
   250     value = 32767; \
   251     else if (value < -32768) \
   252     value = -32768; \
   254 static const int yamaha_indexscale[] = {
   255         230, 230, 230, 230, 307, 409, 512, 614,
   256         230, 230, 230, 230, 307, 409, 512, 614
   257 };
   259 static const int yamaha_difflookup[] = {
   260         1, 3, 5, 7, 9, 11, 13, 15,
   261         -1, -3, -5, -7, -9, -11, -13, -15
   262 };
   264 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
   265                                                 unsigned char nibble )
   266 {
   267     if( c->adpcm_step == 0 ) {
   268         c->adpcm_predict = 0;
   269         c->adpcm_step = 127;
   270     }
   272     c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
   273     CLAMP_TO_SHORT(c->adpcm_predict);
   274     c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
   275     c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
   276     return c->adpcm_predict;
   277 }
   279 /*************************** Sample mixer *****************************/
   281 /**
   282  * Mix a single output sample.
   283  */
   284 void audio_mix_samples( int num_samples )
   285 {
   286     int i, j;
   287     int32_t result_buf[num_samples][2];
   289     memset( &result_buf, 0, sizeof(result_buf) );
   291     for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
   292         audio_channel_t channel = &audio.channels[i];
   293         if( channel->active ) {
   294             int32_t sample;
   295             int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
   296             int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
   297             switch( channel->sample_format ) {
   298             case AUDIO_FMT_16BIT:
   299                 for( j=0; j<num_samples; j++ ) {
   300                     sample = ((int16_t *)(aica_main_ram + channel->start))[channel->posn];
   301                     result_buf[j][0] += sample * vol_left;
   302                     result_buf[j][1] += sample * vol_right;
   304                     channel->posn_left += channel->sample_rate;
   305                     while( channel->posn_left > audio.output_rate ) {
   306                         channel->posn_left -= audio.output_rate;
   307                         channel->posn++;
   309                         if( channel->posn == channel->end ) {
   310                             if( channel->loop ) {
   311                                 channel->posn = channel->loop_start;
   312                                 channel->loop = LOOP_LOOPED;
   313                             } else {
   314                                 audio_stop_channel(i);
   315                                 j = num_samples;
   316                                 break;
   317                             }
   318                         }
   319                     }
   320                 }
   321                 break;
   322             case AUDIO_FMT_8BIT:
   323                 for( j=0; j<num_samples; j++ ) {
   324                     sample = ((int8_t *)(aica_main_ram + channel->start))[channel->posn] << 8;
   325                     result_buf[j][0] += sample * vol_left;
   326                     result_buf[j][1] += sample * vol_right;
   328                     channel->posn_left += channel->sample_rate;
   329                     while( channel->posn_left > audio.output_rate ) {
   330                         channel->posn_left -= audio.output_rate;
   331                         channel->posn++;
   333                         if( channel->posn == channel->end ) {
   334                             if( channel->loop ) {
   335                                 channel->posn = channel->loop_start;
   336                                 channel->loop = LOOP_LOOPED;
   337                             } else {
   338                                 audio_stop_channel(i);
   339                                 j = num_samples;
   340                                 break;
   341                             }
   342                         }
   343                     }
   344                 }
   345                 break;
   346             case AUDIO_FMT_ADPCM:
   347                 for( j=0; j<num_samples; j++ ) {
   348                     sample = (int16_t)channel->adpcm_predict;
   349                     result_buf[j][0] += sample * vol_left;
   350                     result_buf[j][1] += sample * vol_right;
   351                     channel->posn_left += channel->sample_rate;
   352                     while( channel->posn_left > audio.output_rate ) {
   353                         channel->posn_left -= audio.output_rate;
   354                         channel->posn++;
   355                         if( channel->posn == channel->end ) {
   356                             if( channel->loop ) {
   357                                 channel->posn = channel->loop_start;
   358                                 channel->loop = LOOP_LOOPED;
   359                                 channel->adpcm_predict = 0;
   360                                 channel->adpcm_step = 0;
   361                             } else {
   362                                 audio_stop_channel(i);
   363                                 j = num_samples;
   364                                 break;
   365                             }
   366                         }
   367                         uint8_t data = ((uint8_t *)(aica_main_ram + channel->start))[channel->posn>>1];
   368                         if( channel->posn&1 ) {
   369                             adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
   370                         } else {
   371                             adpcm_yamaha_decode_nibble( channel, data & 0x0F );
   372                         }
   373                     }
   374                 }
   375                 break;
   376             default:
   377                 break;
   378             }
   379         }
   380     }
   382     /* Down-render to the final output format */
   383     audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
   384     if( buf->status == BUFFER_FULL ) {
   385         buf = audio_next_write_buffer();
   386         if( buf == NULL ) { // no available space
   387             return;
   388         }
   389     }
   391     switch( audio.output_format & AUDIO_FMT_SAMPLE_MASK ) {
   392     case AUDIO_FMT_FLOAT: {
   393         float scale = 1.0/SHRT_MAX;
   394         float *data = (float *)&buf->data[buf->posn];
   395         for( j=0; j<num_samples; j++ ) {
   396             *data++ = scale * (result_buf[j][0] >> 6);
   397             *data++ = scale * (result_buf[j][1] >> 6);
   398             buf->posn += 8;
   399             if( buf->posn == buf->length ) {
   400                 buf = audio_next_write_buffer();
   401                 if( buf == NULL ) {
   402                     break;
   403                 }
   404                 data = (float *)&buf->data[0];
   405             }
   406         }
   407         break;
   408     }
   409     case AUDIO_FMT_16BIT: {
   410         int16_t *data = (int16_t *)&buf->data[buf->posn];
   411         for( j=0; j < num_samples; j++ ) {
   412             *data++ = (int16_t)(result_buf[j][0] >> 6);
   413             *data++ = (int16_t)(result_buf[j][1] >> 6);	
   414             buf->posn += 4;
   415             if( buf->posn == buf->length ) {
   416                 buf = audio_next_write_buffer();
   417                 if( buf == NULL ) {
   418                     // All buffers are full
   419                     break;
   420                 }
   421                 data = (int16_t *)&buf->data[0];
   422             }
   423         }
   424         break;
   425     }
   426     case AUDIO_FMT_8BIT: {
   427         int8_t *data = (int8_t *)&buf->data[buf->posn];
   428         for( j=0; j < num_samples; j++ ) {
   429             *data++ = (int8_t)(result_buf[j][0] >> 16);
   430             *data++ = (int8_t)(result_buf[j][1] >> 16);	
   431             buf->posn += 2;
   432             if( buf->posn == buf->length ) {
   433                 buf = audio_next_write_buffer();
   434                 if( buf == NULL ) {
   435                     // All buffers are full
   436                     break;
   437                 }
   438                 buf = audio.output_buffers[audio.write_buffer];
   439                 data = (int8_t *)&buf->data[0];
   440             }
   441         }
   442         break;
   443     }
   444     }
   445 }
   447 /********************** Internal AICA calls ***************************/
   449 audio_channel_t audio_get_channel( int channel ) 
   450 {
   451     return &audio.channels[channel];
   452 }
   454 void audio_start_stop_channel( int channel, gboolean start )
   455 {
   456     if( audio.channels[channel].active ) {
   457         if( !start ) {
   458             audio_stop_channel(channel);
   459         }
   460     } else if( start ) {
   461         audio_start_channel(channel);
   462     }
   463 }
   465 void audio_stop_channel( int channel ) 
   466 {
   467     audio.channels[channel].active = FALSE;
   468 }
   471 void audio_start_channel( int channel )
   472 {
   473     audio.channels[channel].posn = 0;
   474     audio.channels[channel].posn_left = 0;
   475     audio.channels[channel].active = TRUE;
   476     if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
   477         audio.channels[channel].adpcm_step = 0;
   478         audio.channels[channel].adpcm_predict = 0;
   479         uint8_t data = ((uint8_t *)(aica_main_ram + audio.channels[channel].start))[0];
   480         adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
   481     }
   482 }
.