+ return(sample);
+}
+#endif /* AUDIO_UNIX_NATIVE && !AUDIO_STREAMING_DSP */
+
+
+/* THE STUFF ABOVE IS ONLY USED BY THE SOUND SERVER CHILD PROCESS */
+/* ========================================================================= */
+/* THE STUFF BELOW IS ONLY USED BY THE MAIN PROCESS */
+
+#define CHUNK_ID_LEN 4 /* IFF style chunk id length */
+#define WAV_HEADER_SIZE 16 /* size of WAV file header */
+
+static SoundInfo *Load_WAV(char *filename)
+{
+ SoundInfo *snd_info;
+#if defined(AUDIO_UNIX_NATIVE)
+ struct SoundHeader_WAV header;
+#if 0
+ byte sound_header_buffer[WAV_HEADER_SIZE];
+ int i;
+#endif
+ char chunk_name[CHUNK_ID_LEN + 1];
+ int chunk_size;
+ FILE *file;
+#endif
+
+ if (!audio.sound_available)
+ return NULL;
+
+#if 0
+ printf("loading WAV file '%s'\n", filename);
+#endif
+
+ snd_info = checked_calloc(sizeof(SoundInfo));
+
+#if defined(TARGET_SDL)
+
+ if ((snd_info->data_ptr = Mix_LoadWAV(filename)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read sound file '%s'", filename);
+ free(snd_info);
+ return NULL;
+ }
+
+ snd_info->data_len = ((Mix_Chunk *)snd_info->data_ptr)->alen;
+
+#elif defined(TARGET_ALLEGRO)
+
+ if ((snd_info->data_ptr = load_sample(filename)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read sound file '%s'", filename);
+ free(snd_info);
+ return NULL;
+ }
+
+ snd_info->data_len = ((SAMPLE *)snd_info->data_ptr)->len;
+
+#else /* AUDIO_UNIX_NATIVE */
+
+ if ((file = fopen(filename, MODE_READ)) == NULL)
+ {
+ Error(ERR_WARN, "cannot open sound file '%s'", filename);
+ free(snd_info);
+ return NULL;
+ }
+
+ /* read chunk id "RIFF" */
+ getFileChunkLE(file, chunk_name, &chunk_size);
+ if (strcmp(chunk_name, "RIFF") != 0)
+ {
+ Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ /* read "RIFF" type id "WAVE" */
+ getFileChunkLE(file, chunk_name, NULL);
+ if (strcmp(chunk_name, "WAVE") != 0)
+ {
+ Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ while (getFileChunkLE(file, chunk_name, &chunk_size))
+ {
+ if (strcmp(chunk_name, "fmt ") == 0)
+ {
+ if (chunk_size < WAV_HEADER_SIZE)
+ {
+ Error(ERR_WARN, "sound file '%s': chunk 'fmt ' too short", filename);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ header.compression_code = getFile16BitLE(file);
+ header.num_channels = getFile16BitLE(file);
+ header.sample_rate = getFile32BitLE(file);
+ header.bytes_per_second = getFile32BitLE(file);
+ header.block_align = getFile16BitLE(file);
+ header.bits_per_sample = getFile16BitLE(file);
+
+ if (chunk_size > WAV_HEADER_SIZE)
+ ReadUnusedBytesFromFile(file, chunk_size - WAV_HEADER_SIZE);
+
+ if (header.compression_code != 1)
+ {
+ Error(ERR_WARN, "sound file '%s': compression code %d not supported",
+ filename, header.compression_code);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ if (header.num_channels != 1)
+ {
+ Error(ERR_WARN, "sound file '%s': number of %d channels not supported",
+ filename, header.num_channels);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ if (header.bits_per_sample != 8 && header.bits_per_sample != 16)
+ {
+ Error(ERR_WARN, "sound file '%s': %d bits per sample not supported",
+ filename, header.bits_per_sample);
+ fclose(file);
+ free(snd_info);
+ return NULL;
+ }
+
+ /* warn, but accept wrong sample rate (may be only slightly different) */
+ if (header.sample_rate != DEFAULT_AUDIO_SAMPLE_RATE)
+ Error(ERR_WARN, "sound file '%s': wrong sample rate %d instead of %d",
+ filename, header.sample_rate, DEFAULT_AUDIO_SAMPLE_RATE);
+
+#if 0
+ printf("WAV file: '%s'\n", filename);
+ printf(" Compression code: %d'\n", header.compression_code);
+ printf(" Number of channels: %d'\n", header.num_channels);
+ printf(" Sample rate: %ld'\n", header.sample_rate);
+ printf(" Average bytes per second: %ld'\n", header.bytes_per_second);
+ printf(" Block align: %d'\n", header.block_align);
+ printf(" Significant bits per sample: %d'\n", header.bits_per_sample);
+#endif
+ }
+ else if (strcmp(chunk_name, "data") == 0)
+ {
+ snd_info->data_len = chunk_size;
+ snd_info->data_ptr = checked_malloc(snd_info->data_len);
+
+ /* read sound data */
+ if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) !=
+ snd_info->data_len)
+ {
+ Error(ERR_WARN,"cannot read 'data' chunk of sound file '%s'",filename);
+ fclose(file);
+ free(snd_info->data_ptr);
+ free(snd_info);
+ return NULL;
+ }
+
+ /* check for odd number of sample bytes (data chunk is word aligned) */
+ if ((chunk_size % 2) == 1)
+ ReadUnusedBytesFromFile(file, 1);
+ }
+ else /* unknown chunk -- ignore */
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }