src/audio/amigaos/SDL_ahiaudio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:30:25 +0000
changeset 249 e3d0d44f6f2e
parent 21 75a95f82bc1f
child 252 e8157fcb3114
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
#ifdef SAVE_RCSID
slouken@0
    26
static char rcsid =
slouken@0
    27
 "@(#) $Id$";
slouken@0
    28
#endif
slouken@0
    29
slouken@21
    30
/* Allow access to a raw mixing buffer (for AmigaOS) */
slouken@21
    31
slouken@21
    32
#include <stdio.h>
slouken@21
    33
#include <stdlib.h>
slouken@21
    34
#include <string.h>
slouken@0
    35
slouken@0
    36
#include "SDL_endian.h"
slouken@0
    37
#include "SDL_audio.h"
slouken@0
    38
#include "SDL_audiomem.h"
slouken@0
    39
#include "SDL_audio_c.h"
slouken@21
    40
#include "SDL_ahiaudio.h"
slouken@0
    41
slouken@0
    42
/* Audio driver functions */
slouken@0
    43
static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    44
static void AHI_WaitAudio(_THIS);
slouken@0
    45
static void AHI_PlayAudio(_THIS);
slouken@0
    46
static Uint8 *AHI_GetAudioBuf(_THIS);
slouken@0
    47
static void AHI_CloseAudio(_THIS);
slouken@0
    48
slouken@0
    49
#ifndef __SASC
slouken@0
    50
	#define mymalloc(x) AllocVec(x,MEMF_PUBLIC) 
slouken@0
    51
	#define myfree FreeVec
slouken@0
    52
#else
slouken@0
    53
	#define mymalloc malloc
slouken@0
    54
	#define myfree free
slouken@0
    55
#endif
slouken@0
    56
slouken@0
    57
/* Audio driver bootstrap functions */
slouken@0
    58
slouken@0
    59
static int Audio_Available(void)
slouken@0
    60
{
slouken@21
    61
	int ok=0;
slouken@21
    62
	struct MsgPort *p;
slouken@21
    63
	struct AHIRequest *req;
slouken@0
    64
slouken@21
    65
	if(p=CreateMsgPort())
slouken@21
    66
	{
slouken@21
    67
		if(req=(struct AHIRequest *)CreateIORequest(p,sizeof(struct AHIRequest)))
slouken@21
    68
		{
slouken@21
    69
			req->ahir_Version=4;
slouken@21
    70
slouken@21
    71
			if(!OpenDevice(AHINAME,0,(struct IORequest *)req,NULL))
slouken@21
    72
			{
slouken@21
    73
				D(bug("AHI available.\n"));
slouken@21
    74
				ok=1;
slouken@21
    75
				CloseDevice((struct IORequest *)req);
slouken@21
    76
			}
slouken@21
    77
			DeleteIORequest((struct IORequest *)req);
slouken@21
    78
		}
slouken@21
    79
		DeleteMsgPort(p);
slouken@21
    80
	}
slouken@21
    81
slouken@21
    82
	D(if(!ok) bug("AHI not available\n"));
slouken@21
    83
	return ok;
slouken@0
    84
}
slouken@0
    85
slouken@0
    86
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    87
{
slouken@0
    88
	free(device->hidden);
slouken@0
    89
	free(device);
slouken@0
    90
}
slouken@0
    91
slouken@0
    92
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    93
{
slouken@0
    94
	SDL_AudioDevice *this;
slouken@0
    95
slouken@0
    96
#ifndef NO_AMIGADEBUG
slouken@0
    97
	D(bug("AHI created...\n"));
slouken@0
    98
#endif
slouken@0
    99
slouken@0
   100
	/* Initialize all variables that we clean on shutdown */
slouken@0
   101
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   102
	if ( this ) {
slouken@0
   103
		memset(this, 0, (sizeof *this));
slouken@0
   104
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   105
				malloc((sizeof *this->hidden));
slouken@0
   106
	}
slouken@0
   107
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   108
		SDL_OutOfMemory();
slouken@0
   109
		if ( this ) {
slouken@0
   110
			free(this);
slouken@0
   111
		}
slouken@0
   112
		return(0);
slouken@0
   113
	}
slouken@0
   114
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   115
slouken@0
   116
	/* Set the function pointers */
slouken@0
   117
	this->OpenAudio = AHI_OpenAudio;
slouken@0
   118
	this->WaitAudio = AHI_WaitAudio;
slouken@0
   119
	this->PlayAudio = AHI_PlayAudio;
slouken@0
   120
	this->GetAudioBuf = AHI_GetAudioBuf;
slouken@0
   121
	this->CloseAudio = AHI_CloseAudio;
slouken@0
   122
slouken@0
   123
	this->free = Audio_DeleteDevice;
slouken@0
   124
slouken@0
   125
	return this;
slouken@0
   126
}
slouken@0
   127
slouken@0
   128
AudioBootStrap AHI_bootstrap = {
slouken@0
   129
	"AHI", Audio_Available, Audio_CreateDevice
slouken@0
   130
};
slouken@0
   131
slouken@0
   132
slouken@0
   133
void static AHI_WaitAudio(_THIS)
slouken@0
   134
{
slouken@0
   135
	if(!CheckIO((struct IORequest *)audio_req[current_buffer]))
slouken@0
   136
	{
slouken@0
   137
		WaitIO((struct IORequest *)audio_req[current_buffer]);
slouken@0
   138
//		AbortIO((struct IORequest *)audio_req[current_buffer]);
slouken@0
   139
	}
slouken@0
   140
}
slouken@0
   141
slouken@0
   142
static void AHI_PlayAudio(_THIS)
slouken@0
   143
{
slouken@0
   144
	if(playing>1)
slouken@0
   145
		WaitIO((struct IORequest *)audio_req[current_buffer]);
slouken@0
   146
slouken@0
   147
	/* Write the audio data out */
slouken@0
   148
	audio_req[current_buffer] -> ahir_Std. io_Message.mn_Node.ln_Pri = 60;
slouken@0
   149
	audio_req[current_buffer] -> ahir_Std. io_Data                = mixbuf[current_buffer];
slouken@21
   150
	audio_req[current_buffer] -> ahir_Std. io_Length              = this->hidden->size;
slouken@0
   151
	audio_req[current_buffer] -> ahir_Std. io_Offset              = 0;
slouken@0
   152
	audio_req[current_buffer] -> ahir_Std . io_Command            = CMD_WRITE;
slouken@0
   153
	audio_req[current_buffer] -> ahir_Frequency                   = this->hidden->freq;
slouken@0
   154
	audio_req[current_buffer] -> ahir_Volume                      = 0x10000;
slouken@0
   155
	audio_req[current_buffer] -> ahir_Type                        = this->hidden->type;
slouken@0
   156
	audio_req[current_buffer] -> ahir_Position                    = 0x8000;
slouken@0
   157
	audio_req[current_buffer] -> ahir_Link                        = (playing>0 ? audio_req[current_buffer^1] : NULL);
slouken@0
   158
slouken@0
   159
	SendIO((struct IORequest *)audio_req[current_buffer]);
slouken@0
   160
	current_buffer^=1;
slouken@0
   161
slouken@0
   162
	playing++;
slouken@0
   163
}
slouken@0
   164
slouken@0
   165
static Uint8 *AHI_GetAudioBuf(_THIS)
slouken@0
   166
{
slouken@0
   167
	return(mixbuf[current_buffer]);
slouken@0
   168
}
slouken@0
   169
slouken@0
   170
static void AHI_CloseAudio(_THIS)
slouken@0
   171
{
slouken@0
   172
	D(bug("Closing audio...\n"));
slouken@0
   173
slouken@21
   174
	playing=0;
slouken@21
   175
slouken@21
   176
	if(audio_req[0])
slouken@21
   177
	{
slouken@21
   178
		if(audio_req[1])
slouken@21
   179
		{
slouken@21
   180
			D(bug("Break req[1]...\n"));
slouken@21
   181
slouken@21
   182
			AbortIO((struct IORequest *)audio_req[1]);
slouken@21
   183
			WaitIO((struct IORequest *)audio_req[1]);
slouken@21
   184
		}
slouken@21
   185
slouken@21
   186
		D(bug("Break req[0]...\n"));
slouken@21
   187
slouken@21
   188
		AbortIO((struct IORequest *)audio_req[0]);
slouken@21
   189
		WaitIO((struct IORequest *)audio_req[0]);
slouken@21
   190
slouken@21
   191
		if(audio_req[1])
slouken@21
   192
		{
slouken@21
   193
			D(bug("Break AGAIN req[1]...\n"));
slouken@21
   194
			AbortIO((struct IORequest *)audio_req[1]);
slouken@21
   195
			WaitIO((struct IORequest *)audio_req[1]);
slouken@21
   196
		}
slouken@21
   197
// Double abort to be sure to break the dbuffering process.
slouken@21
   198
slouken@21
   199
		SDL_Delay(200);
slouken@21
   200
slouken@21
   201
		D(bug("Reqs breaked, closing device...\n"));
slouken@21
   202
		CloseDevice((struct IORequest *)audio_req[0]);
slouken@21
   203
		D(bug("Device closed, freeing memory...\n"));
slouken@21
   204
		myfree(audio_req[1]);
slouken@21
   205
		D(bug("Memory freed, deleting IOReq...\n")); 
slouken@21
   206
		DeleteIORequest((struct IORequest *)audio_req[0]);
slouken@21
   207
		audio_req[0]=audio_req[1]=NULL;
slouken@21
   208
	}
slouken@21
   209
slouken@21
   210
	D(bug("Freeing mixbuf[0]...\n"));
slouken@0
   211
	if ( mixbuf[0] != NULL ) {
slouken@0
   212
		myfree(mixbuf[0]);
slouken@0
   213
//		SDL_FreeAudioMem(mixbuf[0]);
slouken@0
   214
		mixbuf[0] = NULL;
slouken@0
   215
	}
slouken@0
   216
slouken@21
   217
	D(bug("Freeing mixbuf[1]...\n"));
slouken@0
   218
	if ( mixbuf[1] != NULL ) {
slouken@0
   219
		myfree(mixbuf[1]);
slouken@0
   220
//		SDL_FreeAudioMem(mixbuf[1]);
slouken@0
   221
		mixbuf[1] = NULL;
slouken@0
   222
	}
slouken@0
   223
slouken@21
   224
	D(bug("Freeing audio_port...\n"));
slouken@0
   225
slouken@0
   226
	if ( audio_port != NULL ) {
slouken@0
   227
		DeleteMsgPort(audio_port);
slouken@0
   228
		audio_port = NULL;
slouken@0
   229
	}
slouken@0
   230
	D(bug("...done!\n"));
slouken@0
   231
}
slouken@0
   232
slouken@0
   233
static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   234
{	
slouken@0
   235
//	int width;
slouken@0
   236
slouken@0
   237
	D(bug("AHI opening...\n"));
slouken@0
   238
slouken@0
   239
	/* Determine the audio parameters from the AudioSpec */
slouken@0
   240
	switch ( spec->format & 0xFF ) {
slouken@0
   241
slouken@0
   242
		case 8: { /* Signed 8 bit audio data */
slouken@0
   243
			D(bug("Samples a 8 bit...\n"));
slouken@0
   244
			spec->format = AUDIO_S8;
slouken@0
   245
			this->hidden->bytespersample=1;
slouken@21
   246
			if(spec->channels<2)
slouken@21
   247
				this->hidden->type = AHIST_M8S;
slouken@21
   248
			else
slouken@21
   249
				this->hidden->type = AHIST_S8S;
slouken@0
   250
		}
slouken@0
   251
		break;
slouken@0
   252
slouken@0
   253
		case 16: { /* Signed 16 bit audio data */
slouken@0
   254
			D(bug("Samples a 16 bit...\n"));
slouken@0
   255
			spec->format = AUDIO_S16MSB;
slouken@0
   256
			this->hidden->bytespersample=2;
slouken@21
   257
			if(spec->channels<2)
slouken@21
   258
				this->hidden->type = AHIST_M16S;
slouken@21
   259
			else
slouken@21
   260
				this->hidden->type = AHIST_S16S;
slouken@0
   261
		}
slouken@0
   262
		break;
slouken@0
   263
slouken@0
   264
		default: {
slouken@0
   265
			SDL_SetError("Unsupported audio format");
slouken@0
   266
			return(-1);
slouken@0
   267
		}
slouken@0
   268
	}
slouken@0
   269
slouken@21
   270
	if(spec->channels!=1 && spec->channels!=2)
slouken@21
   271
	{
slouken@21
   272
		D(bug("Wrong channel number!\n"));
slouken@21
   273
		SDL_SetError("Channel number non supported");
slouken@21
   274
		return -1;
slouken@21
   275
	}
slouken@21
   276
slouken@0
   277
	D(bug("Before CalculateAudioSpec\n"));
slouken@0
   278
	/* Update the fragment size as size in bytes */
slouken@0
   279
	SDL_CalculateAudioSpec(spec);
slouken@0
   280
slouken@0
   281
	D(bug("Before CreateMsgPort\n"));
slouken@0
   282
slouken@0
   283
	if(!(audio_port=CreateMsgPort()))
slouken@0
   284
	{
slouken@0
   285
		SDL_SetError("Unable to create a MsgPort");
slouken@0
   286
		return -1;
slouken@0
   287
	}
slouken@0
   288
slouken@0
   289
	D(bug("Before CreateIORequest\n"));
slouken@0
   290
slouken@0
   291
	if(!(audio_req[0]=(struct AHIRequest *)CreateIORequest(audio_port,sizeof(struct AHIRequest))))
slouken@0
   292
	{
slouken@0
   293
		SDL_SetError("Unable to create an AHIRequest");
slouken@0
   294
		DeleteMsgPort(audio_port);
slouken@0
   295
		return -1;
slouken@0
   296
	}
slouken@0
   297
slouken@0
   298
	audio_req[0]->ahir_Version = 4;
slouken@0
   299
slouken@0
   300
	if(OpenDevice(AHINAME,0,(struct IORequest *)audio_req[0],NULL))
slouken@0
   301
	{
slouken@0
   302
		SDL_SetError("Unable to open AHI device!\n");
slouken@0
   303
		DeleteIORequest((struct IORequest *)audio_req[0]);
slouken@0
   304
		DeleteMsgPort(audio_port);
slouken@0
   305
		return -1;
slouken@0
   306
	}
slouken@0
   307
	
slouken@0
   308
	D(bug("AFTER opendevice\n"));
slouken@0
   309
slouken@21
   310
	/* Set output frequency and size */
slouken@0
   311
	this->hidden->freq = spec->freq;
slouken@21
   312
	this->hidden->size = spec->size;
slouken@0
   313
slouken@0
   314
	D(bug("Before buffer allocation\n"));
slouken@0
   315
slouken@0
   316
	/* Allocate mixing buffer */
slouken@0
   317
	mixbuf[0] = (Uint8 *)mymalloc(spec->size);
slouken@0
   318
	mixbuf[1] = (Uint8 *)mymalloc(spec->size);
slouken@0
   319
slouken@0
   320
	D(bug("Before audio_req allocation\n"));
slouken@0
   321
slouken@0
   322
	if(!(audio_req[1]=mymalloc(sizeof(struct AHIRequest))))
slouken@0
   323
	{
slouken@0
   324
		SDL_OutOfMemory();
slouken@0
   325
		return(-1);
slouken@0
   326
	}
slouken@0
   327
	
slouken@0
   328
	D(bug("Before audio_req memcpy\n"));
slouken@0
   329
slouken@0
   330
	memcpy(audio_req[1],audio_req[0],sizeof(struct AHIRequest));
slouken@0
   331
slouken@0
   332
	if ( mixbuf[0] == NULL || mixbuf[1] == NULL ) {
slouken@0
   333
		SDL_OutOfMemory();
slouken@0
   334
		return(-1);
slouken@0
   335
	}
slouken@0
   336
slouken@0
   337
	D(bug("Before mixbuf memset\n"));
slouken@0
   338
slouken@0
   339
	memset(mixbuf[0], spec->silence, spec->size);
slouken@0
   340
	memset(mixbuf[1], spec->silence, spec->size);
slouken@0
   341
slouken@0
   342
	current_buffer=0;
slouken@0
   343
	playing=0;
slouken@0
   344
slouken@21
   345
	D(bug("AHI opened: freq:%ld mixbuf:%lx/%lx buflen:%ld bits:%ld channels:%ld\n",spec->freq,mixbuf[0],mixbuf[1],spec->size,this->hidden->bytespersample*8,spec->channels));
slouken@0
   346
slouken@0
   347
	/* We're ready to rock and roll. :-) */
slouken@0
   348
	return(0);
slouken@0
   349
}