src/audio/sun/SDL_sunaudio.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, 1999, 2000, 2001  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
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Allow access to a raw mixing buffer */
slouken@0
    29
slouken@0
    30
#include <stdlib.h>
slouken@0
    31
#include <stdio.h>
slouken@0
    32
#include <fcntl.h>
slouken@0
    33
#include <errno.h>
slouken@0
    34
#include <string.h>
slouken@0
    35
#ifdef __NetBSD__
slouken@0
    36
#include <sys/ioctl.h>
slouken@0
    37
#include <sys/audioio.h>
slouken@0
    38
#endif
slouken@0
    39
#ifdef __SVR4
slouken@0
    40
#include <sys/audioio.h>
slouken@0
    41
#else
slouken@0
    42
#include <sys/time.h>
slouken@0
    43
#include <sys/types.h>
slouken@0
    44
#endif
slouken@0
    45
#include <unistd.h>
slouken@0
    46
slouken@0
    47
#include "SDL_endian.h"
slouken@0
    48
#include "SDL_audio.h"
slouken@0
    49
#include "SDL_audiomem.h"
slouken@0
    50
#include "SDL_audiodev_c.h"
slouken@0
    51
#include "SDL_sunaudio.h"
slouken@0
    52
#include "SDL_audio_c.h"
slouken@0
    53
#include "SDL_timer.h"
slouken@0
    54
slouken@0
    55
/* Open the audio device for playback, and don't block if busy */
slouken@0
    56
#define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
slouken@0
    57
slouken@0
    58
/* Audio driver functions */
slouken@0
    59
static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    60
static void DSP_WaitAudio(_THIS);
slouken@0
    61
static void DSP_PlayAudio(_THIS);
slouken@0
    62
static Uint8 *DSP_GetAudioBuf(_THIS);
slouken@0
    63
static void DSP_CloseAudio(_THIS);
slouken@0
    64
slouken@0
    65
/* Audio driver bootstrap functions */
slouken@0
    66
slouken@0
    67
static int Audio_Available(void)
slouken@0
    68
{
slouken@0
    69
	int fd;
slouken@0
    70
	int available;
slouken@0
    71
slouken@0
    72
	available = 0;
slouken@0
    73
	fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1);
slouken@0
    74
	if ( fd >= 0 ) {
slouken@0
    75
		available = 1;
slouken@0
    76
		close(fd);
slouken@0
    77
	}
slouken@0
    78
	return(available);
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    82
{
slouken@0
    83
	free(device->hidden);
slouken@0
    84
	free(device);
slouken@0
    85
}
slouken@0
    86
slouken@0
    87
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    88
{
slouken@0
    89
	SDL_AudioDevice *this;
slouken@0
    90
slouken@0
    91
	/* Initialize all variables that we clean on shutdown */
slouken@0
    92
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    93
	if ( this ) {
slouken@0
    94
		memset(this, 0, (sizeof *this));
slouken@0
    95
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    96
				malloc((sizeof *this->hidden));
slouken@0
    97
	}
slouken@0
    98
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    99
		SDL_OutOfMemory();
slouken@0
   100
		if ( this ) {
slouken@0
   101
			free(this);
slouken@0
   102
		}
slouken@0
   103
		return(0);
slouken@0
   104
	}
slouken@0
   105
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   106
	audio_fd = -1;
slouken@0
   107
slouken@0
   108
	/* Set the function pointers */
slouken@0
   109
	this->OpenAudio = DSP_OpenAudio;
slouken@0
   110
	this->WaitAudio = DSP_WaitAudio;
slouken@0
   111
	this->PlayAudio = DSP_PlayAudio;
slouken@0
   112
	this->GetAudioBuf = DSP_GetAudioBuf;
slouken@0
   113
	this->CloseAudio = DSP_CloseAudio;
slouken@0
   114
slouken@0
   115
	this->free = Audio_DeleteDevice;
slouken@0
   116
slouken@0
   117
	return this;
slouken@0
   118
}
slouken@0
   119
slouken@148
   120
AudioBootStrap SUNAUDIO_bootstrap = {
slouken@0
   121
	"audio", "UNIX /dev/audio interface",
slouken@0
   122
	Audio_Available, Audio_CreateDevice
slouken@0
   123
};
slouken@0
   124
slouken@0
   125
#ifdef DEBUG_AUDIO
slouken@0
   126
void CheckUnderflow(_THIS)
slouken@0
   127
{
slouken@0
   128
#ifdef AUDIO_GETINFO
slouken@0
   129
	audio_info_t info;
slouken@0
   130
	int left;
slouken@0
   131
slouken@0
   132
	ioctl(audio_fd, AUDIO_GETINFO, &info);
slouken@0
   133
	left = (written - info.play.samples);
slouken@0
   134
	if ( written && (left == 0) ) {
slouken@0
   135
		fprintf(stderr, "audio underflow!\n");
slouken@0
   136
	}
slouken@0
   137
#endif
slouken@0
   138
}
slouken@0
   139
#endif
slouken@0
   140
slouken@0
   141
void DSP_WaitAudio(_THIS)
slouken@0
   142
{
slouken@0
   143
#ifdef AUDIO_GETINFO
slouken@0
   144
#define SLEEP_FUDGE	10		/* 10 ms scheduling fudge factor */
slouken@0
   145
	audio_info_t info;
slouken@0
   146
	Sint32 left;
slouken@0
   147
slouken@0
   148
	ioctl(audio_fd, AUDIO_GETINFO, &info);
slouken@0
   149
	left = (written - info.play.samples);
slouken@0
   150
	if ( left > fragsize ) {
slouken@0
   151
		Sint32 sleepy;
slouken@0
   152
slouken@0
   153
		sleepy = ((left - fragsize)/frequency);
slouken@0
   154
		sleepy -= SLEEP_FUDGE;
slouken@0
   155
		if ( sleepy > 0 ) {
slouken@0
   156
			SDL_Delay(sleepy);
slouken@0
   157
		}
slouken@0
   158
	}
slouken@0
   159
#else
slouken@0
   160
	fd_set fdset;
slouken@0
   161
slouken@0
   162
	FD_ZERO(&fdset);
slouken@0
   163
	FD_SET(audio_fd, &fdset);
slouken@0
   164
	select(audio_fd+1, NULL, &fdset, NULL, NULL);
slouken@0
   165
#endif
slouken@0
   166
}
slouken@0
   167
slouken@0
   168
void DSP_PlayAudio(_THIS)
slouken@0
   169
{
slouken@0
   170
	static Uint8 snd2au(int sample);
slouken@0
   171
	/* Write the audio data */
slouken@0
   172
	if ( ulaw_only ) {
slouken@0
   173
		/* Assuming that this->spec.freq >= 8000 Hz */
slouken@0
   174
		int accum, incr, pos;
slouken@0
   175
		Uint8 *aubuf;
slouken@0
   176
slouken@0
   177
		accum = 0;
slouken@0
   178
		incr  = this->spec.freq/8;
slouken@0
   179
		aubuf = ulaw_buf;
slouken@0
   180
		switch (audio_fmt & 0xFF) {
slouken@0
   181
			case 8: {
slouken@0
   182
				Uint8 *sndbuf;
slouken@0
   183
slouken@0
   184
				sndbuf = mixbuf;
slouken@0
   185
				for ( pos=0; pos < fragsize; ++pos ) {
slouken@0
   186
					*aubuf = snd2au((0x80-*sndbuf)*64);
slouken@0
   187
					accum += incr;
slouken@0
   188
					while ( accum > 0 ) {
slouken@0
   189
						accum -= 1000;
slouken@0
   190
						sndbuf += 1;
slouken@0
   191
					}
slouken@0
   192
					aubuf += 1;
slouken@0
   193
				}
slouken@0
   194
			}
slouken@0
   195
			break;
slouken@0
   196
			case 16: {
slouken@0
   197
				Sint16 *sndbuf;
slouken@0
   198
slouken@0
   199
				sndbuf = (Sint16 *)mixbuf;
slouken@0
   200
				for ( pos=0; pos < fragsize; ++pos ) {
slouken@0
   201
					*aubuf = snd2au(*sndbuf/4);
slouken@0
   202
					accum += incr;
slouken@0
   203
					while ( accum > 0 ) {
slouken@0
   204
						accum -= 1000;
slouken@0
   205
						sndbuf += 1;
slouken@0
   206
					}
slouken@0
   207
					aubuf += 1;
slouken@0
   208
				}
slouken@0
   209
			}
slouken@0
   210
			break;
slouken@0
   211
		}
slouken@0
   212
#ifdef DEBUG_AUDIO
slouken@0
   213
		CheckUnderflow(this);
slouken@0
   214
#endif
slouken@0
   215
		if ( write(audio_fd, ulaw_buf, fragsize) < 0 ) {
slouken@0
   216
			/* Assume fatal error, for now */
slouken@0
   217
			this->enabled = 0;
slouken@0
   218
		}
slouken@0
   219
		written += fragsize;
slouken@0
   220
	} else {
slouken@0
   221
#ifdef DEBUG_AUDIO
slouken@0
   222
		CheckUnderflow(this);
slouken@0
   223
#endif
slouken@0
   224
		if ( write(audio_fd, mixbuf, this->spec.size) < 0 ) {
slouken@0
   225
			/* Assume fatal error, for now */
slouken@0
   226
			this->enabled = 0;
slouken@0
   227
		}
slouken@0
   228
		written += fragsize;
slouken@0
   229
	}
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
Uint8 *DSP_GetAudioBuf(_THIS)
slouken@0
   233
{
slouken@0
   234
	return(mixbuf);
slouken@0
   235
}
slouken@0
   236
slouken@0
   237
void DSP_CloseAudio(_THIS)
slouken@0
   238
{
slouken@0
   239
	if ( mixbuf != NULL ) {
slouken@0
   240
		SDL_FreeAudioMem(mixbuf);
slouken@0
   241
		mixbuf = NULL;
slouken@0
   242
	}
slouken@0
   243
	if ( ulaw_buf != NULL ) {
slouken@0
   244
		free(ulaw_buf);
slouken@0
   245
		ulaw_buf = NULL;
slouken@0
   246
	}
slouken@0
   247
	close(audio_fd);
slouken@0
   248
}
slouken@0
   249
slouken@0
   250
int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   251
{
slouken@0
   252
	char audiodev[1024];
slouken@0
   253
#ifdef AUDIO_SETINFO
slouken@0
   254
	int enc;
slouken@0
   255
#endif
slouken@0
   256
	int desired_freq = spec->freq;
slouken@0
   257
slouken@0
   258
	/* Initialize our freeable variables, in case we fail*/
slouken@0
   259
	audio_fd = -1;
slouken@0
   260
	mixbuf = NULL;
slouken@0
   261
	ulaw_buf = NULL;
slouken@0
   262
slouken@0
   263
	/* Determine the audio parameters from the AudioSpec */
slouken@0
   264
	switch ( spec->format & 0xFF ) {
slouken@0
   265
slouken@0
   266
		case 8: { /* Unsigned 8 bit audio data */
slouken@0
   267
			spec->format = AUDIO_U8;
slouken@0
   268
#ifdef AUDIO_SETINFO
slouken@0
   269
			enc = AUDIO_ENCODING_LINEAR8;
slouken@0
   270
#endif
slouken@0
   271
		}
slouken@0
   272
		break;
slouken@0
   273
slouken@0
   274
		case 16: { /* Signed 16 bit audio data */
slouken@0
   275
		        spec->format = AUDIO_S16SYS;
slouken@0
   276
#ifdef AUDIO_SETINFO
slouken@0
   277
			enc = AUDIO_ENCODING_LINEAR;
slouken@0
   278
#endif
slouken@0
   279
		}
slouken@0
   280
		break;
slouken@0
   281
slouken@0
   282
		default: {
slouken@0
   283
			SDL_SetError("Unsupported audio format");
slouken@0
   284
			return(-1);
slouken@0
   285
		}
slouken@0
   286
	}
slouken@0
   287
	audio_fmt = spec->format;
slouken@0
   288
slouken@0
   289
	/* Open the audio device */
slouken@0
   290
	audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1);
slouken@0
   291
	if ( audio_fd < 0 ) {
slouken@0
   292
		SDL_SetError("Couldn't open %s: %s", audiodev,
slouken@0
   293
			     strerror(errno));
slouken@0
   294
		return(-1);
slouken@0
   295
	}
slouken@0
   296
slouken@0
   297
	ulaw_only = 0;		/* modern Suns do support linear audio */
slouken@0
   298
#ifdef AUDIO_SETINFO
slouken@0
   299
	for(;;) {
slouken@0
   300
	    audio_info_t info;
slouken@0
   301
	    AUDIO_INITINFO(&info); /* init all fields to "no change" */
slouken@0
   302
slouken@0
   303
	    /* Try to set the requested settings */
slouken@0
   304
	    info.play.sample_rate = spec->freq;
slouken@0
   305
	    info.play.channels = spec->channels;
slouken@0
   306
	    info.play.precision = (enc == AUDIO_ENCODING_ULAW)
slouken@0
   307
		                  ? 8 : spec->format & 0xff;
slouken@0
   308
	    info.play.encoding = enc;
slouken@0
   309
	    if( ioctl(audio_fd, AUDIO_SETINFO, &info) == 0 ) {
slouken@0
   310
slouken@0
   311
		/* Check to be sure we got what we wanted */
slouken@0
   312
		if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
slouken@0
   313
		    SDL_SetError("Error getting audio parameters: %s",
slouken@0
   314
				 strerror(errno));
slouken@0
   315
		    return -1;
slouken@0
   316
		}
slouken@0
   317
		if(info.play.encoding == enc
slouken@0
   318
		   && info.play.precision == (spec->format & 0xff)
slouken@0
   319
		   && info.play.channels == spec->channels) {
slouken@0
   320
		    /* Yow! All seems to be well! */
slouken@0
   321
		    spec->freq = info.play.sample_rate;
slouken@0
   322
		    break;
slouken@0
   323
		}
slouken@0
   324
	    }
slouken@0
   325
slouken@0
   326
	    switch(enc) {
slouken@0
   327
	    case AUDIO_ENCODING_LINEAR8:
slouken@0
   328
		/* unsigned 8bit apparently not supported here */
slouken@0
   329
		enc = AUDIO_ENCODING_LINEAR;
slouken@0
   330
		spec->format = AUDIO_S16SYS;
slouken@0
   331
		break;	/* try again */
slouken@0
   332
slouken@0
   333
	    case AUDIO_ENCODING_LINEAR:
slouken@0
   334
		/* linear 16bit didn't work either, resort to -law */
slouken@0
   335
		enc = AUDIO_ENCODING_ULAW;
slouken@0
   336
		spec->channels = 1;
slouken@0
   337
		spec->freq = 8000;
slouken@0
   338
		spec->format = AUDIO_U8;
slouken@0
   339
		ulaw_only = 1;
slouken@0
   340
		break;
slouken@0
   341
slouken@0
   342
	    default:
slouken@0
   343
		/* oh well... */
slouken@0
   344
		SDL_SetError("Error setting audio parameters: %s",
slouken@0
   345
			     strerror(errno));
slouken@0
   346
		return -1;
slouken@0
   347
	    }
slouken@0
   348
	}
slouken@0
   349
#endif /* AUDIO_SETINFO */
slouken@0
   350
	written = 0;
slouken@0
   351
slouken@0
   352
	/* We can actually convert on-the-fly to U-Law */
slouken@0
   353
	if ( ulaw_only ) {
slouken@0
   354
	        spec->freq = desired_freq;
slouken@0
   355
		fragsize = (spec->samples*1000)/(spec->freq/8);
slouken@0
   356
		frequency = 8;
slouken@0
   357
		ulaw_buf = (Uint8 *)malloc(fragsize);
slouken@0
   358
		if ( ulaw_buf == NULL ) {
slouken@0
   359
			SDL_OutOfMemory();
slouken@0
   360
			return(-1);
slouken@0
   361
		}
slouken@0
   362
		spec->channels = 1;
slouken@0
   363
	} else {
slouken@0
   364
		fragsize = spec->samples;
slouken@0
   365
		frequency = spec->freq/1000;
slouken@0
   366
	}
slouken@0
   367
#ifdef DEBUG_AUDIO
slouken@0
   368
	fprintf(stderr, "Audio device %s U-Law only\n", 
slouken@0
   369
				ulaw_only ? "is" : "is not");
slouken@0
   370
	fprintf(stderr, "format=0x%x chan=%d freq=%d\n",
slouken@0
   371
		spec->format, spec->channels, spec->freq);
slouken@0
   372
#endif
slouken@0
   373
slouken@0
   374
	/* Update the fragment size as size in bytes */
slouken@0
   375
	SDL_CalculateAudioSpec(spec);
slouken@0
   376
slouken@0
   377
	/* Allocate mixing buffer */
slouken@0
   378
	mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size);
slouken@0
   379
	if ( mixbuf == NULL ) {
slouken@0
   380
		SDL_OutOfMemory();
slouken@0
   381
		return(-1);
slouken@0
   382
	}
slouken@0
   383
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   384
slouken@0
   385
	/* We're ready to rock and roll. :-) */
slouken@0
   386
	return(0);
slouken@0
   387
}
slouken@0
   388
slouken@0
   389
/************************************************************************/
slouken@0
   390
/* This function (snd2au()) copyrighted:                                */
slouken@0
   391
/************************************************************************/
slouken@0
   392
/*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
slouken@0
   393
/*                                                                      */
slouken@0
   394
/*      Permission to use, copy, modify, and distribute this software   */
slouken@0
   395
/*      and its documentation for any purpose and without fee is        */
slouken@0
   396
/*      hereby granted, provided that the above copyright notice        */
slouken@0
   397
/*      appears in all copies and that both that copyright notice and   */
slouken@0
   398
/*      this permission notice appear in supporting documentation, and  */
slouken@0
   399
/*      that the name of Rich Gopstein and Harris Corporation not be    */
slouken@0
   400
/*      used in advertising or publicity pertaining to distribution     */
slouken@0
   401
/*      of the software without specific, written prior permission.     */
slouken@0
   402
/*      Rich Gopstein and Harris Corporation make no representations    */
slouken@0
   403
/*      about the suitability of this software for any purpose.  It     */
slouken@0
   404
/*      provided "as is" without express or implied warranty.           */
slouken@0
   405
/************************************************************************/
slouken@0
   406
slouken@0
   407
static Uint8 snd2au(int sample)
slouken@0
   408
{
slouken@0
   409
slouken@0
   410
	int mask;
slouken@0
   411
slouken@0
   412
	if (sample < 0) {
slouken@0
   413
		sample = -sample;
slouken@0
   414
		mask = 0x7f;
slouken@0
   415
	} else {
slouken@0
   416
		mask = 0xff;
slouken@0
   417
	}
slouken@0
   418
slouken@0
   419
	if (sample < 32) {
slouken@0
   420
		sample = 0xF0 | (15 - sample / 2);
slouken@0
   421
	} else if (sample < 96) {
slouken@0
   422
		sample = 0xE0 | (15 - (sample - 32) / 4);
slouken@0
   423
	} else if (sample < 224) {
slouken@0
   424
		sample = 0xD0 | (15 - (sample - 96) / 8);
slouken@0
   425
	} else if (sample < 480) {
slouken@0
   426
		sample = 0xC0 | (15 - (sample - 224) / 16);
slouken@0
   427
	} else if (sample < 992) {
slouken@0
   428
		sample = 0xB0 | (15 - (sample - 480) / 32);
slouken@0
   429
	} else if (sample < 2016) {
slouken@0
   430
		sample = 0xA0 | (15 - (sample - 992) / 64);
slouken@0
   431
	} else if (sample < 4064) {
slouken@0
   432
		sample = 0x90 | (15 - (sample - 2016) / 128);
slouken@0
   433
	} else if (sample < 8160) {
slouken@0
   434
		sample = 0x80 | (15 - (sample - 4064) /  256);
slouken@0
   435
	} else {
slouken@0
   436
		sample = 0x80;
slouken@0
   437
	}
slouken@0
   438
	return (mask & sample);
slouken@0
   439
}