src/audio/mint/SDL_mintaudio_gsxb.c
author Patrice Mandin
Fri, 29 Oct 2004 11:19:03 +0000
changeset 962 176240cf4405
parent 961 185acc07127a
child 1097 c83fa1c650dd
permissions -rw-r--r--
Forgot to disable debug messages
patmandin@644
     1
/*
patmandin@644
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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
*/
patmandin@644
    22
patmandin@644
    23
/*
patmandin@644
    24
	MiNT audio driver
patmandin@644
    25
	using XBIOS functions (GSXB compatible driver)
patmandin@644
    26
patmandin@644
    27
	Patrice Mandin
patmandin@644
    28
*/
patmandin@644
    29
patmandin@644
    30
#include <stdlib.h>
patmandin@644
    31
#include <stdio.h>
patmandin@644
    32
#include <string.h>
patmandin@644
    33
patmandin@644
    34
/* Mint includes */
patmandin@644
    35
#include <mint/osbind.h>
patmandin@644
    36
#include <mint/falcon.h>
patmandin@644
    37
#include <mint/cookie.h>
patmandin@644
    38
patmandin@644
    39
#include "SDL_endian.h"
patmandin@644
    40
#include "SDL_audio.h"
patmandin@644
    41
#include "SDL_audio_c.h"
patmandin@644
    42
#include "SDL_audiomem.h"
patmandin@644
    43
#include "SDL_sysaudio.h"
patmandin@644
    44
patmandin@644
    45
#include "SDL_atarimxalloc_c.h"
patmandin@644
    46
patmandin@644
    47
#include "SDL_mintaudio.h"
patmandin@644
    48
#include "SDL_mintaudio_gsxb.h"
patmandin@644
    49
patmandin@644
    50
/*--- Defines ---*/
patmandin@644
    51
patmandin@644
    52
#define MINT_AUDIO_DRIVER_NAME "mint_gsxb"
patmandin@644
    53
patmandin@644
    54
/* Debug print info */
patmandin@644
    55
#define DEBUG_NAME "audio:gsxb: "
patmandin@962
    56
#if 0
patmandin@644
    57
#define DEBUG_PRINT(what) \
patmandin@644
    58
	{ \
patmandin@644
    59
		printf what; \
patmandin@644
    60
	}
patmandin@644
    61
#else
patmandin@644
    62
#define DEBUG_PRINT(what)
patmandin@644
    63
#endif
patmandin@644
    64
patmandin@644
    65
/*--- Static variables ---*/
patmandin@644
    66
patmandin@644
    67
static unsigned long cookie_snd, cookie_gsxb;
patmandin@644
    68
patmandin@644
    69
/*--- Audio driver functions ---*/
patmandin@644
    70
patmandin@644
    71
static void Mint_CloseAudio(_THIS);
patmandin@644
    72
static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
patmandin@644
    73
static void Mint_LockAudio(_THIS);
patmandin@644
    74
static void Mint_UnlockAudio(_THIS);
patmandin@644
    75
patmandin@644
    76
/* To check/init hardware audio */
patmandin@644
    77
static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
patmandin@644
    78
static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
patmandin@644
    79
patmandin@644
    80
/*--- Audio driver bootstrap functions ---*/
patmandin@644
    81
patmandin@644
    82
static int Audio_Available(void)
patmandin@644
    83
{
patmandin@644
    84
	const char *envr = getenv("SDL_AUDIODRIVER");
patmandin@644
    85
patmandin@644
    86
	/* Check if user asked a different audio driver */
patmandin@644
    87
	if ((envr) && (strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
patmandin@644
    88
		DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
patmandin@644
    89
		return(0);
patmandin@644
    90
	}
patmandin@644
    91
patmandin@644
    92
	/* Cookie _SND present ? if not, assume ST machine */
patmandin@644
    93
	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
patmandin@644
    94
		cookie_snd = SND_PSG;
patmandin@644
    95
	}
patmandin@644
    96
patmandin@644
    97
	/* Check if we have 16 bits audio */
patmandin@644
    98
	if ((cookie_snd & SND_16BIT)==0) {
patmandin@644
    99
		DEBUG_PRINT((DEBUG_NAME "no 16 bits sound\n"));
patmandin@644
   100
	    return(0);
patmandin@644
   101
	}
patmandin@644
   102
patmandin@644
   103
	/* Cookie GSXB present ? */
patmandin@644
   104
	cookie_gsxb = (Getcookie(C_GSXB, &cookie_gsxb) == C_FOUND);
patmandin@644
   105
patmandin@644
   106
	/* Is it GSXB ? */
patmandin@644
   107
	if (((cookie_snd & SND_GSXB)==0) || (cookie_gsxb==0)) {
patmandin@644
   108
		DEBUG_PRINT((DEBUG_NAME "no GSXB audio\n"));
patmandin@644
   109
		return(0);
patmandin@644
   110
	}
patmandin@644
   111
patmandin@644
   112
	/* Check if audio is lockable */
patmandin@644
   113
	if (Locksnd()!=1) {
patmandin@644
   114
		DEBUG_PRINT((DEBUG_NAME "audio locked by other application\n"));
patmandin@644
   115
		return(0);
patmandin@644
   116
	}
patmandin@644
   117
patmandin@644
   118
	Unlocksnd();
patmandin@644
   119
patmandin@644
   120
	DEBUG_PRINT((DEBUG_NAME "GSXB audio available!\n"));
patmandin@644
   121
	return(1);
patmandin@644
   122
}
patmandin@644
   123
patmandin@644
   124
static void Audio_DeleteDevice(SDL_AudioDevice *device)
patmandin@644
   125
{
patmandin@644
   126
    free(device->hidden);
patmandin@644
   127
    free(device);
patmandin@644
   128
}
patmandin@644
   129
patmandin@644
   130
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
patmandin@644
   131
{
patmandin@644
   132
	SDL_AudioDevice *this;
patmandin@644
   133
patmandin@644
   134
	/* Initialize all variables that we clean on shutdown */
patmandin@644
   135
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
patmandin@644
   136
    if ( this ) {
patmandin@644
   137
        memset(this, 0, (sizeof *this));
patmandin@644
   138
        this->hidden = (struct SDL_PrivateAudioData *)
patmandin@644
   139
                malloc((sizeof *this->hidden));
patmandin@644
   140
    }
patmandin@644
   141
    if ( (this == NULL) || (this->hidden == NULL) ) {
patmandin@644
   142
        SDL_OutOfMemory();
patmandin@644
   143
        if ( this ) {
patmandin@644
   144
            free(this);
patmandin@644
   145
        }
patmandin@644
   146
        return(0);
patmandin@644
   147
    }
patmandin@644
   148
    memset(this->hidden, 0, (sizeof *this->hidden));
patmandin@644
   149
patmandin@644
   150
    /* Set the function pointers */
patmandin@644
   151
    this->OpenAudio   = Mint_OpenAudio;
patmandin@644
   152
    this->CloseAudio  = Mint_CloseAudio;
patmandin@644
   153
    this->LockAudio   = Mint_LockAudio;
patmandin@644
   154
    this->UnlockAudio = Mint_UnlockAudio;
patmandin@644
   155
    this->free        = Audio_DeleteDevice;
patmandin@644
   156
patmandin@644
   157
    return this;
patmandin@644
   158
}
patmandin@644
   159
patmandin@644
   160
AudioBootStrap MINTAUDIO_GSXB_bootstrap = {
patmandin@644
   161
	MINT_AUDIO_DRIVER_NAME, "MiNT GSXB audio driver",
patmandin@644
   162
	Audio_Available, Audio_CreateDevice
patmandin@644
   163
};
patmandin@644
   164
patmandin@644
   165
static void Mint_LockAudio(_THIS)
patmandin@644
   166
{
patmandin@644
   167
	/* Stop replay */
patmandin@644
   168
	Buffoper(0);
patmandin@644
   169
}
patmandin@644
   170
patmandin@644
   171
static void Mint_UnlockAudio(_THIS)
patmandin@644
   172
{
patmandin@644
   173
	/* Restart replay */
patmandin@644
   174
	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
patmandin@644
   175
}
patmandin@644
   176
patmandin@644
   177
static void Mint_CloseAudio(_THIS)
patmandin@644
   178
{
patmandin@644
   179
	/* Stop replay */
patmandin@644
   180
	Buffoper(0);
patmandin@644
   181
patmandin@644
   182
	/* Uninstall interrupt */
patmandin@644
   183
	if (NSetinterrupt(2, SI_NONE, SDL_MintAudio_EmptyGsxbInterrupt)<0) {
patmandin@644
   184
		DEBUG_PRINT((DEBUG_NAME "NSetinterrupt() failed in close\n"));
patmandin@644
   185
	}
patmandin@644
   186
patmandin@644
   187
	/* Wait if currently playing sound */
patmandin@644
   188
	while (SDL_MintAudio_mutex != 0) {
patmandin@644
   189
	}
patmandin@644
   190
patmandin@644
   191
	/* Clear buffers */
patmandin@644
   192
	if (SDL_MintAudio_audiobuf[0]) {
patmandin@644
   193
		Mfree(SDL_MintAudio_audiobuf[0]);
patmandin@644
   194
		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
patmandin@644
   195
	}
patmandin@644
   196
patmandin@644
   197
	/* Unlock sound system */
patmandin@644
   198
	Unlocksnd();
patmandin@644
   199
}
patmandin@644
   200
patmandin@644
   201
static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
patmandin@644
   202
{
patmandin@644
   203
	long snd_format;
patmandin@644
   204
	int i, resolution, format_signed, format_bigendian;
patmandin@644
   205
patmandin@644
   206
	resolution = spec->format & 0x00ff;
patmandin@644
   207
	format_signed = ((spec->format & 0x8000)!=0);
patmandin@644
   208
	format_bigendian = ((spec->format & 0x1000)!=0);
patmandin@704
   209
patmandin@704
   210
	DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
patmandin@704
   211
	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
patmandin@704
   212
	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
patmandin@644
   213
	DEBUG_PRINT(("channels=%d, ", spec->channels));
patmandin@644
   214
	DEBUG_PRINT(("freq=%d\n", spec->freq));
patmandin@644
   215
patmandin@644
   216
	/* Check formats available */
patmandin@644
   217
	snd_format = Sndstatus(SND_QUERYFORMATS);
patmandin@644
   218
	switch (resolution) {
patmandin@644
   219
		case 8:
patmandin@644
   220
			if ((snd_format & SND_FORMAT8)==0) {
patmandin@644
   221
				SDL_SetError("Mint_CheckAudio: 8 bits samples not supported");
patmandin@644
   222
				return -1;
patmandin@644
   223
			}
patmandin@644
   224
			snd_format = Sndstatus(SND_QUERY8BIT);
patmandin@644
   225
			break;
patmandin@644
   226
		case 16:
patmandin@644
   227
			if ((snd_format & SND_FORMAT16)==0) {
patmandin@644
   228
				SDL_SetError("Mint_CheckAudio: 16 bits samples not supported");
patmandin@644
   229
				return -1;
patmandin@644
   230
			}
patmandin@644
   231
			snd_format = Sndstatus(SND_QUERY16BIT);
patmandin@644
   232
			break;
patmandin@644
   233
		default:
patmandin@644
   234
			SDL_SetError("Mint_CheckAudio: Unsupported sample resolution");
patmandin@644
   235
			return -1;
patmandin@644
   236
			break;
patmandin@644
   237
	}
patmandin@644
   238
patmandin@644
   239
	/* Check signed/unsigned format */
patmandin@644
   240
	if (format_signed) {
patmandin@644
   241
		if (snd_format & SND_FORMATSIGNED) {
patmandin@644
   242
			/* Ok */
patmandin@644
   243
		} else if (snd_format & SND_FORMATUNSIGNED) {
patmandin@644
   244
			/* Give unsigned format */
patmandin@644
   245
			spec->format = spec->format & (~0x8000);
patmandin@644
   246
		}
patmandin@644
   247
	} else {
patmandin@644
   248
		if (snd_format & SND_FORMATUNSIGNED) {
patmandin@644
   249
			/* Ok */
patmandin@644
   250
		} else if (snd_format & SND_FORMATSIGNED) {
patmandin@644
   251
			/* Give signed format */
patmandin@644
   252
			spec->format |= 0x8000;
patmandin@644
   253
		}
patmandin@644
   254
	}
patmandin@644
   255
patmandin@644
   256
	if (format_bigendian) {
patmandin@644
   257
		if (snd_format & SND_FORMATBIGENDIAN) {
patmandin@644
   258
			/* Ok */
patmandin@644
   259
		} else if (snd_format & SND_FORMATLITTLEENDIAN) {
patmandin@644
   260
			/* Give little endian format */
patmandin@644
   261
			spec->format = spec->format & (~0x1000);
patmandin@644
   262
		}
patmandin@644
   263
	} else {
patmandin@704
   264
		if (snd_format & SND_FORMATLITTLEENDIAN) {
patmandin@644
   265
			/* Ok */
patmandin@704
   266
		} else if (snd_format & SND_FORMATBIGENDIAN) {
patmandin@644
   267
			/* Give big endian format */
patmandin@644
   268
			spec->format |= 0x1000;
patmandin@644
   269
		}
patmandin@644
   270
	}
patmandin@644
   271
	
patmandin@644
   272
	/* Calculate and select the closest frequency */
patmandin@961
   273
	MINTAUDIO_freqcount=0;
patmandin@961
   274
	for (i=1;i<4;i++) {
patmandin@961
   275
		SDL_MintAudio_AddFrequency(this, MASTERCLOCK_44K/(MASTERPREDIV_MILAN*(1<<i)), MASTERCLOCK_44K, (1<<i)-1);
patmandin@644
   276
	}
patmandin@644
   277
patmandin@961
   278
#if 1
patmandin@961
   279
	for (i=0; i<MINTAUDIO_freqcount; i++) {
patmandin@961
   280
		DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
patmandin@961
   281
			i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
patmandin@961
   282
			MINTAUDIO_frequencies[i].predivisor
patmandin@961
   283
		));
patmandin@961
   284
	}
patmandin@961
   285
#endif
patmandin@961
   286
patmandin@961
   287
	MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
patmandin@961
   288
	spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
patmandin@644
   289
patmandin@644
   290
	DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
patmandin@644
   291
	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
patmandin@704
   292
	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
patmandin@644
   293
	DEBUG_PRINT(("channels=%d, ", spec->channels));
patmandin@644
   294
	DEBUG_PRINT(("freq=%d\n", spec->freq));
patmandin@644
   295
patmandin@644
   296
	return 0;
patmandin@644
   297
}
patmandin@644
   298
patmandin@644
   299
static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
patmandin@644
   300
{
patmandin@961
   301
	int channels_mode, prediv;
patmandin@644
   302
	void *buffer;
patmandin@644
   303
patmandin@644
   304
	/* Stop currently playing sound */
patmandin@644
   305
	Buffoper(0);
patmandin@644
   306
patmandin@644
   307
	/* Set replay tracks */
patmandin@644
   308
	Settracks(0,0);
patmandin@644
   309
	Setmontracks(0);
patmandin@644
   310
patmandin@644
   311
	/* Select replay format */
patmandin@644
   312
	switch (spec->format & 0xff) {
patmandin@644
   313
		case 8:
patmandin@644
   314
			if (spec->channels==2) {
patmandin@644
   315
				channels_mode=STEREO8;
patmandin@644
   316
			} else {
patmandin@644
   317
				channels_mode=MONO8;
patmandin@644
   318
			}
patmandin@644
   319
			break;
patmandin@644
   320
		case 16:
patmandin@644
   321
			if (spec->channels==2) {
patmandin@644
   322
				channels_mode=STEREO16;
patmandin@644
   323
			} else {
patmandin@644
   324
				channels_mode=MONO16;
patmandin@644
   325
			}
patmandin@644
   326
			break;
patmandin@644
   327
		default:
patmandin@644
   328
			channels_mode=STEREO16;
patmandin@644
   329
			break;
patmandin@644
   330
	}
patmandin@644
   331
	if (Setmode(channels_mode)<0) {
patmandin@644
   332
		DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
patmandin@644
   333
	}
patmandin@644
   334
patmandin@961
   335
	prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
patmandin@961
   336
	Devconnect(DMAPLAY, DAC, CLKEXT, prediv, 1);
patmandin@644
   337
patmandin@644
   338
	/* Set buffer */
patmandin@644
   339
	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
patmandin@644
   340
	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
patmandin@644
   341
		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
patmandin@644
   342
	}
patmandin@644
   343
	
patmandin@644
   344
	/* Install interrupt */
patmandin@644
   345
	if (NSetinterrupt(2, SI_PLAY, SDL_MintAudio_GsxbInterrupt)<0) {
patmandin@644
   346
		DEBUG_PRINT((DEBUG_NAME "NSetinterrupt() failed\n"));
patmandin@644
   347
	}
patmandin@644
   348
patmandin@644
   349
	/* Go */
patmandin@644
   350
	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
patmandin@644
   351
	DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
patmandin@644
   352
}
patmandin@644
   353
patmandin@644
   354
static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
patmandin@644
   355
{
patmandin@644
   356
	/* Lock sound system */
patmandin@644
   357
	if (Locksnd()!=1) {
patmandin@644
   358
   	    SDL_SetError("Mint_OpenAudio: Audio system already in use");
patmandin@644
   359
        return(-1);
patmandin@644
   360
	}
patmandin@644
   361
patmandin@644
   362
	SDL_MintAudio_device = this;
patmandin@644
   363
patmandin@644
   364
	/* Check audio capabilities */
patmandin@644
   365
	if (Mint_CheckAudio(this, spec)==-1) {
patmandin@644
   366
		return -1;
patmandin@644
   367
	}
patmandin@644
   368
patmandin@644
   369
	SDL_CalculateAudioSpec(spec);
patmandin@644
   370
patmandin@644
   371
	/* Allocate memory for audio buffers in DMA-able RAM */
patmandin@644
   372
	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
patmandin@644
   373
patmandin@644
   374
	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
patmandin@644
   375
	if (SDL_MintAudio_audiobuf[0]==NULL) {
patmandin@644
   376
		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
patmandin@644
   377
		return (-1);
patmandin@644
   378
	}
patmandin@644
   379
	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
patmandin@644
   380
	SDL_MintAudio_numbuf=0;
patmandin@644
   381
	memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
patmandin@644
   382
	SDL_MintAudio_audiosize = spec->size;
patmandin@644
   383
	SDL_MintAudio_mutex = 0;
patmandin@644
   384
patmandin@644
   385
	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
patmandin@644
   386
	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
patmandin@644
   387
patmandin@644
   388
	/* Setup audio hardware */
patmandin@644
   389
	Mint_InitAudio(this, spec);
patmandin@644
   390
patmandin@644
   391
    return(1);	/* We don't use threaded audio */
patmandin@644
   392
}