Skip to content

Commit

Permalink
Address various issues in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Sep 14, 2018
1 parent f2d84e4 commit 5979f79
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 53 deletions.
10 changes: 5 additions & 5 deletions src/audio/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace Audio
virtual void stop() = 0;
};

using SoundRef = std::shared_ptr<Sound>;
using SoundPtr = std::shared_ptr<Sound>;
using SoundStream = std::function<int(std::int16_t* buf, std::size_t size)>;

struct Orientation {
Expand Down Expand Up @@ -161,15 +161,15 @@ namespace Audio
/**
* @brief Creates a sound object from an audio buffer
*
* @return SoundRef
* @return SoundPtr
*/
virtual SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t samplingFreq) = 0;
virtual SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t samplingFreq) = 0;

/**
* @brief Creates a sound object from an audio stream
*
* @return SoundRef
* @return SoundPtr
*/
virtual SoundRef createSound(SoundStream, Format, std::size_t samplingFreq) = 0;
virtual SoundPtr createSound(SoundStream, Format, std::size_t samplingFreq) = 0;
};
}
22 changes: 11 additions & 11 deletions src/audio/AudioWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace World
LogInfo() << "All segments loaded.";

m_MusicSource = m_Engine.getAudioEngine().createSound(
[&](std::int16_t* buf, std::size_t len) -> int {
[&](auto buf, auto len) -> int {
if (m_exiting) return -1;

m_musicContext->renderBlock(buf, len);
Expand Down Expand Up @@ -171,7 +171,7 @@ namespace World

try
{
const std::int16_t* dataPtr = (std::int16_t*)(wav.getData());
auto dataPtr = reinterpret_cast<const std::int16_t*>(wav.getData());
auto format = wav.getChannels() == 1 ? Audio::Format::Mono : Audio::Format::Stereo;
auto sound = m_Engine.getAudioEngine().createSound(dataPtr, wav.getDataSize(), format, wav.getRate());
snd->sound = sound;
Expand Down Expand Up @@ -201,7 +201,7 @@ namespace World
return loadAudioVDF(m_VDFSIndex, name);
}

Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist)
Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist)
{
Sound& snd = m_Allocator.getElement(h);

Expand Down Expand Up @@ -321,12 +321,12 @@ namespace World
} while (v.isValid());
}

Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h)
Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h)
{
return playSound(h, Math::float3(0, 0, 0), true);
}

Audio::SoundRef AudioWorld::playSound(const std::string& name)
Audio::SoundPtr AudioWorld::playSound(const std::string& name)
{
// Check if that sound has already been loaded. If not, load it now.
Handle::SfxHandle h = loadAudioVDF(name);
Expand All @@ -340,12 +340,12 @@ namespace World
return playSound(h, Math::float3(0, 0, 0), true);
}

Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist)
Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist)
{
return playSound(h, position, false, maxDist);
}

Audio::SoundRef AudioWorld::playSound(const std::string& name, const Math::float3& position, float maxDist)
Audio::SoundPtr AudioWorld::playSound(const std::string& name, const Math::float3& position, float maxDist)
{
// Check if that sound has already been loaded. If not, load it now.
Handle::SfxHandle h = loadAudioVDF(name);
Expand All @@ -359,7 +359,7 @@ namespace World
return playSound(h, position, false, maxDist);
}

Audio::SoundRef AudioWorld::playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist)
Audio::SoundPtr AudioWorld::playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist)
{
// Check if that sound has already been loaded. If not, load it now.
Handle::SfxHandle h = loadAudioVDF(name);
Expand All @@ -373,14 +373,14 @@ namespace World
return playSoundVariantRandom(h, position, maxDist);
}

Audio::SoundRef AudioWorld::playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist)
Audio::SoundPtr AudioWorld::playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist)
{
Sound& snd = m_Allocator.getElement(h);

return playSound(snd.variants[rand() % snd.variants.size()], position, false, maxDist);
}

Audio::SoundRef AudioWorld::playSoundVariantRandom(const std::string& name)
Audio::SoundPtr AudioWorld::playSoundVariantRandom(const std::string& name)
{
// Check if that sound has already been loaded. If not, load it now.
Handle::SfxHandle h = loadAudioVDF(name);
Expand All @@ -394,7 +394,7 @@ namespace World
return playSoundVariantRandom(h);
}

Audio::SoundRef AudioWorld::playSoundVariantRandom(Handle::SfxHandle h)
Audio::SoundPtr AudioWorld::playSoundVariantRandom(Handle::SfxHandle h)
{
Sound& snd = m_Allocator.getElement(h);

Expand Down
24 changes: 12 additions & 12 deletions src/audio/AudioWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ namespace World
/**
* Plays the sound of the given handle/name
*/
Audio::SoundRef playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist = FLT_MAX);
Audio::SoundRef playSound(Handle::SfxHandle h);
Audio::SoundRef playSound(const std::string& name);
Audio::SoundRef playSoundVariantRandom(const std::string& name);
Audio::SoundRef playSoundVariantRandom(Handle::SfxHandle h);
Audio::SoundPtr playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist = FLT_MAX);
Audio::SoundPtr playSound(Handle::SfxHandle h);
Audio::SoundPtr playSound(const std::string& name);
Audio::SoundPtr playSoundVariantRandom(const std::string& name);
Audio::SoundPtr playSoundVariantRandom(Handle::SfxHandle h);

Audio::SoundRef playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundRef playSound(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundRef playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundRef playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundPtr playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundPtr playSound(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundPtr playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX);
Audio::SoundPtr playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX);

/**
* Plays the segment identified by a name
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace World
{
Daedalus::GEngineClasses::C_SFX sfx;
std::vector<Handle::SfxHandle> variants; // Instances ending with "_Ax"
Audio::SoundRef sound = nullptr;
Audio::SoundPtr sound;
std::string name;
};

Expand All @@ -146,13 +146,13 @@ namespace World
/**
* List of currently playing sounds or sounds that have been playing
*/
std::list<Audio::SoundRef> m_Sources;
std::list<Audio::SoundPtr> m_Sources;

/**
* Audio source for music
*
*/
Audio::SoundRef m_MusicSource;
Audio::SoundPtr m_MusicSource;

/**
* Holds the music state
Expand Down
4 changes: 2 additions & 2 deletions src/audio/NullAudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class NullAudioSound final
};
}

SoundRef NullAudioEngine::createSound(const std::int16_t*, std::size_t, Format, std::size_t)
SoundPtr NullAudioEngine::createSound(const std::int16_t*, std::size_t, Format, std::size_t)
{
return std::make_shared<NullAudioSound>();
}

SoundRef NullAudioEngine::createSound(SoundStream, Format, std::size_t)
SoundPtr NullAudioEngine::createSound(SoundStream, Format, std::size_t)
{
return std::make_shared<NullAudioSound>();
}
4 changes: 2 additions & 2 deletions src/audio/NullAudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Audio

void orientation(const Orientation& o) override {}

SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override;
SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override;

SoundRef createSound(SoundStream, Format, std::size_t) override;
SoundPtr createSound(SoundStream, Format, std::size_t) override;
};
}
35 changes: 19 additions & 16 deletions src/audio/OpenALAudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ OpenALAudioEngine::OpenALAudioEngine(const std::string& deviceName)

OpenALAudioEngine::~OpenALAudioEngine()
{
m_bufferedSounds.erase(std::begin(m_bufferedSounds), std::end(m_bufferedSounds));
m_streamingSounds.erase(std::begin(m_streamingSounds), std::end(m_streamingSounds));
m_bufferedSounds.clear();
m_streamingSounds.clear();

alDeleteSources(m_freeSources.size(), m_freeSources.data());
alcMakeContextCurrent(nullptr);
Expand All @@ -120,19 +120,19 @@ void OpenALAudioEngine::gain(float g)
void OpenALAudioEngine::position(const Math::float3& p)
{
captureContext();
alListenerfv(AL_POSITION, (ALfloat*)&p);
alListenerfv(AL_POSITION, reinterpret_cast<const ALfloat*>(&p));
}

void OpenALAudioEngine::velocity(const Math::float3& v)
{
captureContext();
alListenerfv(AL_VELOCITY, (ALfloat*)&v);
alListenerfv(AL_VELOCITY, reinterpret_cast<const ALfloat*>(&v));
}

void OpenALAudioEngine::orientation(const Orientation& o)
{
captureContext();
alListenerfv(AL_ORIENTATION, (ALfloat*)&o);
alListenerfv(AL_ORIENTATION, reinterpret_cast<const ALfloat*>(&o));
}

namespace Audio
Expand All @@ -149,7 +149,8 @@ namespace Audio
, m_engine(engine)
{}
public:
OpenALSound(OpenALAudioEngine* engine, const std::int16_t* buf, std::size_t len, Format fmt, std::size_t samplingFreq)
OpenALSound(OpenALAudioEngine* engine, const std::int16_t* buf,
std::size_t len, Format fmt, std::size_t samplingFreq)
: m_buffer(0)
, m_hasBuffer(true)
, m_source(0)
Expand Down Expand Up @@ -227,19 +228,19 @@ namespace Audio
void position(const Math::float3& p) override
{
getSource();
alSourcefv(m_source, AL_POSITION, (ALfloat*)&p);
alSourcefv(m_source, AL_POSITION, reinterpret_cast<const ALfloat*>(&p));
}

void velocity(const Math::float3& v) override
{
getSource();
alSourcefv(m_source, AL_VELOCITY, (ALfloat*)&v);
alSourcefv(m_source, AL_VELOCITY, reinterpret_cast<const ALfloat*>(&v));
}

void direction(const Math::float3& d) override
{
getSource();
alSourcefv(m_source, AL_DIRECTION, (ALfloat*)&d);
alSourcefv(m_source, AL_DIRECTION, reinterpret_cast<const ALfloat*>(&d));
}

void relative(bool r) override
Expand Down Expand Up @@ -325,7 +326,8 @@ namespace Audio
*/
static constexpr std::size_t BUFFER_SIZE = 1024;
public:
StreamingOpenALSound(OpenALAudioEngine* engine, SoundStream stream, Format fmt, std::size_t sampleRate)
StreamingOpenALSound(OpenALAudioEngine* engine,
SoundStream stream, Format fmt, std::size_t sampleRate)
: OpenALSound(engine)
, m_stream(stream)
, m_format(fmt == Format::Mono ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16)
Expand Down Expand Up @@ -432,15 +434,15 @@ namespace Audio
};
} // namespace Audio

SoundRef OpenALAudioEngine::createSound(const std::int16_t* buf, std::size_t len, Format fmt, std::size_t sampleRate)
SoundPtr OpenALAudioEngine::createSound(const std::int16_t* buf, std::size_t len, Format fmt, std::size_t sampleRate)
{
captureContext();
auto ptr = std::make_shared<OpenALSound>(this, buf, len, fmt, sampleRate);
m_bufferedSounds.push_back(ptr);
return ptr;
}

SoundRef OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::size_t sampleRate)
SoundPtr OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::size_t sampleRate)
{
captureContext();
auto ptr = std::make_shared<StreamingOpenALSound>(this, stream, fmt, sampleRate);
Expand All @@ -451,11 +453,12 @@ SoundRef OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::siz
ALuint OpenALAudioEngine::getFreeSource()
{
captureContext();

auto lambda = [](const auto& sound) { return sound.use_count() == 1; };

// Remove all expired sounds
std::remove_if(std::begin(m_bufferedSounds), std::end(m_bufferedSounds),
[](const std::shared_ptr<OpenALSound>& sound) { return sound.use_count() == 1; });
std::remove_if(std::begin(m_streamingSounds), std::end(m_streamingSounds),
[](const std::shared_ptr<OpenALSound>& sound) { return sound.use_count() == 1; });
std::remove_if(std::begin(m_bufferedSounds), std::end(m_bufferedSounds), lambda);
std::remove_if(std::begin(m_streamingSounds), std::end(m_streamingSounds), lambda);

if (m_freeSources.empty())
{
Expand Down
4 changes: 2 additions & 2 deletions src/audio/OpenALAudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace Audio

void orientation(const Orientation&) override;

SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override;
SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override;

SoundRef createSound(SoundStream, Format, std::size_t) override;
SoundPtr createSound(SoundStream, Format, std::size_t) override;
private:
ALCdevice* m_device = nullptr;
ALCcontext* m_context = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/logic/PlayerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,6 @@ namespace Logic
size_t m_LastAniRootPosUpdatedAniHash;

// Main noise sound slot. Other sounds using it won't play if there is already a sound playing here.
Audio::SoundRef m_MainNoiseSoundSlot;
Audio::SoundPtr m_MainNoiseSoundSlot;
};
} // namespace Logic
2 changes: 1 addition & 1 deletion src/logic/SoundController.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace Logic
/**
* Handle to the currently played or last played sound
*/
Audio::SoundRef m_PlayedSound;
Audio::SoundPtr m_PlayedSound;

/**
* Time when to play this for the next time (seconds)
Expand Down
2 changes: 1 addition & 1 deletion src/logic/messages/EventMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ namespace Logic
/**
* Ticket. Can be used to ask AudioWorld if sound is playing.
*/
Audio::SoundRef soundTicket;
Audio::SoundPtr soundTicket;
};

struct MagicMessage : public NpcMessage
Expand Down

0 comments on commit 5979f79

Please sign in to comment.