Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 463:0655796f9bb5
prev434:8af49a412d92
next465:3bd7be575792
author nkeynes
date Wed Oct 24 21:24:09 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement channel position readback
file annotate diff log raw
nkeynes@66
     1
/**
nkeynes@463
     2
 * $Id: audio.c,v 1.10 2007-10-24 21:24:09 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@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@431
    78
	return TRUE;
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@434
   201
		    sample = ((int16_t *)(arm_mem + channel->start))[channel->posn];
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@463
   211
			    if( channel->loop ) {
nkeynes@73
   212
				channel->posn = channel->loop_start;
nkeynes@463
   213
				channel->loop = LOOP_LOOPED;
nkeynes@463
   214
			    } else {
nkeynes@73
   215
				audio_stop_channel(i);
nkeynes@73
   216
				j = num_samples;
nkeynes@73
   217
				break;
nkeynes@73
   218
			    }
nkeynes@73
   219
			}
nkeynes@73
   220
		    }
nkeynes@73
   221
		}
nkeynes@66
   222
		break;
nkeynes@66
   223
	    case AUDIO_FMT_8BIT:
nkeynes@73
   224
		for( j=0; j<num_samples; j++ ) {
nkeynes@434
   225
		    sample = ((int8_t *)(arm_mem + channel->start))[channel->posn] << 8;
nkeynes@82
   226
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   227
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   228
		    
nkeynes@73
   229
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   230
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   231
			channel->posn_left -= audio.output_rate;
nkeynes@73
   232
			channel->posn++;
nkeynes@73
   233
			
nkeynes@73
   234
			if( channel->posn == channel->end ) {
nkeynes@463
   235
			    if( channel->loop ) {
nkeynes@73
   236
				channel->posn = channel->loop_start;
nkeynes@463
   237
				channel->loop = LOOP_LOOPED;
nkeynes@463
   238
			    } else {
nkeynes@73
   239
				audio_stop_channel(i);
nkeynes@73
   240
				j = num_samples;
nkeynes@73
   241
				break;
nkeynes@73
   242
			    }
nkeynes@73
   243
			}
nkeynes@73
   244
		    }
nkeynes@73
   245
		}
nkeynes@66
   246
		break;
nkeynes@66
   247
	    case AUDIO_FMT_ADPCM:
nkeynes@73
   248
		for( j=0; j<num_samples; j++ ) {
nkeynes@73
   249
		    sample = (int16_t)channel->adpcm_predict;
nkeynes@82
   250
		    result_buf[j][0] += sample * vol_left;
nkeynes@82
   251
		    result_buf[j][1] += sample * vol_right;
nkeynes@73
   252
		    channel->posn_left += channel->sample_rate;
nkeynes@73
   253
		    while( channel->posn_left > audio.output_rate ) {
nkeynes@73
   254
			channel->posn_left -= audio.output_rate;
nkeynes@434
   255
			channel->posn++;
nkeynes@434
   256
			if( channel->posn == channel->end ) {
nkeynes@434
   257
			    if( channel->loop ) {
nkeynes@434
   258
				channel->posn = channel->loop_start;
nkeynes@463
   259
				channel->loop = LOOP_LOOPED;
nkeynes@434
   260
				channel->adpcm_predict = 0;
nkeynes@434
   261
				channel->adpcm_step = 0;
nkeynes@434
   262
			    } else {
nkeynes@434
   263
				audio_stop_channel(i);
nkeynes@434
   264
				j = num_samples;
nkeynes@73
   265
				break;
nkeynes@73
   266
			    }
nkeynes@434
   267
			}
nkeynes@434
   268
			uint8_t data = ((uint8_t *)(arm_mem + channel->start))[channel->posn>>1];
nkeynes@434
   269
			if( channel->posn&1 ) {
nkeynes@434
   270
			    adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
nkeynes@434
   271
			} else {
nkeynes@73
   272
			    adpcm_yamaha_decode_nibble( channel, data & 0x0F );
nkeynes@73
   273
			}
nkeynes@73
   274
		    }
nkeynes@73
   275
		}
nkeynes@73
   276
		break;
nkeynes@66
   277
	    default:
nkeynes@73
   278
		break;
nkeynes@66
   279
	    }
nkeynes@66
   280
	}
nkeynes@66
   281
    }
nkeynes@73
   282
	    
nkeynes@66
   283
    /* Down-render to the final output format */
nkeynes@73
   284
    
nkeynes@66
   285
    if( audio.output_format & AUDIO_FMT_16BIT ) {
nkeynes@73
   286
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   287
	uint16_t *data = (uint16_t *)&buf->data[buf->posn];
nkeynes@73
   288
	for( j=0; j < num_samples; j++ ) {
nkeynes@82
   289
	    *data++ = (int16_t)(result_buf[j][0] >> 6);
nkeynes@82
   290
	    *data++ = (int16_t)(result_buf[j][1] >> 6);	
nkeynes@73
   291
	    buf->posn += 4;
nkeynes@73
   292
	    if( buf->posn == buf->length ) {
nkeynes@73
   293
		audio_next_write_buffer();
nkeynes@73
   294
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   295
		data = (uint16_t *)&buf->data[0];
nkeynes@73
   296
	    }
nkeynes@73
   297
	}
nkeynes@66
   298
    } else {
nkeynes@73
   299
	audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   300
	uint8_t *data = (uint8_t *)&buf->data[buf->posn];
nkeynes@73
   301
	for( j=0; j < num_samples; j++ ) {
nkeynes@73
   302
	    *data++ = (uint8_t)(result_buf[j][0] >> 16);
nkeynes@73
   303
	    *data++ = (uint8_t)(result_buf[j][1] >> 16);	
nkeynes@73
   304
	    buf->posn += 2;
nkeynes@73
   305
	    if( buf->posn == buf->length ) {
nkeynes@73
   306
		audio_next_write_buffer();
nkeynes@73
   307
		buf = audio.output_buffers[audio.write_buffer];
nkeynes@73
   308
		data = (uint8_t *)&buf->data[0];
nkeynes@73
   309
	    }
nkeynes@73
   310
	}
nkeynes@66
   311
    }
nkeynes@66
   312
}
nkeynes@66
   313
nkeynes@66
   314
/********************** Internal AICA calls ***************************/
nkeynes@66
   315
nkeynes@66
   316
audio_channel_t audio_get_channel( int channel ) 
nkeynes@66
   317
{
nkeynes@66
   318
    return &audio.channels[channel];
nkeynes@66
   319
}
nkeynes@66
   320
nkeynes@434
   321
void audio_start_stop_channel( int channel, gboolean start )
nkeynes@434
   322
{
nkeynes@434
   323
    if( audio.channels[channel].active ) {
nkeynes@434
   324
	if( !start ) {
nkeynes@434
   325
	    audio_stop_channel(channel);
nkeynes@434
   326
	}
nkeynes@434
   327
    } else if( start ) {
nkeynes@434
   328
	audio_start_channel(channel);
nkeynes@434
   329
    }
nkeynes@434
   330
}
nkeynes@434
   331
nkeynes@66
   332
void audio_stop_channel( int channel ) 
nkeynes@66
   333
{
nkeynes@66
   334
    audio.channels[channel].active = FALSE;
nkeynes@66
   335
}
nkeynes@66
   336
nkeynes@66
   337
nkeynes@66
   338
void audio_start_channel( int channel )
nkeynes@66
   339
{
nkeynes@66
   340
    audio.channels[channel].posn = 0;
nkeynes@66
   341
    audio.channels[channel].posn_left = 0;
nkeynes@66
   342
    audio.channels[channel].active = TRUE;
nkeynes@434
   343
    if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
nkeynes@434
   344
	audio.channels[channel].adpcm_step = 0;
nkeynes@434
   345
	audio.channels[channel].adpcm_predict = 0;
nkeynes@434
   346
	uint8_t data = ((uint8_t *)(arm_mem + audio.channels[channel].start))[0];
nkeynes@434
   347
	adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
nkeynes@434
   348
    }
nkeynes@66
   349
}
.