Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@
[submodule "vendor/assimp"]
path = vendor/assimp
url = https://github.com/assimp/assimp.git
[submodule "vendor/miniaudio"]
path = vendor/miniaudio
url = https://github.com/mackron/miniaudio.git
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export examplesDir := $(abspath examples)
export utilsLib := $(libDir)/libutils.a
export resourcesLib := $(libDir)/libresources.a
export windowLib := $(libDir)/libwindow.a
export audioLib := $(libDir)/libaudio.a
export coreLib := $(libDir)/libcore.a
export renderLib := $(libDir)/librender.a
export testApp := $(binDir)/tests/build/app
Expand All @@ -70,7 +71,10 @@ $(resourcesLib): buildFlags $(utilsLib)
$(windowLib): buildFlags $(utilsLib)
"$(MAKE)" -C $(engineDir)/window CXXFLAGS="$(CXXFLAGS)"

$(coreLib): buildFlags $(utilsLib)
$(audioLib): buildFlags $(utilsLib)
"$(MAKE)" -C $(engineDir)/audio CXXFLAGS="$(CXXFLAGS)"

$(coreLib): buildFlags $(utilsLib) $(audioLib) # TODO remove redundant audio dependency
"$(MAKE)" -C $(engineDir)/core CXXFLAGS="$(CXXFLAGS)"

$(renderLib): buildFlags $(utilsLib) $(windowLib)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
[![macOS Status](../../workflows/macOS/badge.svg)](https://github.com/CapsCollective/siege-engine/actions/workflows/macOS.yml)
[![Linux Status](../../workflows/Ubuntu/badge.svg)](https://github.com/CapsCollective/siege-engine/actions/workflows/ubuntu.yml)
[![Windows Status](../../workflows/Windows/badge.svg)](https://github.com/CapsCollective/siege-engine/actions/workflows/windows.yml)
[![Tests Status](../../workflows/Tests/badge.svg)](https://github.com/CapsCollective/siege-engine/actions/workflows/tests.yml)
[![Formatting Status](../../workflows/Formatting/badge.svg)](https://github.com/CapsCollective/siege-engine/actions/workflows/formatting.yml)
[![License: Zlib](https://img.shields.io/badge/License-Zlib-lightgrey.svg)](https://opensource.org/licenses/Zlib)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)

Sometimes game development can feel like a bit of a slog; an endless battle in which the developer is set against an overwhelming bulwark of required knowledge to create a functional game. On one hand, one must understand _linear algebra, calculus, and the intricacies of the rasterised rendering pipeline_ whilst also expecting a mastery of _memory mangement, build systems, algorithms, and coherent software architecture_.
Sometimes game development can feel like a bit of a slog; an endless battle in which the developer is set against an overwhelming bulwark of required knowledge to create a functional game. On one hand, one must understand _linear algebra, calculus, and the intricacies of the rasterised rendering pipeline_ whilst also expecting a mastery of _memory management, build systems, algorithms, and coherent software architecture_.

The reality is that making games is hard, and making games can be even harder when one does not fully understand the underlying foundations below the many layers of abstraction present in many commercial and general game engines. Using them can often feel disempowering as they discourage deeper learning through sheer size and complexity.

The ultimate purpose of this project is to act as the metaphorical siege engine to break down the barriers to game engine development. To this end, Siege aims to be a **light-weight, cross platform, 3D game engine**. In doing so, we hope to create an engine for game developers, by game developers.
The ultimate purpose of this project is to act as the metaphorical siege engine to break down the barriers to game engine development. To this end, Siege aims to be a **light-weight, cross-platform, 3D game engine**. In doing so, we hope to create an engine for game developers, by game developers.

### Current Compatability

Expand Down Expand Up @@ -156,6 +155,7 @@ You can see examples of this in any of the application targets under `examples`.
```
[root]
├─[engine]
│ ├─[audio] <- the engine's audio system
│ ├─[core] <- the engine's core library
│ ├─[render] <- the engine's renderer
| ├─[resources] <- the engine's resource types and loading system
Expand Down
343 changes: 343 additions & 0 deletions engine/audio/AudioSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//

#include "AudioSystem.h"

#include <utils/Logging.h>

#include <miniaudio.c>
#include <mutex>

#define MA_NO_RESOURCE_MANAGER

#define AUDIO_SYSTEM_MAX_VOICES 16

namespace Siege
{

struct Voice
{
uint64_t playerId = 0;
ma_decoder decoder = {};
ma_sound sound = {};
bool pendingKill = false;
};

static ma_engine audioEngine;
static ma_decoder_config decoderConfig;

static Voice voices[AUDIO_SYSTEM_MAX_VOICES];
static std::mutex voicesMutex;

bool SoundPlayer::Play()
{
return AudioSystem::GetInstance().AssignPlayerToVoice(this);
}

bool SoundPlayer::Stop()
{
return AudioSystem::GetInstance().UnassignPlayerFromVoices(this);
}

bool SoundPlayer::UpdateVoices()
{
return AudioSystem::GetInstance().UpdateSoundPlayerVoices(this);
}

bool SoundPlayer::IsPlaying() const
{
return AudioSystem::GetInstance().IsSoundPlayingForPlayer(this);
}

AudioSystem& AudioSystem::GetInstance()
{
static AudioSystem audioSystem;
return audioSystem;
}

bool AudioSystem::Initialise()
{
decoderConfig = ma_decoder_config_init_default();
ma_engine_config engineConfig = ma_engine_config_init();
engineConfig.listenerCount = 1;
ma_result result = ma_engine_init(&engineConfig, &audioEngine);
return result == MA_SUCCESS;
}

static int32_t FindPolyphonyCountForSound(const SoundPlayer* soundPlayer)
{
int32_t count = 0;
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
const Voice& voice = voices[i];
if (voice.playerId == soundPlayer->playerId)
{
++count;
}
}
return count;
}

static int32_t FindAvailableVoice()
{
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
const Voice& voice = voices[i];
uint64_t id = voice.playerId;
if (id == 0)
{
return i;
}
}
return -1;
}

static void OnSoundEnded(void* userData, ma_sound* sound)
{
std::scoped_lock lock(voicesMutex);
uint64_t voiceIdx = reinterpret_cast<uint64_t>(userData);
Voice& voice = voices[voiceIdx];
voice.pendingKill = true;
CC_LOG_INFO("Marked pending kill sound clip with id {} on voice idx {}",
voice.playerId,
voiceIdx)
}

bool AudioSystem::AssignPlayerToVoice(SoundPlayer* soundPlayer)
{
std::scoped_lock lock(voicesMutex);

void* clipData = soundPlayer->clipData->data;
uint32_t clipDataLength = soundPlayer->clipData->dataSize;

if (soundPlayer->playerId != 0 && soundPlayer->maxPolyphony != 0)
{
int32_t currentPolyphony = FindPolyphonyCountForSound(soundPlayer);
if (currentPolyphony >= soundPlayer->maxPolyphony)
{
CC_LOG_WARNING("Cannot play requested clip at address above {} max polyphony of {}",
reinterpret_cast<uint64_t>(clipData),
soundPlayer->maxPolyphony);
return false;
}
}

int32_t voiceIdx = FindAvailableVoice();
if (voiceIdx == -1)
{
CC_LOG_WARNING("Failed to find available voice for requested clip at address {}",
reinterpret_cast<uint64_t>(clipData));
return false;
}

Voice& voice = voices[voiceIdx];

ma_result result =
ma_decoder_init_memory(clipData, clipDataLength, &decoderConfig, &voice.decoder);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to initialise decoder for requested clip at address {}",
reinterpret_cast<uint64_t>(clipData))
return false;
}

result = ma_sound_init_from_data_source(&audioEngine, &voice.decoder, 0, nullptr, &voice.sound);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to initialise sound for requested clip at address {}",
reinterpret_cast<uint64_t>(clipData))
result = ma_decoder_uninit(&voice.decoder);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to uninitialise decoder after bad sound initialisation")
}
return false;
}

static uint64_t globalId = 0;
if (soundPlayer->playerId == 0)
{
soundPlayer->playerId = ++globalId;
}
voice.playerId = soundPlayer->playerId;

result =
ma_sound_set_end_callback(&voice.sound, OnSoundEnded, reinterpret_cast<void*>(voiceIdx));
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to set sound ended callback for requested clip at address {}",
reinterpret_cast<uint64_t>(clipData))
ma_sound_uninit(&voice.sound);
result = ma_decoder_uninit(&voice.decoder);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to uninitialise decoder after failure to set callback")
}
--globalId;
return false;
}

if (soundPlayer->looping)
{
ma_sound_set_looping(&voice.sound, true);
}

ma_sound_set_spatialization_enabled(&voice.sound, !soundPlayer->disableSpatialisation);
if (!soundPlayer->disableSpatialisation)
{
const Vec3& pos = soundPlayer->location;
ma_sound_set_position(&voice.sound, pos.x, pos.y, pos.z);
}

result = ma_sound_start(&voice.sound);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to start sound for requested clip at address {}",
reinterpret_cast<uint64_t>(clipData))
ma_sound_uninit(&voice.sound);
result = ma_decoder_uninit(&voice.decoder);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to uninitialise decoder after sound failed to start")
}
--globalId;
return false;
}

CC_LOG_INFO("Started playing sound clip at address {} with id {} on voice idx {}",
reinterpret_cast<uint64_t>(clipData),
voice.playerId,
voiceIdx)
return true;
}

bool AudioSystem::UnassignPlayerFromVoices(const SoundPlayer* soundPlayer)
{
std::scoped_lock lock(voicesMutex);
bool stoppedSounds = false;
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
Voice& voice = voices[i];
if (voice.playerId != soundPlayer->playerId)
{
continue;
}

ma_result result = ma_sound_stop(&voice.sound);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to stop sound with id {} on voice idx {}", voice.playerId, i)
}
CC_LOG_INFO("Stopped sound clip with id {} on voice idx {}", voice.playerId, i)

voice.pendingKill = true;
CC_LOG_INFO("Marked pending kill sound clip with id {} on voice idx {}", voice.playerId, i)
stoppedSounds = true;
}
return stoppedSounds;
}

bool AudioSystem::UpdateSoundPlayerVoices(const SoundPlayer* soundPlayer)
{
std::scoped_lock lock(voicesMutex);
bool foundVoices = false;
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
Voice& voice = voices[i];
if (voice.playerId != soundPlayer->playerId)
{
continue;
}

const Vec3& pos = soundPlayer->location;
ma_sound_set_position(&voice.sound, pos.x, pos.y, pos.z);
foundVoices = true;
}
return foundVoices;
}

bool AudioSystem::IsSoundPlayingForPlayer(const SoundPlayer* soundPlayer) const
{
std::scoped_lock lock(voicesMutex);
int32_t currentPolyphony = FindPolyphonyCountForSound(soundPlayer);
return currentPolyphony > 0;
}

void AudioSystem::SetSoundListenerTransform(const Xform& transform)
{
uint32_t listenerIdx = 0;
ma_engine_listener_set_world_up(&audioEngine, listenerIdx, 0, 1, 0);

const Vec3& pos = transform.GetPosition();
const Vec3& rot = transform.GetRotation();
ma_engine_listener_set_position(&audioEngine, listenerIdx, pos.x, pos.y, pos.z);
ma_engine_listener_set_direction(&audioEngine, listenerIdx, rot.x, rot.y, rot.z);
}

void AudioSystem::Update()
{
std::scoped_lock lock(voicesMutex);
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
Voice& voice = voices[i];
uint64_t id = voice.playerId;
if (id == 0)
{
continue;
}

if (!voice.pendingKill)
{
continue;
}

ma_sound_uninit(&voice.sound);
ma_result result = ma_decoder_uninit(&voice.decoder);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to uninitialise decoder")
}

std::memset(&voice, 0, sizeof(Voice));
CC_LOG_INFO("Flushed voice playing sound clip with id {} on voice idx {}", id, i)
}
}

void AudioSystem::Uninitialise()
{
{
std::scoped_lock lock(voicesMutex);
for (int32_t i = 0; i < AUDIO_SYSTEM_MAX_VOICES; ++i)
{
Voice& voice = voices[i];
uint64_t id = voice.playerId;
if (id == 0)
{
continue;
}

ma_result result = ma_sound_stop(&voice.sound);
if (result != MA_SUCCESS)
{
CC_LOG_ERROR("Failed to stop sound with id {} on voice idx {}", voice.playerId, i)
}
CC_LOG_INFO("Stopped sound clip with id {} on voice idx {}", voice.playerId, i)

voice.pendingKill = true;
CC_LOG_INFO("Marked pending kill sound clip with id {} on voice idx {}",
voice.playerId,
i)
}
}
Update();

ma_engine_uninit(&audioEngine);
}

} // namespace Siege
Loading
Loading