src/audio/dsp/SDL_dspaudio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 10 Feb 2006 06:48:43 +0000
changeset 1358 c71e05b4dc2e
parent 1341 d02b552e5304
child 1361 19418e4422cb
permissions -rw-r--r--
More header massaging... works great on Windows. ;-)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
     3
    Copyright (C) 1997-2006 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
    13
    Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
    16
    License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1281
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 248
diff changeset
    20
    slouken@libsdl.org
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
    21
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
    22
    Modified in Oct 2004 by Hannu Savolainen 
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
    23
    hannu@opensound.com
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
/* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
1341
d02b552e5304 Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    28
#include <stdio.h>	/* For perror() */
d02b552e5304 Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    29
#include <string.h>	/* For strerror() */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include <errno.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
#include <unistd.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
#include <fcntl.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include <sys/time.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
#include <sys/ioctl.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
#include <sys/stat.h>
94
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    37
#ifdef OSS_USE_SOUNDCARD_H
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    38
/* This is installed on some systems */
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    39
#include <soundcard.h>
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    40
#else
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    41
/* This is recommended by OSS */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
#include <sys/soundcard.h>
94
ae6e6b73333f Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents: 93
diff changeset
    43
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
1358
c71e05b4dc2e More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents: 1341
diff changeset
    45
#include "SDL_timer.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
#include "SDL_audio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
#include "SDL_audiomem.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
#include "SDL_audio_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
#include "SDL_audiodev_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
#include "SDL_dspaudio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
/* The tag name used by DSP audio */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
#define DSP_DRIVER_NAME         "dsp"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    55
/* Open the audio device for playback, and don't block if busy */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
#define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
/* Audio driver functions */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    59
static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    60
static void DSP_WaitAudio(_THIS);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    61
static void DSP_PlayAudio(_THIS);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    62
static Uint8 *DSP_GetAudioBuf(_THIS);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
static void DSP_CloseAudio(_THIS);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
/* Audio driver bootstrap functions */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    66
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    67
static int Audio_Available(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
	int fd;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
	int available;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    71
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
	available = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
	fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
	if ( fd >= 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
		available = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
		close(fd);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
	return(available);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
static void Audio_DeleteDevice(SDL_AudioDevice *device)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
{
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    83
	SDL_free(device->hidden);
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    84
	SDL_free(device);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    85
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    86
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    88
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    89
	SDL_AudioDevice *this;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    91
	/* Initialize all variables that we clean on shutdown */
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    92
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
	if ( this ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    94
		SDL_memset(this, 0, (sizeof *this));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
		this->hidden = (struct SDL_PrivateAudioData *)
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    96
				SDL_malloc((sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
	if ( (this == NULL) || (this->hidden == NULL) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    99
		SDL_OutOfMemory();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   100
		if ( this ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   101
			SDL_free(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
		return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   104
	}
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   105
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   106
	audio_fd = -1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   108
	/* Set the function pointers */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   109
	this->OpenAudio = DSP_OpenAudio;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
	this->WaitAudio = DSP_WaitAudio;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   111
	this->PlayAudio = DSP_PlayAudio;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   112
	this->GetAudioBuf = DSP_GetAudioBuf;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   113
	this->CloseAudio = DSP_CloseAudio;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   114
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
	this->free = Audio_DeleteDevice;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   116
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   117
	return this;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   118
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   119
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   120
AudioBootStrap DSP_bootstrap = {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   121
	DSP_DRIVER_NAME, "OSS /dev/dsp standard audio",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   122
	Audio_Available, Audio_CreateDevice
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   123
};
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   125
/* This function waits until it is possible to write a full sound buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   126
static void DSP_WaitAudio(_THIS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   127
{
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   128
	/* Not needed at all since OSS handles waiting automagically */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   129
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   130
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   131
static void DSP_PlayAudio(_THIS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   132
{
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   133
	if (write(audio_fd, mixbuf, mixlen)==-1)
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   134
	{
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   135
		perror("Audio write");
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   136
		this->enabled = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   137
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   138
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   139
#ifdef DEBUG_AUDIO
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   140
	fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   143
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   144
static Uint8 *DSP_GetAudioBuf(_THIS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   145
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   146
	return(mixbuf);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   148
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   149
static void DSP_CloseAudio(_THIS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   150
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
	if ( mixbuf != NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   152
		SDL_FreeAudioMem(mixbuf);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
		mixbuf = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   154
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   155
	if ( audio_fd >= 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   156
		close(audio_fd);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   157
		audio_fd = -1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   158
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   159
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   160
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   161
static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   162
{
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   163
	char audiodev[1024];
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   164
	int format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   165
	int value;
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   166
	int frag_spec;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   167
	Uint16 test_format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   168
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   169
	/* Open the audio device */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   170
	audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   171
	if ( audio_fd < 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   172
		SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   173
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   174
	}
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   175
	mixbuf = NULL;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   176
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   177
	/* Make the file descriptor use blocking writes with fcntl() */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   178
	{ long flags;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   179
		flags = fcntl(audio_fd, F_GETFL);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   180
		flags &= ~O_NONBLOCK;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   181
		if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   182
			SDL_SetError("Couldn't set audio blocking mode");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   183
			DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   184
			return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   185
		}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   186
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   187
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   188
	/* Get a list of supported hardware formats */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   189
	if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   190
		perror("SNDCTL_DSP_GETFMTS");
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   191
		SDL_SetError("Couldn't get audio format list");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   192
		DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   193
		return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   194
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   195
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   196
	/* Try for a closest match on audio format */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   197
	format = 0;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   198
	for ( test_format = SDL_FirstAudioFormat(spec->format);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   199
						! format && test_format; ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   200
#ifdef DEBUG_AUDIO
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   201
		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   202
#endif
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   203
		switch ( test_format ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   204
			case AUDIO_U8:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   205
				if ( value & AFMT_U8 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   206
					format = AFMT_U8;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   207
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   208
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   209
			case AUDIO_S16LSB:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   210
				if ( value & AFMT_S16_LE ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   211
					format = AFMT_S16_LE;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   212
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   213
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   214
			case AUDIO_S16MSB:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   215
				if ( value & AFMT_S16_BE ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   216
					format = AFMT_S16_BE;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   217
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   218
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   219
#if 0
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   220
/*
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   221
 * These formats are not used by any real life systems so they are not 
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   222
 * needed here.
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   223
 */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   224
			case AUDIO_S8:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   225
				if ( value & AFMT_S8 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   226
					format = AFMT_S8;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   227
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   228
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   229
			case AUDIO_U16LSB:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   230
				if ( value & AFMT_U16_LE ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   231
					format = AFMT_U16_LE;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   232
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   233
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   234
			case AUDIO_U16MSB:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   235
				if ( value & AFMT_U16_BE ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   236
					format = AFMT_U16_BE;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   237
				}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   238
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   239
#endif
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   240
			default:
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   241
				format = 0;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   242
				break;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   243
		}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   244
		if ( ! format ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   245
			test_format = SDL_NextAudioFormat();
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   246
		}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   247
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   248
	if ( format == 0 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   249
		SDL_SetError("Couldn't find any hardware audio formats");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   250
		DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   251
		return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   252
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   253
	spec->format = test_format;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   254
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   255
	/* Set the audio format */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   256
	value = format;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   257
	if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   258
						(value != format) ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   259
		perror("SNDCTL_DSP_SETFMT");
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   260
		SDL_SetError("Couldn't set audio format");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   261
		DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   262
		return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   263
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   264
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   265
	/* Set the number of channels of output */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   266
	value = spec->channels;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   267
	if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   268
		perror("SNDCTL_DSP_CHANNELS");
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   269
		SDL_SetError("Cannot set the number of channels");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   270
		DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   271
		return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   272
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   273
	spec->channels = value;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   274
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   275
	/* Set the DSP frequency */
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   276
	value = spec->freq;
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   277
	if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) {
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   278
		perror("SNDCTL_DSP_SPEED");
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   279
		SDL_SetError("Couldn't set audio frequency");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   280
		DSP_CloseAudio(this);
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   281
		return(-1);
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   282
	}
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   283
	spec->freq = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   284
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
	/* Calculate the final parameters for this audio specification */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
	SDL_CalculateAudioSpec(spec);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   288
	/* Determine the power of two of the fragment size */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   290
	if ( (0x01<<frag_spec) != spec->size ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
		SDL_SetError("Fragment size must be a power of two");
1037
c5dedfdb4e42 Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents: 968
diff changeset
   292
		DSP_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   293
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   294
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   295
	frag_spec |= 0x00020000;	/* two fragments, for low latency */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   296
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   297
	/* Set the audio buffering parameters */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   298
#ifdef DEBUG_AUDIO
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   299
	fprintf(stderr, "Requesting %d fragments of size %d\n",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   300
		(frag_spec >> 16), 1<<(frag_spec&0xFFFF));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   301
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   302
	if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
968
4675910b0b7b Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents: 960
diff changeset
   303
		perror("SNDCTL_DSP_SETFRAGMENT");
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   304
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
#ifdef DEBUG_AUDIO
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
	{ audio_buf_info info;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   307
	  ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   308
	  fprintf(stderr, "fragments = %d\n", info.fragments);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   309
	  fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   310
	  fprintf(stderr, "fragsize = %d\n", info.fragsize);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   311
	  fprintf(stderr, "bytes = %d\n", info.bytes);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   312
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   313
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   315
	/* Allocate mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   316
	mixlen = spec->size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   317
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   318
	if ( mixbuf == NULL ) {
1281
644b39bf7253 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 1037
diff changeset
   319
		DSP_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   320
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   321
	}
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   322
	SDL_memset(mixbuf, spec->silence, spec->size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   323
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   324
	/* Get the parent process id (we're the parent of the audio thread) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   325
	parent = getpid();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   326
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   327
	/* We're ready to rock and roll. :-) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   328
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   329
}