File: snd_bsd.c

package info (click to toggle)
darkplaces 0~20180412~beta1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,200 kB
  • sloc: ansic: 176,886; makefile: 485; pascal: 455; perl: 372; objc: 245; sh: 102
file content (243 lines) | stat: -rw-r--r-- 5,335 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "quakedef.h"

#include <sys/param.h>
#include <sys/audioio.h>
#ifndef SUNOS
#	include <sys/endian.h>
#endif
#include <sys/ioctl.h>

#include <fcntl.h>
#ifndef SUNOS
#	include <paths.h>
#endif
#include <unistd.h>

#include "snd_main.h"


static int audio_fd = -1;


/*
====================
SndSys_Init

Create "snd_renderbuffer" with the proper sound format if the call is successful
May return a suggested format if the requested format isn't available
====================
*/
qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
{
	unsigned int i;
	const char *snddev;
	audio_info_t info;

	// Open the audio device
#ifdef _PATH_SOUND
	snddev = _PATH_SOUND;
#elif defined(SUNOS)
	snddev = "/dev/audio";
#else
	snddev = "/dev/sound";
#endif
	audio_fd = open (snddev, O_WRONLY | O_NDELAY | O_NONBLOCK);
	if (audio_fd < 0)
	{
		Con_Printf("Can't open the sound device (%s)\n", snddev);
		return false;
	}

	AUDIO_INITINFO (&info);
#ifdef AUMODE_PLAY	// NetBSD / OpenBSD
	info.mode = AUMODE_PLAY;
#endif
	info.play.sample_rate = requested->speed;
	info.play.channels = requested->channels;
	info.play.precision = requested->width * 8;
	if (requested->width == 1)
#ifdef SUNOS
		info.play.encoding = AUDIO_ENCODING_LINEAR8;
#else
		info.play.encoding = AUDIO_ENCODING_ULINEAR;
#endif
	else
#ifdef SUNOS
		info.play.encoding = AUDIO_ENCODING_LINEAR;
#else
	if (mem_bigendian)
		info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
	else
		info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
#endif

	if (ioctl (audio_fd, AUDIO_SETINFO, &info) != 0)
	{
		Con_Printf("Can't set up the sound device (%s)\n", snddev);
		return false;
	}

	// TODO: check the parameters with AUDIO_GETINFO
	// TODO: check AUDIO_ENCODINGFLAG_EMULATED with AUDIO_GETENC

	snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
	return true;
}


/*
====================
SndSys_Shutdown

Stop the sound card, delete "snd_renderbuffer" and free its other resources
====================
*/
void SndSys_Shutdown (void)
{
	if (audio_fd >= 0)
	{
		close(audio_fd);
		audio_fd = -1;
	}

	if (snd_renderbuffer != NULL)
	{
		Mem_Free(snd_renderbuffer->ring);
		Mem_Free(snd_renderbuffer);
		snd_renderbuffer = NULL;
	}
}


/*
====================
SndSys_Submit

Submit the contents of "snd_renderbuffer" to the sound card
====================
*/
void SndSys_Submit (void)
{
	unsigned int startoffset, factor, limit, nbframes;
	int written;

	if (audio_fd < 0 ||
		snd_renderbuffer->startframe == snd_renderbuffer->endframe)
		return;

	startoffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
	factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
	limit = snd_renderbuffer->maxframes - startoffset;
	nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
	if (nbframes > limit)
	{
		written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], limit * factor);
		if (written < 0)
		{
			Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
			return;
		}

		if (written % factor != 0)
			Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written);

		snd_renderbuffer->startframe += written / factor;

		if ((unsigned int)written < limit * factor)
		{
			Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, limit * factor);
			return;
		}

		nbframes -= limit;
		startoffset = 0;
	}

	written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], nbframes * factor);
	if (written < 0)
	{
		Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
		return;
	}
	snd_renderbuffer->startframe += written / factor;
}


/*
====================
SndSys_GetSoundTime

Returns the number of sample frames consumed since the sound started
====================
*/
unsigned int SndSys_GetSoundTime (void)
{
	audio_info_t info;

	if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0)
	{
		Con_Print("Error: can't get audio info\n");
		SndSys_Shutdown ();
		return 0;
	}

	return info.play.samples;
}


/*
====================
SndSys_LockRenderBuffer

Get the exclusive lock on "snd_renderbuffer"
====================
*/
qboolean SndSys_LockRenderBuffer (void)
{
	// Nothing to do
	return true;
}


/*
====================
SndSys_UnlockRenderBuffer

Release the exclusive lock on "snd_renderbuffer"
====================
*/
void SndSys_UnlockRenderBuffer (void)
{
	// Nothing to do
}

/*
====================
SndSys_SendKeyEvents

Send keyboard events originating from the sound system (e.g. MIDI)
====================
*/
void SndSys_SendKeyEvents(void)
{
	// not supported
}