19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __NETBSD__
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/audioio.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __SVR4
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/audioio.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sunaudio.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the audio device for playback, and don't block if busy */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_WaitAudio(_THIS);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_PlayAudio(_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DSP_GetAudioBuf(_THIS);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DSP_CloseAudio(_THIS);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 snd2au(int sample);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int fd;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int available;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	available = 0;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fd >= 0 ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		available = 1;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(fd);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(available);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = -1;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = DSP_OpenAudio;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = DSP_WaitAudio;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = DSP_PlayAudio;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = DSP_GetAudioBuf;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = DSP_CloseAudio;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap SUNAUDIO_bootstrap = {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"audio", "UNIX /dev/audio interface",
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid CheckUnderflow(_THIS)
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_GETINFO
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_info_t info;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int left;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(audio_fd, AUDIO_GETINFO, &info);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	left = (written - info.play.samples);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( written && (left == 0) ) {
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "audio underflow!\n");
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DSP_WaitAudio(_THIS)
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_GETINFO
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SLEEP_FUDGE	10		/* 10 ms scheduling fudge factor */
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_info_t info;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 left;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(audio_fd, AUDIO_GETINFO, &info);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	left = (written - info.play.samples);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( left > fragsize ) {
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Sint32 sleepy;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sleepy = ((left - fragsize)/frequency);
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sleepy -= SLEEP_FUDGE;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( sleepy > 0 ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Delay(sleepy);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fd_set fdset;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FD_ZERO(&fdset);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FD_SET(audio_fd, &fdset);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	select(audio_fd+1, NULL, &fdset, NULL, NULL);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DSP_PlayAudio(_THIS)
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data */
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ulaw_only ) {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Assuming that this->spec.freq >= 8000 Hz */
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int accum, incr, pos;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint8 *aubuf;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		accum = 0;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		incr  = this->spec.freq/8;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		aubuf = ulaw_buf;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (audio_fmt & 0xFF) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 8: {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *sndbuf;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				sndbuf = mixbuf;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( pos=0; pos < fragsize; ++pos ) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*aubuf = snd2au((0x80-*sndbuf)*64);
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					accum += incr;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					while ( accum > 0 ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						accum -= 1000;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						sndbuf += 1;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					aubuf += 1;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 16: {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Sint16 *sndbuf;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				sndbuf = (Sint16 *)mixbuf;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( pos=0; pos < fragsize; ++pos ) {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*aubuf = snd2au(*sndbuf/4);
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					accum += incr;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					while ( accum > 0 ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						accum -= 1000;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						sndbuf += 1;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					aubuf += 1;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CheckUnderflow(this);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( write(audio_fd, ulaw_buf, fragsize) < 0 ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Assume fatal error, for now */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->enabled = 0;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		written += fragsize;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CheckUnderflow(this);
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( write(audio_fd, mixbuf, this->spec.size) < 0 ) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Assume fatal error, for now */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->enabled = 0;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		written += fragsize;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 *DSP_GetAudioBuf(_THIS)
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(mixbuf);
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DSP_CloseAudio(_THIS)
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(mixbuf);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ulaw_buf != NULL ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(ulaw_buf);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ulaw_buf = NULL;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	close(audio_fd);
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char audiodev[1024];
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_SETINFO
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int enc;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int desired_freq = spec->freq;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize our freeable variables, in case we fail*/
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = -1;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = NULL;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ulaw_buf = NULL;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the audio parameters from the AudioSpec */
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( spec->format & 0xFF ) {
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 8: { /* Unsigned 8 bit audio data */
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spec->format = AUDIO_U8;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_SETINFO
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			enc = AUDIO_ENCODING_LINEAR8;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 16: { /* Signed 16 bit audio data */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        spec->format = AUDIO_S16SYS;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_SETINFO
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			enc = AUDIO_ENCODING_LINEAR;
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default: {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unsupported audio format");
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fmt = spec->format;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open the audio device */
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1);
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_fd < 0 ) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't open %s: %s", audiodev,
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     strerror(errno));
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ulaw_only = 0;		/* modern Suns do support linear audio */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef AUDIO_SETINFO
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(;;) {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    audio_info_t info;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    AUDIO_INITINFO(&info); /* init all fields to "no change" */
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Try to set the requested settings */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    info.play.sample_rate = spec->freq;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    info.play.channels = spec->channels;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    info.play.precision = (enc == AUDIO_ENCODING_ULAW)
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                  ? 8 : spec->format & 0xff;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    info.play.encoding = enc;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if( ioctl(audio_fd, AUDIO_SETINFO, &info) == 0 ) {
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check to be sure we got what we wanted */
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDL_SetError("Error getting audio parameters: %s",
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 strerror(errno));
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    return -1;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(info.play.encoding == enc
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   && info.play.precision == (spec->format & 0xff)
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   && info.play.channels == spec->channels) {
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    /* Yow! All seems to be well! */
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    spec->freq = info.play.sample_rate;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    break;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    switch(enc) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AUDIO_ENCODING_LINEAR8:
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* unsigned 8bit apparently not supported here */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		enc = AUDIO_ENCODING_LINEAR;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->format = AUDIO_S16SYS;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;	/* try again */
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AUDIO_ENCODING_LINEAR:
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* linear 16bit didn't work either, resort to �-law */
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		enc = AUDIO_ENCODING_ULAW;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->channels = 1;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->freq = 8000;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->format = AUDIO_U8;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ulaw_only = 1;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* oh well... */
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Error setting audio parameters: %s",
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     strerror(errno));
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* AUDIO_SETINFO */
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	written = 0;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We can actually convert on-the-fly to U-Law */
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ulaw_only ) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        spec->freq = desired_freq;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fragsize = (spec->samples*1000)/(spec->freq/8);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		frequency = 8;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ulaw_buf = (Uint8 *)SDL_malloc(fragsize);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ulaw_buf == NULL ) {
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->channels = 1;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fragsize = spec->samples;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		frequency = spec->freq/1000;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Audio device %s U-Law only\n",
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ulaw_only ? "is" : "is not");
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "format=0x%x chan=%d freq=%d\n",
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spec->format, spec->channels, spec->freq);
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the fragment size as size in bytes */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(mixbuf, spec->silence, spec->size);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/************************************************************************/
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function (snd2au()) copyrighted:                                */
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/************************************************************************/
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*                                                                      */
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      Permission to use, copy, modify, and distribute this software   */
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      and its documentation for any purpose and without fee is        */
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      hereby granted, provided that the above copyright notice        */
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      appears in all copies and that both that copyright notice and   */
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      this permission notice appear in supporting documentation, and  */
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      that the name of Rich Gopstein and Harris Corporation not be    */
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      used in advertising or publicity pertaining to distribution     */
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      of the software without specific, written prior permission.     */
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      Rich Gopstein and Harris Corporation make no representations    */
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      about the suitability of this software for any purpose.  It     */
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*      provided "as is" without express or implied warranty.           */
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/************************************************************************/
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 snd2au(int sample)
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int mask;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (sample < 0) {
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = -sample;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mask = 0x7f;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mask = 0xff;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (sample < 32) {
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xF0 | (15 - sample / 2);
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 96) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xE0 | (15 - (sample - 32) / 4);
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 224) {
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xD0 | (15 - (sample - 96) / 8);
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 480) {
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xC0 | (15 - (sample - 224) / 16);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 992) {
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xB0 | (15 - (sample - 480) / 32);
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 2016) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0xA0 | (15 - (sample - 992) / 64);
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 4064) {
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0x90 | (15 - (sample - 2016) / 128);
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (sample < 8160) {
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0x80 | (15 - (sample - 4064) /  256);
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sample = 0x80;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (mask & sample);
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
433