Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 561:533f6b478071
prev531:f0fee3ba71d1
next643:653b0a70f173
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@66
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@66
     3
 * 
nkeynes@66
     4
 * Audio mixer core. Combines all the active streams into a single sound
nkeynes@66
     5
 * buffer for output. 
nkeynes@66
     6
 *
nkeynes@66
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@66
     8
 *
nkeynes@66
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@66
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@66
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@66
    12
 * (at your option) any later version.
nkeynes@66
    13
 *
nkeynes@66
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@66
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@66
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@66
    17
 * GNU General Public License for more details.
nkeynes@66
    18
 */
nkeynes@66
    19
nkeynes@66
    20
#include "aica/aica.h"
nkeynes@66
    21
#include "aica/audio.h"
nkeynes@66
    22
#include "glib/gmem.h"
nkeynes@66
    23
#include "dream.h"
nkeynes@66
    24
#include <assert.h>
nkeynes@66
    25
#include <string.h>
nkeynes@66
    26
nkeynes@531
    27
audio_driver_t audio_driver_list[] = { 
nkeynes@531
    28
#ifdef HAVE_ESOUND
nkeynes@531
    29
				       &audio_esd_driver,
nkeynes@531
    30
#endif
nkeynes@531
    31
				       &audio_null_driver,
nkeynes@531
    32
				       NULL };
nkeynes@531
    33
nkeynes@66
    34
#define NUM_BUFFERS 3
nkeynes@434
    35
#define MS_PER_BUFFER 100
nkeynes@66
    36
nkeynes@66
    37
#define BUFFER_EMPTY   0
nkeynes@66
    38
#define BUFFER_WRITING 1
nkeynes@66
    39
#define BUFFER_FULL    2
nkeynes@66
    40
nkeynes@66
    41
struct audio_state {
nkeynes@66
    42
    audio_buffer_t output_buffers[NUM_BUFFERS];
nkeynes@66
    43
    int write_buffer;
nkeynes@66
    44
    int read_buffer;
nkeynes@66
    45
    uint32_t output_format;
nkeynes@66
    46
    uint32_t output_rate;
nkeynes@66
    47
    uint32_t output_sample_size;
nkeynes@465
    48
    struct audio_channel channels[AUDIO_CHANNEL_COUNT];
nkeynes@66
    49
} audio;
nkeynes@66
    50
nkeynes@66
    51
audio_driver_t audio_driver = NULL;
nkeynes@66
    52
nkeynes@66
    53
#define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
nkeynes@66
    54
nkeynes@66
    55
extern char *arm_mem;
nkeynes@66
    56
nkeynes@66
    57
/**
nkeynes@465
    58
 * Preserve audio channel state only - don't bother saving the buffers
nkeynes@465
    59
 */
nkeynes@465
    60
void audio_save_state( FILE *f )
nkeynes@465
    61
{
nkeynes@465
    62
    fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
nkeynes@465
    63
}
nkeynes@465
    64
nkeynes@465
    65
int audio_load_state( FILE *f )
nkeynes@465
    66
{
nkeynes@465
    67
    int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
nkeynes@465
    68
    return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
nkeynes@465
    69
}
nkeynes@465
    70
nkeynes@531
    71
audio_driver_t get_audio_driver_by_name( const char *name )
nkeynes@531
    72
{
nkeynes@531
    73
    int i;
nkeynes@531
    74
    if( name == NULL ) {
nkeynes@531
    75
	return audio_driver_list[0];
nkeynes@531
    76
    }
nkeynes@531
    77
    for( i=0; audio_driver_list[i] != NULL; i++ ) {
nkeynes@531
    78
	if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
nkeynes@531
    79
	    return audio_driver_list[i];
nkeynes@531
    80
	}
nkeynes@531
    81
    }
nkeynes@531
    82
nkeynes@531
    83
    return NULL;
nkeynes@531
    84
}
nkeynes@531
    85
nkeynes@465
    86
/**
nkeynes@66
    87
 * Set the output driver, sample rate and format. Also initializes the 
nkeynes@66
    88
 * output buffers, flushing any current data and reallocating as 
nkeynes@66
    89
 * necessary.
nkeynes@66
    90
 */
nkeynes@111
    91
gboolean audio_set_driver( audio_driver_t driver, 
nkeynes@111
    92
			   uint32_t samplerate, int format )
nkeynes@66
    93
{
nkeynes@66
    94
    uint32_t bytes_per_sample = 1;
nkeynes@66
    95
    uint32_t samples_per_buffer;
nkeynes@66
    96
    int i;
nkeynes@66
    97
nkeynes@111
    98
    if( audio_driver == NULL || driver != NULL ) {
nkeynes@111
    99
	if( driver == NULL  )
nkeynes@111
   100
	    driver = &audio_null_driver;
nkeynes@111
   101
	if( driver != audio_driver ) {	
nkeynes@111
   102
	    if( !driver->set_output_format( samplerate, format ) )
nkeynes@111
   103
		return FALSE;
nkeynes@111
   104
	    audio_driver = driver;
nkeynes@111
   105
	}
nkeynes@111
   106
    }
nkeynes@111
   107
nkeynes@66
   108
    if( format & AUDIO_FMT_16BIT )
nkeynes@66
   109
	bytes_per_sample = 2;
nkeynes@66
   110
    if( format & AUDIO_FMT_STEREO )
nkeynes@66
   111
	bytes_per_sample <<= 1;
nkeynes@66
   112
    if( samplerate == audio.output_rate &&
nkeynes@66
   113
	bytes_per_sample == audio.output_sample_size )
nkeynes@431
   114
	return TRUE;
nkeynes@66
   115
    samples_per_buffer = (samplerate * MS_PER_BUFFER / 1000);
nkeynes@66
   116
    for( i=0; i<NUM_BUFFERS; i++ ) {
nkeynes@66
   117
	if( audio.output_buffers[i] != NULL )
nkeynes@66
   118
	    free(audio.output_buffers[i]);
nkeynes@66
   119
	audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
nkeynes@73
   120
	audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
nkeynes@66
   121
	audio.output_buffers[i]->posn = 0;
nkeynes@66
   122
	audio.output_buffers[i]->status = BUFFER_EMPTY;
nkeynes@66
   123
    }
nkeynes@66
   124
    audio.output_format = format;
nkeynes@66
   125
    audio.output_rate = samplerate;
nkeynes@66
   126
    audio.output_sample_size = bytes_per_sample;
nkeynes@66
   127
    audio.write_buffer = 0;
nkeynes@66
   128
    audio.read_buffer = 0;
nkeynes@66
   129
nkeynes@111
   130
    return TRUE;
nkeynes@66
   131
}
nkeynes@66
   132
nkeynes@66
   133
/**
nkeynes@66
   134
 * Mark the current write buffer as full and prepare the next buffer for
nkeynes@66
   135
 * writing. Returns the next buffer to write to.
nkeynes@66
   136
 * If all buffers are full, returns NULL.
nkeynes@66
   137
 */
nkeynes@66
   138
audio_buffer_t audio_next_write_buffer( )
nkeynes@66
   139
{
nkeynes@66
   140
    audio_buffer_t result = NULL;
nkeynes@66
   141
    audio_buffer_t current = audio.output_buffers[audio.write_buffer];
nkeynes@66
   142
    current->status = BUFFER_FULL;
nkeynes@66
   143
    if( audio.read_buffer == audio.write_buffer &&
nkeynes@66
   144
	audio_driver->process_buffer( current ) ) {
nkeynes@66
   145
	audio_next_read_buffer();
nkeynes@66
   146
    }
nkeynes@66
   147
    audio.write_buffer = NEXT_BUFFER();
nkeynes@66
   148
    result = audio.output_buffers[audio.write_buffer];
nkeynes@66
   149
    if( result->status == BUFFER_FULL )
nkeynes@66
   150
	return NULL;
nkeynes@66
   151
    else {
nkeynes@66
   152
	result->status = BUFFER_WRITING;
nkeynes@66
   153
	return result;
nkeynes@66
   154
    }
nkeynes@66
   155
}
nkeynes@66
   156
nkeynes@66
   157
/**
nkeynes@66
   158
 * Mark the current read buffer as empty and return the next buffer for
nkeynes@66
   159
 * reading. If there is no next buffer yet, returns NULL.
nkeynes@66
   160
 */
nkeynes@66
   161
audio_buffer_t audio_next_read_buffer( )
nkeynes@66
   162
{
nkeynes@66
   163
    audio_buffer_t current = audio.output_buffers[audio.read_buffer];
nkeynes@66
   164
    assert( current->status == BUFFER_FULL );
nkeynes@66
   165
    current->status = BUFFER_EMPTY;
nkeynes@66
   166
    current->posn = 0;
nkeynes@66
   167
    audio.read_buffer++;
nkeynes@66
   168
    if( audio.read_buffer == NUM_BUFFERS )
nkeynes@66
   169
	audio.read_buffer = 0;
nkeynes@66
   170
    
nkeynes@66
   171
    current = audio.output_buffers[audio.read_buffer];
nkeynes@66
   172
    if( current->status == BUFFER_FULL )
nkeynes@66
   173
	return current;
nkeynes@66
   174
    else return NULL;
nkeynes@66
   175
}
nkeynes@66
   176
nkeynes@66
   177
/*************************** ADPCM ***********************************/
nkeynes@66
   178
nkeynes@66
   179
/**
nkeynes@66
   180
 * The following section borrows heavily from ffmpeg, which is
nkeynes@66
   181
 * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
nkeynes@66
   182
 * distributed under the GPL version 2 or later.
nkeynes@66
   183
 */
nkeynes@66
   184
nkeynes@66
   185
#define CLAMP_TO_SHORT(value) \
nkeynes@66
   186
if (value > 32767) \
nkeynes@66
   187
    value = 32767; \
nkeynes@66
   188
else if (value < -32768) \
nkeynes@66
   189
    value = -32768; \
nkeynes@66
   190
nkeynes@66
   191
static const int yamaha_indexscale[] = {
nkeynes@66
   192
    230, 230, 230, 230, 307, 409, 512, 614,
nkeynes@66
   193
    230, 230, 230, 230, 307, 409, 512, 614
nkeynes@66
   194
};
nkeynes@66
   195
nkeynes@66
   196
static const int yamaha_difflookup[] = {
nkeynes@66
   197
    1, 3, 5, 7, 9, 11, 13, 15,
nkeynes@66
   198
    -1, -3, -5, -7, -9, -11, -13, -15
nkeynes@66
   199
};
nkeynes@66
   200
nkeynes@66
   201
static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
nkeynes@66
   202
						unsigned char nibble )
nkeynes@66
   203
{
nkeynes@66
   204
    if( c->adpcm_step == 0 ) {
nkeynes@66
   205
        c->adpcm_predict = 0;
nkeynes@66
   206
        c->adpcm_step = 127;
nkeynes@66
   207
    }
nkeynes@66
   208
nkeynes@66
   209
    c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
nkeynes@66
   210
    CLAMP_TO_SHORT(c->adpcm_predict);
nkeynes@66
   211
    c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
nkeynes@66
   212
    c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
nkeynes@66
   213
    return c->adpcm_predict;
nkeynes@66
   214
}
nkeynes@66
   215
nkeynes@66
   216
/*************************** Sample mixer *****************************/
nkeynes@66
   217
nkeynes@66
   218
/**
nkeynes@66
   219
 * Mix a single output sample.
nkeynes@66
   220
 */
nkeynes@73
   221
void audio_mix_samples( int num_samples )
nkeynes@66
   222
{
nkeynes@66
   223
    int i, j;
nkeynes@73
   224
    int32_t result_buf[num_samples][2];
nkeynes@73
   225
nkeynes@73
   226
    memset( &result_buf, 0, sizeof(result_buf) );
nkeynes@66
   227
nkeynes@465
   228
    for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
nkeynes@66
   229
	audio_channel_t channel = &audio.channels[i];
nkeynes@66
   230
	if( channel->active ) {
nkeynes@66
   231
	    int32_t sample;
nkeynes@82
   232
	    int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
nkeynes@82
   233
	    int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
nkeynes@66
   234
	    switch( channel->sample_format ) {
nkeynes@66
   235
	    case AUDIO_FMT_16BIT:
nkeynes@73
   236
		for( j=0; j<num_samples; j++ ) {
nkeynes@434
   237
		    sample = ((int16_t *)(arm_mem + channel->start))[channel->posn];
nkeynes@82
   238
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   239
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   240
		    
nkeynes@73
   241
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   242
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   243
			channel->posn_left -= audio.output_rate;
nkeynes@73
   244
			channel->posn++;
nkeynes@73
   245
			
nkeynes@73
   246
			if( channel->posn == channel->end ) {
nkeynes@463
   247
			    if( channel->loop ) {
nkeynes@73
   248
				channel->posn = channel->loop_start;
nkeynes@463
   249
				channel->loop = LOOP_LOOPED;
nkeynes@463
   250
			    } else {
nkeynes@73
   251
				audio_stop_channel(i);
nkeynes@73
   252
				j = num_samples;
nkeynes@73
   253
				break;
nkeynes@73
   254
			    }
nkeynes@73
   255
			}
nkeynes@73
   256
		    }
nkeynes@73
   257
		}
nkeynes@66
   258
		break;
nkeynes@66
   259
	    case AUDIO_FMT_8BIT:
nkeynes@73
   260
		for( j=0; j<num_samples; j++ ) {
nkeynes@434
   261
		    sample = ((int8_t *)(arm_mem + channel->start))[channel->posn] << 8;
nkeynes@82
   262
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   263
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   264
		    
nkeynes@73
   265
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   266
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   267
			channel->posn_left -= audio.output_rate;
nkeynes@73
   268
			channel->posn++;
nkeynes@73
   269
			
nkeynes@73
   270
			if( channel->posn == channel->end ) {
nkeynes@463
   271
			    if( channel->loop ) {
nkeynes@73
   272
				channel->posn = channel->loop_start;
nkeynes@463
   273
				channel->loop = LOOP_LOOPED;
nkeynes@463
   274
			    } else {
nkeynes@73
   275
				audio_stop_channel(i);
nkeynes@73
   276
				j = num_samples;
nkeynes@73
   277
				break;
nkeynes@73
   278
			    }
nkeynes@73
   279
			}
nkeynes@73
   280
		    }
nkeynes@73
   281
		}
nkeynes@66
   282
		break;
nkeynes@66
   283
	    case AUDIO_FMT_ADPCM:
nkeynes@73
   284
		for( j=0; j<num_samples; j++ ) {
nkeynes@73
   285
		    sample = (int16_t)channel->adpcm_predict;
nkeynes@82
   286
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   287
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   288
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   289
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   290
			channel->posn_left -= audio.output_rate;
nkeynes@434
   291
			channel->posn++;
nkeynes@434
   292
			if( channel->posn == channel->end ) {
nkeynes@434
   293
			    if( channel->loop ) {
nkeynes@434
   294
				channel->posn = channel->loop_start;
nkeynes@463
   295
				channel->loop = LOOP_LOOPED;
nkeynes@434
   296
				channel->adpcm_predict = 0;
nkeynes@434
   297
				channel->adpcm_step = 0;
nkeynes@434
   298
			    } else {
nkeynes@434
   299
				audio_stop_channel(i);
nkeynes@434
   300
				j = num_samples;
nkeynes@73
   301
				break;
nkeynes@73
   302
			    }
nkeynes@434
   303
			}
nkeynes@434
   304
			uint8_t data = ((uint8_t *)(arm_mem + channel->start))[channel->posn>>1];
nkeynes@434
   305
			if( channel->posn&1 ) {
nkeynes@434
   306
			    adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
nkeynes@434
   307
			} else {
nkeynes@73
   308
			    adpcm_yamaha_decode_nibble( channel, data & 0x0F );
nkeynes@73
   309
			}
nkeynes@73
   310
		    }
nkeynes@73
   311
		}
nkeynes@73
   312
		break;
nkeynes@66
   313
	    default:
nkeynes@73
   314
		break;
nkeynes@66
   315
	    }
nkeynes@66
   316
	}
nkeynes@66
   317
    }
nkeynes@73
   318
	    
nkeynes@66
   319
    /* Down-render to the final output format */
nkeynes@73
   320
    
nkeynes@66
   321
    if( audio.output_format & AUDIO_FMT_16BIT ) {
nkeynes@73
   322
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   323
	uint16_t *data = (uint16_t *)&buf->data[buf->posn];
nkeynes@73
   324
	for( j=0; j < num_samples; j++ ) {
nkeynes@82
   325
	    *data++ = (int16_t)(result_buf[j][0] >> 6);
nkeynes@82
   326
	    *data++ = (int16_t)(result_buf[j][1] >> 6);	
nkeynes@73
   327
	    buf->posn += 4;
nkeynes@73
   328
	    if( buf->posn == buf->length ) {
nkeynes@73
   329
		audio_next_write_buffer();
nkeynes@73
   330
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   331
		data = (uint16_t *)&buf->data[0];
nkeynes@73
   332
	    }
nkeynes@73
   333
	}
nkeynes@66
   334
    } else {
nkeynes@73
   335
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   336
	uint8_t *data = (uint8_t *)&buf->data[buf->posn];
nkeynes@73
   337
	for( j=0; j < num_samples; j++ ) {
nkeynes@73
   338
	    *data++ = (uint8_t)(result_buf[j][0] >> 16);
nkeynes@73
   339
	    *data++ = (uint8_t)(result_buf[j][1] >> 16);	
nkeynes@73
   340
	    buf->posn += 2;
nkeynes@73
   341
	    if( buf->posn == buf->length ) {
nkeynes@73
   342
		audio_next_write_buffer();
nkeynes@73
   343
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   344
		data = (uint8_t *)&buf->data[0];
nkeynes@73
   345
	    }
nkeynes@73
   346
	}
nkeynes@66
   347
    }
nkeynes@66
   348
}
nkeynes@66
   349
nkeynes@66
   350
/********************** Internal AICA calls ***************************/
nkeynes@66
   351
nkeynes@66
   352
audio_channel_t audio_get_channel( int channel ) 
nkeynes@66
   353
{
nkeynes@66
   354
    return &audio.channels[channel];
nkeynes@66
   355
}
nkeynes@66
   356
nkeynes@434
   357
void audio_start_stop_channel( int channel, gboolean start )
nkeynes@434
   358
{
nkeynes@434
   359
    if( audio.channels[channel].active ) {
nkeynes@434
   360
	if( !start ) {
nkeynes@434
   361
	    audio_stop_channel(channel);
nkeynes@434
   362
	}
nkeynes@434
   363
    } else if( start ) {
nkeynes@434
   364
	audio_start_channel(channel);
nkeynes@434
   365
    }
nkeynes@434
   366
}
nkeynes@434
   367
nkeynes@66
   368
void audio_stop_channel( int channel ) 
nkeynes@66
   369
{
nkeynes@66
   370
    audio.channels[channel].active = FALSE;
nkeynes@66
   371
}
nkeynes@66
   372
nkeynes@66
   373
nkeynes@66
   374
void audio_start_channel( int channel )
nkeynes@66
   375
{
nkeynes@66
   376
    audio.channels[channel].posn = 0;
nkeynes@66
   377
    audio.channels[channel].posn_left = 0;
nkeynes@66
   378
    audio.channels[channel].active = TRUE;
nkeynes@434
   379
    if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
nkeynes@434
   380
	audio.channels[channel].adpcm_step = 0;
nkeynes@434
   381
	audio.channels[channel].adpcm_predict = 0;
nkeynes@434
   382
	uint8_t data = ((uint8_t *)(arm_mem + audio.channels[channel].start))[0];
nkeynes@434
   383
	adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
nkeynes@434
   384
    }
nkeynes@66
   385
}
.