Compare commits

..

12 Commits

5 changed files with 209 additions and 52 deletions

View File

@@ -5,7 +5,7 @@ project(ChargeAudio VERSION 1.0)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_MODULE_PATH "modules/" ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH "modules/" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-subobject-linkage")
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
endif() endif()

View File

@@ -1,47 +1,75 @@
#ifndef CHARGE_AUDIO_BASE_H #ifndef CHARGE_AUDIO_BASE_H
#define CHARGE_AUDIO_BASE_H #define CHARGE_AUDIO_BASE_H
#include "miniaudio/miniaudio.h"
#include <Corrade/Containers/Containers.h> #include <Corrade/Containers/Containers.h>
#include <Magnum/Magnum.h> #include <Corrade/Containers/Pointer.h>
#include <Magnum/Math/Vector.h> #include <Magnum/Math/Vector3.h>
#include <functional>
#include <string> #include <string>
namespace ChargeAudio { namespace ChargeAudio {
namespace _ma {
#include "miniaudio/miniaudio.h"
}
using namespace Corrade; using namespace Corrade;
using namespace _ma;
typedef Containers::Pointer<class Sound> SoundContainer;
typedef Containers::Pointer<class Listener> ListenerContainer;
class Sound { class Sound {
public: public:
enum SoundState { Idle, Playing, Paused, Finished }; enum class SoundState { Idle, Playing, Paused, Finished };
enum class SoundType { FromFile, StreamedRawPCM, RawPCM };
// No copying
Sound(const Sound &) = delete;
Sound &operator=(const Sound &) = delete;
// No moving
Sound(Sound &&) = delete;
Sound &operator=(Sound &&) = delete;
~Sound(); ~Sound();
void Play(); void Play();
void Pause(); void Pause();
void Reset(); void Reset();
SoundState GetState(); const SoundState GetState();
const SoundType GetSoundType();
const float GetPlaybackTime();
bool SetPlaybackTime(float time);
const float GetDuration();
void SetPosition(Magnum::Vector3 position); void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition(); const Magnum::Vector3 GetPosition();
void SetVolume(float value); void SetVolume(float value);
float GetVolume(); const float GetVolume();
private: private:
Sound(class Engine *engine); Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
SoundType type, std::string additionalErrorMessage = "");
static void onSoundFinish(void *customData, ma_sound *); static void onSoundFinish(void *customData, ma_sound *);
class Engine *baseEngine; class Engine *baseEngine;
ma_sound maSound; ma_sound maSound;
ma_sound_config maConfig;
ma_pcm_rb maRingBuffer;
ma_audio_buffer maAudioBuffer;
SoundState state = SoundState::Idle; SoundState state = SoundState::Idle;
SoundType type;
friend class Engine; friend class Engine;
}; };
class Listener { class Listener {
public: public:
// No copying, can move
Listener(const Listener &) = delete;
Listener &operator=(const Listener &) = delete;
void SetEnabled(bool isEnabled); void SetEnabled(bool isEnabled);
bool GetEnabled(); const bool GetEnabled();
void SetDirection(Magnum::Vector3 position);
const Magnum::Vector3 GetDirection();
void SetPosition(Magnum::Vector3 position); void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition(); const Magnum::Vector3 GetPosition();
private: private:
Listener(); Listener();
@@ -52,19 +80,35 @@ private:
class Engine { class Engine {
public: public:
Engine(); Engine(uint32_t sampleRate = 44100, uint32_t channels = 2);
// No copying
Engine(const Engine &) = delete;
Engine &operator=(const Engine &) = delete;
// No movement
Engine(Engine &&) = delete;
Engine &operator=(Engine &&) = delete;
~Engine(); ~Engine();
// Creating tools // Creating tools
Containers::Pointer<Sound> CreateSound(std::string filepath); SoundContainer CreateSound(int bufferLengthInSeconds);
Containers::Pointer<Listener> CreateListener(); SoundContainer CreateSound(uint8_t *data, int length);
SoundContainer CreateSound(std::string filepath, bool streamFile = false);
ListenerContainer CreateListener();
void SetVolume(float value); void SetVolume(float value);
float GetVolume(); const float GetVolume();
uint32_t GetSampleRate();
uint32_t GetChannelCount();
private: private:
ma_engine maEngine; ma_engine maEngine;
ma_engine_config maConfig;
ma_result maResponse; ma_result maResponse;
ma_decoder maStero;
ma_uint64 listenerCounter = 0; ma_uint64 listenerCounter = 0;
friend class Listener; friend class Listener;
friend class Sound; friend class Sound;

View File

@@ -1,17 +1,19 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include <Corrade/Containers/Pointer.h> #include "miniaudio/miniaudio.h"
#include <Corrade/Utility/Debug.h>
#include <Magnum/Math/Vector3.h>
#include <cstddef> #include <Corrade/Utility/Debug.h>
#include <cstdint>
#include <stdexcept> #include <stdexcept>
#include <string>
using namespace ChargeAudio; using namespace ChargeAudio;
using namespace Corrade; using namespace Corrade;
Engine::Engine() { Engine::Engine(uint32_t sampleRate, uint32_t channels) {
if ((maResponse = ma_engine_init(NULL, &maEngine)) != MA_SUCCESS) { maConfig = ma_engine_config_init();
maConfig.sampleRate = sampleRate;
maConfig.channels = channels;
if ((maResponse = ma_engine_init(&maConfig, &maEngine)) != MA_SUCCESS) {
Utility::Error{} << "Could not init miniaudio (" << maResponse << ")"; Utility::Error{} << "Could not init miniaudio (" << maResponse << ")";
throw new std::runtime_error( throw new std::runtime_error(
"Failed to init miniaudio engine. Check STDERR for more info."); "Failed to init miniaudio engine. Check STDERR for more info.");
@@ -20,25 +22,67 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); } Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) { uint32_t Engine::GetSampleRate() { return maEngine.sampleRate; }
auto sound = Containers::Pointer<Sound>(new Sound(this)); uint32_t Engine::GetChannelCount() { return ma_engine_get_channels(&maEngine); }
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL,
NULL, &sound->maSound);
sound->maSound.endCallback = Sound::onSoundFinish;
sound->maSound.pEndCallbackUserData = sound.get();
if (maResponse != MA_SUCCESS) { // Use case: Stream of PCM data
Utility::Error{} << "Failed to create the sound from the file: " SoundContainer Engine::CreateSound(int bufferLengthInSeconds) {
<< filepath.c_str() << " (" << maResponse << ")"; return SoundContainer(new Sound(
throw new std::runtime_error( this,
"Failed to create the sound from file. Check STDERR for more info."); [length = bufferLengthInSeconds, channels = GetChannelCount(),
sampleRate = GetSampleRate()](Sound *sound) {
ma_result result = ma_pcm_rb_init(
ma_format_s32, channels, sampleRate * channels * length, nullptr,
nullptr, &sound->maRingBuffer);
if (result != MA_SUCCESS) {
Utility::Error{} << "Failed to create a new ring buffer!" << " ("
<< result << ")";
throw new std::runtime_error("Failed to create a new ring buffer! "
"Check STDERR for more info.");
} }
},
return sound; Sound::SoundType::StreamedRawPCM,
"Failed to create the sound from ring buffer: "));
} }
Containers::Pointer<Listener> Engine::CreateListener() { // Use case: 1 time set up and use audio
auto listener = Containers::Pointer<Listener>(new Listener()); SoundContainer Engine::CreateSound(uint8_t *data, int length) {
return SoundContainer(new Sound(
this,
[data, length, channels = GetChannelCount(),
sampleRate = GetSampleRate()](Sound *sound) {
ma_audio_buffer_config config = ma_audio_buffer_config_init(
ma_format_s32, channels, length / (4 * channels), data, nullptr);
ma_result result =
ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer);
if (result != MA_SUCCESS) {
Utility::Error{} << "Failed to create a new audio buffer!" << " ("
<< result << ")";
throw new std::runtime_error("Failed to create a new audio buffer! "
"Check STDERR for more info.");
}
sound->maConfig.pDataSource = &sound->maAudioBuffer;
},
Sound::SoundType::RawPCM,
"Failed to create the sound from the PCM data: "));
}
SoundContainer Engine::CreateSound(const std::string filepath,
bool streamFile) {
return SoundContainer(new Sound(
this,
[filepath, streamFile](Sound *sound) {
sound->maConfig.pFilePath = filepath.c_str();
sound->maConfig.flags = (streamFile ? MA_SOUND_FLAG_STREAM : 0);
},
Sound::SoundType::FromFile,
"Failed to create the sound from the file: " + filepath));
}
ListenerContainer Engine::CreateListener() {
auto listener = ListenerContainer(new Listener());
listener->baseEngine = this; listener->baseEngine = this;
listener->listenerID = listenerCounter++; listener->listenerID = listenerCounter++;
@@ -47,4 +91,4 @@ Containers::Pointer<Listener> Engine::CreateListener() {
// Controls // Controls
void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); } void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); }
float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); } const float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); }

View File

@@ -1,5 +1,4 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include <Magnum/Math/Vector3.h>
using namespace ChargeAudio; using namespace ChargeAudio;
@@ -10,15 +9,27 @@ void Listener::SetEnabled(bool isEnabled) {
ma_engine_listener_set_enabled(&baseEngine->maEngine, listenerID, isEnabled); ma_engine_listener_set_enabled(&baseEngine->maEngine, listenerID, isEnabled);
} }
bool Listener::GetEnabled() { const bool Listener::GetEnabled() {
return ma_engine_listener_is_enabled(&baseEngine->maEngine, listenerID); return ma_engine_listener_is_enabled(&baseEngine->maEngine, listenerID);
} }
void Listener::SetDirection(Magnum::Vector3 position) {
ma_engine_listener_set_direction(&baseEngine->maEngine, listenerID,
position.x(), position.y(), position.z());
}
const Magnum::Vector3 Listener::GetDirection() {
ma_vec3f dir =
ma_engine_listener_get_direction(&baseEngine->maEngine, listenerID);
return Magnum::Vector3{dir.x, dir.y, dir.z};
}
void Listener::SetPosition(Magnum::Vector3 position) { void Listener::SetPosition(Magnum::Vector3 position) {
ma_engine_listener_set_position(&baseEngine->maEngine, listenerID, ma_engine_listener_set_position(&baseEngine->maEngine, listenerID,
position.x(), position.y(), position.z()); position.x(), position.y(), position.z());
} }
Magnum::Vector3 Listener::GetPosition() { const Magnum::Vector3 Listener::GetPosition() {
ma_vec3f pos = ma_vec3f pos =
ma_engine_listener_get_position(&baseEngine->maEngine, listenerID); ma_engine_listener_get_position(&baseEngine->maEngine, listenerID);
return Magnum::Vector3(pos.x, pos.y, pos.z); return Magnum::Vector3(pos.x, pos.y, pos.z);

View File

@@ -1,13 +1,71 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector3.h> #include <functional>
#include <stdexcept>
using namespace ChargeAudio; using namespace ChargeAudio;
Sound::Sound(Engine *engine) : baseEngine(engine) {} Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
SoundType soundType, std::string additionalErrorMessage)
: baseEngine(engine) {
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
maConfig.endCallback = Sound::onSoundFinish;
maConfig.pEndCallbackUserData = this;
setupFunction(this);
ma_result maResponse =
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
if (maResponse != MA_SUCCESS) {
Utility::Error{} << "Failed to create a new sound" << " (" << maResponse
<< ")";
throw new std::runtime_error(
"Failed to create a new sound. Check STDERR for more info.\n" +
additionalErrorMessage);
}
type = soundType;
}
Sound::~Sound() { ma_sound_uninit(&maSound); } Sound::~Sound() {
switch (type) {
case Sound::SoundType::RawPCM:
ma_audio_buffer_uninit_and_free(&maAudioBuffer);
break;
case Sound::SoundType::StreamedRawPCM:
ma_pcm_rb_uninit(&maRingBuffer);
break;
default:
break;
}
ma_sound_uninit(&maSound);
}
Sound::SoundState Sound::GetState() { return state; } const Sound::SoundState Sound::GetState() { return state; }
const Sound::SoundType Sound::GetSoundType() { return type; }
const float Sound::GetDuration() {
float time;
ma_sound_get_length_in_seconds(&this->maSound, &time);
return time;
}
const float Sound::GetPlaybackTime() {
float time;
ma_sound_get_cursor_in_seconds(&this->maSound, &time);
return time;
}
// true or false depending on if the playback was set
bool Sound::SetPlaybackTime(float time) {
// Better to just catch it from the start
if (time < 0) {
return false;
}
bool result = ma_sound_seek_to_second(&maSound, time) != MA_SUCCESS;
if (result) {
Utility::Error{} << "Failed to set playback time to " << time
<< " on a sound instance";
}
return result;
}
// Controls // Controls
void Sound::Play() { void Sound::Play() {
@@ -25,12 +83,12 @@ void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); }
void Sound::SetPosition(Magnum::Vector3 position) { void Sound::SetPosition(Magnum::Vector3 position) {
ma_sound_set_position(&maSound, position.x(), position.y(), position.z()); ma_sound_set_position(&maSound, position.x(), position.y(), position.z());
} }
Magnum::Vector3 Sound::GetPosition() { const Magnum::Vector3 Sound::GetPosition() {
ma_vec3f pos = ma_sound_get_position(&maSound); ma_vec3f pos = ma_sound_get_position(&maSound);
return Magnum::Vector3(pos.x, pos.y, pos.z); return Magnum::Vector3(pos.x, pos.y, pos.z);
} }
void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); } void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); }
float Sound::GetVolume() { return ma_sound_get_volume(&maSound); } const float Sound::GetVolume() { return ma_sound_get_volume(&maSound); }
// STATICs // STATICs
void Sound::onSoundFinish(void *customData, ma_sound *) { void Sound::onSoundFinish(void *customData, ma_sound *) {