(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_EXPORT_COMPILE_COMMANDS 1)
|
||||
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")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
|
||||
endif()
|
||||
|
@@ -1,26 +1,23 @@
|
||||
#ifndef CHARGE_AUDIO_BASE_H
|
||||
#define CHARGE_AUDIO_BASE_H
|
||||
#include "miniaudio/miniaudio.h"
|
||||
|
||||
#include <Corrade/Containers/Containers.h>
|
||||
#include <Corrade/Containers/Optional.h>
|
||||
#include <Corrade/Containers/Pointer.h>
|
||||
#include <Magnum/Magnum.h>
|
||||
#include <Magnum/Math/Vector.h>
|
||||
#include <Magnum/Math/Vector3.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace ChargeAudio {
|
||||
namespace _ma {
|
||||
#include "miniaudio/miniaudio.h"
|
||||
}
|
||||
using namespace Corrade;
|
||||
using namespace _ma;
|
||||
|
||||
typedef Containers::Pointer<class Sound> SoundContainer;
|
||||
typedef Containers::Pointer<class Listener> ListenerContainer;
|
||||
class Sound {
|
||||
public:
|
||||
enum class SoundState { Idle, Playing, Paused, Finished };
|
||||
|
||||
enum class SoundType { FromFile, RawPCM };
|
||||
// No copying
|
||||
Sound(const Sound &) = delete;
|
||||
Sound &operator=(const Sound &) = delete;
|
||||
@@ -35,6 +32,8 @@ public:
|
||||
void Reset();
|
||||
|
||||
const SoundState GetState();
|
||||
const SoundType GetSoundType();
|
||||
|
||||
const float GetPlaybackTime();
|
||||
bool SetPlaybackTime(float time);
|
||||
const float GetDuration();
|
||||
@@ -45,13 +44,15 @@ public:
|
||||
|
||||
private:
|
||||
Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
|
||||
std::string additionalErrorMessage = "");
|
||||
SoundType type, std::string additionalErrorMessage = "");
|
||||
static void onSoundFinish(void *customData, ma_sound *);
|
||||
|
||||
class Engine *baseEngine;
|
||||
ma_sound maSound;
|
||||
ma_sound_config maConfig;
|
||||
ma_pcm_rb maRingBuffer;
|
||||
SoundState state = SoundState::Idle;
|
||||
SoundType type;
|
||||
|
||||
friend class Engine;
|
||||
};
|
||||
@@ -78,7 +79,7 @@ private:
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
Engine();
|
||||
Engine(uint32_t sampleRate = 44100, uint32_t channels = 2);
|
||||
|
||||
// No copying
|
||||
Engine(const Engine &) = delete;
|
||||
@@ -91,15 +92,21 @@ public:
|
||||
~Engine();
|
||||
|
||||
// Creating tools
|
||||
SoundContainer CreateSound(int bufferLengthInSeconds);
|
||||
SoundContainer CreateSound(std::string filepath, bool streamFile = false);
|
||||
ListenerContainer CreateListener();
|
||||
|
||||
void SetVolume(float value);
|
||||
const float GetVolume();
|
||||
|
||||
uint32_t GetSampleRate();
|
||||
uint32_t GetChannelCount();
|
||||
|
||||
private:
|
||||
ma_engine maEngine;
|
||||
ma_engine_config maConfig;
|
||||
ma_result maResponse;
|
||||
ma_decoder maStero;
|
||||
ma_uint64 listenerCounter = 0;
|
||||
friend class Listener;
|
||||
friend class Sound;
|
||||
|
@@ -1,19 +1,19 @@
|
||||
#include "ChargeAudio.hpp"
|
||||
#include <Corrade/Containers/Containers.h>
|
||||
#include <Corrade/Containers/Pointer.h>
|
||||
#include <Corrade/Tags.h>
|
||||
#include <Corrade/Utility/Debug.h>
|
||||
#include <Magnum/Math/Vector3.h>
|
||||
#include "miniaudio/miniaudio.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <Corrade/Utility/Debug.h>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
using namespace ChargeAudio;
|
||||
using namespace Corrade;
|
||||
|
||||
Engine::Engine() {
|
||||
if ((maResponse = ma_engine_init(NULL, &maEngine)) != MA_SUCCESS) {
|
||||
Engine::Engine(uint32_t sampleRate, uint32_t channels) {
|
||||
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 << ")";
|
||||
throw new std::runtime_error(
|
||||
"Failed to init miniaudio engine. Check STDERR for more info.");
|
||||
@@ -22,6 +22,27 @@ Engine::Engine() {
|
||||
|
||||
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,
|
||||
bool streamFile) {
|
||||
return SoundContainer(new Sound(
|
||||
@@ -30,6 +51,7 @@ SoundContainer Engine::CreateSound(const std::string filepath,
|
||||
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));
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,4 @@
|
||||
#include "ChargeAudio.hpp"
|
||||
#include <Magnum/Magnum.h>
|
||||
#include <Magnum/Math/Vector3.h>
|
||||
|
||||
using namespace ChargeAudio;
|
||||
|
||||
|
@@ -1,14 +1,11 @@
|
||||
#include "ChargeAudio.hpp"
|
||||
#include <Corrade/Utility/Debug.h>
|
||||
#include <Magnum/Magnum.h>
|
||||
#include <Magnum/Math/Vector3.h>
|
||||
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace ChargeAudio;
|
||||
Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
||||
std::string additionalErrorMessage)
|
||||
SoundType soundType, std::string additionalErrorMessage)
|
||||
: baseEngine(engine) {
|
||||
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
|
||||
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" +
|
||||
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::SoundType Sound::GetSoundType() { return type; }
|
||||
|
||||
const float Sound::GetDuration() {
|
||||
float time;
|
||||
|
Reference in New Issue
Block a user