Skip to content

Commit

Permalink
[Audio] Instrumented audio features for profiling purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Mar 9, 2024
1 parent 131ec46 commit 3d50f0c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
3 changes: 1 addition & 2 deletions include/RaZ/Audio/Microphone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class Microphone {
/// \param duration Amount of time to record, in seconds. This is a minimum; the actual available duration may exceed this value.
/// \param deviceName Name of the audio capture device to open; if empty, will use the default one.
/// \see Microphone::recoverDevices()
Microphone(AudioFormat format, unsigned int frequency, float duration, const std::string& deviceName = {}) { openDevice(format, frequency,
duration, deviceName); }
Microphone(AudioFormat format, unsigned int frequency, float duration, const std::string& deviceName = {});
Microphone(const Microphone&) = delete;
Microphone(Microphone&&) = delete;

Expand Down
10 changes: 10 additions & 0 deletions src/RaZ/Audio/AudioSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "RaZ/Physics/RigidBody.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <AL/al.h>
#include <AL/alc.h>

Expand Down Expand Up @@ -37,6 +39,8 @@ inline void checkError(void* device, const std::string& errorMsg) {
} // namespace

AudioSystem::AudioSystem(const std::string& deviceName) {
ZoneScopedN("AudioSystem::AudioSystem");

registerComponents<Sound, Listener>();
openDevice(deviceName);

Expand All @@ -63,6 +67,8 @@ std::vector<std::string> AudioSystem::recoverDevices() {
}

void AudioSystem::openDevice(const std::string& deviceName) {
ZoneScopedN("AudioSystem::openDevice");

Logger::debug("[AudioSystem] Opening " + (!deviceName.empty() ? + "device '" + deviceName + '\'' : "default device") + "...");

destroy();
Expand Down Expand Up @@ -95,6 +101,8 @@ std::string AudioSystem::recoverCurrentDevice() const {
}

bool AudioSystem::update(const FrameTimeInfo&) {
ZoneScopedN("AudioSystem::update");

#if defined(RAZ_CONFIG_DEBUG)
// Checking that only one Listener exists
bool hasOneListener = false;
Expand Down Expand Up @@ -147,6 +155,8 @@ bool AudioSystem::update(const FrameTimeInfo&) {
}

void AudioSystem::destroy() {
ZoneScopedN("AudioSystem::destroy");

if (m_context == nullptr && m_device == nullptr)
return;

Expand Down
15 changes: 15 additions & 0 deletions src/RaZ/Audio/Microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "RaZ/Audio/Sound.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <AL/al.h>
#include <AL/alc.h>

Expand Down Expand Up @@ -83,6 +85,11 @@ constexpr int recoverFrameSize(AudioFormat format) {

} // namespace

Microphone::Microphone(AudioFormat format, unsigned int frequency, float duration, const std::string& deviceName) {
ZoneScopedN("Microphone::Microphone");
openDevice(format, frequency, duration, deviceName);
}

std::vector<std::string> Microphone::recoverDevices() {
if (!alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT")) // If the needed extension is unsupported, return an empty vector
return {};
Expand All @@ -102,6 +109,8 @@ std::vector<std::string> Microphone::recoverDevices() {
}

void Microphone::openDevice(AudioFormat format, unsigned int frequency, float duration, const std::string& deviceName) {
ZoneScopedN("Microphone::openDevice");

Logger::debug("[Microphone] Opening capture " + (!deviceName.empty() ? + "device '" + deviceName + '\'' : "default device") + "...");

destroy();
Expand Down Expand Up @@ -169,6 +178,8 @@ std::vector<uint8_t> Microphone::recoverData(float maxDuration) const {
}

void Microphone::recoverData(std::vector<uint8_t>& data, float maxDuration) const {
ZoneScopedN("Microphone::recoverData");

data.clear();

if (maxDuration == 0.f)
Expand All @@ -189,6 +200,8 @@ void Microphone::recoverData(std::vector<uint8_t>& data, float maxDuration) cons
}

Sound Microphone::recoverSound(float maxDuration) const {
ZoneScopedN("Microphone::recoverSound");

Sound sound;

sound.m_format = m_format;
Expand All @@ -200,6 +213,8 @@ Sound Microphone::recoverSound(float maxDuration) const {
}

void Microphone::destroy() {
ZoneScopedN("Microphone::destroy");

if (m_device == nullptr)
return;

Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Audio/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "RaZ/Math/Vector.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <AL/al.h>
#if !defined(RAZ_PLATFORM_EMSCRIPTEN)
#include <AL/efx.h>
Expand Down Expand Up @@ -34,6 +36,8 @@ inline void checkError(const std::string& errorMsg) {
} // namespace

void Sound::init() {
ZoneScopedN("Sound::init");

Logger::debug("[Sound] Initializing...");

alGetError(); // Flushing errors
Expand All @@ -54,6 +58,8 @@ void Sound::init() {
}

void Sound::load() {
ZoneScopedN("Sound::load");

stop(); // Making sure the sound isn't paused or currently playing
alSourcei(m_source, AL_BUFFER, 0); // Detaching the previous buffer (if any) from the source

Expand Down Expand Up @@ -197,6 +203,8 @@ float Sound::recoverElapsedTime() const noexcept {
}

void Sound::destroy() {
ZoneScopedN("Sound::destroy");

if (!m_source.isValid() && !m_buffer.isValid())
return;

Expand Down
6 changes: 6 additions & 0 deletions src/RaZ/Audio/SoundEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "RaZ/Utils/CompilerUtils.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <AL/efx.h>
#include <AL/efx-presets.h>

Expand Down Expand Up @@ -65,6 +67,8 @@ inline bool loadFunctions() noexcept {
} // namespace

void SoundEffect::init() {
ZoneScopedN("SoundEffect::init");

if (!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX")) {
Logger::error("[SoundEffect] Sound effects are unavailable.");
return;
Expand Down Expand Up @@ -200,6 +204,8 @@ void SoundEffect::reset() {
}

void SoundEffect::destroy() {
ZoneScopedN("SoundEffect::destroy");

if (!m_index.isValid())
return;

Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Audio/SoundEffectSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "RaZ/Utils/CompilerUtils.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <AL/al.h>
#if !defined(RAZ_PLATFORM_EMSCRIPTEN)
#include <AL/efx.h>
Expand Down Expand Up @@ -68,6 +70,8 @@ inline bool loadFunctions() noexcept {
} // namespace

void SoundEffectSlot::init() {
ZoneScopedN("SoundEffectSlot::init");

if (!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX")) {
Logger::error("[SoundEffectSlot] Sound effects are unavailable.");
return;
Expand All @@ -88,11 +92,15 @@ void SoundEffectSlot::init() {
}

void SoundEffectSlot::loadEffect(const SoundEffect& effect) const noexcept {
ZoneScopedN("SoundEffectSlot::loadEffect");

alAuxiliaryEffectSloti(m_index, AL_EFFECTSLOT_EFFECT, static_cast<int>(effect.getIndex()));
checkError("Failed to load the sound effect");
}

void SoundEffectSlot::destroy() {
ZoneScopedN("SoundEffectSlot::destroy");

if (!m_index.isValid())
return;

Expand Down

0 comments on commit 3d50f0c

Please sign in to comment.