2020-11-06 14:56:32 +01:00
|
|
|
#include "pch.h"
|
2022-11-21 08:02:56 +01:00
|
|
|
#include "options.h"
|
2020-11-06 14:56:32 +01:00
|
|
|
#include "Sound.h"
|
2022-05-30 10:23:47 +02:00
|
|
|
#include "maths.h"
|
2020-11-06 14:56:32 +01:00
|
|
|
|
2021-01-23 17:28:29 +01:00
|
|
|
int Sound::num_channels;
|
2021-10-09 16:28:30 +02:00
|
|
|
bool Sound::enabled_flag = false;
|
2022-06-01 15:19:27 +02:00
|
|
|
std::vector<ChannelInfo> Sound::Channels{};
|
2022-01-12 15:17:38 +01:00
|
|
|
int Sound::Volume = MIX_MAX_VOLUME;
|
2022-12-02 12:46:22 +01:00
|
|
|
bool Sound::MixOpen = false;
|
2021-01-23 17:28:29 +01:00
|
|
|
|
2022-12-02 12:46:22 +01:00
|
|
|
void Sound::Init(bool mixOpen, int channels, bool enableFlag, int volume)
|
2020-12-02 18:12:34 +01:00
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
MixOpen = mixOpen;
|
2022-01-12 15:17:38 +01:00
|
|
|
Volume = volume;
|
2021-10-09 16:28:30 +02:00
|
|
|
SetChannels(channels);
|
|
|
|
Enable(enableFlag);
|
2020-12-02 18:12:34 +01:00
|
|
|
}
|
|
|
|
|
2021-10-09 16:28:30 +02:00
|
|
|
void Sound::Enable(bool enableFlag)
|
2020-11-06 14:56:32 +01:00
|
|
|
{
|
2021-10-09 16:28:30 +02:00
|
|
|
enabled_flag = enableFlag;
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen && !enableFlag)
|
2021-10-09 16:28:30 +02:00
|
|
|
Mix_HaltChannel(-1);
|
2020-11-29 16:50:49 +01:00
|
|
|
}
|
2020-12-02 18:12:34 +01:00
|
|
|
|
|
|
|
void Sound::Activate()
|
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen)
|
|
|
|
Mix_Resume(-1);
|
2020-12-02 18:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::Deactivate()
|
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen)
|
|
|
|
Mix_Pause(-1);
|
2020-12-02 18:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::Close()
|
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
Enable(false);
|
2022-06-01 15:19:27 +02:00
|
|
|
Channels.clear();
|
2021-01-23 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:19:27 +02:00
|
|
|
void Sound::PlaySound(Mix_Chunk* wavePtr, int time, TPinballComponent* soundSource, const char* info)
|
2021-01-23 17:28:29 +01:00
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen && wavePtr && enabled_flag)
|
2021-10-09 16:28:30 +02:00
|
|
|
{
|
|
|
|
if (Mix_Playing(-1) == num_channels)
|
|
|
|
{
|
2022-06-01 15:19:27 +02:00
|
|
|
auto cmp = [](const ChannelInfo& a, const ChannelInfo& b)
|
|
|
|
{
|
|
|
|
return a.TimeStamp < b.TimeStamp;
|
|
|
|
};
|
|
|
|
auto min = std::min_element(Channels.begin(), Channels.end(), cmp);
|
2023-09-05 09:51:19 +02:00
|
|
|
auto oldestChannel = static_cast<int>(std::distance(Channels.begin(), min));
|
2021-10-09 16:28:30 +02:00
|
|
|
Mix_HaltChannel(oldestChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto channel = Mix_PlayChannel(-1, wavePtr, 0);
|
2022-06-01 15:19:27 +02:00
|
|
|
if (channel != -1)
|
|
|
|
{
|
|
|
|
Channels[channel].TimeStamp = time;
|
|
|
|
if (options::Options.SoundStereo)
|
|
|
|
{
|
|
|
|
// Positional audio uses collision grid 2D coordinates normalized to [0, 1]
|
|
|
|
// Point (0, 0) is bottom left table corner; point (1, 1) is top right table corner.
|
|
|
|
// Z is defined as: 0 at table level, positive axis goes up from table surface.
|
|
|
|
|
|
|
|
// Get the source sound position.
|
|
|
|
// Sound without position are assumed to be at the center top of the table.
|
|
|
|
vector3 soundPos{};
|
|
|
|
if (soundSource)
|
|
|
|
{
|
|
|
|
auto soundPos2D = soundSource->get_coordinates();
|
|
|
|
soundPos = {soundPos2D.X, soundPos2D.Y, 0.0f};
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
}
|
2022-06-01 15:19:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
soundPos = {0.5f, 1.0f, 0.0f};
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
}
|
2022-06-01 15:19:27 +02:00
|
|
|
Channels[channel].Position = soundPos;
|
|
|
|
|
|
|
|
// Listener is positioned at the bottom center of the table,
|
|
|
|
// at 0.5 height, so roughly a table half - length.
|
|
|
|
vector3 playerPos = {0.5f, 0.0f, 0.5f};
|
|
|
|
auto soundDir = maths::vector_sub(soundPos, playerPos);
|
|
|
|
|
|
|
|
// Find sound angle from positive Y axis in clockwise direction with atan2
|
|
|
|
// Remap atan2 output from (-Pi, Pi] to [0, 2 * Pi)
|
|
|
|
auto angle = fmodf(atan2(soundDir.X, soundDir.Y) + Pi * 2, Pi * 2);
|
|
|
|
auto angleDeg = angle * 180.0f / Pi;
|
|
|
|
auto angleSdl = static_cast<Sint16>(angleDeg);
|
|
|
|
|
|
|
|
// Distance from listener to the sound position is roughly in the [0, ~1.22] range.
|
|
|
|
// Remap to [0, 122] by multiplying by 100 and cast to an integer.
|
|
|
|
auto distance = static_cast<Uint8>(100.0f * maths::magnitude(soundDir));
|
|
|
|
|
|
|
|
// Mix_SetPosition expects an angle in (Sint16)degrees, where
|
|
|
|
// angle 0 is due north, and rotates clockwise as the value increases.
|
|
|
|
// Mix_SetPosition expects a (Uint8)distance from 0 (near) to 255 (far).
|
|
|
|
Mix_SetPosition(channel, angleSdl, distance);
|
|
|
|
|
|
|
|
// Output position of each sound emitted so we can verify
|
|
|
|
// the sanity of the implementation.
|
|
|
|
/*printf("X: %3.3f Y: %3.3f Angle: %3.3f Distance: %3d, Object: %s\n",
|
|
|
|
soundPos.X,
|
|
|
|
soundPos.Y,
|
|
|
|
angleDeg,
|
|
|
|
distance,
|
|
|
|
info
|
|
|
|
);*/
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 16:28:30 +02:00
|
|
|
}
|
2020-12-25 14:46:06 +01:00
|
|
|
}
|
|
|
|
|
2021-10-09 16:28:30 +02:00
|
|
|
Mix_Chunk* Sound::LoadWaveFile(const std::string& lpName)
|
2020-12-25 14:46:06 +01:00
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
if (!MixOpen)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-11-06 17:22:56 +01:00
|
|
|
auto wavFile = fopenu(lpName.c_str(), "r");
|
2021-09-22 14:50:07 +02:00
|
|
|
if (!wavFile)
|
|
|
|
return nullptr;
|
|
|
|
fclose(wavFile);
|
|
|
|
|
2021-09-14 14:33:18 +02:00
|
|
|
return Mix_LoadWAV(lpName.c_str());
|
2020-12-25 14:46:06 +01:00
|
|
|
}
|
|
|
|
|
2021-08-27 12:29:41 +02:00
|
|
|
void Sound::FreeSound(Mix_Chunk* wave)
|
2020-12-25 14:46:06 +01:00
|
|
|
{
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen && wave)
|
2021-08-27 12:29:41 +02:00
|
|
|
Mix_FreeChunk(wave);
|
2020-12-25 14:46:06 +01:00
|
|
|
}
|
2021-10-09 16:28:30 +02:00
|
|
|
|
|
|
|
void Sound::SetChannels(int channels)
|
|
|
|
{
|
|
|
|
if (channels <= 0)
|
|
|
|
channels = 8;
|
|
|
|
|
|
|
|
num_channels = channels;
|
2022-06-01 15:19:27 +02:00
|
|
|
Channels.resize(num_channels);
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen)
|
|
|
|
Mix_AllocateChannels(num_channels);
|
2022-01-12 15:17:38 +01:00
|
|
|
SetVolume(Volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::SetVolume(int volume)
|
|
|
|
{
|
|
|
|
Volume = volume;
|
2022-12-02 12:46:22 +01:00
|
|
|
if (MixOpen)
|
|
|
|
Mix_Volume(-1, volume);
|
2021-10-09 16:28:30 +02:00
|
|
|
}
|