Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 697:479b8c213f61
prev657:c4143facbfcb
next700:4650d0c7f6f9
author nkeynes
date Sun Jun 22 04:01:27 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Commit work-in-progress CoreAudio driver
(along with various changes to the audio subsystem)
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 1000
    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 audio_driver_t audio_init_driver( const char *preferred_driver )
   103 {
   104     audio_driver_t audio_driver = get_audio_driver_by_name(preferred_driver);
   105     if( audio_driver == NULL ) {
   106         ERROR( "Audio driver '%s' not found, aborting.", preferred_driver );
   107         exit(2);
   108     } else if( audio_set_driver( audio_driver ) == FALSE ) {
   109         ERROR( "Failed to initialize audio driver '%s', using null driver", 
   110                 audio_driver->name );
   111         audio_set_driver( &audio_null_driver );
   112     }    
   113 }
   115 /**
   116  * Set the output driver, sample rate and format. Also initializes the 
   117  * output buffers, flushing any current data and reallocating as 
   118  * necessary.
   119  */
   120 gboolean audio_set_driver( audio_driver_t driver )
   121 {
   122     uint32_t bytes_per_sample = 1;
   123     uint32_t samples_per_buffer;
   124     int i;
   126     if( audio_driver == NULL || driver != NULL ) {
   127         if( driver == NULL  )
   128             driver = &audio_null_driver;
   129         if( driver != audio_driver ) {	
   130             if( !driver->init() )
   131                 return FALSE;
   132             audio_driver = driver;
   133         }
   134     }
   136     switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
   137     case AUDIO_FMT_8BIT:
   138         bytes_per_sample = 1;
   139         break;
   140     case AUDIO_FMT_16BIT:
   141         bytes_per_sample = 2;
   142         break;
   143     case AUDIO_FMT_FLOAT:
   144         bytes_per_sample = 4;
   145         break;
   146     }
   148     if( driver->sample_format & AUDIO_FMT_STEREO )
   149         bytes_per_sample <<= 1;
   150     if( driver->sample_rate == audio.output_rate &&
   151             bytes_per_sample == audio.output_sample_size )
   152         return TRUE;
   153     samples_per_buffer = (driver->sample_rate * MS_PER_BUFFER / 1000);
   154     for( i=0; i<NUM_BUFFERS; i++ ) {
   155         if( audio.output_buffers[i] != NULL )
   156             free(audio.output_buffers[i]);
   157         audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
   158         audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
   159         audio.output_buffers[i]->posn = 0;
   160         audio.output_buffers[i]->status = BUFFER_EMPTY;
   161     }
   162     audio.output_format = driver->sample_format;
   163     audio.output_rate = driver->sample_rate;
   164     audio.output_sample_size = bytes_per_sample;
   165     audio.write_buffer = 0;
   166     audio.read_buffer = 0;
   168     return TRUE;
   169 }
   171 /**
   172  * Mark the current write buffer as full and prepare the next buffer for
   173  * writing. Returns the next buffer to write to.
   174  * If all buffers are full, returns NULL.
   175  */
   176 audio_buffer_t audio_next_write_buffer( )
   177 {
   178     audio_buffer_t result = NULL;
   179     audio_buffer_t current = audio.output_buffers[audio.write_buffer];
   180     current->status = BUFFER_FULL;
   181     if( audio.read_buffer == audio.write_buffer &&
   182             audio_driver->process_buffer( current ) ) {
   183         audio_next_read_buffer();
   184     }
   185     int next_buffer = NEXT_BUFFER();
   186     result = audio.output_buffers[next_buffer];
   187     if( result->status == BUFFER_FULL )
   188         return NULL;
   189     else {
   190         audio.write_buffer = next_buffer;
   191         result->status = BUFFER_WRITING;
   192         return result;
   193     }
   194 }
   196 /**
   197  * Mark the current read buffer as empty and return the next buffer for
   198  * reading. If there is no next buffer yet, returns NULL.
   199  */
   200 audio_buffer_t audio_next_read_buffer( )
   201 {
   202     audio_buffer_t current = audio.output_buffers[audio.read_buffer];
   203     if( current->status == BUFFER_FULL ) {
   204         // Current read buffer has data, which we've just emptied
   205         current->status = BUFFER_EMPTY;
   206         current->posn = 0;
   207         audio.read_buffer++;
   208         if( audio.read_buffer == NUM_BUFFERS )
   209             audio.read_buffer = 0;
   211         current = audio.output_buffers[audio.read_buffer];
   212         if( current->status == BUFFER_FULL ) {
   213             current->posn = 0;
   214             return current;
   215         }
   216         else return NULL;
   217     } else {
   218         return NULL;
   219     }
   221 }
   223 /*************************** ADPCM ***********************************/
   225 /**
   226  * The following section borrows heavily from ffmpeg, which is
   227  * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
   228  * distributed under the GPL version 2 or later.
   229  */
   231 #define CLAMP_TO_SHORT(value) \
   232 if (value > 32767) \
   233     value = 32767; \
   234 else if (value < -32768) \
   235     value = -32768; \
   237 static const int yamaha_indexscale[] = {
   238     230, 230, 230, 230, 307, 409, 512, 614,
   239     230, 230, 230, 230, 307, 409, 512, 614
   240 };
   242 static const int yamaha_difflookup[] = {
   243     1, 3, 5, 7, 9, 11, 13, 15,
   244     -1, -3, -5, -7, -9, -11, -13, -15
   245 };
   247 static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
   248 						unsigned char nibble )
   249 {
   250     if( c->adpcm_step == 0 ) {
   251         c->adpcm_predict = 0;
   252         c->adpcm_step = 127;
   253     }
   255     c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
   256     CLAMP_TO_SHORT(c->adpcm_predict);
   257     c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
   258     c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
   259     return c->adpcm_predict;
   260 }
   262 /*************************** Sample mixer *****************************/
   264 /**
   265  * Mix a single output sample.
   266  */
   267 void audio_mix_samples( int num_samples )
   268 {
   269     int i, j;
   270     int32_t result_buf[num_samples][2];
   272     memset( &result_buf, 0, sizeof(result_buf) );
   274     for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
   275         audio_channel_t channel = &audio.channels[i];
   276         if( channel->active ) {
   277             int32_t sample;
   278             int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
   279             int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
   280             switch( channel->sample_format ) {
   281             case AUDIO_FMT_16BIT:
   282                 for( j=0; j<num_samples; j++ ) {
   283                     sample = ((int16_t *)(arm_mem + channel->start))[channel->posn];
   284                     result_buf[j][0] += sample * vol_left;
   285                     result_buf[j][1] += sample * vol_right;
   287                     channel->posn_left += channel->sample_rate;
   288                     while( channel->posn_left > audio.output_rate ) {
   289                         channel->posn_left -= audio.output_rate;
   290                         channel->posn++;
   292                         if( channel->posn == channel->end ) {
   293                             if( channel->loop ) {
   294                                 channel->posn = channel->loop_start;
   295                                 channel->loop = LOOP_LOOPED;
   296                             } else {
   297                                 audio_stop_channel(i);
   298                                 j = num_samples;
   299                                 break;
   300                             }
   301                         }
   302                     }
   303                 }
   304                 break;
   305             case AUDIO_FMT_8BIT:
   306                 for( j=0; j<num_samples; j++ ) {
   307                     sample = ((int8_t *)(arm_mem + channel->start))[channel->posn] << 8;
   308                     result_buf[j][0] += sample * vol_left;
   309                     result_buf[j][1] += sample * vol_right;
   311                     channel->posn_left += channel->sample_rate;
   312                     while( channel->posn_left > audio.output_rate ) {
   313                         channel->posn_left -= audio.output_rate;
   314                         channel->posn++;
   316                         if( channel->posn == channel->end ) {
   317                             if( channel->loop ) {
   318                                 channel->posn = channel->loop_start;
   319                                 channel->loop = LOOP_LOOPED;
   320                             } else {
   321                                 audio_stop_channel(i);
   322                                 j = num_samples;
   323                                 break;
   324                             }
   325                         }
   326                     }
   327                 }
   328                 break;
   329             case AUDIO_FMT_ADPCM:
   330                 for( j=0; j<num_samples; j++ ) {
   331                     sample = (int16_t)channel->adpcm_predict;
   332                     result_buf[j][0] += sample * vol_left;
   333                     result_buf[j][1] += sample * vol_right;
   334                     channel->posn_left += channel->sample_rate;
   335                     while( channel->posn_left > audio.output_rate ) {
   336                         channel->posn_left -= audio.output_rate;
   337                         channel->posn++;
   338                         if( channel->posn == channel->end ) {
   339                             if( channel->loop ) {
   340                                 channel->posn = channel->loop_start;
   341                                 channel->loop = LOOP_LOOPED;
   342                                 channel->adpcm_predict = 0;
   343                                 channel->adpcm_step = 0;
   344                             } else {
   345                                 audio_stop_channel(i);
   346                                 j = num_samples;
   347                                 break;
   348                             }
   349                         }
   350                         uint8_t data = ((uint8_t *)(arm_mem + channel->start))[channel->posn>>1];
   351                         if( channel->posn&1 ) {
   352                             adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
   353                         } else {
   354                             adpcm_yamaha_decode_nibble( channel, data & 0x0F );
   355                         }
   356                     }
   357                 }
   358                 break;
   359             default:
   360                 break;
   361             }
   362         }
   363     }
   365     /* Down-render to the final output format */
   366     audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
   367     if( buf->status == BUFFER_FULL ) {
   368         buf = audio_next_write_buffer();
   369         if( buf == NULL ) { // no available space
   370             return;
   371         }
   372     }
   374     switch( audio.output_format & AUDIO_FMT_SAMPLE_MASK ) {
   375     case AUDIO_FMT_FLOAT: {
   376         float scale = 1.0/SHRT_MAX;
   377         float *data = (float *)&buf->data[buf->posn];
   378         for( j=0; j<num_samples; j++ ) {
   379             *data++ = scale * (result_buf[j][0] >> 6);
   380             *data++ = scale * (result_buf[j][1] >> 6);
   381             buf->posn += 8;
   382             if( buf->posn == buf->length ) {
   383                 buf = audio_next_write_buffer();
   384                 if( buf == NULL ) {
   385                     break;
   386                 }
   387                 data = (float *)&buf->data[0];
   388             }
   389         }
   390         break;
   391     }
   392     case AUDIO_FMT_16BIT: {
   393         int16_t *data = (int16_t *)&buf->data[buf->posn];
   394         for( j=0; j < num_samples; j++ ) {
   395             *data++ = (int16_t)(result_buf[j][0] >> 6);
   396             *data++ = (int16_t)(result_buf[j][1] >> 6);	
   397             buf->posn += 4;
   398             if( buf->posn == buf->length ) {
   399                 buf = audio_next_write_buffer();
   400                 if( buf == NULL ) {
   401                     // All buffers are full
   402                     break;
   403                 }
   404                 data = (int16_t *)&buf->data[0];
   405             }
   406         }
   407         break;
   408     }
   409     case AUDIO_FMT_8BIT: {
   410         int8_t *data = (uint8_t *)&buf->data[buf->posn];
   411         for( j=0; j < num_samples; j++ ) {
   412             *data++ = (int8_t)(result_buf[j][0] >> 16);
   413             *data++ = (int8_t)(result_buf[j][1] >> 16);	
   414             buf->posn += 2;
   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                 buf = audio.output_buffers[audio.write_buffer];
   422                 data = (uint8_t *)&buf->data[0];
   423             }
   424         }
   425         break;
   426     }
   427     }
   428 }
   430 /********************** Internal AICA calls ***************************/
   432 audio_channel_t audio_get_channel( int channel ) 
   433 {
   434     return &audio.channels[channel];
   435 }
   437 void audio_start_stop_channel( int channel, gboolean start )
   438 {
   439     if( audio.channels[channel].active ) {
   440 	if( !start ) {
   441 	    audio_stop_channel(channel);
   442 	}
   443     } else if( start ) {
   444 	audio_start_channel(channel);
   445     }
   446 }
   448 void audio_stop_channel( int channel ) 
   449 {
   450     audio.channels[channel].active = FALSE;
   451 }
   454 void audio_start_channel( int channel )
   455 {
   456     audio.channels[channel].posn = 0;
   457     audio.channels[channel].posn_left = 0;
   458     audio.channels[channel].active = TRUE;
   459     if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
   460 	audio.channels[channel].adpcm_step = 0;
   461 	audio.channels[channel].adpcm_predict = 0;
   462 	uint8_t data = ((uint8_t *)(arm_mem + audio.channels[channel].start))[0];
   463 	adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
   464     }
   465 }
.