1 /* 2000-08-10T17:39:15Z
3 * handle sounds in emerald mine
9 #if defined(AUDIO_UNIX_NATIVE)
11 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
14 #include <sys/ioctl.h>
15 #include <sys/soundcard.h>
20 #include <soundcard.h>
26 static char audioname[] = "/dev/audio";
28 static const int sound_priority[SAMPLE_MAX] =
67 int sound_thread(void)
69 int audio_fd; /* file descriptor of /dev/audio or -1 if not open */
73 unsigned char *audio_buffer; /* actual buffer pumped to /dev/audio */
76 char sound_play[SAMPLE_MAX]; /* if set, we should be playing these sounds */
77 long sound_pos[SAMPLE_MAX]; /* position in the sound */
78 int mix_play[MIXER_MAX]; /* which sounds we have chosen to mix (calculated each time) */
85 audio_format = AUDIO_ULAW; /* defaults for non-OSS /dev/audio */
92 memset(sound_play, 0, sizeof(sound_play)); /* not playing any sounds */
99 /* pick sounds to play, if any */
100 if (sound_play[SAMPLE_exit] || sound_play[SAMPLE_die])
101 sound_play[SAMPLE_boom] = 0; /* no explosions if player goes home */
104 for (i = 0; i < SAMPLE_MAX; i++)
106 if (sound_play[sound_priority[i]])
108 mix_play[mix_count++] = sound_priority[i];
110 if (mix_count == MIXER_MAX)
111 break; /* cant mix too many sounds at once */
115 /* check for incoming messages */
116 if (mix_count || audio_fd != -1)
118 /* dont block if we are playing sounds */
122 FD_SET(sound_pipe[0], &rfds);
124 tv.tv_usec = 0; /* (900000 * fragment_size / sample_rate) */
125 i = select(sound_pipe[0] + 1, &rfds, 0, 0, &tv); /* dont block */
129 Error(ERR_WARN, "select() failed in sound thread");
135 break; /* no messages */
138 /* get a message and start a sound */
139 i = read(sound_pipe[0], &play, sizeof(play));
143 Error(ERR_WARN, "read() failed in sound thread");
150 Error(ERR_WARN, "reading sound failed in sound thread");
155 if (i != sizeof(play))
157 Error(ERR_WARN, "bad message length in sound thread");
162 for (i = 0; i < SAMPLE_MAX; i++)
166 sound_play[i] = 1; /* play this sound */
167 sound_pos[i] = 0; /* start it from the start */
172 /* open the audio device if there are sounds to play */
173 if (mix_count && audio_fd == -1)
175 audio_fd = open(audioname, O_WRONLY);
180 #ifdef OPEN_SOUND_SYSTEM
183 if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &i) == -1)
185 Error(ERR_WARN, "unable to set fragment size in sound thread");
190 if (ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &i) == -1)
192 Error(ERR_WARN, "unable to query audio format in sound thread");
197 /* prefer 8 bit unsigned and fall back on mu-law */
198 audio_format = (i & AFMT_U8) ? AFMT_U8 : AFMT_MU_LAW;
201 if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &i) == -1)
203 Error(ERR_WARN, "unable to set audio format in sound thread");
208 if (i == AFMT_MU_LAW)
210 audio_format = AUDIO_ULAW;
212 else if (i == AFMT_U8)
214 audio_format = AUDIO_U8;
218 Error(ERR_WARN, "audio format required by device not supported");
224 if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &i) == -1)
226 Error(ERR_WARN, "unable to set channels to mono in sound thread");
233 Error(ERR_WARN, "channels required by device not supported");
239 if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &i) == -1)
241 Error(ERR_WARN, "unable to set sampling rate in sound thread");
247 if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &i) == -1)
249 Error(ERR_WARN, "unable to get block size in sound thread");
257 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) == -1)
259 Error(ERR_WARN, "unable to make audio non blocking in sound thread");
264 #endif /* OPEN_SOUND_SYSTEM */
266 audio_buffer = malloc(fragment_size * sizeof(*audio_buffer));
267 if (audio_buffer == 0)
269 Error(ERR_WARN, "unable to malloc audio buffer in sound thread");
274 mix_buffer = malloc(fragment_size * sizeof(*mix_buffer));
277 Error(ERR_WARN, "unable to malloc mixing buffer in sound thread");
283 /* close the audio device if no sounds are playing */
284 if (mix_count == 0 && audio_fd != -1)
294 /* if we are playing sounds and the audio device is open, mix them */
295 if (mix_count && audio_fd != -1)
297 /* prepare mix buffer */
298 memset(mix_buffer, 0, fragment_size * sizeof(*mix_buffer));
300 for (i = 0; i < mix_count; i++)
302 register short *mix_ptr = mix_buffer;
303 register short *sound_ptr =
304 sound_data[mix_play[i]] + sound_pos[mix_play[i]];
305 register long count =
306 sound_length[mix_play[i]] - sound_pos[mix_play[i]];
308 if (count > fragment_size)
309 count = fragment_size;
312 *mix_ptr++ += *sound_ptr++; /* mix the sounds in */
318 for (i = 0; i < fragment_size; i++)
319 audio_buffer[i] = linear_to_ulaw[mix_buffer[i] + 32768];
323 for (i = 0; i < fragment_size; i++)
324 audio_buffer[i] = (mix_buffer[i] + 32768) >> 8;
328 /* advance sound pointers */
329 for (i = 0; i < SAMPLE_MAX; i++)
333 if (sound_pos[i] + fragment_size < sound_length[i])
335 sound_pos[i] += fragment_size;
344 /* send the data to the audio device */
345 i = write(audio_fd, audio_buffer, fragment_size);
348 Error(ERR_WARN, "cannot write to audio device in sound thread");
353 if (i != fragment_size)
355 Error(ERR_WARN, "bad write length to audio device in sound thread");
371 goto loop; /* back to top */
384 int read_sample(char *name, short **data, long *length)
392 unsigned char buffer[24];
395 file = fopen(name, "rb");
398 Error(ERR_WARN, "cannot open file '%s' in sound thread", name);
404 actual = fread(buffer, 1, 24, file);
407 Error(ERR_WARN, "cannot read file '%s' in sound thread", name);
415 Error(ERR_WARN, "premature eof of file '%s' in sound thread", name);
422 temp = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
423 if (temp != 0x2e736e64)
425 Error(ERR_WARN, "unrecognized format of file '%s' in sound thread", name);
432 temp = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
435 Error(ERR_WARN, "bad header length of file '%s' in sound thread", name);
442 for (i = 24; i < actual; i++)
444 /* skip the rest of the header */
451 temp = buffer[8] << 24 | buffer[9] << 16 | buffer[10] << 8 | buffer[11];
455 temp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
458 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
459 "bad encoding type", temp);
465 temp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
468 fprintf(stderr, "%s: \"%s\": %s (%ld != 8000)\n", progname, name,
469 "bad sample rate", temp);
475 temp = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
478 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
479 "unsupported channels", temp);
484 dataptr = malloc(datalength * sizeof(*dataptr));
487 Error(ERR_WARN, "unable to malloc buffer for file '%s' in sound thread",
494 for (i = 0; i < datalength; i++)
497 if (ch == EOF) break;
498 dataptr[i] = ulaw_to_linear[ch];
505 *length = datalength;
516 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
518 #endif /* AUDIO_UNIX_NATIVE */