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