mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2025-06-19 19:26:36 +02:00
initial import, >900commits predate this
This commit is contained in:
44
framework/sound/src/mm/services/sound_service.cpp
Normal file
44
framework/sound/src/mm/services/sound_service.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "./sound_service.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOG_CRIT(...) __LOG_CRIT( "Sound", __VA_ARGS__)
|
||||
#define LOG_ERROR(...) __LOG_ERROR("Sound", __VA_ARGS__)
|
||||
#define LOG_WARN(...) __LOG_WARN( "Sound", __VA_ARGS__)
|
||||
#define LOG_INFO(...) __LOG_INFO( "Sound", __VA_ARGS__)
|
||||
#define LOG_DEBUG(...) __LOG_DEBUG("Sound", __VA_ARGS__)
|
||||
#define LOG_TRACE(...) __LOG_TRACE("Sound", __VA_ARGS__)
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
SoundService::SoundService(void) : engine() {
|
||||
MM::Logger::initSectionLogger("Sound");
|
||||
}
|
||||
|
||||
SoundService::~SoundService(void) {
|
||||
}
|
||||
|
||||
bool SoundService::enable(Engine&) {
|
||||
unsigned int flags = SoLoud::Soloud::CLIP_ROUNDOFF;
|
||||
auto r = engine.init(flags);
|
||||
if (r != 0) {
|
||||
LOG_ERROR("SoLoud initialization failed: {}", r);
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_INFO("SoLoud v{} backend: {}", engine.getVersion(), engine.getBackendString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SoundService::disable(Engine&) {
|
||||
engine.deinit();
|
||||
}
|
||||
|
||||
void SoundService::printErrorString(SoLoud::result errorCode) {
|
||||
LOG_ERROR("error string: {}", engine.getErrorString(errorCode));
|
||||
}
|
||||
|
||||
} // MM::Services
|
||||
|
27
framework/sound/src/mm/services/sound_service.hpp
Normal file
27
framework/sound/src/mm/services/sound_service.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <soloud.h>
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
class SoundService : public Service {
|
||||
public:
|
||||
SoLoud::Soloud engine;
|
||||
|
||||
public:
|
||||
bool enable(Engine&) override;
|
||||
void disable(Engine&) override;
|
||||
|
||||
const char* name(void) override { return "SoundService"; }
|
||||
|
||||
public:
|
||||
SoundService(void);
|
||||
~SoundService(void);
|
||||
|
||||
void printErrorString(SoLoud::result errorCode);
|
||||
};
|
||||
|
||||
} // MM::Services
|
||||
|
71
framework/sound/src/mm/soloud_filesystem_file_impl.cpp
Normal file
71
framework/sound/src/mm/soloud_filesystem_file_impl.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include "soloud_filesystem_file_impl.hpp"
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOGSLF(x) LOG("SoLoudFilesystemFile", x)
|
||||
|
||||
namespace MM::SoLoud {
|
||||
|
||||
FilesystemFile::FilesystemFile(Services::FilesystemService::fs_file_t file, Engine& engine)
|
||||
: _file_handle(file),
|
||||
_fs_ptr(engine.tryService<Services::FilesystemService>()) {
|
||||
|
||||
if (!_fs_ptr) {
|
||||
LOGSLF("error: engine has no FilesystemService!");
|
||||
}
|
||||
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: created with invalid file handle!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FilesystemFile::~FilesystemFile(void) {
|
||||
}
|
||||
|
||||
int FilesystemFile::eof(void) {
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: invalid file handle!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _fs_ptr->eof(_file_handle);
|
||||
}
|
||||
|
||||
unsigned int FilesystemFile::read(unsigned char* aDst, unsigned int aBytes) {
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: invalid file handle!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _fs_ptr->read(_file_handle, aDst, aBytes);
|
||||
}
|
||||
|
||||
unsigned int FilesystemFile::length(void) {
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: invalid file handle!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _fs_ptr->length(_file_handle);
|
||||
}
|
||||
|
||||
void FilesystemFile::seek(int aOffset) {
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: invalid file handle!");
|
||||
return;
|
||||
}
|
||||
|
||||
_fs_ptr->seek(_file_handle, aOffset);
|
||||
}
|
||||
|
||||
unsigned int FilesystemFile::pos(void) {
|
||||
if (!_file_handle) {
|
||||
LOGSLF("error: invalid file handle!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _fs_ptr->tell(_file_handle);
|
||||
}
|
||||
|
||||
} // MM::SoLoud
|
||||
|
32
framework/sound/src/mm/soloud_filesystem_file_impl.hpp
Normal file
32
framework/sound/src/mm/soloud_filesystem_file_impl.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <soloud_file.h>
|
||||
|
||||
#include <mm/services/filesystem.hpp>
|
||||
|
||||
namespace MM::SoLoud {
|
||||
|
||||
class FilesystemFile : public ::SoLoud::File {
|
||||
private:
|
||||
Services::FilesystemService::fs_file_t _file_handle;
|
||||
Services::FilesystemService* _fs_ptr; // convinience
|
||||
|
||||
public:
|
||||
FilesystemFile(void) = delete;
|
||||
FilesystemFile(Services::FilesystemService::fs_file_t file, Engine& engine);
|
||||
FilesystemFile(const char* file_path, Engine& engine);
|
||||
|
||||
~FilesystemFile(void);
|
||||
|
||||
// soloud api
|
||||
public:
|
||||
int eof(void) override;
|
||||
unsigned int read(unsigned char* aDst, unsigned int aBytes) override;
|
||||
unsigned int length(void) override;
|
||||
void seek(int aOffset) override;
|
||||
unsigned int pos(void) override;
|
||||
|
||||
};
|
||||
|
||||
} // MM
|
||||
|
102
framework/sound/src/mm/soloud_json.cpp
Normal file
102
framework/sound/src/mm/soloud_json.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "./soloud_json.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace SoLoud {
|
||||
|
||||
void to_json(nlohmann::json& j, const Sfxr& sfxr) {
|
||||
j = nlohmann::json::object();
|
||||
|
||||
#define X(x) j[#x] = sfxr.mParams.x
|
||||
X(wave_type);
|
||||
|
||||
X(p_base_freq);
|
||||
X(p_freq_limit);
|
||||
X(p_freq_ramp);
|
||||
X(p_freq_dramp);
|
||||
X(p_duty);
|
||||
X(p_duty_ramp);
|
||||
|
||||
X(p_vib_strength);
|
||||
X(p_vib_speed);
|
||||
X(p_vib_delay);
|
||||
|
||||
X(p_env_attack);
|
||||
X(p_env_sustain);
|
||||
X(p_env_decay);
|
||||
X(p_env_punch);
|
||||
|
||||
X(filter_on);
|
||||
X(p_lpf_resonance);
|
||||
X(p_lpf_freq);
|
||||
X(p_lpf_ramp);
|
||||
X(p_hpf_freq);
|
||||
X(p_hpf_ramp);
|
||||
|
||||
X(p_pha_offset);
|
||||
X(p_pha_ramp);
|
||||
|
||||
X(p_repeat_speed);
|
||||
|
||||
X(p_arp_speed);
|
||||
X(p_arp_mod);
|
||||
|
||||
X(master_vol);
|
||||
|
||||
X(sound_vol);
|
||||
#undef X
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json& j, Sfxr& sfxr) {
|
||||
if (j.empty()) {
|
||||
sfxr.resetParams();
|
||||
}
|
||||
|
||||
if (j.contains("preset")) {
|
||||
sfxr.loadPreset(j["preset"].get<int>(), j.value("preset_seed", 0));
|
||||
}
|
||||
|
||||
#define X(x) if (j.contains(#x)) sfxr.mParams.x = j[#x]
|
||||
X(wave_type);
|
||||
|
||||
X(p_base_freq);
|
||||
X(p_freq_limit);
|
||||
X(p_freq_ramp);
|
||||
X(p_freq_dramp);
|
||||
X(p_duty);
|
||||
X(p_duty_ramp);
|
||||
|
||||
X(p_vib_strength);
|
||||
X(p_vib_speed);
|
||||
X(p_vib_delay);
|
||||
|
||||
X(p_env_attack);
|
||||
X(p_env_sustain);
|
||||
X(p_env_decay);
|
||||
X(p_env_punch);
|
||||
|
||||
X(filter_on);
|
||||
X(p_lpf_resonance);
|
||||
X(p_lpf_freq);
|
||||
X(p_lpf_ramp);
|
||||
X(p_hpf_freq);
|
||||
X(p_hpf_ramp);
|
||||
|
||||
X(p_pha_offset);
|
||||
X(p_pha_ramp);
|
||||
|
||||
X(p_repeat_speed);
|
||||
|
||||
X(p_arp_speed);
|
||||
X(p_arp_mod);
|
||||
|
||||
X(master_vol);
|
||||
|
||||
X(sound_vol);
|
||||
#undef X
|
||||
|
||||
}
|
||||
|
||||
} // SoLoud
|
||||
|
14
framework/sound/src/mm/soloud_json.hpp
Normal file
14
framework/sound/src/mm/soloud_json.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
|
||||
#include <soloud_sfxr.h>
|
||||
|
||||
namespace SoLoud {
|
||||
|
||||
//SoLoud::Sfxr
|
||||
void to_json(nlohmann::json& j, const Sfxr& sfxr);
|
||||
void from_json(const nlohmann::json& j, Sfxr& sfxr);
|
||||
|
||||
} // SoLoud
|
||||
|
30
framework/sound/src/mm/sound_loader_wav.cpp
Normal file
30
framework/sound/src/mm/sound_loader_wav.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "./sound_loader_wav.hpp"
|
||||
|
||||
#include "./soloud_filesystem_file_impl.hpp"
|
||||
|
||||
#include <mm/services/filesystem.hpp>
|
||||
|
||||
namespace MM {
|
||||
|
||||
std::shared_ptr<::SoLoud::Wav> SoundLoaderWavFile::load(const std::string& path, Engine& engine) const {
|
||||
auto& fs = engine.getService<Services::FilesystemService>();
|
||||
|
||||
if (!fs.isFile(path.c_str()))
|
||||
return nullptr;
|
||||
|
||||
auto h = fs.open(path.c_str());
|
||||
|
||||
MM::SoLoud::FilesystemFile sl_f(h, engine);
|
||||
|
||||
auto ptr = std::make_shared<::SoLoud::Wav>();
|
||||
auto r = ptr->loadFile(&sl_f);
|
||||
if (r != ::SoLoud::SO_NO_ERROR) {
|
||||
// log error
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
} // MM
|
||||
|
17
framework/sound/src/mm/sound_loader_wav.hpp
Normal file
17
framework/sound/src/mm/sound_loader_wav.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <soloud_wav.h>
|
||||
|
||||
namespace MM {
|
||||
|
||||
class Engine;
|
||||
|
||||
struct SoundLoaderWavFile {
|
||||
std::shared_ptr<::SoLoud::Wav> load(const std::string& path, Engine& engine) const;
|
||||
};
|
||||
|
||||
} // MM
|
||||
|
Reference in New Issue
Block a user