minecraft-pi-reborn/mods/src/sound/sound.cpp

125 lines
3.8 KiB
C++
Raw Normal View History

2021-09-12 03:18:12 +00:00
#include <string>
#include <libreborn/libreborn.h>
#include <symbols/minecraft.h>
#include <media-layer/audio.h>
2022-06-25 21:30:08 +00:00
#include "sound-internal.h"
#include <mods/feature/feature.h>
#include <mods/override/override.h>
#include <mods/init/init.h>
2021-09-12 03:18:12 +00:00
// Resolve Source File Path
#define SOURCE_FILE_BASE "data/libminecraftpe.so"
std::string _sound_get_source_file() {
2021-09-12 03:18:12 +00:00
static bool source_loaded = false;
static std::string source;
// Check
if (source_loaded) {
// Already Resolved
return source;
} else {
// Resolve
2022-04-22 23:38:15 +00:00
// Get Path
char *path = strdup(SOURCE_FILE_BASE);
ALLOC_CHECK(path);
2021-09-12 03:18:12 +00:00
// Handle Overrides
2022-04-22 23:38:15 +00:00
char *overridden_full_path = override_get_path(path);
2024-04-02 23:22:01 +00:00
if (overridden_full_path != nullptr) {
2022-04-22 23:38:15 +00:00
free(path);
path = overridden_full_path;
2021-09-12 03:18:12 +00:00
}
// Check If Sound Exists
2022-04-22 23:38:15 +00:00
if (access(path, F_OK) == -1) {
2021-09-12 03:18:12 +00:00
// Fail
2024-03-02 22:16:16 +00:00
WARN("Audio Source File Doesn't Exist: " SOURCE_FILE_BASE " (See: https://gitea.thebrokenrail.com/minecraft-pi-reborn/minecraft-pi-reborn/src/branch/master/docs/SOUND.md)");
2021-09-12 03:18:12 +00:00
source.assign("");
} else {
// Set
2022-04-22 23:38:15 +00:00
source.assign(path);
2021-09-12 03:18:12 +00:00
}
// Free
2022-04-22 23:38:15 +00:00
free(path);
2021-09-12 03:18:12 +00:00
// Mark As Loaded
source_loaded = true;
// Return
return _sound_get_source_file();
2021-09-12 03:18:12 +00:00
}
}
// Play Sound
// The pitch value is unsued because it causes glitchy sounds, it is seemingly unused in MCPE as well.
2022-07-14 22:11:44 +00:00
static void play(std::string name, float x, float y, float z, float volume, float pitch, bool is_ui) {
std::string source = _sound_get_source_file();
2021-09-13 21:06:04 +00:00
std::string resolved_name = _sound_pick(name);
2023-05-28 04:45:58 +00:00
if (pitch < 0.01f) {
pitch = 1;
2022-07-14 22:11:44 +00:00
}
2021-09-13 21:06:04 +00:00
if (source.size() > 0 && resolved_name.size() > 0) {
2022-07-14 22:11:44 +00:00
media_audio_play(source.c_str(), resolved_name.c_str(), x, y, z, pitch, volume, is_ui);
2021-09-12 03:18:12 +00:00
}
}
2022-07-14 22:11:44 +00:00
static void SoundEngine_playUI_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, float volume, float pitch) {
play(name, 0, 0, 0, volume, pitch, true);
2021-09-13 21:06:04 +00:00
}
2022-07-14 22:11:44 +00:00
static void SoundEngine_play_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, float x, float y, float z, float volume, float pitch) {
play(name, x, y, z, volume, pitch, false);
2021-09-12 03:18:12 +00:00
}
// Refresh Data
2024-01-06 11:30:23 +00:00
static void SoundEngine_update_injection(SoundEngine *sound_engine, Mob *listener_mob, __attribute__((unused)) float listener_angle) {
2021-09-12 03:18:12 +00:00
// Variables
static float volume = 0;
static float x = 0;
static float y = 0;
static float z = 0;
static float yaw = 0;
// SoundEngine Properties
2024-01-06 11:30:23 +00:00
Options *options = sound_engine->options;
2021-09-12 03:18:12 +00:00
// Volume
2024-01-06 11:30:23 +00:00
int32_t sound_enabled = options->sound;
2021-09-12 03:18:12 +00:00
volume = sound_enabled ? 1 : 0;
// Position And Rotation
2024-04-02 23:22:01 +00:00
if (listener_mob != nullptr) {
2021-09-12 03:18:12 +00:00
// Values
2024-01-06 11:30:23 +00:00
x = listener_mob->x;
y = listener_mob->y;
z = listener_mob->z;
yaw = listener_mob->yaw;
2021-09-12 03:18:12 +00:00
}
// Log
media_audio_update(volume, x, y, z, yaw);
}
// Resolve All Sounds On Init
// SoundEngine::init Is Called After The Audio Engine Has Been Loaded
2024-04-03 07:19:12 +00:00
static void SoundEngine_init_injection(SoundEngine_init_t original, SoundEngine *sound_engine, Minecraft *minecraft, Options *options) {
// Call Original Method
2024-04-03 07:19:12 +00:00
original(sound_engine, minecraft, options);
// Resolve Sounds
_sound_resolve_all();
}
2021-09-12 03:18:12 +00:00
// Init
void init_sound() {
// Implement Sound Engine
2022-04-10 00:01:16 +00:00
if (feature_has("Implement Sound Engine", server_disabled)) {
2024-04-03 07:19:12 +00:00
overwrite(SoundEngine_playUI, SoundEngine_playUI_injection);
overwrite(SoundEngine_play, SoundEngine_play_injection);
overwrite(SoundEngine_update, SoundEngine_update_injection);
overwrite_calls(SoundEngine_init, SoundEngine_init_injection);
2021-09-12 03:18:12 +00:00
}
}