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