Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/audio'
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniLipponen committed Apr 25, 2023
2 parents 623ae17 + a8d44bf commit 459c06b
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 244 deletions.
1 change: 0 additions & 1 deletion include/TML/Audio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <TML/Audio/Music.h>
#include <TML/Audio/Sound.h>
#include <TML/Audio/AudioEffect.h>
#include <TML/Audio/Mixer.h>
30 changes: 0 additions & 30 deletions include/TML/Audio/AudioEffect.h

This file was deleted.

6 changes: 1 addition & 5 deletions include/TML/Audio/AudioType.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unordered_map>
#include <memory>
#include <string>
#include <optional>

namespace tml
{
Expand All @@ -31,9 +32,6 @@ namespace tml
virtual uint64_t GetLength() noexcept;
virtual uint64_t GetLengthInSeconds() noexcept;
[[nodiscard]] virtual double GetProgress() const noexcept;
AudioEffect* AddEffect(const std::string& name, AudioEffect* effect);
AudioEffect* GetEffect(const std::string& name);
void RemoveEffect(const std::string& name);
virtual uint32_t ReadFrames(AudioFrame* output, uint32_t frameCount) = 0;

protected:
Expand All @@ -47,7 +45,5 @@ namespace tml
bool m_valid = false;
State m_state = Stopped;
uint64_t m_id;

std::unordered_map<std::string, std::unique_ptr<AudioEffect>> m_effects;
};
}
3 changes: 0 additions & 3 deletions include/TML/Audio/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@
namespace tml::Mixer
{
TML_API void SetGain(float gain) noexcept;
TML_API AudioEffect* AddEffect(const std::string& name, AudioEffect* effect);
TML_API AudioEffect* GetEffect(const std::string& name);
TML_API void RemoveEffect(const std::string& name);
}
21 changes: 0 additions & 21 deletions src/Audio/AudioType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,4 @@ namespace tml
{
return static_cast<double>(m_framesRead) / static_cast<double>(m_frameCount);
}

AudioEffect* AudioType::AddEffect(const std::string& name, AudioEffect* effect)
{
m_effects[name] = std::unique_ptr<AudioEffect>(effect);
return effect;
}

AudioEffect* AudioType::GetEffect(const std::string& name)
{
if(m_effects.find(name) == m_effects.end())
{
return nullptr;
}

return m_effects.at(name).get();
}

void AudioType::RemoveEffect(const std::string& name)
{
m_effects.erase(name);
}
}
163 changes: 1 addition & 162 deletions src/Audio/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace tml::Mixer
{
static ma_device* s_outputDevice = nullptr;
static std::map<uint64_t, AudioType*> s_sounds;
static std::map<std::string, std::unique_ptr<AudioEffect>> s_effects;
static uint64_t s_soundCount = 0;

void OnAudioCallback(void* device, void* output, const void*, ma_uint32 frameCount)
Expand All @@ -55,17 +54,6 @@ namespace tml::Mixer
}
}

for(auto& [name, effect] : s_effects)
{
if(effect->IsEnabled())
{
for(uint32_t i = 0; i < frameCount; i++)
{
effect->Process(outputBuffer + i, 0, 0);
}
}
}

for(uint32_t i = 0; i < frameCount; i++)
{
outputBuffer[i] *= s_outputDevice->masterVolumeFactor;
Expand Down Expand Up @@ -123,153 +111,4 @@ namespace tml::Mixer
{
return s_soundCount++;
}

AudioEffect* AddEffect(const std::string& name, AudioEffect* effect)
{
s_effects[name] = std::unique_ptr<AudioEffect>(effect);
return effect;
}

AudioEffect* GetEffect(const std::string& name)
{
if(s_effects.find(name) == s_effects.end())
{
return nullptr;
}

return s_effects.at(name).get();
}

void RemoveEffect(const std::string& name)
{
s_effects.erase(name);
}
}

// namespace tml
// {
// void MixerOnAudioCallback(void* device, void* pOutput, const void* pInput, ma_uint32 frameCount)
// {
// (void)pInput;
// auto* maDevice = static_cast<ma_device*>(device);
// auto* mixer = static_cast<Mixer*>(maDevice->pUserData);
// auto& sounds = mixer->m_sounds;
// auto* pOutputF32 = static_cast<float*>(pOutput);

// if(!sounds.empty())
// {
// for(auto& [id, sound] : sounds)
// {
// if(sound->IsPlaying())
// {
// const uint32_t frames = sound->ReadFrames(pOutputF32, frameCount);

// if(frames < frameCount)
// {
// sound->Stop();
// if(sound->IsLooping())
// {
// sound->Play();
// }
// }
// }
// }

// for(uint32_t i = 0; i < frameCount*2; i += 2)
// {
// for(auto& [name, effect] : mixer->m_effects)
// {
// effect->Process(pOutputF32 + i, 0, 0);
// }
// }

// for(uint32_t i = 0; i < frameCount*2; ++i)
// {
// pOutputF32[i] *= maDevice->masterVolumeFactor;
// }
// }
// }

// Mixer::Mixer()
// {
// ma_device_config config = ma_device_config_init(ma_device_type_playback);
// config.playback.format = ma_format_f32;
// config.playback.channels = 2;
// config.sampleRate = 48000;
// config.dataCallback = (ma_device_callback_proc)MixerOnAudioCallback;
// config.stopCallback = nullptr;
// config.pUserData = this;

// m_outputDevice = new ::ma_device;
// auto* outputDevice = static_cast<::ma_device*>(m_outputDevice);
// auto result = ma_device_init(nullptr, &config, outputDevice);
// outputDevice->masterVolumeFactor = 1;

// if(result == MA_SUCCESS)
// {
// ma_device_start(outputDevice);
// }
// else
// {
// std::puts("[Error]: Failed to initialize audio output device");
// }
// }

// Mixer::~Mixer()
// {
// auto* outputDevice = static_cast<ma_device*>(m_outputDevice);
// ma_device_stop(outputDevice);
// ma_device_uninit(outputDevice);
// delete outputDevice;
// }

// Mixer& Mixer::GetInstance() noexcept
// {
// static Mixer instance;
// return instance;
// }

// void Mixer::SetGain(float gain) noexcept
// {
// reinterpret_cast<ma_device*>(m_outputDevice)->masterVolumeFactor = gain;
// }

// void Mixer::AddSound(uint64_t id, AudioType* sound) noexcept
// {
// m_sounds.insert(std::pair<uint32_t, AudioType*>(id, sound));
// }

// void Mixer::RemoveSound(uint64_t id) noexcept
// {
// if(m_sounds.find(id) != m_sounds.end())
// {
// m_sounds.erase(id);
// }
// }

// uint64_t Mixer::GetAudioID() noexcept
// {
// return m_soundCount++;
// }

// AudioEffect* Mixer::AddEffect(const std::string& name, AudioEffect* effect)
// {
// m_effects[name] = std::unique_ptr<AudioEffect>(effect);
// return effect;
// }

// AudioEffect* Mixer::GetEffect(const std::string& name)
// {
// if(m_effects.find(name) == m_effects.end())
// {
// return nullptr;
// }

// return m_effects.at(name).get();
// }

// void Mixer::RemoveEffect(const std::string& name)
// {
// m_effects.erase(name);
// }
// }
}
11 changes: 0 additions & 11 deletions src/Audio/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ namespace tml
break;
}

for(auto& [name, effect] : m_effects)
{
if(effect->IsEnabled())
{
for(int i = 0; i < framesReadThisIteration; i++)
{
effect->Process(temp.data() + i, m_framesRead + totalFramesRead + i, m_frameCount);
}
}
}

for(iSample = 0; iSample < framesReadThisIteration; iSample++)
{
output[totalFramesRead + iSample] += temp[iSample] * m_volume * balance;
Expand Down
11 changes: 0 additions & 11 deletions src/Audio/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,6 @@ namespace tml
buffer[i] = m_buffer->GetData()[m_framesRead + i];
}

for(auto& [name, effect] : m_effects)
{
if(effect->IsEnabled())
{
for(int i = 0; i < framesRead; i++)
{
effect->Process(buffer.data() + i, m_framesRead + i, m_frameCount);
}
}
}

for(uint32_t i = 0; i < framesRead; i++)
{
output[i] += (buffer[i] * m_volume * balance);
Expand Down

0 comments on commit 459c06b

Please sign in to comment.