Skip to content

Commit

Permalink
Simplified Audio: removed abstraction of buffers & sources
Browse files Browse the repository at this point in the history
  • Loading branch information
WillisMedwell committed Mar 5, 2024
1 parent c42e45d commit 7180544
Show file tree
Hide file tree
Showing 19 changed files with 419 additions and 437 deletions.
48 changes: 17 additions & 31 deletions code/Demos/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace std::literals;
static inline auto print_then_quit = [](const auto& error) {
std::cout << error.what();
std::this_thread::sleep_for(std::chrono::seconds(1));
throw std::runtime_error(std::string(error.what()));
exit(EXIT_FAILURE);
};

Expand Down Expand Up @@ -367,7 +368,7 @@ struct FontLogic {
renderer.screen_frame_buffer.clear(data.background_colour);
renderer.screen_frame_buffer.resize(renderer.window_width, renderer.window_height);

Renderer::FontBatchRenderer::BatchConfig config {
Renderer::FontBatchRenderer::BatchConfig config {
.resource_manager = data.resource_manager,
.screen_dimensions = glm::vec2 { renderer.window_width, renderer.window_height },
.font_colour = { 0, 0, 0, 1 },
Expand Down Expand Up @@ -402,52 +403,37 @@ struct IsoData {
std::chrono::steady_clock::time_point start_time;
glm::vec4 background_colour = { 1, 1, 0, 1 };

Audio::Device audio_device;
Audio::Context audio_context;
Audio::Buffer audio_buffer;
Audio::Source audio_source;

// Cameras::Isometric camera;
Core::AudioManager::BufferHandle sound_buffer;
};
struct IsoLogic {
void init(AppRenderer& renderer, entt::registry& ecs, IsoData& data) {
void init(AppRenderer& renderer, Core::AudioManager& audio, IsoData& data) {
data.start_time = std::chrono::high_resolution_clock::now();

Media::Sound sound {};

auto wav_file_data = Utily::FileReader::load_entire_file("assets/woosh.wav");
wav_file_data.on_error(Utily::ErrorHandler::print_then_quit);
sound.init_from_wav(wav_file_data.value());


// data.camera.position = { 0, 1, -1};
// data.camera.set_direction_via_angles(-45, 0);

data.audio_device.init().on_error(Utily::ErrorHandler::print_then_quit);
data.audio_context.init(data.audio_device).on_error(Utily::ErrorHandler::print_then_quit);
data.audio_buffer.init().on_error(Utily::ErrorHandler::print_then_quit);
data.audio_buffer.load_sound(sound).on_error(Utily::ErrorHandler::print_then_quit);

data.audio_source.init().on_error(Utily::ErrorHandler::print_then_quit);
data.audio_source.bind(data.audio_buffer).on_error(Utily::ErrorHandler::print_then_quit);
data.audio_source.play().on_error(Utily::ErrorHandler::print_then_quit);

wav_file_data.on_error(print_then_quit);
sound.init_from_wav(wav_file_data.value()).on_error(print_then_quit);

auto res = audio.load_sound_into_buffer(sound).on_error(print_then_quit);

data.sound_buffer = res.value();

audio.play_sound(data.sound_buffer).on_error(print_then_quit);
}
void update(float dt, const Core::InputManager& input, AppState& state, entt::registry& ecs, IsoData& data) {
void update(float dt, const Core::InputManager& input, Core::AudioManager& audio, AppState& state, IsoData& data) {
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - data.start_time);
if (duration > std::chrono::milliseconds(250)) {
data.audio_source.play().on_error(Utily::ErrorHandler::print_then_quit);
if (duration > std::chrono::milliseconds(1)) {
audio.play_sound(data.sound_buffer).on_error(print_then_quit);
data.start_time = std::chrono::high_resolution_clock::now();
}
}
void draw(AppRenderer& renderer, entt::registry& ecs, IsoData& data) {
void draw(AppRenderer& renderer, IsoData& data) {
renderer.screen_frame_buffer.bind();
renderer.screen_frame_buffer.clear(data.background_colour);
renderer.screen_frame_buffer.resize(renderer.window_width, renderer.window_height);
}
void play() {
}
void stop() {
void stop(IsoData& data) {
}
};

Expand Down
39 changes: 19 additions & 20 deletions code/Engine/include/App/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
#include "Core/Core.hpp"
#include "Profiler/Profiler.hpp"


#include "Core/Input.hpp"

#include "Audio/Audio.hpp"

#include "App/AppRenderer.hpp"

#include <chrono>
Expand All @@ -28,18 +25,18 @@ struct AppState {
};

template <typename T, typename AppData>
concept HasValidAppLogic = requires(T t, double dt, AppState& state, AppData& data, AppRenderer& renderer, const Core::InputManager& input, entt::registry& ecs) {
concept HasValidAppLogic = requires(T t, double dt, AppState& state, AppData& data, AppRenderer& renderer, Core::AudioManager& audio, const Core::InputManager& input) {
{
t.init(renderer, ecs, data)
t.init(renderer, audio, data)
} -> std::same_as<void>;
{
t.update(dt, input, state, ecs, data)
t.update(dt, input, audio, state, data)
} -> std::same_as<void>;
{
t.draw(renderer, ecs, data)
t.draw(renderer, data)
} -> std::same_as<void>;
{
t.stop()
t.stop(data)
} -> std::same_as<void>;
};

Expand All @@ -55,53 +52,55 @@ class App
AppData _data;
AppLogic _logic;

Audio::Device _audio_device;
Audio::Context _audio_context;
Core::AudioManager _audio;

Core::OpenglContext _context;
bool _has_init = false;
bool _has_stopped = false;
std::chrono::high_resolution_clock::time_point _last_update;

inline static auto _panic = [](auto& error) {
std::cerr << error.what() << std::endl;
throw std::runtime_error(std::string(error.what()));
};

public:
auto init(std::string_view app_name, uint_fast16_t width, uint_fast16_t height) -> void {
Profiler::instance().switch_to_process(Utily::Reflection::get_type_name<AppLogic>());
Profiler::Timer timer("App::init()", { "App" });

_context.init(app_name, width, height).on_error(Utily::ErrorHandler::print_then_quit);
_ecs = entt::registry {};
_logic.init(_renderer, _ecs, _data);
_input.init(_context.unsafe_window_handle());

/*_audio_device.init().on_error(Utily::ErrorHandler::print_then_quit);
_audio_context.init(_audio_device).on_error(Utily::ErrorHandler::print_then_quit);*/
_audio.init().on_error(_panic);
_ecs = entt::registry {};

_logic.init(_renderer, _audio, _data);

_has_init = true;
_has_stopped = false;
}
auto stop() -> void {
if (!_has_stopped) {
Profiler::Timer timer("App::stop()", { "App" });
_logic.stop(_data);
_renderer.stop();
_logic.stop();
_audio.stop();
_context.stop();

/*_audio_context.stop();
_audio_device.stop();*/
}
_has_stopped = true;
}
auto update() -> void {
Profiler::Timer timer("App::update()", { "App" });
double dt = std::chrono::duration<double> { std::chrono::high_resolution_clock::now() - _last_update }.count();
_logic.update(dt, _input, _state, _ecs, _data);
_logic.update(dt, _input, _audio, _state, _data);
_last_update = std::chrono::high_resolution_clock::now();
}
auto render() -> void {
Profiler::Timer timer("App::render()", { "App" });
_renderer.window_width = _context.window_width;
_renderer.window_height = _context.window_height;
_logic.draw(_renderer, _ecs, _data);
_logic.draw(_renderer, _data);
_context.swap_buffers();
}

Expand Down
8 changes: 0 additions & 8 deletions code/Engine/include/Audio/Audio.hpp

This file was deleted.

31 changes: 0 additions & 31 deletions code/Engine/include/Audio/Buffer.hpp

This file was deleted.

26 changes: 0 additions & 26 deletions code/Engine/include/Audio/Context.hpp

This file was deleted.

25 changes: 0 additions & 25 deletions code/Engine/include/Audio/Device.hpp

This file was deleted.

32 changes: 0 additions & 32 deletions code/Engine/include/Audio/Source.hpp

This file was deleted.

2 changes: 2 additions & 0 deletions code/Engine/include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace Config {
constexpr static bool ENABLE_VSYNC = false;
}



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

Expand Down
65 changes: 65 additions & 0 deletions code/Engine/include/Core/AudioManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <array>
#include <optional>
#include <span>

#include <Utily/Utily.hpp>
#include <glm/vec3.hpp>

#include "Config.hpp"
#include "Media/Sound.hpp"

namespace Core {

class AudioManager
{
public:
struct BufferHandle {
int index = -1;
};

[[nodiscard]] auto init() -> Utily::Result<void, Utily::Error>;
void stop();

[[nodiscard]] auto load_sound_into_buffer(const Media::Sound& sound) -> Utily::Result<BufferHandle, Utily::Error>;
[[nodiscard]] auto play_sound(BufferHandle buffer_handle, glm::vec3 pos = { 0, 0, 0 }) -> Utily::Result<void, Utily::Error>;

private:
constexpr static size_t MAX_BUFFERS = 1024;
constexpr static size_t MAX_SOURCES = 256;

bool _has_init = false;
bool _has_stopped = false;

std::optional<void*> _device = std::nullopt;
std::optional<void*> _context = std::nullopt;

struct Buffer {
uint32_t id = std::numeric_limits<uint32_t>::max();
bool is_populated = false;
std::optional<std::chrono::milliseconds> duration = std::nullopt;
};
struct Source {
uint32_t id = std::numeric_limits<uint32_t>::max();
std::optional<BufferHandle> attached_buffer = std::nullopt;
std::optional<std::chrono::steady_clock::time_point> expected_finish = std::nullopt;
};

std::array<Buffer, MAX_BUFFERS> _raw_buffers {};
std::array<Source, MAX_SOURCES> _raw_sources {};

std::span<Buffer> _buffers {};
std::span<Source> _sources {};

[[nodiscard]] auto init_device() -> Utily::Result<void, Utily::Error>;
[[nodiscard]] auto init_context() -> Utily::Result<void, Utily::Error>;
[[nodiscard]] auto init_buffers() -> Utily::Result<void, Utily::Error>;
[[nodiscard]] auto init_sources() -> Utily::Result<void, Utily::Error>;

void stop_device();
void stop_context();
void stop_buffers();
void stop_sources();
};
}
Loading

0 comments on commit 7180544

Please sign in to comment.