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>
23 static char audioname[] = "/dev/audio";
25 static const int sound_priority[SAMPLE_MAX] =
64 int sound_thread(void)
66 int audio_fd; /* file descriptor of /dev/audio or -1 if not open */
70 unsigned char *audio_buffer; /* actual buffer pumped to /dev/audio */
73 char sound_play[SAMPLE_MAX]; /* if set, we should be playing these sounds */
74 long sound_pos[SAMPLE_MAX]; /* position in the sound */
75 int mix_play[MIXER_MAX]; /* which sounds we have chosen to mix (calculated each time) */
82 audio_format = AUDIO_ULAW; /* defaults for non-OSS /dev/audio */
89 memset(sound_play, 0, sizeof(sound_play)); /* not playing any sounds */
96 /* pick sounds to play, if any */
97 if (sound_play[SAMPLE_exit_open] ||
98 sound_play[SAMPLE_exit_leave] ||
99 sound_play[SAMPLE_die])
100 sound_play[SAMPLE_boom] = 0; /* no explosions if player goes home */
103 for (i = 0; i < SAMPLE_MAX; i++)
105 if (sound_play[sound_priority[i]])
107 mix_play[mix_count++] = sound_priority[i];
109 if (mix_count == MIXER_MAX)
110 break; /* cant mix too many sounds at once */
114 /* check for incoming messages */
115 if (mix_count || audio_fd != -1)
117 /* dont block if we are playing sounds */
121 FD_SET(sound_pipe[0], &rfds);
123 tv.tv_usec = 0; /* (900000 * fragment_size / sample_rate) */
124 i = select(sound_pipe[0] + 1, &rfds, 0, 0, &tv); /* dont block */
128 Error(ERR_WARN, "select() failed in sound thread");
134 break; /* no messages */
137 /* get a message and start a sound */
138 i = read(sound_pipe[0], &play, sizeof(play));
142 Error(ERR_WARN, "read() failed in sound thread");
149 Error(ERR_WARN, "reading sound failed in sound thread");
154 if (i != sizeof(play))
156 Error(ERR_WARN, "bad message length in sound thread");
161 for (i = 0; i < SAMPLE_MAX; i++)
165 sound_play[i] = 1; /* play this sound */
166 sound_pos[i] = 0; /* start it from the start */
171 /* open the audio device if there are sounds to play */
172 if (mix_count && audio_fd == -1)
174 audio_fd = open(audioname, O_WRONLY);
179 #ifdef OPEN_SOUND_SYSTEM
182 if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &i) == -1)
184 Error(ERR_WARN, "unable to set fragment size in sound thread");
189 if (ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &i) == -1)
191 Error(ERR_WARN, "unable to query audio format in sound thread");
196 /* prefer 8 bit unsigned and fall back on mu-law */
197 audio_format = (i & AFMT_U8) ? AFMT_U8 : AFMT_MU_LAW;
200 if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &i) == -1)
202 Error(ERR_WARN, "unable to set audio format in sound thread");
207 if (i == AFMT_MU_LAW)
209 audio_format = AUDIO_ULAW;
211 else if (i == AFMT_U8)
213 audio_format = AUDIO_U8;
217 Error(ERR_WARN, "audio format required by device not supported");
223 if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &i) == -1)
225 Error(ERR_WARN, "unable to set channels to mono in sound thread");
232 Error(ERR_WARN, "channels required by device not supported");
238 if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &i) == -1)
240 Error(ERR_WARN, "unable to set sampling rate in sound thread");
246 if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &i) == -1)
248 Error(ERR_WARN, "unable to get block size in sound thread");
256 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) == -1)
258 Error(ERR_WARN, "unable to make audio non blocking in sound thread");
263 #endif /* OPEN_SOUND_SYSTEM */
265 audio_buffer = malloc(fragment_size * sizeof(*audio_buffer));
266 if (audio_buffer == 0)
268 Error(ERR_WARN, "unable to malloc audio buffer in sound thread");
273 mix_buffer = malloc(fragment_size * sizeof(*mix_buffer));
276 Error(ERR_WARN, "unable to malloc mixing buffer in sound thread");
282 /* close the audio device if no sounds are playing */
283 if (mix_count == 0 && audio_fd != -1)
293 /* if we are playing sounds and the audio device is open, mix them */
294 if (mix_count && audio_fd != -1)
296 /* prepare mix buffer */
297 memset(mix_buffer, 0, fragment_size * sizeof(*mix_buffer));
299 for (i = 0; i < mix_count; i++)
301 register short *mix_ptr = mix_buffer;
302 register short *sound_ptr =
303 sound_data[mix_play[i]] + sound_pos[mix_play[i]];
304 register long count =
305 sound_length[mix_play[i]] - sound_pos[mix_play[i]];
307 if (count > fragment_size)
308 count = fragment_size;
311 *mix_ptr++ += *sound_ptr++; /* mix the sounds in */
317 for (i = 0; i < fragment_size; i++)
318 audio_buffer[i] = linear_to_ulaw[mix_buffer[i] + 32768];
322 for (i = 0; i < fragment_size; i++)
323 audio_buffer[i] = (mix_buffer[i] + 32768) >> 8;
327 /* advance sound pointers */
328 for (i = 0; i < SAMPLE_MAX; i++)
332 if (sound_pos[i] + fragment_size < sound_length[i])
334 sound_pos[i] += fragment_size;
343 /* send the data to the audio device */
344 i = write(audio_fd, audio_buffer, fragment_size);
347 Error(ERR_WARN, "cannot write to audio device in sound thread");
352 if (i != fragment_size)
354 Error(ERR_WARN, "bad write length to audio device in sound thread");
370 goto loop; /* back to top */
383 int read_sample(char *name, short **data, long *length)
391 unsigned char buffer[24];
394 file = fopen(name, "rb");
397 Error(ERR_WARN, "cannot open file '%s' in sound thread", name);
403 actual = fread(buffer, 1, 24, file);
406 Error(ERR_WARN, "cannot read file '%s' in sound thread", name);
414 Error(ERR_WARN, "premature eof of file '%s' in sound thread", name);
421 temp = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
422 if (temp != 0x2e736e64)
424 Error(ERR_WARN, "unrecognized format of file '%s' in sound thread", name);
431 temp = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
434 Error(ERR_WARN, "bad header length of file '%s' in sound thread", name);
441 for (i = 24; i < actual; i++)
443 /* skip the rest of the header */
450 temp = buffer[8] << 24 | buffer[9] << 16 | buffer[10] << 8 | buffer[11];
454 temp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
457 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
458 "bad encoding type", temp);
464 temp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
467 fprintf(stderr, "%s: \"%s\": %s (%ld != 8000)\n", progname, name,
468 "bad sample rate", temp);
474 temp = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
477 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
478 "unsupported channels", temp);
483 dataptr = malloc(datalength * sizeof(*dataptr));
486 Error(ERR_WARN, "unable to malloc buffer for file '%s' in sound thread",
493 for (i = 0; i < datalength; i++)
496 if (ch == EOF) break;
497 dataptr[i] = ulaw_to_linear[ch];
504 *length = datalength;
515 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
517 #endif /* AUDIO_UNIX_NATIVE */