Skip to content

Commit

Permalink
Merge pull request #653 from Wargus/Sound
Browse files Browse the repository at this point in the history
Sound clean up
  • Loading branch information
Jarod42 authored Apr 16, 2024
2 parents 1896f59 + 4bcae56 commit e58bf6e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 99 deletions.
19 changes: 12 additions & 7 deletions src/include/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class LuaActionListener;
----------------------------------------------------------------------------*/

#define MaxSampleVolume 255 /// Maximum sample volume
#define NO_SOUND 0 /// No valid sound ID

/**
** Voice groups for a unit
Expand Down Expand Up @@ -163,8 +162,17 @@ extern int DistanceSilent;
-- Functions
----------------------------------------------------------------------------*/

/// Calculates volume level
extern unsigned char CalculateVolume(bool isVolume, int power, unsigned char range);
/**
** Compute a suitable volume for something taking place at a given
** distance from the current view point.
**
** @param d distance
** @param range range
**
** @return volume for given distance (0..??)
*/
unsigned char VolumeForDistance(unsigned short d, unsigned char range);

/// Play a unit sound
extern void PlayUnitSound(const CUnit &, EUnitVoice, bool sampleUnique = false);
/// Play a unit sound
Expand All @@ -177,9 +185,6 @@ extern void PlayGameSound(CSound *sound, unsigned char volume, bool always = fal
/// Play a sound file
extern int PlayFile(const std::string &name, LuaActionListener *listener = nullptr);

/// Modify the range of a given sound.
extern void SetSoundRange(CSound *sound, unsigned char range);

/// Register a sound (can be a simple sound or a group)
extern CSound *RegisterSound(const std::vector<std::string> &files);

Expand Down Expand Up @@ -209,7 +214,7 @@ extern void CallbackMusicTrigger();
/// Map sound to identifier
extern void MapSound(const std::string &sound_name, CSound *id);
/// Get the sound id bound to an identifier
extern CSound *SoundForName(const std::string &sound_name);
extern CSound *SoundForName(const std::string_view &sound_name);
/// Make a sound bound to identifier
extern CSound *MakeSound(const std::string &sound_name, const std::vector<std::string> &files);
/// Make a sound group bound to identifier
Expand Down
1 change: 0 additions & 1 deletion src/include/sound_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
-- Definitons
----------------------------------------------------------------------------*/

#define MaxVolume 255
#define SOUND_BUFFER_SIZE 65536

/*----------------------------------------------------------------------------
Expand Down
46 changes: 17 additions & 29 deletions src/sound/script_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ static int CclSoundForName(lua_State *l)
*/
static CSound *CclGetSound(lua_State *l)
{
LuaUserData *data;
int pop;

pop = 0;
bool pop = false;
if (lua_isstring(l, -1)) {
CclSoundForName(l);
pop = 1;
pop = true;
}
if (lua_isuserdata(l, -1)) {
data = (LuaUserData *)lua_touserdata(l, -1);
LuaUserData *data = (LuaUserData *)lua_touserdata(l, -1);
if (data->Type == LuaSoundType) {
if (pop) {
lua_pop(l, 1);
Expand Down Expand Up @@ -114,7 +111,7 @@ static int CclMakeSound(lua_State *l)

std::string c_name = std::string{LuaToString(l, 1)};
std::vector<std::string> files;
CSound *id;
CSound *id = nullptr;
if (lua_isstring(l, 2)) {
// only one file
files.push_back(std::string{LuaToString(l, 2)});
Expand Down Expand Up @@ -147,22 +144,16 @@ static int CclMakeSound(lua_State *l)
*/
static int CclMakeSoundGroup(lua_State *l)
{
CSound *id;
std::string c_name;
CSound *first;
CSound *second;
LuaUserData *data;

LuaCheckArgs(l, 3);

c_name = LuaToString(l, 1);
std::string c_name{LuaToString(l, 1)};

lua_pushvalue(l, 2);
first = CclGetSound(l);
CSound *first = CclGetSound(l);
lua_pop(l, 1);
second = CclGetSound(l);
id = MakeSoundGroup(c_name, first, second);
data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
CSound *second = CclGetSound(l);
CSound *id = MakeSoundGroup(c_name, first, second);
LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
data->Type = LuaSoundType;
data->Data = id;
return 1;
Expand Down Expand Up @@ -206,10 +197,7 @@ static int CclPlaySound(lua_State *l)
lua_pushvalue(l, 1);
CSound *id = CclGetSound(l);
lua_pop(l, 1);
bool always = false;
if (args == 2) {
always = LuaToBoolean(l, 2);
}
const bool always = args == 2 && LuaToBoolean(l, 2);
PlayGameSound(id, MaxSampleVolume, always);
return 0;
}
Expand Down Expand Up @@ -339,17 +327,17 @@ static int CclSetGlobalSoundRange(lua_State *l)
*/
static int CclSetSoundRange(lua_State *l)
{

LuaCheckArgs(l, 2);

int tmp = LuaToNumber(l, 2);
clamp(&tmp, 0, 255);
const unsigned char theRange = static_cast<unsigned char>(tmp);
int range = LuaToNumber(l, 2);
clamp(&range, 0, 255);
const unsigned char theRange = static_cast<unsigned char>(range);

lua_pushvalue(l, 1);
CSound *id = CclGetSound(l);
SetSoundRange(id, theRange);
return 1;
if (CSound* id = CclGetSound(l)) {
id->Range = theRange;
}
return 0;
}

/**
Expand Down
80 changes: 27 additions & 53 deletions src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
-- Variables
----------------------------------------------------------------------------*/

static constexpr unsigned char MaxVolume = 255;

/**
** Various sounds used in game.
*/
Expand All @@ -65,9 +67,6 @@ struct SelectionHandling {
unsigned char HowMany; /// number of sound played in this group
};

/// FIXME: docu
SelectionHandling SelectionHandler;

static int ViewPointOffset; /// Distance to Volume Mapping
int DistanceSilent; /// silent distance

Expand All @@ -92,23 +91,24 @@ static Mix_Chunk *SimpleChooseSample(const CSound &sound)
/**
** Choose the sample to play
*/
static Mix_Chunk *ChooseSample(CSound *sound, bool selection, Origin &source)
static Mix_Chunk *ChooseSample(CSound &sound, bool selection, Origin &source)
{
Mix_Chunk *result = nullptr;

if (!sound || !SoundEnabled()) {
if (!SoundEnabled()) {
return nullptr;
}

if (sound->Number == TWO_GROUPS) {
Mix_Chunk *result = nullptr;
static SelectionHandling SelectionHandler{};

if (sound.Number == TWO_GROUPS) {
// handle a special sound (selection)
if (SelectionHandler.Sound != nullptr && (SelectionHandler.Source.Base == source.Base && SelectionHandler.Source.Id == source.Id)) {
if (SelectionHandler.Sound == sound->Sound.TwoGroups.First) {
if (SelectionHandler.Sound == sound.Sound.TwoGroups.First) {
result = SimpleChooseSample(*SelectionHandler.Sound);
SelectionHandler.HowMany++;
if (SelectionHandler.HowMany >= 3) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.Second;
SelectionHandler.Sound = sound.Sound.TwoGroups.Second;
}
} else {
//FIXME: checks for error
Expand All @@ -118,23 +118,23 @@ static Mix_Chunk *ChooseSample(CSound *sound, bool selection, Origin &source)
SelectionHandler.HowMany++;
if (SelectionHandler.HowMany >= SelectionHandler.Sound->Number) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
}
} else {
result = SelectionHandler.Sound->Sound.OneSound;
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
}
}
} else {
SelectionHandler.Source = source;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
result = SimpleChooseSample(*SelectionHandler.Sound);
SelectionHandler.HowMany = 1;
}
} else {
// normal sound/sound group handling
result = SimpleChooseSample(*sound);
result = SimpleChooseSample(sound);
if (SelectionHandler.Source.Base == source.Base && SelectionHandler.Source.Id == source.Id) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = nullptr;
Expand Down Expand Up @@ -225,20 +225,6 @@ unsigned char VolumeForDistance(unsigned short d, unsigned char range)
}
}

/**
** Calculate the volume associated with a request, either by clipping the
** range parameter of this request, or by mapping this range to a volume.
*/
unsigned char CalculateVolume(bool isVolume, int power, unsigned char range)
{
if (isVolume) {
return std::min(MaxVolume, power);
} else {
// map distance to volume
return VolumeForDistance(power, range);
}
}

/**
** Calculate the stereo value for a unit
*/
Expand Down Expand Up @@ -273,7 +259,7 @@ void PlayUnitSound(const CUnit &unit, EUnitVoice voice, bool sampleUnique)
return;
}

Mix_Chunk *sample = ChooseSample(sound, selection, source);
Mix_Chunk *sample = ChooseSample(*sound, selection, source);

if (sampleUnique && SampleIsPlaying(sample)) {
return;
Expand All @@ -283,7 +269,7 @@ void PlayUnitSound(const CUnit &unit, EUnitVoice voice, bool sampleUnique)
if (channel == -1) {
return;
}
SetChannelVolume(channel, CalculateVolume(false, ViewPointDistanceToUnit(unit), sound->Range));
SetChannelVolume(channel, VolumeForDistance(ViewPointDistanceToUnit(unit), sound->Range));
SetChannelStereo(channel, CalculateStereo(unit));
#ifdef USE_MNG
const CUnitType &type = *unit.Type;
Expand All @@ -309,12 +295,12 @@ void PlayUnitSound(const CUnit &unit, CSound *sound)
return;
}
Origin source = {&unit, unsigned(UnitNumber(unit))};
unsigned char volume = CalculateVolume(false, ViewPointDistanceToUnit(unit), sound->Range);
const unsigned char volume = VolumeForDistance(ViewPointDistanceToUnit(unit), sound->Range);
if (volume == 0) {
return;
}

int channel = PlaySample(ChooseSample(sound, false, source));
int channel = PlaySample(ChooseSample(*sound, false, source));
if (channel == -1) {
return;
}
Expand All @@ -339,12 +325,13 @@ void PlayMissileSound(const Missile &missile, CSound *sound)
clamp(&stereo, -128, 127);

Origin source = {nullptr, 0};
unsigned char volume = CalculateVolume(false, ViewPointDistanceToMissile(missile), sound->Range);
const unsigned char volume =
VolumeForDistance(ViewPointDistanceToMissile(missile), sound->Range);
if (volume == 0) {
return;
}

int channel = PlaySample(ChooseSample(sound, false, source));
int channel = PlaySample(ChooseSample(*sound, false, source));
if (channel == -1) {
return;
}
Expand All @@ -365,7 +352,7 @@ void PlayGameSound(CSound *sound, unsigned char volume, bool always)
}
Origin source = {nullptr, 0};

Mix_Chunk *sample = ChooseSample(sound, false, source);
Mix_Chunk *sample = ChooseSample(*sound, false, source);

if (!sample || (!always && SampleIsPlaying(sample))) {
return;
Expand All @@ -375,7 +362,7 @@ void PlayGameSound(CSound *sound, unsigned char volume, bool always)
if (channel == -1) {
return;
}
SetChannelVolume(channel, CalculateVolume(true, volume, sound->Range));
SetChannelVolume(channel, std::min(MaxVolume, volume));
}

static std::map<int, LuaActionListener *> ChannelMap;
Expand Down Expand Up @@ -425,19 +412,6 @@ int PlayFile(const std::string &name, LuaActionListener *listener)
return channel;
}

/**
** Ask the sound server to change the range of a sound.
**
** @param sound the id of the sound to modify.
** @param range the new range for this sound.
*/
void SetSoundRange(CSound *sound, unsigned char range)
{
if (sound != NO_SOUND) {
sound->Range = range;
}
}

/**
** Ask the sound server to register a sound (and currently to load it)
** and to return an unique identifier for it. The unique identifier is
Expand All @@ -464,14 +438,14 @@ CSound *RegisterSound(const std::vector<std::string> &files)
if (!id->Sound.OneGroup[i]) {
//delete[] id->Sound.OneGroup;
delete id;
return NO_SOUND;
return nullptr;
}
}
} else { // load a unique sound
id->Sound.OneSound = LoadSample(files[0]);
if (!id->Sound.OneSound) {
delete id;
return NO_SOUND;
return nullptr;
}
id->Number = ONE_SOUND;
}
Expand All @@ -489,8 +463,8 @@ CSound *RegisterSound(const std::vector<std::string> &files)
*/
CSound *RegisterTwoGroups(CSound *first, CSound *second)
{
if (first == NO_SOUND || second == NO_SOUND) {
return NO_SOUND;
if (first == nullptr || second == nullptr) {
return nullptr;
}
CSound *id = new CSound;
id->Number = TWO_GROUPS;
Expand Down
Loading

0 comments on commit e58bf6e

Please sign in to comment.