Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 111:230243c2b520
prev106:9048bac046c3
next242:04f5cdb68d8e
author nkeynes
date Thu Mar 16 12:41:59 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Handle driver-unable-to-init case
file annotate diff log raw
nkeynes@66
     1
/**
nkeynes@111
     2
 * $Id: audio.c,v 1.6 2006-03-16 12:41:59 nkeynes Exp $
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@66
    27
#define NUM_BUFFERS 3
nkeynes@82
    28
#define MS_PER_BUFFER 1000
nkeynes@66
    29
nkeynes@66
    30
#define BUFFER_EMPTY   0
nkeynes@66
    31
#define BUFFER_WRITING 1
nkeynes@66
    32
#define BUFFER_FULL    2
nkeynes@66
    33
nkeynes@66
    34
struct audio_state {
nkeynes@66
    35
    audio_buffer_t output_buffers[NUM_BUFFERS];
nkeynes@66
    36
    int write_buffer;
nkeynes@66
    37
    int read_buffer;
nkeynes@66
    38
    uint32_t output_format;
nkeynes@66
    39
    uint32_t output_rate;
nkeynes@66
    40
    uint32_t output_sample_size;
nkeynes@66
    41
    struct audio_channel channels[64];
nkeynes@66
    42
} audio;
nkeynes@66
    43
nkeynes@66
    44
audio_driver_t audio_driver = NULL;
nkeynes@66
    45
nkeynes@66
    46
#define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
nkeynes@66
    47
nkeynes@66
    48
extern char *arm_mem;
nkeynes@66
    49
nkeynes@66
    50
/**
nkeynes@66
    51
 * Set the output driver, sample rate and format. Also initializes the 
nkeynes@66
    52
 * output buffers, flushing any current data and reallocating as 
nkeynes@66
    53
 * necessary.
nkeynes@66
    54
 */
nkeynes@111
    55
gboolean audio_set_driver( audio_driver_t driver, 
nkeynes@111
    56
			   uint32_t samplerate, int format )
nkeynes@66
    57
{
nkeynes@66
    58
    uint32_t bytes_per_sample = 1;
nkeynes@66
    59
    uint32_t samples_per_buffer;
nkeynes@66
    60
    int i;
nkeynes@66
    61
nkeynes@111
    62
    if( audio_driver == NULL || driver != NULL ) {
nkeynes@111
    63
	if( driver == NULL  )
nkeynes@111
    64
	    driver = &audio_null_driver;
nkeynes@111
    65
	if( driver != audio_driver ) {	
nkeynes@111
    66
	    if( !driver->set_output_format( samplerate, format ) )
nkeynes@111
    67
		return FALSE;
nkeynes@111
    68
	    audio_driver = driver;
nkeynes@111
    69
	}
nkeynes@111
    70
    }
nkeynes@111
    71
nkeynes@66
    72
    if( format & AUDIO_FMT_16BIT )
nkeynes@66
    73
	bytes_per_sample = 2;
nkeynes@66
    74
    if( format & AUDIO_FMT_STEREO )
nkeynes@66
    75
	bytes_per_sample <<= 1;
nkeynes@66
    76
    if( samplerate == audio.output_rate &&
nkeynes@66
    77
	bytes_per_sample == audio.output_sample_size )
nkeynes@66
    78
	return;
nkeynes@66
    79
    samples_per_buffer = (samplerate * MS_PER_BUFFER / 1000);
nkeynes@66
    80
    for( i=0; i<NUM_BUFFERS; i++ ) {
nkeynes@66
    81
	if( audio.output_buffers[i] != NULL )
nkeynes@66
    82
	    free(audio.output_buffers[i]);
nkeynes@66
    83
	audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
nkeynes@73
    84
	audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
nkeynes@66
    85
	audio.output_buffers[i]->posn = 0;
nkeynes@66
    86
	audio.output_buffers[i]->status = BUFFER_EMPTY;
nkeynes@66
    87
    }
nkeynes@66
    88
    audio.output_format = format;
nkeynes@66
    89
    audio.output_rate = samplerate;
nkeynes@66
    90
    audio.output_sample_size = bytes_per_sample;
nkeynes@66
    91
    audio.write_buffer = 0;
nkeynes@66
    92
    audio.read_buffer = 0;
nkeynes@66
    93
nkeynes@111
    94
    return TRUE;
nkeynes@66
    95
}
nkeynes@66
    96
nkeynes@66
    97
/**
nkeynes@66
    98
 * Mark the current write buffer as full and prepare the next buffer for
nkeynes@66
    99
 * writing. Returns the next buffer to write to.
nkeynes@66
   100
 * If all buffers are full, returns NULL.
nkeynes@66
   101
 */
nkeynes@66
   102
audio_buffer_t audio_next_write_buffer( )
nkeynes@66
   103
{
nkeynes@66
   104
    audio_buffer_t result = NULL;
nkeynes@66
   105
    audio_buffer_t current = audio.output_buffers[audio.write_buffer];
nkeynes@66
   106
    current->status = BUFFER_FULL;
nkeynes@66
   107
    if( audio.read_buffer == audio.write_buffer &&
nkeynes@66
   108
	audio_driver->process_buffer( current ) ) {
nkeynes@66
   109
	audio_next_read_buffer();
nkeynes@66
   110
    }
nkeynes@66
   111
    audio.write_buffer = NEXT_BUFFER();
nkeynes@66
   112
    result = audio.output_buffers[audio.write_buffer];
nkeynes@66
   113
    if( result->status == BUFFER_FULL )
nkeynes@66
   114
	return NULL;
nkeynes@66
   115
    else {
nkeynes@66
   116
	result->status = BUFFER_WRITING;
nkeynes@66
   117
	return result;
nkeynes@66
   118
    }
nkeynes@66
   119
}
nkeynes@66
   120
nkeynes@66
   121
/**
nkeynes@66
   122
 * Mark the current read buffer as empty and return the next buffer for
nkeynes@66
   123
 * reading. If there is no next buffer yet, returns NULL.
nkeynes@66
   124
 */
nkeynes@66
   125
audio_buffer_t audio_next_read_buffer( )
nkeynes@66
   126
{
nkeynes@66
   127
    audio_buffer_t current = audio.output_buffers[audio.read_buffer];
nkeynes@66
   128
    assert( current->status == BUFFER_FULL );
nkeynes@66
   129
    current->status = BUFFER_EMPTY;
nkeynes@66
   130
    current->posn = 0;
nkeynes@66
   131
    audio.read_buffer++;
nkeynes@66
   132
    if( audio.read_buffer == NUM_BUFFERS )
nkeynes@66
   133
	audio.read_buffer = 0;
nkeynes@66
   134
    
nkeynes@66
   135
    current = audio.output_buffers[audio.read_buffer];
nkeynes@66
   136
    if( current->status == BUFFER_FULL )
nkeynes@66
   137
	return current;
nkeynes@66
   138
    else return NULL;
nkeynes@66
   139
}
nkeynes@66
   140
nkeynes@66
   141
/*************************** ADPCM ***********************************/
nkeynes@66
   142
nkeynes@66
   143
/**
nkeynes@66
   144
 * The following section borrows heavily from ffmpeg, which is
nkeynes@66
   145
 * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
nkeynes@66
   146
 * distributed under the GPL version 2 or later.
nkeynes@66
   147
 */
nkeynes@66
   148
nkeynes@66
   149
#define CLAMP_TO_SHORT(value) \
nkeynes@66
   150
if (value > 32767) \
nkeynes@66
   151
    value = 32767; \
nkeynes@66
   152
else if (value < -32768) \
nkeynes@66
   153
    value = -32768; \
nkeynes@66
   154
nkeynes@66
   155
static const int yamaha_indexscale[] = {
nkeynes@66
   156
    230, 230, 230, 230, 307, 409, 512, 614,
nkeynes@66
   157
    230, 230, 230, 230, 307, 409, 512, 614
nkeynes@66
   158
};
nkeynes@66
   159
nkeynes@66
   160
static const int yamaha_difflookup[] = {
nkeynes@66
   161
    1, 3, 5, 7, 9, 11, 13, 15,
nkeynes@66
   162
    -1, -3, -5, -7, -9, -11, -13, -15
nkeynes@66
   163
};
nkeynes@66
   164
nkeynes@66
   165
static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
nkeynes@66
   166
						unsigned char nibble )
nkeynes@66
   167
{
nkeynes@66
   168
    if( c->adpcm_step == 0 ) {
nkeynes@66
   169
        c->adpcm_predict = 0;
nkeynes@66
   170
        c->adpcm_step = 127;
nkeynes@66
   171
    }
nkeynes@66
   172
nkeynes@66
   173
    c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
nkeynes@66
   174
    CLAMP_TO_SHORT(c->adpcm_predict);
nkeynes@66
   175
    c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
nkeynes@66
   176
    c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
nkeynes@66
   177
    return c->adpcm_predict;
nkeynes@66
   178
}
nkeynes@66
   179
nkeynes@66
   180
/*************************** Sample mixer *****************************/
nkeynes@66
   181
nkeynes@66
   182
/**
nkeynes@66
   183
 * Mix a single output sample.
nkeynes@66
   184
 */
nkeynes@73
   185
void audio_mix_samples( int num_samples )
nkeynes@66
   186
{
nkeynes@66
   187
    int i, j;
nkeynes@73
   188
    int32_t result_buf[num_samples][2];
nkeynes@73
   189
nkeynes@73
   190
    memset( &result_buf, 0, sizeof(result_buf) );
nkeynes@66
   191
nkeynes@66
   192
    for( i=0; i < 64; i++ ) {
nkeynes@66
   193
	audio_channel_t channel = &audio.channels[i];
nkeynes@66
   194
	if( channel->active ) {
nkeynes@66
   195
	    int32_t sample;
nkeynes@82
   196
	    int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
nkeynes@82
   197
	    int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
nkeynes@66
   198
	    switch( channel->sample_format ) {
nkeynes@66
   199
	    case AUDIO_FMT_16BIT:
nkeynes@73
   200
		for( j=0; j<num_samples; j++ ) {
nkeynes@73
   201
		    sample = *(int16_t *)(arm_mem + channel->posn + channel->start);
nkeynes@82
   202
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   203
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   204
		    
nkeynes@73
   205
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   206
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   207
			channel->posn_left -= audio.output_rate;
nkeynes@73
   208
			channel->posn++;
nkeynes@73
   209
			
nkeynes@73
   210
			if( channel->posn == channel->end ) {
nkeynes@73
   211
			    if( channel->loop )
nkeynes@73
   212
				channel->posn = channel->loop_start;
nkeynes@73
   213
			    else {
nkeynes@73
   214
				audio_stop_channel(i);
nkeynes@73
   215
				j = num_samples;
nkeynes@73
   216
				break;
nkeynes@73
   217
			    }
nkeynes@73
   218
			}
nkeynes@73
   219
		    }
nkeynes@73
   220
		}
nkeynes@66
   221
		break;
nkeynes@66
   222
	    case AUDIO_FMT_8BIT:
nkeynes@73
   223
		for( j=0; j<num_samples; j++ ) {
nkeynes@73
   224
		    sample = (*(int8_t *)(arm_mem + channel->posn + channel->start)) << 8;
nkeynes@82
   225
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   226
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   227
		    
nkeynes@73
   228
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   229
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   230
			channel->posn_left -= audio.output_rate;
nkeynes@73
   231
			channel->posn++;
nkeynes@73
   232
			
nkeynes@73
   233
			if( channel->posn == channel->end ) {
nkeynes@73
   234
			    if( channel->loop )
nkeynes@73
   235
				channel->posn = channel->loop_start;
nkeynes@73
   236
			    else {
nkeynes@73
   237
				audio_stop_channel(i);
nkeynes@73
   238
				j = num_samples;
nkeynes@73
   239
				break;
nkeynes@73
   240
			    }
nkeynes@73
   241
			}
nkeynes@73
   242
		    }
nkeynes@73
   243
		}
nkeynes@66
   244
		break;
nkeynes@66
   245
	    case AUDIO_FMT_ADPCM:
nkeynes@73
   246
		for( j=0; j<num_samples; j++ ) {
nkeynes@73
   247
		    sample = (int16_t)channel->adpcm_predict;
nkeynes@82
   248
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   249
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   250
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   251
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   252
			channel->posn_left -= audio.output_rate;
nkeynes@73
   253
			if( channel->adpcm_nibble == 0 ) {
nkeynes@73
   254
			    uint8_t data = *(uint8_t *)(arm_mem + channel->posn + channel->start);
nkeynes@73
   255
			    adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
nkeynes@73
   256
			    channel->adpcm_nibble = 1;
nkeynes@73
   257
			} else {
nkeynes@73
   258
			    channel->posn++;
nkeynes@73
   259
			    if( channel->posn == channel->end ) {
nkeynes@73
   260
				if( channel->loop )
nkeynes@73
   261
				    channel->posn = channel->loop_start;
nkeynes@73
   262
				else
nkeynes@73
   263
				    audio_stop_channel(i);
nkeynes@73
   264
				break;
nkeynes@73
   265
			    }
nkeynes@73
   266
			    uint8_t data = *(uint8_t *)(arm_mem + channel->posn + channel->start);
nkeynes@73
   267
			    adpcm_yamaha_decode_nibble( channel, data & 0x0F );
nkeynes@73
   268
			    channel->adpcm_nibble = 0;
nkeynes@73
   269
			}
nkeynes@73
   270
		    }
nkeynes@73
   271
		}
nkeynes@73
   272
		break;
nkeynes@66
   273
	    default:
nkeynes@73
   274
		break;
nkeynes@66
   275
	    }
nkeynes@66
   276
	}
nkeynes@66
   277
    }
nkeynes@73
   278
	    
nkeynes@66
   279
    /* Down-render to the final output format */
nkeynes@73
   280
    
nkeynes@66
   281
    if( audio.output_format & AUDIO_FMT_16BIT ) {
nkeynes@73
   282
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   283
	uint16_t *data = (uint16_t *)&buf->data[buf->posn];
nkeynes@73
   284
	for( j=0; j < num_samples; j++ ) {
nkeynes@82
   285
	    *data++ = (int16_t)(result_buf[j][0] >> 6);
nkeynes@82
   286
	    *data++ = (int16_t)(result_buf[j][1] >> 6);	
nkeynes@73
   287
	    buf->posn += 4;
nkeynes@73
   288
	    if( buf->posn == buf->length ) {
nkeynes@73
   289
		audio_next_write_buffer();
nkeynes@73
   290
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   291
		data = (uint16_t *)&buf->data[0];
nkeynes@73
   292
	    }
nkeynes@73
   293
	}
nkeynes@66
   294
    } else {
nkeynes@73
   295
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   296
	uint8_t *data = (uint8_t *)&buf->data[buf->posn];
nkeynes@73
   297
	for( j=0; j < num_samples; j++ ) {
nkeynes@73
   298
	    *data++ = (uint8_t)(result_buf[j][0] >> 16);
nkeynes@73
   299
	    *data++ = (uint8_t)(result_buf[j][1] >> 16);	
nkeynes@73
   300
	    buf->posn += 2;
nkeynes@73
   301
	    if( buf->posn == buf->length ) {
nkeynes@73
   302
		audio_next_write_buffer();
nkeynes@73
   303
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   304
		data = (uint8_t *)&buf->data[0];
nkeynes@73
   305
	    }
nkeynes@73
   306
	}
nkeynes@66
   307
    }
nkeynes@66
   308
}
nkeynes@66
   309
nkeynes@66
   310
/********************** Internal AICA calls ***************************/
nkeynes@66
   311
nkeynes@66
   312
audio_channel_t audio_get_channel( int channel ) 
nkeynes@66
   313
{
nkeynes@66
   314
    return &audio.channels[channel];
nkeynes@66
   315
}
nkeynes@66
   316
nkeynes@66
   317
void audio_stop_channel( int channel ) 
nkeynes@66
   318
{
nkeynes@66
   319
    audio.channels[channel].active = FALSE;
nkeynes@66
   320
}
nkeynes@66
   321
nkeynes@66
   322
nkeynes@66
   323
void audio_start_channel( int channel )
nkeynes@66
   324
{
nkeynes@66
   325
    audio.channels[channel].posn = 0;
nkeynes@66
   326
    audio.channels[channel].posn_left = 0;
nkeynes@66
   327
    audio.channels[channel].adpcm_nibble = 0;
nkeynes@66
   328
    audio.channels[channel].adpcm_step = 0;
nkeynes@66
   329
    audio.channels[channel].adpcm_predict = 0;
nkeynes@66
   330
    audio.channels[channel].active = TRUE;
nkeynes@66
   331
}
.