ref: 5f62d4213cb279eb4ecb20b2f7a4120d02746c76
dir: /i_sdlsound.c/
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005-2014 Simon Howard
// Copyright(C) 2008 David Flater
// Copyright(C) 2021 Andrew Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// DESCRIPTION:
// System interface for sound.
//
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_mixer.h"
#include "deh_str.h"
#include "i_sound.h"
#include "i_system.h"
#include "m_argv.h"
#include "m_misc.h"
#include "doomtype.h"
#define NUM_CHANNELS 16
struct allocated_sound_s
{
sfxinfo_t *sfxinfo;
Mix_Chunk chunk;
int use_count;
allocated_sound_t *prev, *next;
};
static boolean sound_initialized = false;
static allocated_sound_t *channels_playing[NUM_CHANNELS];
static int mixer_freq;
static Uint16 mixer_format;
static int mixer_channels;
// Doubly-linked list of allocated sounds.
// When a sound is played, it is moved to the head, so that the oldest
// sounds not used recently are at the tail.
static allocated_sound_t *allocated_sounds_head = NULL;
static allocated_sound_t *allocated_sounds_tail = NULL;
static int allocated_sounds_size = 0;
// Hook a sound into the linked list at the head.
static void AllocatedSoundLink(allocated_sound_t *snd)
{
snd->prev = NULL;
snd->next = allocated_sounds_head;
allocated_sounds_head = snd;
if (allocated_sounds_tail == NULL)
{
allocated_sounds_tail = snd;
}
else
{
snd->next->prev = snd;
}
}
// Unlink a sound from the linked list.
static void AllocatedSoundUnlink(allocated_sound_t *snd)
{
if (snd->prev == NULL)
{
allocated_sounds_head = snd->next;
}
else
{
snd->prev->next = snd->next;
}
if (snd->next == NULL)
{
allocated_sounds_tail = snd->prev;
}
else
{
snd->next->prev = snd->prev;
}
}
static void FreeAllocatedSound(allocated_sound_t *snd)
{
// Unlink from linked list.
AllocatedSoundUnlink(snd);
// Keep track of the amount of allocated sound data:
allocated_sounds_size -= snd->chunk.alen;
free(snd);
}
// Search from the tail backwards along the allocated sounds list, find
// and free a sound that is not in use, to free up memory. Return true
// for success.
static boolean FindAndFreeSound(void)
{
allocated_sound_t *snd;
snd = allocated_sounds_tail;
while (snd != NULL)
{
if (snd->use_count == 0)
{
FreeAllocatedSound(snd);
return true;
}
snd = snd->prev;
}
// No available sounds to free...
return false;
}
// Enforce SFX cache size limit. We are just about to allocate "len"
// bytes on the heap for a new sound effect, so free up some space
// so that we keep allocated_sounds_size < snd_cachesize
static void ReserveCacheSpace(size_t len)
{
if (snd_cachesize <= 0)
{
return;
}
// Keep freeing sound effects that aren't currently being played,
// until there is enough space for the new sound.
while (allocated_sounds_size + len > snd_cachesize)
{
// Free a sound. If there is nothing more to free, stop.
if (!FindAndFreeSound())
{
break;
}
}
}
// Allocate a block for a new sound effect.
static allocated_sound_t *AllocateSound(sfxinfo_t *sfxinfo, size_t len)
{
allocated_sound_t *snd;
// Keep allocated sounds within the cache size.
ReserveCacheSpace(len);
// Allocate the sound structure and data. The data will immediately
// follow the structure, which acts as a header.
do
{
snd = malloc(sizeof(allocated_sound_t) + len);
// Out of memory? Try to free an old sound, then loop round
// and try again.
if (snd == NULL && !FindAndFreeSound())
{
return NULL;
}
} while (snd == NULL);
// Skip past the chunk structure for the audio buffer
snd->chunk.abuf = (byte *) (snd + 1);
snd->chunk.alen = len;
snd->chunk.allocated = 1;
snd->chunk.volume = MIX_MAX_VOLUME;
snd->sfxinfo = sfxinfo;
snd->use_count = 0;
// Keep track of how much memory all these cached sounds are using...
allocated_sounds_size += len;
AllocatedSoundLink(snd);
return snd;
}
// Lock a sound, to indicate that it may not be freed.
static void LockAllocatedSound(allocated_sound_t *snd)
{
// Increase use count, to stop the sound being freed.
++snd->use_count;
//printf("++ %s: Use count=%i\n", snd->sfxinfo->name, snd->use_count);
// When we use a sound, re-link it into the list at the head, so
// that the oldest sounds fall to the end of the list for freeing.
AllocatedSoundUnlink(snd);
AllocatedSoundLink(snd);
}
// Unlock a sound to indicate that it may now be freed.
static void UnlockAllocatedSound(allocated_sound_t *snd)
{
if (snd->use_count <= 0)
{
I_Error("Sound effect released more times than it was locked...");
}
--snd->use_count;
//printf("-- %s: Use count=%i\n", snd->sfxinfo->name, snd->use_count);
}
// Search through the list of allocated sounds and return the one that matches
// the supplied sfxinfo entry.
allocated_sound_t * I_FindAllocatedSound(sfxinfo_t *sfxinfo)
{
if (!sound_initialized)
{
return NULL;
}
allocated_sound_t * p = allocated_sounds_head;
while (p != NULL)
{
if (p->sfxinfo == sfxinfo)
{
return p;
}
p = p->next;
}
return NULL;
}
// When a sound stops, check if it is still playing. If it is not,
// we can mark the sound data as CACHE to be freed back for other
// means.
static void ReleaseSoundOnChannel(int channel)
{
allocated_sound_t *snd = channels_playing[channel];
Mix_HaltChannel(channel);
if (snd != NULL)
{
channels_playing[channel] = NULL;
UnlockAllocatedSound(snd);
}
}
static boolean ConvertibleRatio(int freq1, int freq2)
{
int ratio;
if (freq1 > freq2)
{
return ConvertibleRatio(freq2, freq1);
}
else if ((freq2 % freq1) != 0)
{
// Not in a direct ratio
return false;
}
else
{
// Check the ratio is a power of 2
ratio = freq2 / freq1;
while ((ratio & 1) == 0)
{
ratio = ratio >> 1;
}
return ratio == 1;
}
}
// Generic sound expansion function for any sample rate.
// Returns number of clipped samples (always 0).
static allocated_sound_t * ExpandSoundData(sfxinfo_t *sfxinfo, byte *data,
int samplerate, int length)
{
SDL_AudioCVT convertor;
allocated_sound_t *snd;
Mix_Chunk *chunk;
uint32_t expanded_length;
// Calculate the length of the expanded version of the sample.
expanded_length = (uint32_t) ((((uint64_t) length) * mixer_freq) / samplerate);
// Double up twice: 8 -> 16 bit and mono -> stereo
expanded_length *= 4;
// Allocate a chunk in which to expand the sound
snd = AllocateSound(sfxinfo, expanded_length);
if (snd == NULL)
{
return NULL;
}
chunk = &snd->chunk;
// If we can, use the standard / optimized SDL conversion routines.
if (samplerate <= mixer_freq
&& ConvertibleRatio(samplerate, mixer_freq)
&& SDL_BuildAudioCVT(&convertor,
AUDIO_U8, 1, samplerate,
mixer_format, mixer_channels, mixer_freq))
{
convertor.len = length;
convertor.buf = malloc(convertor.len * convertor.len_mult);
assert(convertor.buf != NULL);
memcpy(convertor.buf, data, length);
SDL_ConvertAudio(&convertor);
memcpy(chunk->abuf, convertor.buf, chunk->alen);
free(convertor.buf);
}
else
{
Sint16 *expanded = (Sint16 *) chunk->abuf;
// Generic expansion if conversion does not work:
//
// SDL's audio conversion only works for rate conversions that are
// powers of 2; if the two formats are not in a direct power of 2
// ratio, do this naive conversion instead.
// number of samples in the converted sound
int expanded_length = ((uint64_t) length * mixer_freq) / samplerate;
int expand_ratio = (length << 8) / expanded_length;
int i;
for (i=0; i<expanded_length; ++i)
{
Sint16 sample;
int src;
src = (i * expand_ratio) >> 8;
sample = data[src] | (data[src] << 8);
sample -= 32768;
// expand 8->16 bits, mono->stereo
expanded[i * 2] = expanded[i * 2 + 1] = sample;
}
}
return snd;
}
// Load and convert a sound effect
// Returns true if successful
allocated_sound_t * I_CreateAllocatedSound(sfxinfo_t *sfxinfo, byte *data, int lumplen)
{
int samplerate;
unsigned int length;
allocated_sound_t *snd = NULL;
if (!sound_initialized)
{
return NULL;
}
// Check the header, and ensure this is a valid sound
if (lumplen < 8 || data[0] != 0x03 || data[1] != 0x00)
{
// Invalid sound
return NULL;
}
// 16 bit sample rate field, 32 bit length field
samplerate = (data[3] << 8) | data[2];
length = (data[7] << 24) | (data[6] << 16) | (data[5] << 8) | data[4];
// If the header specifies that the length of the sound is greater than
// the length of the lump itself, this is an invalid sound lump
// We also discard sound lumps that are less than 49 samples long,
// as this is how DMX behaves - although the actual cut-off length
// seems to vary slightly depending on the sample rate. This needs
// further investigation to better understand the correct
// behavior.
if (length > lumplen - 8 || length <= 48)
{
return NULL;
}
// The DMX sound library seems to skip the first 16 and last 16
// bytes of the lump - reason unknown.
data += 16;
length -= 32;
// Sample rate conversion
snd = ExpandSoundData(sfxinfo, data + 8, samplerate, length);
return snd;
}
static void I_SDL_UpdateSoundParams(int handle, int vol, int sep)
{
int left, right;
if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS)
{
return;
}
left = ((254 - sep) * vol) / 127;
right = ((sep) * vol) / 127;
if (left < 0) left = 0;
else if ( left > 255) left = 255;
if (right < 0) right = 0;
else if (right > 255) right = 255;
Mix_SetPanning(handle, left, right);
}
//
// Starting a sound means adding it
// to the current list of active sounds
// in the internal channels.
// As the SFX info struct contains
// e.g. a pointer to the raw data,
// it is ignored.
// As our sound handling does not handle
// priority, it is ignored.
// Pitching (that is, increased speed of playback)
// is set, but currently not used by mixing.
//
int I_SDL_StartSound(void *snddata, int channel, int vol, int sep, int pitch)
{
allocated_sound_t *snd = (allocated_sound_t *)snddata;
if (!sound_initialized || channel < 0 || channel >= NUM_CHANNELS)
{
return -1;
}
// Release a sound effect if there is already one playing
// on this channel
ReleaseSoundOnChannel(channel);
LockAllocatedSound(snd);
// play sound
Mix_PlayChannel(channel, &snd->chunk, 0);
channels_playing[channel] = snd;
// set separation, etc.
I_SDL_UpdateSoundParams(channel, vol, sep);
return channel;
}
static void I_SDL_StopSound(int handle)
{
if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS)
{
return;
}
// Sound data is no longer needed; release the
// sound data being used for this channel
ReleaseSoundOnChannel(handle);
}
static boolean I_SDL_SoundIsPlaying(int handle)
{
if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS)
{
return false;
}
return Mix_Playing(handle);
}
//
// Periodically called to update the sound system
//
static void I_SDL_UpdateSound(void)
{
int i;
// Check all channels to see if a sound has finished
for (i=0; i<NUM_CHANNELS; ++i)
{
if (channels_playing[i] && !I_SDL_SoundIsPlaying(i))
{
// Sound has finished playing on this channel,
// but sound data has not been released to cache
ReleaseSoundOnChannel(i);
}
}
}
static void I_SDL_ShutdownSound(void)
{
if (!sound_initialized)
{
return;
}
Mix_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
sound_initialized = false;
}
// Calculate slice size, based on snd_maxslicetime_ms.
// The result must be a power of two.
static int GetSliceSize(void)
{
int limit;
int n;
limit = (snd_samplerate * snd_maxslicetime_ms) / 1000;
// Try all powers of two, not exceeding the limit.
for (n=0;; ++n)
{
// 2^n <= limit < 2^n+1 ?
if ((1 << (n + 1)) > limit)
{
return (1 << n);
}
}
// Should never happen?
return 1024;
}
static boolean I_SDL_InitSound(boolean _use_sfx_prefix)
{
int i;
// SDL 2.0.6 has a bug that makes it unusable.
if (SDL_COMPILEDVERSION == SDL_VERSIONNUM(2, 0, 6))
{
I_Error(
"I_SDL_InitSound: "
"You are trying to launch with SDL 2.0.6 which has a known bug "
"that makes the game crash. Please either downgrade to "
"SDL 2.0.5 or upgrade to 2.0.7.\n");
}
// No sounds yet
for (i=0; i<NUM_CHANNELS; ++i)
{
channels_playing[i] = NULL;
}
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
fprintf(stderr, "Unable to set up sound.\n");
return false;
}
if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, GetSliceSize()) < 0)
{
fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
return false;
}
Mix_QuerySpec(&mixer_freq, &mixer_format, &mixer_channels);
Mix_AllocateChannels(NUM_CHANNELS);
SDL_PauseAudio(0);
sound_initialized = true;
return true;
}
static snddevice_t sound_sdl_devices[] =
{
SNDDEVICE_SB,
SNDDEVICE_PAS,
SNDDEVICE_GUS,
SNDDEVICE_WAVEBLASTER,
SNDDEVICE_SOUNDCANVAS,
SNDDEVICE_AWE32,
};
sound_module_t sound_sdl_module =
{
sound_sdl_devices,
arrlen(sound_sdl_devices),
I_SDL_InitSound,
I_SDL_ShutdownSound,
I_SDL_UpdateSound,
I_SDL_UpdateSoundParams,
I_SDL_StartSound,
I_SDL_StopSound,
I_SDL_SoundIsPlaying,
};