src/audio/dmedia/SDL_irixaudio.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Aug 2001 13:09:47 +0000
changeset 148 8758b8d42cd9
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Audio subsystem no longer assumes sun audio API on UNIX systems
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@0
    30
/* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
slouken@0
    31
slouken@0
    32
#include <stdlib.h>
slouken@0
    33
slouken@0
    34
#include "SDL_endian.h"
slouken@0
    35
#include "SDL_timer.h"
slouken@0
    36
#include "SDL_audio.h"
slouken@0
    37
#include "SDL_audiomem.h"
slouken@0
    38
#include "SDL_audio_c.h"
slouken@0
    39
#include "SDL_irixaudio.h"
slouken@0
    40
slouken@0
    41
slouken@0
    42
/* Audio driver functions */
slouken@0
    43
static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    44
static void AL_WaitAudio(_THIS);
slouken@0
    45
static void AL_PlayAudio(_THIS);
slouken@0
    46
static Uint8 *AL_GetAudioBuf(_THIS);
slouken@0
    47
static void AL_CloseAudio(_THIS);
slouken@0
    48
slouken@0
    49
/* Audio driver bootstrap functions */
slouken@0
    50
slouken@0
    51
static int Audio_Available(void)
slouken@0
    52
{
slouken@0
    53
	return 1;
slouken@0
    54
}
slouken@0
    55
slouken@0
    56
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    57
{
slouken@0
    58
	free(device->hidden);
slouken@0
    59
	free(device);
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    63
{
slouken@0
    64
	SDL_AudioDevice *this;
slouken@0
    65
slouken@0
    66
	/* Initialize all variables that we clean on shutdown */
slouken@0
    67
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    68
	if ( this ) {
slouken@0
    69
		memset(this, 0, (sizeof *this));
slouken@0
    70
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    71
				malloc((sizeof *this->hidden));
slouken@0
    72
	}
slouken@0
    73
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    74
		SDL_OutOfMemory();
slouken@0
    75
		if ( this ) {
slouken@0
    76
			free(this);
slouken@0
    77
		}
slouken@0
    78
		return(0);
slouken@0
    79
	}
slouken@0
    80
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    81
slouken@0
    82
	/* Set the function pointers */
slouken@0
    83
	this->OpenAudio = AL_OpenAudio;
slouken@0
    84
	this->WaitAudio = AL_WaitAudio;
slouken@0
    85
	this->PlayAudio = AL_PlayAudio;
slouken@0
    86
	this->GetAudioBuf = AL_GetAudioBuf;
slouken@0
    87
	this->CloseAudio = AL_CloseAudio;
slouken@0
    88
slouken@0
    89
	this->free = Audio_DeleteDevice;
slouken@0
    90
slouken@0
    91
	return this;
slouken@0
    92
}
slouken@0
    93
slouken@148
    94
AudioBootStrap DMEDIA_bootstrap = {
slouken@0
    95
	"AL", "IRIX DMedia audio",
slouken@0
    96
	Audio_Available, Audio_CreateDevice
slouken@0
    97
};
slouken@0
    98
slouken@0
    99
slouken@0
   100
void static AL_WaitAudio(_THIS)
slouken@0
   101
{
slouken@0
   102
	Sint32 timeleft;
slouken@0
   103
slouken@0
   104
	timeleft = this->spec.samples - alGetFillable(audio_port);
slouken@0
   105
	if ( timeleft > 0 ) {
slouken@0
   106
		timeleft /= (this->spec.freq/1000);
slouken@0
   107
		SDL_Delay((Uint32)timeleft);
slouken@0
   108
	}
slouken@0
   109
}
slouken@0
   110
slouken@0
   111
static void AL_PlayAudio(_THIS)
slouken@0
   112
{
slouken@0
   113
	/* Write the audio data out */
slouken@0
   114
	if ( alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0 ) {
slouken@0
   115
		/* Assume fatal error, for now */
slouken@0
   116
		this->enabled = 0;
slouken@0
   117
	}
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
static Uint8 *AL_GetAudioBuf(_THIS)
slouken@0
   121
{
slouken@0
   122
	return(mixbuf);
slouken@0
   123
}
slouken@0
   124
slouken@0
   125
static void AL_CloseAudio(_THIS)
slouken@0
   126
{
slouken@0
   127
	if ( mixbuf != NULL ) {
slouken@0
   128
		SDL_FreeAudioMem(mixbuf);
slouken@0
   129
		mixbuf = NULL;
slouken@0
   130
	}
slouken@0
   131
	if ( audio_port != NULL ) {
slouken@0
   132
		ALcloseport(audio_port);
slouken@0
   133
		audio_port = NULL;
slouken@0
   134
	}
slouken@0
   135
}
slouken@0
   136
slouken@0
   137
static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   138
{
slouken@0
   139
	ALconfig audio_config;
slouken@0
   140
	ALpv audio_param;
slouken@0
   141
	int width;
slouken@0
   142
slouken@0
   143
	/* Determine the audio parameters from the AudioSpec */
slouken@0
   144
	switch ( spec->format & 0xFF ) {
slouken@0
   145
slouken@0
   146
		case 8: { /* Signed 8 bit audio data */
slouken@0
   147
			spec->format = AUDIO_S8;
slouken@0
   148
			width = AL_SAMPLE_8;
slouken@0
   149
		}
slouken@0
   150
		break;
slouken@0
   151
slouken@0
   152
		case 16: { /* Signed 16 bit audio data */
slouken@0
   153
			spec->format = AUDIO_S16MSB;
slouken@0
   154
			width = AL_SAMPLE_16;
slouken@0
   155
		}
slouken@0
   156
		break;
slouken@0
   157
slouken@0
   158
		default: {
slouken@0
   159
			SDL_SetError("Unsupported audio format");
slouken@0
   160
			return(-1);
slouken@0
   161
		}
slouken@0
   162
	}
slouken@0
   163
slouken@0
   164
	/* Update the fragment size as size in bytes */
slouken@0
   165
	SDL_CalculateAudioSpec(spec);
slouken@0
   166
slouken@0
   167
	/* Set output frequency */
slouken@0
   168
	audio_param.param = AL_RATE;
slouken@0
   169
	audio_param.value.i = spec->freq;
slouken@0
   170
	if( alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0 ) {
slouken@0
   171
		SDL_SetError("alSetParams failed");
slouken@0
   172
		return(-1);
slouken@0
   173
	}
slouken@0
   174
slouken@0
   175
	/* Open the audio port with the requested frequency */
slouken@0
   176
	audio_port = NULL;
slouken@0
   177
	audio_config = alNewConfig();
slouken@0
   178
	if ( audio_config &&
slouken@0
   179
	     (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) &&
slouken@0
   180
	     (alSetWidth(audio_config, width) >= 0) &&
slouken@0
   181
	     (alSetQueueSize(audio_config, spec->samples*2) >= 0) &&
slouken@0
   182
	     (alSetChannels(audio_config, spec->channels) >= 0) ) {
slouken@0
   183
		audio_port = ALopenport("SDL audio", "w", audio_config);
slouken@0
   184
	}
slouken@0
   185
	alFreeConfig(audio_config);
slouken@0
   186
	if( audio_port == NULL ) {
slouken@0
   187
		SDL_SetError("Unable to open audio port");
slouken@0
   188
		return(-1);
slouken@0
   189
	}
slouken@0
   190
slouken@0
   191
	/* Allocate mixing buffer */
slouken@0
   192
	mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size);
slouken@0
   193
	if ( mixbuf == NULL ) {
slouken@0
   194
		SDL_OutOfMemory();
slouken@0
   195
		return(-1);
slouken@0
   196
	}
slouken@0
   197
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   198
slouken@0
   199
	/* We're ready to rock and roll. :-) */
slouken@0
   200
	return(0);
slouken@0
   201
}