src/audio/mint/SDL_mintaudio_stfa.c
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 2942 1e431c2631ee
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
patmandin@644
     1
/*
patmandin@644
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
patmandin@644
     4
patmandin@644
     5
    This library is free software; you can redistribute it and/or
patmandin@644
     6
    modify it under the terms of the GNU Library General Public
patmandin@644
     7
    License as published by the Free Software Foundation; either
patmandin@644
     8
    version 2 of the License, or (at your option) any later version.
patmandin@644
     9
patmandin@644
    10
    This library is distributed in the hope that it will be useful,
patmandin@644
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
patmandin@644
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
patmandin@644
    13
    Library General Public License for more details.
patmandin@644
    14
patmandin@644
    15
    You should have received a copy of the GNU Library General Public
patmandin@644
    16
    License along with this library; if not, write to the Free
patmandin@644
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
patmandin@644
    18
patmandin@644
    19
    Sam Lantinga
patmandin@644
    20
    slouken@libsdl.org
patmandin@644
    21
*/
slouken@1402
    22
#include "SDL_config.h"
patmandin@644
    23
patmandin@644
    24
/*
patmandin@644
    25
	MiNT audio driver
patmandin@644
    26
	using XBIOS functions (STFA driver)
patmandin@644
    27
patmandin@644
    28
	Patrice Mandin
patmandin@644
    29
*/
patmandin@644
    30
patmandin@644
    31
/* Mint includes */
patmandin@644
    32
#include <mint/osbind.h>
patmandin@644
    33
#include <mint/falcon.h>
patmandin@644
    34
#include <mint/cookie.h>
patmandin@644
    35
patmandin@644
    36
#include "SDL_audio.h"
slouken@1361
    37
#include "../SDL_audio_c.h"
slouken@1361
    38
#include "../SDL_sysaudio.h"
patmandin@644
    39
patmandin@1412
    40
#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
patmandin@644
    41
patmandin@644
    42
#include "SDL_mintaudio.h"
patmandin@644
    43
#include "SDL_mintaudio_stfa.h"
patmandin@644
    44
patmandin@644
    45
/*--- Defines ---*/
patmandin@644
    46
patmandin@644
    47
#define MINT_AUDIO_DRIVER_NAME "mint_stfa"
patmandin@644
    48
patmandin@644
    49
/* Debug print info */
patmandin@644
    50
#define DEBUG_NAME "audio:stfa: "
patmandin@962
    51
#if 0
patmandin@644
    52
#define DEBUG_PRINT(what) \
patmandin@644
    53
	{ \
patmandin@644
    54
		printf what; \
patmandin@644
    55
	}
patmandin@644
    56
#else
patmandin@644
    57
#define DEBUG_PRINT(what)
patmandin@644
    58
#endif
patmandin@644
    59
patmandin@644
    60
/*--- Static variables ---*/
patmandin@644
    61
icculus@2049
    62
static unsigned long cookie_snd = 0;
icculus@2049
    63
static unsigned long cookie_mch = 0;
icculus@2049
    64
static cookie_stfa_t *cookie_stfa = NULL;
patmandin@644
    65
slouken@1895
    66
static const int freqs[16] = {
slouken@1895
    67
    4995, 6269, 7493, 8192,
slouken@1895
    68
    9830, 10971, 12538, 14985,
slouken@1895
    69
    16384, 19819, 21943, 24576,
slouken@1895
    70
    30720, 32336, 43885, 49152
patmandin@644
    71
};
patmandin@644
    72
icculus@2049
    73
static void
icculus@2049
    74
MINTSTFA_LockDevice(_THIS)
patmandin@644
    75
{
slouken@1895
    76
    /* Stop replay */
icculus@2049
    77
    void *oldpile = (void *) Super(0);
slouken@1895
    78
    cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
slouken@1895
    79
    Super(oldpile);
patmandin@644
    80
}
patmandin@644
    81
slouken@1895
    82
static void
icculus@2049
    83
MINTSTFA_UnlockDevice(_THIS)
patmandin@644
    84
{
slouken@1895
    85
    /* Restart replay */
icculus@2049
    86
    void *oldpile = (void *) Super(0);
slouken@1895
    87
    cookie_stfa->sound_enable = STFA_PLAY_ENABLE | STFA_PLAY_REPEAT;
slouken@1895
    88
    Super(oldpile);
patmandin@644
    89
}
patmandin@644
    90
slouken@1895
    91
static void
icculus@2049
    92
MINTSTFA_CloseDevice(_THIS)
patmandin@644
    93
{
icculus@2049
    94
    if (this->hidden != NULL) {
icculus@2049
    95
        /* Stop replay */
icculus@2049
    96
        void *oldpile = (void *) Super(0);
icculus@2049
    97
        cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
icculus@2049
    98
        Super(oldpile);
patmandin@644
    99
icculus@2049
   100
        /* Wait if currently playing sound */
slouken@2060
   101
        while (SDL_MintAudio_mutex != 0) {
slouken@2060
   102
        }
patmandin@644
   103
icculus@2049
   104
        /* Clear buffers */
icculus@2049
   105
        if (SDL_MintAudio_audiobuf[0]) {
icculus@2049
   106
            Mfree(SDL_MintAudio_audiobuf[0]);
icculus@2049
   107
            SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
icculus@2049
   108
        }
icculus@2049
   109
icculus@2049
   110
        SDL_free(this->hidden);
icculus@2049
   111
        this->hidden = NULL;
slouken@1895
   112
    }
patmandin@644
   113
}
patmandin@644
   114
slouken@1895
   115
static int
icculus@2049
   116
MINTSTFA_CheckAudio(_THIS)
patmandin@644
   117
{
slouken@1895
   118
    int i;
patmandin@644
   119
slouken@2043
   120
    DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",
icculus@2049
   121
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   122
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   123
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   124
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   125
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   126
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   127
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   128
icculus@2049
   129
    if (SDL_AUDIO_BITSIZE(this->spec.format) > 16) {
slouken@2060
   130
        this->spec.format = AUDIO_S16SYS;       /* clamp out int32/float32 ... */
icculus@2005
   131
    }
icculus@2005
   132
icculus@2049
   133
    if (this->spec.channels > 2) {
slouken@2060
   134
        this->spec.channels = 2;        /* no more than stereo! */
icculus@2005
   135
    }
icculus@2005
   136
slouken@1895
   137
    /* Check formats available */
slouken@1895
   138
    MINTAUDIO_freqcount = 0;
slouken@1895
   139
    for (i = 0; i < 16; i++) {
slouken@1895
   140
        SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1);
slouken@1895
   141
    }
patmandin@644
   142
patmandin@961
   143
#if 1
slouken@1895
   144
    for (i = 0; i < MINTAUDIO_freqcount; i++) {
slouken@1895
   145
        DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
slouken@1895
   146
                     i, MINTAUDIO_frequencies[i].frequency,
slouken@1895
   147
                     MINTAUDIO_frequencies[i].masterclock,
slouken@1895
   148
                     MINTAUDIO_frequencies[i].predivisor));
slouken@1895
   149
    }
patmandin@961
   150
#endif
patmandin@961
   151
icculus@2049
   152
    MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, this->spec.freq);
icculus@2049
   153
    this->spec.freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
patmandin@644
   154
slouken@2043
   155
    DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",
icculus@2049
   156
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   157
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   158
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   159
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   160
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   161
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   162
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   163
slouken@1895
   164
    return 0;
patmandin@644
   165
}
patmandin@644
   166
slouken@1895
   167
static void
icculus@2049
   168
MINTSTFA_InitAudio(_THIS)
patmandin@644
   169
{
icculus@2049
   170
    void *buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
icculus@2049
   171
    void *oldpile = (void *) Super(0);
patmandin@644
   172
slouken@1895
   173
    /* Stop replay */
slouken@1895
   174
    cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
patmandin@644
   175
slouken@1895
   176
    /* Select replay format */
slouken@1895
   177
    cookie_stfa->sound_control =
slouken@1895
   178
        MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
icculus@2049
   179
    if (SDL_AUDIO_BITSIZE(this->spec.format) == 8) {
slouken@1895
   180
        cookie_stfa->sound_control |= STFA_FORMAT_8BIT;
slouken@1895
   181
    } else {
slouken@1895
   182
        cookie_stfa->sound_control |= STFA_FORMAT_16BIT;
slouken@1895
   183
    }
icculus@2049
   184
    if (this->spec.channels == 2) {
slouken@1895
   185
        cookie_stfa->sound_control |= STFA_FORMAT_STEREO;
slouken@1895
   186
    } else {
slouken@1895
   187
        cookie_stfa->sound_control |= STFA_FORMAT_MONO;
slouken@1895
   188
    }
icculus@2049
   189
    if (SDL_AUDIO_ISSIGNED(this->spec.format) != 0) {
slouken@1895
   190
        cookie_stfa->sound_control |= STFA_FORMAT_SIGNED;
slouken@1895
   191
    } else {
slouken@1895
   192
        cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED;
slouken@1895
   193
    }
icculus@2049
   194
    if (SDL_AUDIO_ISBIGENDIAN(this->spec.format) != 0) {
slouken@1895
   195
        cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN;
slouken@1895
   196
    } else {
slouken@1895
   197
        cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN;
slouken@1895
   198
    }
patmandin@644
   199
slouken@1895
   200
    /* Set buffer */
slouken@1895
   201
    cookie_stfa->sound_start = (unsigned long) buffer;
icculus@2049
   202
    cookie_stfa->sound_end = (unsigned long) (buffer + this->spec.size);
patmandin@644
   203
slouken@1895
   204
    /* Set interrupt */
slouken@1895
   205
    cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;
patmandin@644
   206
slouken@1895
   207
    /* Restart replay */
slouken@1895
   208
    cookie_stfa->sound_enable = STFA_PLAY_ENABLE | STFA_PLAY_REPEAT;
patmandin@644
   209
slouken@1895
   210
    Super(oldpile);
patmandin@644
   211
slouken@1895
   212
    DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
patmandin@644
   213
}
patmandin@644
   214
slouken@1895
   215
static int
icculus@2049
   216
MINTSTFA_OpenDevice(_THIS, const char *devname, int iscapture)
patmandin@644
   217
{
slouken@1895
   218
    SDL_MintAudio_device = this;
patmandin@644
   219
slouken@1895
   220
    /* Check audio capabilities */
icculus@2049
   221
    if (MINTSTFA_CheckAudio(this) == -1) {
icculus@2049
   222
        return 0;
slouken@1895
   223
    }
patmandin@644
   224
icculus@2049
   225
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   226
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   227
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   228
    if (this->hidden == NULL) {
icculus@2049
   229
        SDL_OutOfMemory();
icculus@2049
   230
        return 0;
icculus@2049
   231
    }
icculus@2049
   232
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   233
icculus@2049
   234
    SDL_CalculateAudioSpec(&this->spec);
patmandin@644
   235
slouken@1895
   236
    /* Allocate memory for audio buffers in DMA-able RAM */
icculus@2049
   237
    DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", this->spec.size));
patmandin@644
   238
slouken@2060
   239
    SDL_MintAudio_audiobuf[0] =
slouken@2060
   240
        Atari_SysMalloc(this->spec.size * 2, MX_STRAM);
slouken@1895
   241
    if (SDL_MintAudio_audiobuf[0] == NULL) {
patmandin@2191
   242
        SDL_OutOfMemory();
patmandin@2191
   243
        SDL_free(this->hidden);
icculus@2049
   244
        this->hidden = NULL;
icculus@2049
   245
        return 0;
slouken@1895
   246
    }
icculus@2049
   247
    SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + this->spec.size;
slouken@1895
   248
    SDL_MintAudio_numbuf = 0;
slouken@2060
   249
    SDL_memset(SDL_MintAudio_audiobuf[0], this->spec.silence,
slouken@2060
   250
               this->spec.size * 2);
icculus@2049
   251
    SDL_MintAudio_audiosize = this->spec.size;
slouken@1895
   252
    SDL_MintAudio_mutex = 0;
patmandin@644
   253
slouken@1895
   254
    DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n",
slouken@1895
   255
                 SDL_MintAudio_audiobuf[0]));
slouken@1895
   256
    DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n",
slouken@1895
   257
                 SDL_MintAudio_audiobuf[1]));
patmandin@644
   258
patmandin@2027
   259
    SDL_MintAudio_CheckFpu();
patmandin@2027
   260
slouken@1895
   261
    /* Setup audio hardware */
icculus@2049
   262
    MINTSTFA_InitAudio(this);
patmandin@644
   263
slouken@2060
   264
    return 1;                   /* good to go. */
patmandin@644
   265
}
slouken@1895
   266
icculus@2049
   267
icculus@2049
   268
static int
slouken@2060
   269
MINTSTFA_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   270
{
icculus@2049
   271
    /* Cookie _MCH present ? if not, assume ST machine */
icculus@2049
   272
    if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
icculus@2049
   273
        cookie_mch = MCH_ST;
icculus@2049
   274
    }
icculus@2049
   275
icculus@2049
   276
    /* Cookie _SND present ? if not, assume ST machine */
icculus@2049
   277
    if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
icculus@2049
   278
        cookie_snd = SND_PSG;
icculus@2049
   279
    }
icculus@2049
   280
icculus@2049
   281
    /* Cookie STFA present ? */
icculus@2049
   282
    if (Getcookie(C_STFA, (long *) &cookie_stfa) != C_FOUND) {
icculus@2049
   283
        SDL_SetError(DEBUG_NAME "no STFA audio");
icculus@2049
   284
        return (0);
icculus@2049
   285
    }
icculus@2049
   286
icculus@2049
   287
    SDL_MintAudio_stfa = cookie_stfa;
icculus@2049
   288
icculus@2049
   289
    DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n"));
icculus@2049
   290
icculus@2049
   291
    /* Set the function pointers */
icculus@2049
   292
    impl->OpenDevice = MINTSTFA_OpenDevice;
icculus@2049
   293
    impl->CloseDevice = MINTSTFA_CloseDevice;
patmandin@2191
   294
    impl->LockDevice = MINTSTFA_LockDevice;
patmandin@2191
   295
    impl->UnlockDevice = MINTSTFA_UnlockDevice;
icculus@2049
   296
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   297
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   298
    impl->SkipMixerLock = 1;
icculus@2049
   299
slouken@2942
   300
    return 2;                   /* 2 == definitely has an audio device. */
icculus@2049
   301
}
icculus@2049
   302
icculus@2049
   303
AudioBootStrap MINTAUDIO_STFA_bootstrap = {
icculus@2049
   304
    MINT_AUDIO_DRIVER_NAME, "MiNT STFA audio driver", MINTSTFA_Init, 0
icculus@2049
   305
};
icculus@2049
   306
slouken@1895
   307
/* vi: set ts=4 sw=4 expandtab: */