Skip to content

Commit

Permalink
Initial rework on audio system
Browse files Browse the repository at this point in the history
  • Loading branch information
tippesi committed Jan 23, 2024
1 parent b70e92b commit fd1aae4
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 90 deletions.
11 changes: 11 additions & 0 deletions src/demo/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ using namespace Atlas::Scene::Prefabs;

void App::LoadContent() {

music = Atlas::ResourceManager<Atlas::Audio::AudioData>::GetOrLoadResource("more.wav");
audio = Atlas::ResourceManager<Atlas::Audio::AudioData>::GetOrLoadResource("more.wav");
//static auto audioStream = Atlas::Audio::AudioManager::CreateStream(audio);

for (uint32_t i = 0; i < 10000; i++) {
//audioStreams.push_back(Atlas::Audio::AudioManager::CreateStream(audio));
//audioStreams.back()->SetVolume(0.0001);
}

renderTarget = Atlas::RenderTarget(1920, 1080);
pathTraceTarget = Atlas::Renderer::PathTracerRenderTarget(1920, 1080);

Expand Down Expand Up @@ -191,6 +200,7 @@ void App::Update(float deltaTime) {

auto shape = Atlas::Physics::ShapesManager::CreateShapeFromSphere(meshes.back()->data.radius);
entity.AddComponent<RigidBodyComponent>(shape, Atlas::Physics::Layers::MOVABLE);
entity.AddComponent<AudioComponent>(audio);

entities.push_back(entity);
lastSpawn = Atlas::Clock::Get();
Expand All @@ -211,6 +221,7 @@ void App::Update(float deltaTime) {
camera.direction * meshes.back()->data.radius * 2.0f));
auto entity = scene->CreatePrefab<MeshInstance>(meshes.back(), matrix, false);

entity.AddComponent<AudioComponent>(audio);
auto& rigidBodyComponent = entity.AddComponent<RigidBodyComponent>(shape, Atlas::Physics::Layers::MOVABLE);
rigidBodyComponent.SetLinearVelocity(camera.direction * shootVelocity);

Expand Down
5 changes: 5 additions & 0 deletions src/demo/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class App : public Atlas::EngineInstance {
Ref<Atlas::Scene::Scene> scene;
Ref<Atlas::Lighting::DirectionalLight> directionalLight;

Atlas::ResourceHandle<Atlas::Audio::AudioData> audio;
Atlas::ResourceHandle<Atlas::Audio::AudioData> music;

std::vector<Atlas::ResourceHandle<Atlas::Mesh::Mesh>> meshes;
std::vector<Atlas::Scene::Entity> entities;

Expand Down Expand Up @@ -108,4 +111,6 @@ class App : public Atlas::EngineInstance {

ImguiWrapper imguiWrapper;

std::vector<Ref<Atlas::Audio::AudioStream>> audioStreams;

};
2 changes: 2 additions & 0 deletions src/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace Atlas {
PipelineManager::Shutdown();
Physics::PhysicsManager::Shutdown();
Texture::Texture::Shutdown();
Audio::AudioManager::Shutdown();

#ifdef AE_NO_APP
SDL_Quit();
Expand All @@ -97,6 +98,7 @@ namespace Atlas {
Graphics::Profiler::BeginFrame();
Events::EventManager::Update();
PipelineManager::Update();
Audio::AudioManager::Update();

}

Expand Down
13 changes: 9 additions & 4 deletions src/engine/audio/AudioData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Atlas {

}

AudioData::AudioData(std::string filename) : filename(filename) {
AudioData::AudioData(const std::string& filename) : filename(filename) {

uint8_t* data;
uint32_t length;
Expand Down Expand Up @@ -48,16 +48,19 @@ namespace Atlas {

SDL_FreeWAV(data);

// Automatically apply format on load
isValid = ApplyFormat(AudioManager::audioSpec);

}

void AudioData::ApplyFormat(const SDL_AudioSpec& formatSpec) {
bool AudioData::ApplyFormat(const SDL_AudioSpec& formatSpec) {

if (formatSpec.channels == spec.channels && formatSpec.format == spec.format &&
formatSpec.freq == spec.freq) {
return;
return true;
}

Convert(formatSpec.freq, formatSpec.channels, formatSpec.format);
return Convert(formatSpec.freq, formatSpec.channels, formatSpec.format);

}

Expand Down Expand Up @@ -88,6 +91,8 @@ namespace Atlas {

}

Log::Warning("Couldn't convert audio data from " + filename + " internally");

return false;

}
Expand Down
11 changes: 8 additions & 3 deletions src/engine/audio/AudioData.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Atlas {
public:
AudioData();

explicit AudioData(std::string filename);
explicit AudioData(const std::string& filename);

void ApplyFormat(const SDL_AudioSpec& formatSpec);
bool ApplyFormat(const SDL_AudioSpec& formatSpec);

bool Convert(uint32_t frequency, uint8_t channels, uint32_t format);

Expand All @@ -31,11 +31,16 @@ namespace Atlas {

std::vector<int16_t> data;

std::string filename;
const std::string filename;

private:
SDL_AudioSpec spec;

bool isValid;

friend class AudioManager;
friend class AudioStream;

};

}
Expand Down
61 changes: 35 additions & 26 deletions src/engine/audio/AudioManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "AudioManager.h"
#include "Log.h"

#include <SDL_audio.h>
#include <algorithm>
Expand All @@ -10,8 +11,7 @@ namespace Atlas {
SDL_AudioSpec AudioManager::audioSpec;
SDL_AudioDeviceID AudioManager::audioDevice;

std::vector<AudioStream*> AudioManager::musicQueue;
std::vector<AudioStream*> AudioManager::effectQueue;
std::vector<Ref<AudioStream>> AudioManager::audioStreams;

std::mutex AudioManager::mutex;

Expand Down Expand Up @@ -41,10 +41,20 @@ namespace Atlas {

}

Log::Warning("Couldn't configure audio device with required specifications");

return false;

}

void AudioManager::Shutdown() {

audioStreams.clear();

SDL_CloseAudioDevice(audioDevice);

}

void AudioManager::Mute() {


Expand All @@ -69,24 +79,31 @@ namespace Atlas {

}

void AudioManager::AddMusic(AudioStream *stream) {
Ref<AudioStream> AudioManager::CreateStream(ResourceHandle<AudioData> data) {

if (!data->isValid)
return nullptr;

stream->ApplyFormat(audioSpec);
auto stream = CreateRef<AudioStream>(data);

std::lock_guard<std::mutex> lock(mutex);
audioStreams.push_back(stream);

musicQueue.push_back(stream);
return stream;

}

void AudioManager::RemoveMusic(AudioStream *stream) {

std::lock_guard<std::mutex> lock(mutex);
void AudioManager::Update() {

auto item = std::find(musicQueue.begin(), musicQueue.end(), stream);
std::unique_lock<std::mutex> lock(mutex);

if (item != musicQueue.end()) {
musicQueue.erase(item);
for (size_t i = 0; i < audioStreams.size(); i++) {
auto& ref = audioStreams[i];
if (ref.use_count() == 1) {
ref.swap(audioStreams.back());
audioStreams.pop_back();
i--;
}
}

}
Expand All @@ -97,32 +114,24 @@ namespace Atlas {
length /= 2;

std::vector<int16_t> dest(length, 0);
std::vector<int16_t> chunk(length, 0);

// Compute music first
std::unique_lock<std::mutex> lock(mutex);

auto queue = musicQueue;

auto localStreams = audioStreams;
lock.unlock();

for (auto stream : queue) {
// Take ownership of stream here, such that the update can run in parallel
for (auto stream : localStreams) {

if (stream->IsPaused())
if (!stream->IsValid() || stream->IsPaused())
continue;

auto src = stream->GetChunk(length);
stream->GetChunk(chunk);

Mix(dest, src);
Mix(dest, chunk);

}

// Compute audio effects
lock.lock();



lock.unlock();

std::memcpy(stream, dest.data(), length * 2);

}
Expand Down
11 changes: 7 additions & 4 deletions src/engine/audio/AudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Atlas {
public:
static bool Configure(uint32_t frequency, uint8_t channels, uint32_t samples);

static void Shutdown();

static void Mute();

static void Unmute();
Expand All @@ -26,9 +28,9 @@ namespace Atlas {

static void Resume();

static void AddMusic(AudioStream* stream);
static Ref<AudioStream> CreateStream(ResourceHandle<AudioData> data);

static void RemoveMusic(AudioStream* stream);
static void Update();

private:
static void Callback(void* userData, uint8_t* stream, int32_t length);
Expand All @@ -38,11 +40,12 @@ namespace Atlas {
static SDL_AudioSpec audioSpec;
static SDL_AudioDeviceID audioDevice;

static std::vector<AudioStream*> musicQueue;
static std::vector<AudioStream*> effectQueue;
static std::vector<Ref<AudioStream>> audioStreams;

static std::mutex mutex;

friend AudioData;

};

}
Expand Down
Loading

0 comments on commit fd1aae4

Please sign in to comment.