Search
lxdream.org :: lxdream/src/aica/audio.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/audio.c
changeset 989:7baf5ecd8e98
prev934:3acd3b3ee6d1
next1024:c67f2d61ab97
author nkeynes
date Sat Feb 28 06:26:48 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Add SDL audio driver, thanks to Wahrhaft!
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@700
    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@697
    27
nkeynes@697
    28
extern struct audio_driver audio_null_driver;
nkeynes@697
    29
extern struct audio_driver audio_osx_driver;
nkeynes@697
    30
extern struct audio_driver audio_pulse_driver;
nkeynes@697
    31
extern struct audio_driver audio_esd_driver;
nkeynes@697
    32
extern struct audio_driver audio_alsa_driver;
nkeynes@989
    33
extern struct audio_driver audio_sdl_driver;
nkeynes@697
    34
nkeynes@697
    35
audio_driver_t audio_driver_list[] = {
nkeynes@697
    36
#ifdef HAVE_CORE_AUDIO
nkeynes@736
    37
        &audio_osx_driver,
nkeynes@697
    38
#endif
nkeynes@989
    39
#ifdef HAVE_SDL
nkeynes@989
    40
        &audio_sdl_driver,
nkeynes@989
    41
#endif
nkeynes@657
    42
#ifdef HAVE_PULSE
nkeynes@736
    43
        &audio_pulse_driver,
nkeynes@657
    44
#endif
nkeynes@531
    45
#ifdef HAVE_ESOUND
nkeynes@736
    46
        &audio_esd_driver,
nkeynes@531
    47
#endif
bhaal22@643
    48
#ifdef HAVE_ALSA
nkeynes@736
    49
        &audio_alsa_driver,
bhaal22@643
    50
#endif
nkeynes@736
    51
        &audio_null_driver,
nkeynes@736
    52
        NULL };
nkeynes@531
    53
nkeynes@66
    54
#define NUM_BUFFERS 3
nkeynes@700
    55
#define MS_PER_BUFFER 100
nkeynes@66
    56
nkeynes@66
    57
#define BUFFER_EMPTY   0
nkeynes@66
    58
#define BUFFER_WRITING 1
nkeynes@66
    59
#define BUFFER_FULL    2
nkeynes@66
    60
nkeynes@66
    61
struct audio_state {
nkeynes@66
    62
    audio_buffer_t output_buffers[NUM_BUFFERS];
nkeynes@66
    63
    int write_buffer;
nkeynes@66
    64
    int read_buffer;
nkeynes@66
    65
    uint32_t output_format;
nkeynes@66
    66
    uint32_t output_rate;
nkeynes@66
    67
    uint32_t output_sample_size;
nkeynes@465
    68
    struct audio_channel channels[AUDIO_CHANNEL_COUNT];
nkeynes@66
    69
} audio;
nkeynes@66
    70
nkeynes@66
    71
audio_driver_t audio_driver = NULL;
nkeynes@66
    72
nkeynes@66
    73
#define NEXT_BUFFER() ((audio.write_buffer == NUM_BUFFERS-1) ? 0 : audio.write_buffer+1)
nkeynes@66
    74
nkeynes@66
    75
/**
nkeynes@465
    76
 * Preserve audio channel state only - don't bother saving the buffers
nkeynes@465
    77
 */
nkeynes@465
    78
void audio_save_state( FILE *f )
nkeynes@465
    79
{
nkeynes@465
    80
    fwrite( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
nkeynes@465
    81
}
nkeynes@465
    82
nkeynes@465
    83
int audio_load_state( FILE *f )
nkeynes@465
    84
{
nkeynes@465
    85
    int read = fread( &audio.channels[0], sizeof(struct audio_channel), AUDIO_CHANNEL_COUNT, f );
nkeynes@465
    86
    return (read == AUDIO_CHANNEL_COUNT ? 0 : -1 );
nkeynes@465
    87
}
nkeynes@465
    88
nkeynes@531
    89
audio_driver_t get_audio_driver_by_name( const char *name )
nkeynes@531
    90
{
nkeynes@531
    91
    int i;
nkeynes@531
    92
    if( name == NULL ) {
nkeynes@697
    93
        return audio_driver_list[0];
nkeynes@531
    94
    }
nkeynes@531
    95
    for( i=0; audio_driver_list[i] != NULL; i++ ) {
nkeynes@697
    96
        if( strcasecmp( audio_driver_list[i]->name, name ) == 0 ) {
nkeynes@697
    97
            return audio_driver_list[i];
nkeynes@697
    98
        }
nkeynes@531
    99
    }
nkeynes@531
   100
nkeynes@531
   101
    return NULL;
nkeynes@531
   102
}
nkeynes@531
   103
nkeynes@700
   104
void print_audio_drivers( FILE * out )
nkeynes@700
   105
{
nkeynes@700
   106
    int i;
nkeynes@700
   107
    fprintf( out, "Available audio drivers:\n" );
nkeynes@700
   108
    for( i=0; audio_driver_list[i] != NULL; i++ ) {
nkeynes@700
   109
        fprintf( out, "  %-8s %s\n", audio_driver_list[i]->name,
nkeynes@736
   110
                gettext(audio_driver_list[i]->description) );
nkeynes@700
   111
    }
nkeynes@700
   112
}
nkeynes@700
   113
nkeynes@697
   114
audio_driver_t audio_init_driver( const char *preferred_driver )
nkeynes@697
   115
{
nkeynes@697
   116
    audio_driver_t audio_driver = get_audio_driver_by_name(preferred_driver);
nkeynes@697
   117
    if( audio_driver == NULL ) {
nkeynes@697
   118
        ERROR( "Audio driver '%s' not found, aborting.", preferred_driver );
nkeynes@697
   119
        exit(2);
nkeynes@697
   120
    } else if( audio_set_driver( audio_driver ) == FALSE ) {
nkeynes@779
   121
        int i;
nkeynes@779
   122
        for( i=0; audio_driver_list[i] != NULL; i++ ) {
nkeynes@779
   123
            if( audio_driver_list[i] != audio_driver &&
nkeynes@779
   124
                audio_set_driver( audio_driver_list[i] ) ) {
nkeynes@779
   125
                ERROR( "Failed to initialize audio driver %s, falling back to %s", 
nkeynes@779
   126
                       audio_driver->name, audio_driver_list[i]->name );
nkeynes@779
   127
                return audio_driver_list[i];
nkeynes@779
   128
            }
nkeynes@779
   129
        }
nkeynes@779
   130
        ERROR( "Unable to intialize any audio driver, aborting." );
nkeynes@779
   131
        exit(2);
nkeynes@759
   132
    }
nkeynes@759
   133
    return audio_driver;
nkeynes@697
   134
}
nkeynes@697
   135
nkeynes@465
   136
/**
nkeynes@66
   137
 * Set the output driver, sample rate and format. Also initializes the 
nkeynes@66
   138
 * output buffers, flushing any current data and reallocating as 
nkeynes@66
   139
 * necessary.
nkeynes@66
   140
 */
nkeynes@697
   141
gboolean audio_set_driver( audio_driver_t driver )
nkeynes@66
   142
{
nkeynes@66
   143
    uint32_t bytes_per_sample = 1;
nkeynes@66
   144
    uint32_t samples_per_buffer;
nkeynes@66
   145
    int i;
nkeynes@66
   146
nkeynes@111
   147
    if( audio_driver == NULL || driver != NULL ) {
nkeynes@697
   148
        if( driver == NULL  )
nkeynes@697
   149
            driver = &audio_null_driver;
nkeynes@697
   150
        if( driver != audio_driver ) {	
nkeynes@697
   151
            if( !driver->init() )
nkeynes@697
   152
                return FALSE;
nkeynes@697
   153
            audio_driver = driver;
nkeynes@697
   154
        }
nkeynes@111
   155
    }
nkeynes@111
   156
nkeynes@697
   157
    switch( driver->sample_format & AUDIO_FMT_SAMPLE_MASK ) {
nkeynes@697
   158
    case AUDIO_FMT_8BIT:
nkeynes@697
   159
        bytes_per_sample = 1;
nkeynes@697
   160
        break;
nkeynes@697
   161
    case AUDIO_FMT_16BIT:
nkeynes@697
   162
        bytes_per_sample = 2;
nkeynes@697
   163
        break;
nkeynes@697
   164
    case AUDIO_FMT_FLOAT:
nkeynes@697
   165
        bytes_per_sample = 4;
nkeynes@697
   166
        break;
nkeynes@697
   167
    }
nkeynes@697
   168
nkeynes@697
   169
    if( driver->sample_format & AUDIO_FMT_STEREO )
nkeynes@697
   170
        bytes_per_sample <<= 1;
nkeynes@697
   171
    if( driver->sample_rate == audio.output_rate &&
nkeynes@697
   172
            bytes_per_sample == audio.output_sample_size )
nkeynes@697
   173
        return TRUE;
nkeynes@697
   174
    samples_per_buffer = (driver->sample_rate * MS_PER_BUFFER / 1000);
nkeynes@66
   175
    for( i=0; i<NUM_BUFFERS; i++ ) {
nkeynes@697
   176
        if( audio.output_buffers[i] != NULL )
nkeynes@697
   177
            free(audio.output_buffers[i]);
nkeynes@697
   178
        audio.output_buffers[i] = g_malloc0( sizeof(struct audio_buffer) + samples_per_buffer * bytes_per_sample );
nkeynes@697
   179
        audio.output_buffers[i]->length = samples_per_buffer * bytes_per_sample;
nkeynes@697
   180
        audio.output_buffers[i]->posn = 0;
nkeynes@697
   181
        audio.output_buffers[i]->status = BUFFER_EMPTY;
nkeynes@66
   182
    }
nkeynes@697
   183
    audio.output_format = driver->sample_format;
nkeynes@697
   184
    audio.output_rate = driver->sample_rate;
nkeynes@66
   185
    audio.output_sample_size = bytes_per_sample;
nkeynes@66
   186
    audio.write_buffer = 0;
nkeynes@66
   187
    audio.read_buffer = 0;
nkeynes@66
   188
nkeynes@111
   189
    return TRUE;
nkeynes@66
   190
}
nkeynes@66
   191
nkeynes@66
   192
/**
nkeynes@66
   193
 * Mark the current write buffer as full and prepare the next buffer for
nkeynes@66
   194
 * writing. Returns the next buffer to write to.
nkeynes@66
   195
 * If all buffers are full, returns NULL.
nkeynes@66
   196
 */
nkeynes@66
   197
audio_buffer_t audio_next_write_buffer( )
nkeynes@66
   198
{
nkeynes@66
   199
    audio_buffer_t result = NULL;
nkeynes@66
   200
    audio_buffer_t current = audio.output_buffers[audio.write_buffer];
nkeynes@66
   201
    current->status = BUFFER_FULL;
nkeynes@66
   202
    if( audio.read_buffer == audio.write_buffer &&
nkeynes@697
   203
            audio_driver->process_buffer( current ) ) {
nkeynes@697
   204
        audio_next_read_buffer();
nkeynes@66
   205
    }
nkeynes@697
   206
    int next_buffer = NEXT_BUFFER();
nkeynes@697
   207
    result = audio.output_buffers[next_buffer];
nkeynes@66
   208
    if( result->status == BUFFER_FULL )
nkeynes@697
   209
        return NULL;
nkeynes@66
   210
    else {
nkeynes@697
   211
        audio.write_buffer = next_buffer;
nkeynes@697
   212
        result->status = BUFFER_WRITING;
nkeynes@697
   213
        return result;
nkeynes@66
   214
    }
nkeynes@66
   215
}
nkeynes@66
   216
nkeynes@66
   217
/**
nkeynes@66
   218
 * Mark the current read buffer as empty and return the next buffer for
nkeynes@66
   219
 * reading. If there is no next buffer yet, returns NULL.
nkeynes@66
   220
 */
nkeynes@66
   221
audio_buffer_t audio_next_read_buffer( )
nkeynes@66
   222
{
nkeynes@66
   223
    audio_buffer_t current = audio.output_buffers[audio.read_buffer];
nkeynes@697
   224
    if( current->status == BUFFER_FULL ) {
nkeynes@697
   225
        // Current read buffer has data, which we've just emptied
nkeynes@697
   226
        current->status = BUFFER_EMPTY;
nkeynes@697
   227
        current->posn = 0;
nkeynes@697
   228
        audio.read_buffer++;
nkeynes@697
   229
        if( audio.read_buffer == NUM_BUFFERS )
nkeynes@697
   230
            audio.read_buffer = 0;
nkeynes@697
   231
nkeynes@697
   232
        current = audio.output_buffers[audio.read_buffer];
nkeynes@697
   233
        if( current->status == BUFFER_FULL ) {
nkeynes@697
   234
            current->posn = 0;
nkeynes@697
   235
            return current;
nkeynes@697
   236
        }
nkeynes@697
   237
        else return NULL;
nkeynes@697
   238
    } else {
nkeynes@697
   239
        return NULL;
nkeynes@697
   240
    }
nkeynes@697
   241
nkeynes@66
   242
}
nkeynes@66
   243
nkeynes@66
   244
/*************************** ADPCM ***********************************/
nkeynes@66
   245
nkeynes@66
   246
/**
nkeynes@66
   247
 * The following section borrows heavily from ffmpeg, which is
nkeynes@66
   248
 * copyright (c) 2001-2003 by the fine folks at the ffmpeg project,
nkeynes@66
   249
 * distributed under the GPL version 2 or later.
nkeynes@66
   250
 */
nkeynes@66
   251
nkeynes@66
   252
#define CLAMP_TO_SHORT(value) \
nkeynes@736
   253
    if (value > 32767) \
nkeynes@66
   254
    value = 32767; \
nkeynes@736
   255
    else if (value < -32768) \
nkeynes@66
   256
    value = -32768; \
nkeynes@66
   257
nkeynes@66
   258
static const int yamaha_indexscale[] = {
nkeynes@736
   259
        230, 230, 230, 230, 307, 409, 512, 614,
nkeynes@736
   260
        230, 230, 230, 230, 307, 409, 512, 614
nkeynes@66
   261
};
nkeynes@66
   262
nkeynes@66
   263
static const int yamaha_difflookup[] = {
nkeynes@736
   264
        1, 3, 5, 7, 9, 11, 13, 15,
nkeynes@736
   265
        -1, -3, -5, -7, -9, -11, -13, -15
nkeynes@66
   266
};
nkeynes@66
   267
nkeynes@66
   268
static inline short adpcm_yamaha_decode_nibble( audio_channel_t c, 
nkeynes@736
   269
                                                unsigned char nibble )
nkeynes@66
   270
{
nkeynes@66
   271
    if( c->adpcm_step == 0 ) {
nkeynes@66
   272
        c->adpcm_predict = 0;
nkeynes@66
   273
        c->adpcm_step = 127;
nkeynes@66
   274
    }
nkeynes@66
   275
nkeynes@66
   276
    c->adpcm_predict += (c->adpcm_step * yamaha_difflookup[nibble]) >> 3;
nkeynes@66
   277
    CLAMP_TO_SHORT(c->adpcm_predict);
nkeynes@66
   278
    c->adpcm_step = (c->adpcm_step * yamaha_indexscale[nibble]) >> 8;
nkeynes@66
   279
    c->adpcm_step = CLAMP(c->adpcm_step, 127, 24567);
nkeynes@66
   280
    return c->adpcm_predict;
nkeynes@66
   281
}
nkeynes@66
   282
nkeynes@66
   283
/*************************** Sample mixer *****************************/
nkeynes@66
   284
nkeynes@66
   285
/**
nkeynes@66
   286
 * Mix a single output sample.
nkeynes@66
   287
 */
nkeynes@73
   288
void audio_mix_samples( int num_samples )
nkeynes@66
   289
{
nkeynes@66
   290
    int i, j;
nkeynes@73
   291
    int32_t result_buf[num_samples][2];
nkeynes@73
   292
nkeynes@73
   293
    memset( &result_buf, 0, sizeof(result_buf) );
nkeynes@66
   294
nkeynes@465
   295
    for( i=0; i < AUDIO_CHANNEL_COUNT; i++ ) {
nkeynes@697
   296
        audio_channel_t channel = &audio.channels[i];
nkeynes@697
   297
        if( channel->active ) {
nkeynes@697
   298
            int32_t sample;
nkeynes@697
   299
            int vol_left = (channel->vol * (32 - channel->pan)) >> 5;
nkeynes@697
   300
            int vol_right = (channel->vol * (channel->pan + 1)) >> 5;
nkeynes@697
   301
            switch( channel->sample_format ) {
nkeynes@697
   302
            case AUDIO_FMT_16BIT:
nkeynes@697
   303
                for( j=0; j<num_samples; j++ ) {
nkeynes@934
   304
                    sample = ((int16_t *)(aica_main_ram + channel->start))[channel->posn];
nkeynes@697
   305
                    result_buf[j][0] += sample * vol_left;
nkeynes@697
   306
                    result_buf[j][1] += sample * vol_right;
nkeynes@697
   307
nkeynes@697
   308
                    channel->posn_left += channel->sample_rate;
nkeynes@697
   309
                    while( channel->posn_left > audio.output_rate ) {
nkeynes@697
   310
                        channel->posn_left -= audio.output_rate;
nkeynes@697
   311
                        channel->posn++;
nkeynes@697
   312
nkeynes@697
   313
                        if( channel->posn == channel->end ) {
nkeynes@697
   314
                            if( channel->loop ) {
nkeynes@697
   315
                                channel->posn = channel->loop_start;
nkeynes@697
   316
                                channel->loop = LOOP_LOOPED;
nkeynes@697
   317
                            } else {
nkeynes@697
   318
                                audio_stop_channel(i);
nkeynes@697
   319
                                j = num_samples;
nkeynes@697
   320
                                break;
nkeynes@697
   321
                            }
nkeynes@697
   322
                        }
nkeynes@697
   323
                    }
nkeynes@697
   324
                }
nkeynes@697
   325
                break;
nkeynes@697
   326
            case AUDIO_FMT_8BIT:
nkeynes@697
   327
                for( j=0; j<num_samples; j++ ) {
nkeynes@934
   328
                    sample = ((int8_t *)(aica_main_ram + channel->start))[channel->posn] << 8;
nkeynes@697
   329
                    result_buf[j][0] += sample * vol_left;
nkeynes@697
   330
                    result_buf[j][1] += sample * vol_right;
nkeynes@697
   331
nkeynes@697
   332
                    channel->posn_left += channel->sample_rate;
nkeynes@697
   333
                    while( channel->posn_left > audio.output_rate ) {
nkeynes@697
   334
                        channel->posn_left -= audio.output_rate;
nkeynes@697
   335
                        channel->posn++;
nkeynes@697
   336
nkeynes@697
   337
                        if( channel->posn == channel->end ) {
nkeynes@697
   338
                            if( channel->loop ) {
nkeynes@697
   339
                                channel->posn = channel->loop_start;
nkeynes@697
   340
                                channel->loop = LOOP_LOOPED;
nkeynes@697
   341
                            } else {
nkeynes@697
   342
                                audio_stop_channel(i);
nkeynes@697
   343
                                j = num_samples;
nkeynes@697
   344
                                break;
nkeynes@697
   345
                            }
nkeynes@697
   346
                        }
nkeynes@697
   347
                    }
nkeynes@697
   348
                }
nkeynes@697
   349
                break;
nkeynes@697
   350
            case AUDIO_FMT_ADPCM:
nkeynes@697
   351
                for( j=0; j<num_samples; j++ ) {
nkeynes@697
   352
                    sample = (int16_t)channel->adpcm_predict;
nkeynes@697
   353
                    result_buf[j][0] += sample * vol_left;
nkeynes@697
   354
                    result_buf[j][1] += sample * vol_right;
nkeynes@697
   355
                    channel->posn_left += channel->sample_rate;
nkeynes@697
   356
                    while( channel->posn_left > audio.output_rate ) {
nkeynes@697
   357
                        channel->posn_left -= audio.output_rate;
nkeynes@697
   358
                        channel->posn++;
nkeynes@697
   359
                        if( channel->posn == channel->end ) {
nkeynes@697
   360
                            if( channel->loop ) {
nkeynes@697
   361
                                channel->posn = channel->loop_start;
nkeynes@697
   362
                                channel->loop = LOOP_LOOPED;
nkeynes@697
   363
                                channel->adpcm_predict = 0;
nkeynes@697
   364
                                channel->adpcm_step = 0;
nkeynes@697
   365
                            } else {
nkeynes@697
   366
                                audio_stop_channel(i);
nkeynes@697
   367
                                j = num_samples;
nkeynes@697
   368
                                break;
nkeynes@697
   369
                            }
nkeynes@697
   370
                        }
nkeynes@934
   371
                        uint8_t data = ((uint8_t *)(aica_main_ram + channel->start))[channel->posn>>1];
nkeynes@697
   372
                        if( channel->posn&1 ) {
nkeynes@697
   373
                            adpcm_yamaha_decode_nibble( channel, (data >> 4) & 0x0F );
nkeynes@697
   374
                        } else {
nkeynes@697
   375
                            adpcm_yamaha_decode_nibble( channel, data & 0x0F );
nkeynes@697
   376
                        }
nkeynes@697
   377
                    }
nkeynes@697
   378
                }
nkeynes@697
   379
                break;
nkeynes@697
   380
            default:
nkeynes@697
   381
                break;
nkeynes@697
   382
            }
nkeynes@697
   383
        }
nkeynes@66
   384
    }
nkeynes@736
   385
nkeynes@66
   386
    /* Down-render to the final output format */
nkeynes@697
   387
    audio_buffer_t buf = audio.output_buffers[audio.write_buffer];
nkeynes@697
   388
    if( buf->status == BUFFER_FULL ) {
nkeynes@697
   389
        buf = audio_next_write_buffer();
nkeynes@697
   390
        if( buf == NULL ) { // no available space
nkeynes@697
   391
            return;
nkeynes@697
   392
        }
nkeynes@697
   393
    }
nkeynes@736
   394
nkeynes@697
   395
    switch( audio.output_format & AUDIO_FMT_SAMPLE_MASK ) {
nkeynes@697
   396
    case AUDIO_FMT_FLOAT: {
nkeynes@697
   397
        float scale = 1.0/SHRT_MAX;
nkeynes@697
   398
        float *data = (float *)&buf->data[buf->posn];
nkeynes@697
   399
        for( j=0; j<num_samples; j++ ) {
nkeynes@697
   400
            *data++ = scale * (result_buf[j][0] >> 6);
nkeynes@697
   401
            *data++ = scale * (result_buf[j][1] >> 6);
nkeynes@697
   402
            buf->posn += 8;
nkeynes@697
   403
            if( buf->posn == buf->length ) {
nkeynes@697
   404
                buf = audio_next_write_buffer();
nkeynes@697
   405
                if( buf == NULL ) {
nkeynes@697
   406
                    break;
nkeynes@697
   407
                }
nkeynes@697
   408
                data = (float *)&buf->data[0];
nkeynes@697
   409
            }
nkeynes@697
   410
        }
nkeynes@697
   411
        break;
nkeynes@697
   412
    }
nkeynes@697
   413
    case AUDIO_FMT_16BIT: {
nkeynes@697
   414
        int16_t *data = (int16_t *)&buf->data[buf->posn];
nkeynes@697
   415
        for( j=0; j < num_samples; j++ ) {
nkeynes@697
   416
            *data++ = (int16_t)(result_buf[j][0] >> 6);
nkeynes@697
   417
            *data++ = (int16_t)(result_buf[j][1] >> 6);	
nkeynes@697
   418
            buf->posn += 4;
nkeynes@697
   419
            if( buf->posn == buf->length ) {
nkeynes@697
   420
                buf = audio_next_write_buffer();
nkeynes@697
   421
                if( buf == NULL ) {
nkeynes@697
   422
                    // All buffers are full
nkeynes@697
   423
                    break;
nkeynes@697
   424
                }
nkeynes@697
   425
                data = (int16_t *)&buf->data[0];
nkeynes@697
   426
            }
nkeynes@697
   427
        }
nkeynes@697
   428
        break;
nkeynes@697
   429
    }
nkeynes@697
   430
    case AUDIO_FMT_8BIT: {
nkeynes@700
   431
        int8_t *data = (int8_t *)&buf->data[buf->posn];
nkeynes@697
   432
        for( j=0; j < num_samples; j++ ) {
nkeynes@697
   433
            *data++ = (int8_t)(result_buf[j][0] >> 16);
nkeynes@697
   434
            *data++ = (int8_t)(result_buf[j][1] >> 16);	
nkeynes@697
   435
            buf->posn += 2;
nkeynes@697
   436
            if( buf->posn == buf->length ) {
nkeynes@697
   437
                buf = audio_next_write_buffer();
nkeynes@697
   438
                if( buf == NULL ) {
nkeynes@697
   439
                    // All buffers are full
nkeynes@697
   440
                    break;
nkeynes@697
   441
                }
nkeynes@697
   442
                buf = audio.output_buffers[audio.write_buffer];
nkeynes@700
   443
                data = (int8_t *)&buf->data[0];
nkeynes@697
   444
            }
nkeynes@697
   445
        }
nkeynes@697
   446
        break;
nkeynes@697
   447
    }
nkeynes@66
   448
    }
nkeynes@66
   449
}
nkeynes@66
   450
nkeynes@66
   451
/********************** Internal AICA calls ***************************/
nkeynes@66
   452
nkeynes@66
   453
audio_channel_t audio_get_channel( int channel ) 
nkeynes@66
   454
{
nkeynes@66
   455
    return &audio.channels[channel];
nkeynes@66
   456
}
nkeynes@66
   457
nkeynes@434
   458
void audio_start_stop_channel( int channel, gboolean start )
nkeynes@434
   459
{
nkeynes@434
   460
    if( audio.channels[channel].active ) {
nkeynes@736
   461
        if( !start ) {
nkeynes@736
   462
            audio_stop_channel(channel);
nkeynes@736
   463
        }
nkeynes@434
   464
    } else if( start ) {
nkeynes@736
   465
        audio_start_channel(channel);
nkeynes@434
   466
    }
nkeynes@434
   467
}
nkeynes@434
   468
nkeynes@66
   469
void audio_stop_channel( int channel ) 
nkeynes@66
   470
{
nkeynes@66
   471
    audio.channels[channel].active = FALSE;
nkeynes@66
   472
}
nkeynes@66
   473
nkeynes@66
   474
nkeynes@66
   475
void audio_start_channel( int channel )
nkeynes@66
   476
{
nkeynes@66
   477
    audio.channels[channel].posn = 0;
nkeynes@66
   478
    audio.channels[channel].posn_left = 0;
nkeynes@66
   479
    audio.channels[channel].active = TRUE;
nkeynes@434
   480
    if( audio.channels[channel].sample_format == AUDIO_FMT_ADPCM ) {
nkeynes@736
   481
        audio.channels[channel].adpcm_step = 0;
nkeynes@736
   482
        audio.channels[channel].adpcm_predict = 0;
nkeynes@934
   483
        uint8_t data = ((uint8_t *)(aica_main_ram + audio.channels[channel].start))[0];
nkeynes@736
   484
        adpcm_yamaha_decode_nibble( &audio.channels[channel], data & 0x0F );
nkeynes@434
   485
    }
nkeynes@66
   486
}
.