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] =
30 SAMPLE_exit, SAMPLE_die, SAMPLE_time, SAMPLE_boom, SAMPLE_tick,
31 SAMPLE_collect, SAMPLE_roll, SAMPLE_push, SAMPLE_dynamite, SAMPLE_press,
32 SAMPLE_door, SAMPLE_dirt, SAMPLE_blank, SAMPLE_android, SAMPLE_ball,
33 SAMPLE_grow, SAMPLE_squash, SAMPLE_crack, SAMPLE_slurp, SAMPLE_drip,
34 SAMPLE_wonder, SAMPLE_wheel, SAMPLE_stone, SAMPLE_spring, SAMPLE_diamond,
35 SAMPLE_nut, SAMPLE_bug, SAMPLE_tank, SAMPLE_eater, SAMPLE_alien,
39 int sound_thread(void)
41 int audio_fd; /* file descriptor of /dev/audio or -1 if not open */
45 unsigned char *audio_buffer; /* actual buffer pumped to /dev/audio */
48 char sound_play[SAMPLE_MAX]; /* if set, we should be playing these sounds */
49 long sound_pos[SAMPLE_MAX]; /* position in the sound */
50 int mix_play[MIXER_MAX]; /* which sounds we have chosen to mix (calculated each time) */
57 audio_format = AUDIO_ULAW; /* defaults for non-OSS /dev/audio */
64 memset(sound_play, 0, sizeof(sound_play)); /* not playing any sounds */
71 /* pick sounds to play, if any */
72 if (sound_play[SAMPLE_exit] || sound_play[SAMPLE_die])
73 sound_play[SAMPLE_boom] = 0; /* no explosions if player goes home */
76 for (i = 0; i < SAMPLE_MAX; i++)
78 if (sound_play[sound_priority[i]])
80 mix_play[mix_count++] = sound_priority[i];
82 if (mix_count == MIXER_MAX)
83 break; /* cant mix too many sounds at once */
87 /* check for incoming messages */
88 if (mix_count || audio_fd != -1)
90 /* dont block if we are playing sounds */
94 FD_SET(sound_pipe[0], &rfds);
96 tv.tv_usec = 0; /* (900000 * fragment_size / sample_rate) */
97 i = select(sound_pipe[0] + 1, &rfds, 0, 0, &tv); /* dont block */
101 Error(ERR_WARN, "select() failed in sound thread");
107 break; /* no messages */
110 /* get a message and start a sound */
111 i = read(sound_pipe[0], &play, sizeof(play));
115 Error(ERR_WARN, "read() failed in sound thread");
122 Error(ERR_WARN, "reading sound failed in sound thread");
127 if (i != sizeof(play))
129 Error(ERR_WARN, "bad message length in sound thread");
134 for (i = 0; i < SAMPLE_MAX; i++)
138 sound_play[i] = 1; /* play this sound */
139 sound_pos[i] = 0; /* start it from the start */
144 /* open the audio device if there are sounds to play */
145 if (mix_count && audio_fd == -1)
147 audio_fd = open(audioname, O_WRONLY);
152 #ifdef OPEN_SOUND_SYSTEM
155 if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &i) == -1)
157 Error(ERR_WARN, "unable to set fragment size in sound thread");
162 if (ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &i) == -1)
164 Error(ERR_WARN, "unable to query audio format in sound thread");
169 /* prefer 8 bit unsigned and fall back on mu-law */
170 audio_format = (i & AFMT_U8) ? AFMT_U8 : AFMT_MU_LAW;
173 if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &i) == -1)
175 Error(ERR_WARN, "unable to set audio format in sound thread");
180 if (i == AFMT_MU_LAW)
182 audio_format = AUDIO_ULAW;
184 else if (i == AFMT_U8)
186 audio_format = AUDIO_U8;
190 Error(ERR_WARN, "audio format required by device not supported");
196 if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &i) == -1)
198 Error(ERR_WARN, "unable to set channels to mono in sound thread");
205 Error(ERR_WARN, "channels required by device not supported");
211 if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &i) == -1)
213 Error(ERR_WARN, "unable to set sampling rate in sound thread");
219 if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &i) == -1)
221 Error(ERR_WARN, "unable to get block size in sound thread");
229 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) == -1)
231 Error(ERR_WARN, "unable to make audio non blocking in sound thread");
236 #endif /* OPEN_SOUND_SYSTEM */
238 audio_buffer = malloc(fragment_size * sizeof(*audio_buffer));
239 if (audio_buffer == 0)
241 Error(ERR_WARN, "unable to malloc audio buffer in sound thread");
246 mix_buffer = malloc(fragment_size * sizeof(*mix_buffer));
249 Error(ERR_WARN, "unable to malloc mixing buffer in sound thread");
255 /* close the audio device if no sounds are playing */
256 if (mix_count == 0 && audio_fd != -1)
266 /* if we are playing sounds and the audio device is open, mix them */
267 if (mix_count && audio_fd != -1)
269 /* prepare mix buffer */
270 memset(mix_buffer, 0, fragment_size * sizeof(*mix_buffer));
272 for (i = 0; i < mix_count; i++)
274 register short *mix_ptr = mix_buffer;
275 register short *sound_ptr =
276 sound_data[mix_play[i]] + sound_pos[mix_play[i]];
277 register long count =
278 sound_length[mix_play[i]] - sound_pos[mix_play[i]];
280 if (count > fragment_size)
281 count = fragment_size;
284 *mix_ptr++ += *sound_ptr++; /* mix the sounds in */
290 for (i = 0; i < fragment_size; i++)
291 audio_buffer[i] = linear_to_ulaw[mix_buffer[i] + 32768];
295 for (i = 0; i < fragment_size; i++)
296 audio_buffer[i] = (mix_buffer[i] + 32768) >> 8;
300 /* advance sound pointers */
301 for (i = 0; i < SAMPLE_MAX; i++)
305 if (sound_pos[i] + fragment_size < sound_length[i])
307 sound_pos[i] += fragment_size;
316 /* send the data to the audio device */
317 i = write(audio_fd, audio_buffer, fragment_size);
320 Error(ERR_WARN, "cannot write to audio device in sound thread");
325 if (i != fragment_size)
327 Error(ERR_WARN, "bad write length to audio device in sound thread");
343 goto loop; /* back to top */
356 int read_sample(char *name, short **data, long *length)
364 unsigned char buffer[24];
367 file = fopen(name, "rb");
370 Error(ERR_WARN, "cannot open file '%s' in sound thread", name);
376 actual = fread(buffer, 1, 24, file);
379 Error(ERR_WARN, "cannot read file '%s' in sound thread", name);
387 Error(ERR_WARN, "premature eof of file '%s' in sound thread", name);
394 temp = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
395 if (temp != 0x2e736e64)
397 Error(ERR_WARN, "unrecognized format of file '%s' in sound thread", name);
404 temp = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
407 Error(ERR_WARN, "bad header length of file '%s' in sound thread", name);
414 for (i = 24; i < actual; i++)
416 /* skip the rest of the header */
423 temp = buffer[8] << 24 | buffer[9] << 16 | buffer[10] << 8 | buffer[11];
427 temp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
430 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
431 "bad encoding type", temp);
437 temp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
440 fprintf(stderr, "%s: \"%s\": %s (%ld != 8000)\n", progname, name,
441 "bad sample rate", temp);
447 temp = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
450 fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
451 "unsupported channels", temp);
456 dataptr = malloc(datalength * sizeof(*dataptr));
459 Error(ERR_WARN, "unable to malloc buffer for file '%s' in sound thread",
466 for (i = 0; i < datalength; i++)
469 if (ch == EOF) break;
470 dataptr[i] = ulaw_to_linear[ch];
477 *length = datalength;
488 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
490 #endif /* AUDIO_UNIX_NATIVE */