added audio stuff
This commit is contained in:
70
TSE_Core/src/elements/AudioClip.cpp
Normal file
70
TSE_Core/src/elements/AudioClip.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "AudioClip.hpp"
|
||||
#include "Debug.hpp"
|
||||
#include <filesystem>
|
||||
#include "AudioEngine.hpp"
|
||||
|
||||
TSE::AudioClip::AudioClip(string path)
|
||||
{
|
||||
description.path = path;
|
||||
}
|
||||
|
||||
TSE::AudioClip::AudioClip(byte *stream, size_t size)
|
||||
{
|
||||
description.audiostream = stream;
|
||||
description.dataSize = size;
|
||||
}
|
||||
|
||||
ma_sound *TSE::AudioClip::GetAudioSound()
|
||||
{
|
||||
if(description.path != ""){
|
||||
if(std::filesystem::exists(description.path))
|
||||
{
|
||||
ma_result res;
|
||||
ma_sound* sound = (ma_sound*)malloc(sizeof(*sound));
|
||||
|
||||
res = ma_sound_init_from_file(AudioEngine::engine, description.path.c_str(), 0, nullptr, nullptr, sound);
|
||||
if (res != MA_SUCCESS) {
|
||||
TSE_WARNING("ma_sound_init_from_file failed: " + std::to_string(res));
|
||||
delete(sound);
|
||||
return nullptr;
|
||||
}
|
||||
return sound;
|
||||
}
|
||||
else
|
||||
{
|
||||
TSE_WARNING("Can't create ma_sound because the given path dose not exist: \n" + description.path);
|
||||
}
|
||||
}
|
||||
else if(description.audiostream != nullptr && description.dataSize > 0)
|
||||
{
|
||||
ma_result res;
|
||||
ma_decoder m_decoder;
|
||||
res = ma_decoder_init_memory(description.audiostream, description.dataSize, nullptr, &m_decoder);
|
||||
if (res != MA_SUCCESS) {
|
||||
TSE_WARNING("ma_decoder_init_memory failed: " + std::to_string(res));
|
||||
return nullptr;
|
||||
}
|
||||
ma_sound* sound = (ma_sound*)malloc(sizeof(*sound));
|
||||
res = ma_sound_init_from_data_source(AudioEngine::engine, &m_decoder, MA_SOUND_FLAG_STREAM, nullptr, sound);
|
||||
ma_decoder_uninit(&m_decoder);
|
||||
if (res != MA_SUCCESS) {
|
||||
TSE_WARNING("ma_sound_init_from_data_source failed: " + std::to_string(res));
|
||||
delete(sound);
|
||||
return nullptr;
|
||||
}
|
||||
return sound;
|
||||
}
|
||||
else
|
||||
{
|
||||
TSE_WARNING("Can't create ma_sound because description is not set, or unconfigured.");
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TSE::AudioClip::DestroyAudioSound(ma_sound *sound)
|
||||
{
|
||||
if (!sound) return;
|
||||
|
||||
ma_sound_uninit(sound);
|
||||
delete sound;
|
||||
}
|
||||
28
TSE_Core/src/elements/AudioClip.hpp
Normal file
28
TSE_Core/src/elements/AudioClip.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "Types.hpp"
|
||||
#include "miniaudio.h"
|
||||
|
||||
namespace TSE
|
||||
{
|
||||
struct AudioClipDescription
|
||||
{
|
||||
public:
|
||||
string path = "";
|
||||
byte* audiostream = nullptr;
|
||||
size_t dataSize = 0;
|
||||
};
|
||||
|
||||
class AudioClip
|
||||
{
|
||||
public:
|
||||
string name = "";
|
||||
AudioClipDescription description;
|
||||
|
||||
AudioClip(string path);
|
||||
AudioClip(byte* stream, size_t size);
|
||||
|
||||
ma_sound* GetAudioSound();
|
||||
void DestroyAudioSound(ma_sound* sound);
|
||||
};
|
||||
} // namespace TSE
|
||||
22
TSE_Core/src/elements/AudioEngine.cpp
Normal file
22
TSE_Core/src/elements/AudioEngine.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "AudioEngine.hpp"
|
||||
#include "Debug.hpp"
|
||||
|
||||
ma_engine* TSE::AudioEngine::engine = nullptr;
|
||||
|
||||
void TSE::AudioEngine::Init()
|
||||
{
|
||||
ma_result res;
|
||||
engine = (ma_engine*)malloc(sizeof(*engine));
|
||||
res = ma_engine_init(nullptr, engine);
|
||||
if(res != MA_SUCCESS)
|
||||
{
|
||||
TSE_WARNING("Couldn't init audio engine. error code: " + std::to_string(res));
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::AudioEngine::Destroy()
|
||||
{
|
||||
ma_engine_uninit(engine);
|
||||
delete(engine);
|
||||
engine = nullptr;
|
||||
}
|
||||
15
TSE_Core/src/elements/AudioEngine.hpp
Normal file
15
TSE_Core/src/elements/AudioEngine.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "miniaudio.h"
|
||||
|
||||
namespace TSE
|
||||
{
|
||||
class AudioEngine
|
||||
{
|
||||
public:
|
||||
static ma_engine* engine;
|
||||
|
||||
static void Init();
|
||||
static void Destroy();
|
||||
};
|
||||
} // namespace TSE
|
||||
Reference in New Issue
Block a user