Compare commits
2 Commits
64a5d19464
...
5291b08330
| Author | SHA1 | Date | |
|---|---|---|---|
| 5291b08330 | |||
| 4f886574f8 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
|||||||
[submodule "lib/miniaudio"]
|
[submodule "lib/miniaudio"]
|
||||||
path = lib/miniaudio
|
path = lib/miniaudio
|
||||||
url = https://github.com/mackron/miniaudio
|
url = https://github.com/mackron/miniaudio
|
||||||
|
[submodule "lib/magic_enum"]
|
||||||
|
path = lib/magic_enum
|
||||||
|
url = https://github.com/Neargye/magic_enum
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ 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()
|
||||||
|
|
||||||
add_subdirectory(lib/miniaudio)
|
add_subdirectory(lib/miniaudio)
|
||||||
|
add_subdirectory(lib/magic_enum)
|
||||||
|
|
||||||
find_package(Corrade REQUIRED Main)
|
find_package(Corrade REQUIRED Main)
|
||||||
find_package(Magnum REQUIRED)
|
find_package(Magnum REQUIRED)
|
||||||
@@ -20,8 +22,9 @@ pkg_check_modules(AVUTIL REQUIRED libavutil)
|
|||||||
pkg_check_modules(SWRESAMPLE REQUIRED libswresample)
|
pkg_check_modules(SWRESAMPLE REQUIRED libswresample)
|
||||||
|
|
||||||
add_library(
|
add_library(
|
||||||
ChargeAudio SHARED "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp"
|
ChargeAudio SHARED
|
||||||
"src/Listener.cpp" "lib/miniaudio/miniaudio.c")
|
"src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp" "src/Listener.cpp"
|
||||||
|
"src/ErrorHandling.cpp" "lib/miniaudio/miniaudio.c")
|
||||||
|
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
ChargeAudio
|
ChargeAudio
|
||||||
|
|||||||
1
lib/magic_enum
Submodule
1
lib/magic_enum
Submodule
Submodule lib/magic_enum added at ec5734e491
@@ -114,5 +114,8 @@ private:
|
|||||||
friend class Sound;
|
friend class Sound;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void ThrowOnRuntimeError(std::string message,
|
||||||
|
ma_result errorType = ma_result::MA_ERROR);
|
||||||
|
|
||||||
} // namespace ChargeAudio
|
} // namespace ChargeAudio
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
#include "miniaudio/miniaudio.h"
|
#include "miniaudio/miniaudio.h"
|
||||||
|
|
||||||
#include <Corrade/Utility/Debug.h>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
using namespace ChargeAudio;
|
using namespace ChargeAudio;
|
||||||
using namespace Corrade;
|
using namespace Corrade;
|
||||||
@@ -13,11 +11,8 @@ Engine::Engine(uint32_t sampleRate, uint32_t channels) {
|
|||||||
maConfig.sampleRate = sampleRate;
|
maConfig.sampleRate = sampleRate;
|
||||||
maConfig.channels = channels;
|
maConfig.channels = channels;
|
||||||
|
|
||||||
if ((maResponse = ma_engine_init(&maConfig, &maEngine)) != MA_SUCCESS) {
|
ThrowOnRuntimeError("Failed to init miniaudio engine",
|
||||||
Utility::Error{} << "Could not init miniaudio (" << maResponse << ")";
|
ma_engine_init(&maConfig, &maEngine));
|
||||||
throw new std::runtime_error(
|
|
||||||
"Failed to init miniaudio engine. Check STDERR for more info.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
||||||
@@ -34,12 +29,7 @@ SoundContainer Engine::CreateSound(int bufferLengthInSeconds) {
|
|||||||
ma_result result = ma_pcm_rb_init(
|
ma_result result = ma_pcm_rb_init(
|
||||||
ma_format_s32, channels, sampleRate * channels * length, nullptr,
|
ma_format_s32, channels, sampleRate * channels * length, nullptr,
|
||||||
nullptr, &sound->maRingBuffer);
|
nullptr, &sound->maRingBuffer);
|
||||||
if (result != MA_SUCCESS) {
|
ThrowOnRuntimeError("Failed to create a new ring buffer!", result);
|
||||||
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::StreamedRawPCM,
|
Sound::SoundType::StreamedRawPCM,
|
||||||
"Failed to create the sound from ring buffer: "));
|
"Failed to create the sound from ring buffer: "));
|
||||||
@@ -56,13 +46,7 @@ SoundContainer Engine::CreateSound(uint8_t *data, int length) {
|
|||||||
ma_result result =
|
ma_result result =
|
||||||
ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer);
|
ma_audio_buffer_init_copy(&config, &sound->maAudioBuffer);
|
||||||
|
|
||||||
if (result != MA_SUCCESS) {
|
ThrowOnRuntimeError("Failed to create a new audio buffer!", result);
|
||||||
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->maConfig.pDataSource = &sound->maAudioBuffer;
|
||||||
},
|
},
|
||||||
Sound::SoundType::RawPCM,
|
Sound::SoundType::RawPCM,
|
||||||
|
|||||||
15
src/ErrorHandling.cpp
Normal file
15
src/ErrorHandling.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "ChargeAudio.hpp"
|
||||||
|
#include "magic_enum/include/magic_enum/magic_enum.hpp"
|
||||||
|
#include <Corrade/Utility/Debug.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
void ChargeAudio::ThrowOnRuntimeError(std::string message,
|
||||||
|
ma_result errorType) {
|
||||||
|
if (errorType == MA_SUCCESS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utility::Error{} << message.c_str()
|
||||||
|
<< magic_enum::enum_name(errorType).data();
|
||||||
|
throw new std::runtime_error(message + " Check STDERR for more info.\n");
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#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,
|
||||||
@@ -10,16 +9,13 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
|||||||
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
|
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
|
||||||
maConfig.endCallback = Sound::onSoundFinish;
|
maConfig.endCallback = Sound::onSoundFinish;
|
||||||
maConfig.pEndCallbackUserData = this;
|
maConfig.pEndCallbackUserData = this;
|
||||||
|
|
||||||
setupFunction(this);
|
setupFunction(this);
|
||||||
|
|
||||||
ma_result maResponse =
|
ma_result maResponse =
|
||||||
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
|
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
|
||||||
if (maResponse != MA_SUCCESS) {
|
ThrowOnRuntimeError("Failed to create a new sound. " + additionalErrorMessage,
|
||||||
Utility::Error{} << "Failed to create a new sound" << " (" << maResponse
|
maResponse);
|
||||||
<< ")";
|
|
||||||
throw new std::runtime_error(
|
|
||||||
"Failed to create a new sound. Check STDERR for more info.\n" +
|
|
||||||
additionalErrorMessage);
|
|
||||||
}
|
|
||||||
type = soundType;
|
type = soundType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,16 +65,24 @@ bool Sound::SetPlaybackTime(float time) {
|
|||||||
|
|
||||||
// Controls
|
// Controls
|
||||||
void Sound::Play() {
|
void Sound::Play() {
|
||||||
ma_sound_start(&maSound);
|
ThrowOnRuntimeError("Failed to start the sound.", ma_sound_start(&maSound));
|
||||||
state = Sound::SoundState::Playing;
|
state = Sound::SoundState::Playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sound::Pause() {
|
void Sound::Pause() {
|
||||||
ma_sound_stop(&maSound);
|
ThrowOnRuntimeError("Failed to pause the sound.", ma_sound_stop(&maSound));
|
||||||
state = Sound::SoundState::Paused;
|
state = Sound::SoundState::Paused;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); }
|
void Sound::Reset() {
|
||||||
|
if (type == SoundType::StreamedRawPCM) {
|
||||||
|
ThrowOnRuntimeError(
|
||||||
|
"You cannot reset on Streamed RawPCM sounds! Since the buffer is a "
|
||||||
|
"ring buffer there isn't a \"start\" to return to.");
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
|||||||
Reference in New Issue
Block a user