Move kissfft to engine/third_party and add AudioMixer class
This commit is contained in:
84
engine/src/Audio/AudioMixer.cpp
Normal file
84
engine/src/Audio/AudioMixer.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <XCEngine/Audio/AudioMixer.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Audio {
|
||||
|
||||
AudioMixer::AudioMixer()
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(AudioChannel::SideRight) + 1; ++i) {
|
||||
m_channelVolumes[static_cast<AudioChannel>(i)] = ChannelVolume{1.0f, false};
|
||||
}
|
||||
}
|
||||
|
||||
AudioMixer::~AudioMixer() {
|
||||
}
|
||||
|
||||
void AudioMixer::SetVolume(float volume) {
|
||||
m_volume = std::max(0.0f, std::min(1.0f, volume));
|
||||
}
|
||||
|
||||
void AudioMixer::SetMute(bool mute) {
|
||||
m_mute = mute;
|
||||
}
|
||||
|
||||
void AudioMixer::AddEffect(IAudioEffect* effect) {
|
||||
if (effect) {
|
||||
m_effects.push_back(effect);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioMixer::RemoveEffect(IAudioEffect* effect) {
|
||||
if (!effect) return;
|
||||
auto it = std::find(m_effects.begin(), m_effects.end(), effect);
|
||||
if (it != m_effects.end()) {
|
||||
m_effects.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioMixer::ClearEffects() {
|
||||
m_effects.clear();
|
||||
}
|
||||
|
||||
void AudioMixer::ProcessAudio(float* buffer, uint32 sampleCount, uint32 channels) {
|
||||
if (!buffer || sampleCount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
float effectiveVolume = m_mute ? 0.0f : m_volume;
|
||||
|
||||
if (effectiveVolume < 0.001f) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sampleCount * channels; ++i) {
|
||||
buffer[i] *= effectiveVolume;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioMixer::SetOutputMixer(AudioMixer* mixer) {
|
||||
m_outputMixer = mixer;
|
||||
}
|
||||
|
||||
void AudioMixer::Set3DParams(const Audio3DParams& params) {
|
||||
m_3DParams = params;
|
||||
}
|
||||
|
||||
void AudioMixer::SetChannelVolume(AudioChannel channel, float volume) {
|
||||
auto it = m_channelVolumes.find(channel);
|
||||
if (it != m_channelVolumes.end()) {
|
||||
it->second.volume = std::max(0.0f, std::min(1.0f, volume));
|
||||
}
|
||||
}
|
||||
|
||||
float AudioMixer::GetChannelVolume(AudioChannel channel) const {
|
||||
auto it = m_channelVolumes.find(channel);
|
||||
if (it != m_channelVolumes.end()) {
|
||||
return it->second.volume;
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
} // namespace Audio
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user