(Large) Added sample rate, channel count, RingBuffered Audio(Raw PCM), Sound types, and cleared headers
This commit is contained in:
@@ -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()
|
||||||
|
@@ -1,26 +1,23 @@
|
|||||||
#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 <Corrade/Containers/Optional.h>
|
|
||||||
#include <Corrade/Containers/Pointer.h>
|
#include <Corrade/Containers/Pointer.h>
|
||||||
#include <Magnum/Magnum.h>
|
#include <Magnum/Math/Vector3.h>
|
||||||
#include <Magnum/Math/Vector.h>
|
|
||||||
#include <functional>
|
#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 Sound> SoundContainer;
|
||||||
typedef Containers::Pointer<class Listener> ListenerContainer;
|
typedef Containers::Pointer<class Listener> ListenerContainer;
|
||||||
class Sound {
|
class Sound {
|
||||||
public:
|
public:
|
||||||
enum class SoundState { Idle, Playing, Paused, Finished };
|
enum class SoundState { Idle, Playing, Paused, Finished };
|
||||||
|
enum class SoundType { FromFile, RawPCM };
|
||||||
// No copying
|
// No copying
|
||||||
Sound(const Sound &) = delete;
|
Sound(const Sound &) = delete;
|
||||||
Sound &operator=(const Sound &) = delete;
|
Sound &operator=(const Sound &) = delete;
|
||||||
@@ -35,6 +32,8 @@ public:
|
|||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
const SoundState GetState();
|
const SoundState GetState();
|
||||||
|
const SoundType GetSoundType();
|
||||||
|
|
||||||
const float GetPlaybackTime();
|
const float GetPlaybackTime();
|
||||||
bool SetPlaybackTime(float time);
|
bool SetPlaybackTime(float time);
|
||||||
const float GetDuration();
|
const float GetDuration();
|
||||||
@@ -45,13 +44,15 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
|
Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
|
||||||
std::string additionalErrorMessage = "");
|
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_sound_config maConfig;
|
||||||
|
ma_pcm_rb maRingBuffer;
|
||||||
SoundState state = SoundState::Idle;
|
SoundState state = SoundState::Idle;
|
||||||
|
SoundType type;
|
||||||
|
|
||||||
friend class Engine;
|
friend class Engine;
|
||||||
};
|
};
|
||||||
@@ -78,7 +79,7 @@ private:
|
|||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine(uint32_t sampleRate = 44100, uint32_t channels = 2);
|
||||||
|
|
||||||
// No copying
|
// No copying
|
||||||
Engine(const Engine &) = delete;
|
Engine(const Engine &) = delete;
|
||||||
@@ -91,15 +92,21 @@ public:
|
|||||||
~Engine();
|
~Engine();
|
||||||
|
|
||||||
// Creating tools
|
// Creating tools
|
||||||
|
SoundContainer CreateSound(int bufferLengthInSeconds);
|
||||||
SoundContainer CreateSound(std::string filepath, bool streamFile = false);
|
SoundContainer CreateSound(std::string filepath, bool streamFile = false);
|
||||||
ListenerContainer CreateListener();
|
ListenerContainer CreateListener();
|
||||||
|
|
||||||
void SetVolume(float value);
|
void SetVolume(float value);
|
||||||
const 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;
|
||||||
|
@@ -1,19 +1,19 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
#include <Corrade/Containers/Containers.h>
|
#include "miniaudio/miniaudio.h"
|
||||||
#include <Corrade/Containers/Pointer.h>
|
|
||||||
#include <Corrade/Tags.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.");
|
||||||
@@ -22,6 +22,27 @@ Engine::Engine() {
|
|||||||
|
|
||||||
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
||||||
|
|
||||||
|
uint32_t Engine::GetSampleRate() { return maEngine.sampleRate; }
|
||||||
|
uint32_t Engine::GetChannelCount() { return ma_engine_get_channels(&maEngine); }
|
||||||
|
|
||||||
|
SoundContainer Engine::CreateSound(int bufferLengthInSeconds) {
|
||||||
|
return SoundContainer(new Sound(
|
||||||
|
this,
|
||||||
|
[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.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Sound::SoundType::RawPCM, "Failed to create the sound from the data: "));
|
||||||
|
}
|
||||||
|
|
||||||
SoundContainer Engine::CreateSound(const std::string filepath,
|
SoundContainer Engine::CreateSound(const std::string filepath,
|
||||||
bool streamFile) {
|
bool streamFile) {
|
||||||
return SoundContainer(new Sound(
|
return SoundContainer(new Sound(
|
||||||
@@ -30,6 +51,7 @@ SoundContainer Engine::CreateSound(const std::string filepath,
|
|||||||
sound->maConfig.pFilePath = filepath.c_str();
|
sound->maConfig.pFilePath = filepath.c_str();
|
||||||
sound->maConfig.flags = (streamFile ? MA_SOUND_FLAG_STREAM : 0);
|
sound->maConfig.flags = (streamFile ? MA_SOUND_FLAG_STREAM : 0);
|
||||||
},
|
},
|
||||||
|
Sound::SoundType::FromFile,
|
||||||
"Failed to create the sound from the file: " + filepath));
|
"Failed to create the sound from the file: " + filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,4 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
#include <Magnum/Magnum.h>
|
|
||||||
#include <Magnum/Math/Vector3.h>
|
|
||||||
|
|
||||||
using namespace ChargeAudio;
|
using namespace ChargeAudio;
|
||||||
|
|
||||||
|
@@ -1,14 +1,11 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
#include <Corrade/Utility/Debug.h>
|
|
||||||
#include <Magnum/Magnum.h>
|
|
||||||
#include <Magnum/Math/Vector3.h>
|
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
using namespace ChargeAudio;
|
using namespace ChargeAudio;
|
||||||
Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
||||||
std::string additionalErrorMessage)
|
SoundType soundType, std::string additionalErrorMessage)
|
||||||
: baseEngine(engine) {
|
: baseEngine(engine) {
|
||||||
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
|
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
|
||||||
maConfig.endCallback = Sound::onSoundFinish;
|
maConfig.endCallback = Sound::onSoundFinish;
|
||||||
@@ -23,10 +20,16 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
|||||||
"Failed to create a new sound. Check STDERR for more info.\n" +
|
"Failed to create a new sound. Check STDERR for more info.\n" +
|
||||||
additionalErrorMessage);
|
additionalErrorMessage);
|
||||||
}
|
}
|
||||||
|
type = soundType;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sound::~Sound() {
|
||||||
|
ma_pcm_rb_uninit(&maRingBuffer);
|
||||||
|
ma_sound_uninit(&maSound);
|
||||||
}
|
}
|
||||||
|
|
||||||
Sound::~Sound() { ma_sound_uninit(&maSound); }
|
|
||||||
const Sound::SoundState Sound::GetState() { return state; }
|
const Sound::SoundState Sound::GetState() { return state; }
|
||||||
|
const Sound::SoundType Sound::GetSoundType() { return type; }
|
||||||
|
|
||||||
const float Sound::GetDuration() {
|
const float Sound::GetDuration() {
|
||||||
float time;
|
float time;
|
||||||
|
Reference in New Issue
Block a user