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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/* snd.c -- low-level sound I/O
*
* Roger Dannenberg
* 21 Jun 1997
*
* based on sndheader.c:
*
* Jim Zelenka, CMU/ITC, 9 Jun 1992 (rewritten from my old sources)
* Roger Dannenberg, CMU, Mar 1993 (extensive changes and additions)
*
* and on sndread.c & sndwrite.c:
*
* Roger Dannenberg
*/
/* Standard includes */
#include <string.h>
#include <stdio.h>
#include "snd.h"
#include "sndfileio.h"
#include "sndheader.h"
static int snd_initialized = FALSE;
typedef struct {
char *interf;
char *device;
snd_fns_type dictionary;
} descriptor_node, *descriptor_type;
static int descriptor_index = 0;
#define snd_descriptor_max 32
static descriptor_node descriptors[snd_descriptor_max];
/* snd_add_device -- describe interface/device pair to library
*
* This is called at intialization time, once for each
* interface (e.g. DirectSound) and device (e.g. SoundBlaster 1)
* The strings are retained but NOT COPIED, so do not destroy them!
*/
void snd_add_device(char *interf, char *device, snd_fns_type dictionary)
{
if (descriptor_index >= snd_descriptor_max) {
snd_fail("snd_add_device: snd_descriptor_max exceeded");
}
descriptors[descriptor_index].interf = interf;
descriptors[descriptor_index].device = device;
descriptors[descriptor_index].dictionary = dictionary;
descriptor_index++;
}
int snd_device(int n, char *interf, char *device)
{
if (!snd_initialized) {
snd_init();
snd_initialized = TRUE;
}
if (n >= 0 && n < descriptor_index) {
strcpy(interf, descriptors[n].interf);
strcpy(device, descriptors[n].device);
return SND_SUCCESS;
}
return !SND_SUCCESS;
}
/* failure_fn -- "noop" function pointer */
/**/
static int failure_fn(snd_type snd)
{
return !SND_SUCCESS;
}
/* success_fn -- "noop" function pointer */
/**/
static int success_fn(snd_type snd)
{
return SND_SUCCESS;
}
static long file_poll(snd_type snd)
{
return MAX_FILE_BUF_LEN;
}
static long mem_poll(snd_type snd)
{
if (snd->write_flag == SND_READ) {
return (snd->u.mem.buffer_len - snd->u.mem.buffer_pos);
} else {
return (snd->u.mem.buffer_max - snd->u.mem.buffer_len);
}
}
static long none_poll(snd_type snd)
{
return 0;
}
#if !defined(WIN32) && !defined(IRIX) && !defined(SGI)
static void _swab(char *to, char *from, long length)
{
short *to16 = (short *) to;
short *from16 = (short *) from;
int i = 1;
while (i < length) {
short data = *from16++;
*to16++ = (data << 8) | ((data >> 8) & 0xFF);
i += 2;
}
}
#endif
static void change_byte_order(snd_type snd, void *buffer, long length)
{
int bytes = (snd->format.bits + 7) >> 3;
switch (bytes) {
case 1:
break;
case 2:
_swab((char *) buffer, (char *) buffer, length);
break;
case 4: {
long *buff = (long *) buffer;
while (length > 0) {
long data = *buff;
*buff++ = ((data >> 24) & 0xFF) | ((data >> 8) & 0xFF00) |
((data << 8) & 0xFF0000) | (data << 24);
length -= 4;
}
break;
}
default: {
char msg[100];
sprintf(msg, "Cannot handle %d-byte samples", bytes);
snd_fail(msg);
break;
}
}
}
static long file_read(snd_type snd, void *buffer, long length)
{
long togo, cnt;
togo = snd->u.file.end_offset - snd->u.file.current_offset;
if (length > togo) length = togo;
cnt = snd_file_read(snd->u.file.file, (char *) buffer, length);
snd->u.file.current_offset += cnt;
if (snd->u.file.swap) change_byte_order(snd, buffer, cnt);
return cnt;
}
static long mem_read(snd_type snd, void *buffer, long length)
{
/* if there are fewer than length bytes, reduce length: */
if ((snd->u.mem.buffer_len - snd->u.mem.buffer_pos) < length) {
length = snd->u.mem.buffer_len - snd->u.mem.buffer_pos;
}
memcpy(buffer, snd->u.mem.buffer + snd->u.mem.buffer_pos,
length);
snd->u.mem.buffer_pos += length;
return length;
}
static long none_read(snd_type snd, void *buffer, long length)
{
return 0;
}
static long file_write(snd_type snd, void *buffer, long length)
{
long cnt;
if (snd->u.file.swap) change_byte_order(snd, buffer, length);
cnt = snd_file_write(snd->u.file.file, (char *) buffer, length);
snd->u.file.end_offset += cnt; /* keep track of how much data */
return cnt;
}
static long mem_write(snd_type snd, void *buffer, long length)
{
/* if there are fewer than length bytes, reduce length: */
if ((snd->u.mem.buffer_max - snd->u.mem.buffer_len) < length) {
length = snd->u.mem.buffer_max - snd->u.mem.buffer_len;
}
memcpy(snd->u.mem.buffer + snd->u.mem.buffer_len, buffer, length);
snd->u.mem.buffer_len += length;
return length;
}
static long none_write(snd_type snd, void *buffer, long length)
{
return 0;
}
static int mem_open(snd_type snd, long *flags)
{
return SND_SUCCESS;
}
static int none_open(snd_type snd, long *flags)
{
return !SND_SUCCESS;
}
static int file_close(snd_type snd)
{
if (snd->write_flag == SND_WRITE) {
write_sndheader_finish(snd);
}
return snd_file_close(snd->u.file.file);
}
#define mem_close success_fn
#define none_close failure_fn
#define file_reset success_fn
static int mem_reset(snd_type snd)
{
snd->u.mem.buffer_pos = 0;
if (snd->write_flag != SND_READ) {
snd->u.mem.buffer_len = 0;
}
return SND_SUCCESS;
}
#define none_reset failure_fn
#define file_flush success_fn
#define mem_flush success_fn
#define none_flush failure_fn
char *snd_mode_string[] = { "ADPCM", "PCM", "ULAW", "ALAW", "Float", "UPCM", "Unknown" };
char *snd_mode_to_string(long mode)
{
if (mode < 0 || mode >= SND_NUM_MODES) return "InvalidData";
else return snd_mode_string[mode];
}
static snd_fns_node mem_dictionary = {
mem_poll, mem_read, mem_write, mem_open,
mem_close, mem_reset, mem_flush };
static snd_fns_node file_dictionary = {
file_poll, file_read, file_write, snd_open_file,
file_close, file_reset, file_flush };
snd_fns_node snd_none_dictionary = {
none_poll, none_read, none_write, none_open,
none_close, none_reset, none_flush };
long snd_poll(snd_type snd)
{
return (*snd->dictionary->poll)(snd);
}
/* snd_read -- read up to length bytes from source into buffer */
/*
* returns number of bytes actually read
*/
long snd_read(snd_type snd, void *buffer, long length)
{
int bpf = snd_bytes_per_frame(snd);
return ((*snd->dictionary->read)(snd, buffer, length * bpf)) / bpf;
}
long snd_write(snd_type snd, void *buffer, long length)
{
int bpf = snd_bytes_per_frame(snd);
return (*snd->dictionary->write)(snd, buffer, length * bpf) / bpf;
}
int snd_open(snd_type snd, long *flags)
{
if (!snd_initialized) {
snd_init();
snd_initialized = TRUE;
}
*flags = 0;
if (snd->device == SND_DEVICE_FILE) {
snd->dictionary = &file_dictionary;
} else if (snd->device == SND_DEVICE_AUDIO) {
int i;
for (i = 0; i < descriptor_index; i++) {
/* take the first descriptor that matches both interface
and device, where empty string matches anything: */
if ((snd->u.audio.interfacename[0] == 0 ||
strcmp(snd->u.audio.interfacename,
descriptors[i].interf) == 0)
/*&& commented out by dmazzoni - don't want to compare dev
(snd->u.audio.devicename[0] == 0 ||
strcmp(snd->u.audio.devicename,
descriptors[i].device) == 0)*/
) {
snd->dictionary = descriptors[i].dictionary;
break;
}
}
if (i >= descriptor_index) {
return !SND_SUCCESS;
}
} else if (snd->device == SND_DEVICE_MEM) {
snd->dictionary = &mem_dictionary;
}
return (*snd->dictionary->open)(snd, flags);
}
int snd_close(snd_type snd)
{
return (*snd->dictionary->close)(snd);
}
int snd_reset(snd_type snd)
{
return (*snd->dictionary->reset)(snd);
}
/* snd_flush -- allows client to finish audio output before closing */
/**/
int snd_flush(snd_type snd)
{
if (snd->write_flag != SND_READ) {
return (*snd->dictionary->flush)(snd);
}
return SND_SUCCESS;
}
|