looper/backends/playback/sdl_mixer_x/sdl_mixer_x.cpp

155 lines
4.5 KiB
C++
Raw Normal View History

2024-08-08 13:12:37 -07:00
#include "sdl_mixer_x.hpp"
2024-09-28 10:31:06 -07:00
#include <endian.h>
2024-08-08 13:12:37 -07:00
#include <filesystem>
#include "file_backend.hpp"
#include <stddef.h>
2024-09-16 15:05:53 -07:00
#include <log.hpp>
2024-09-28 10:31:06 -07:00
#include <util.hpp>
2024-08-08 13:12:37 -07:00
#include <string.h>
2024-11-12 14:53:44 -08:00
#include <license.hpp>
#include <assets/assets.h>
2024-08-08 13:12:37 -07:00
std::optional<uint64_t> SDLMixerXBackend::get_max_samples() {
2024-09-28 10:31:06 -07:00
return 4096;
2024-08-08 13:12:37 -07:00
}
void SDLMixerXBackend::load(const char *filename) {
memset(&spec, 0, sizeof(spec));
spec.callback = NULL;
spec.channels = 2;
2024-09-28 10:31:06 -07:00
spec.userdata = NULL;
2024-08-08 13:12:37 -07:00
spec.format = AUDIO_F32SYS;
spec.freq = 48000;
spec.samples = get_max_samples().value();
2024-09-28 10:31:06 -07:00
spec.size = spec.samples * 4 * spec.channels;
2024-08-08 13:12:37 -07:00
Mix_InitMixer(&spec, SDL_FALSE);
mixer = Mix_GetGeneralMixer();
{
std::filesystem::path fpath = std::string(filename);
fpath = fpath.parent_path() / fpath.stem();
std::string fpath_str = fpath.string();
std::vector<std::string> soundfonts = {fpath_str + ".sf2", fpath_str + ".dls"};
std::string sf_path_str = "";
bool any_path_exists = false;
for (auto sf_path : soundfonts) {
if (std::filesystem::exists(sf_path)) {
any_path_exists = true;
sf_path_str += ";" + sf_path;
}
}
if (any_path_exists) {
sf_path_str = sf_path_str.substr(1);
Mix_SetSoundFonts(sf_path_str.c_str());
} else {
Mix_SetSoundFonts(NULL);
}
}
2024-09-16 15:05:53 -07:00
DEBUG.writefln("Opening file: %s", filename);
2024-08-08 13:12:37 -07:00
file = open_file(filename);
2024-09-16 15:05:53 -07:00
DEBUG.writeln("Loading file...");
2024-09-28 10:31:06 -07:00
this->music = nullptr;
Mix_Music *output = Mix_LoadMUS_RW(get_sdl_file(this->file), SDL_TRUE);
2024-08-08 13:12:37 -07:00
if (output == nullptr) {
2024-09-28 10:31:06 -07:00
ERROR.writefln("Error occurred: %s", Mix_GetError());
2024-08-08 13:12:37 -07:00
throw std::exception();
}
2024-09-28 10:31:06 -07:00
if (Mix_PlayMusicStream(output, -1) < 0) {
ERROR.writefln("Error occurred: %s", Mix_GetError());
this->music = output;
throw std::exception();
}
Mix_ResumeMusicStream(output);
2024-08-08 13:12:37 -07:00
length = Mix_MusicDuration(output);
current_file = std::string(filename);
const char *title_tag = Mix_GetMusicTitleTag(output);
// Check for an empty string, which indicates there's no title tag.
if (title_tag[0] == '\0') {
std::filesystem::path path(current_file);
current_title = path.stem().string();
} else {
current_title = std::string(title_tag);
}
this->music = output;
streams.clear();
PlaybackStream stream;
stream.id = 0;
stream.length = Mix_MusicDuration(output);
stream.name = current_title;
streams.push_back(stream);
open = true;
2024-09-28 10:31:06 -07:00
initial = true;
}
void SDLMixerXBackend::switch_stream(int idx) {
seek(0.0);
2024-08-08 13:12:37 -07:00
}
void SDLMixerXBackend::seek(double position) {
if (music == nullptr || file == nullptr) {
open = false;
return;
}
Mix_SetMusicPositionStream(music, position);
}
size_t SDLMixerXBackend::render(void *buf, size_t maxlen) {
if (music == nullptr || file == nullptr) {
open = false;
return 0;
}
2024-09-28 10:31:06 -07:00
size_t i = 0;
size_t bytes_per_iter = maxlen;
if (bytes_per_iter > spec.size) {
bytes_per_iter = spec.size;
2024-08-08 13:12:37 -07:00
}
// Remove partial sample frames.
2024-09-28 10:31:06 -07:00
bytes_per_iter /= sizeof(float);
bytes_per_iter *= sizeof(float);
while (i < maxlen) {
if (i + bytes_per_iter > maxlen) {
bytes_per_iter = maxlen - i;
// Remove partial sample frames.
bytes_per_iter /= sizeof(float);
bytes_per_iter *= sizeof(float);
}
mixer(NULL, (Uint8*)(buf) + i, bytes_per_iter);
i += bytes_per_iter;
}
return i;
2024-08-08 13:12:37 -07:00
}
double SDLMixerXBackend::get_position() {
if (music == nullptr || file == nullptr) {
open = false;
return 0.0;
}
return Mix_GetMusicPosition(music);
}
2024-09-28 10:31:06 -07:00
SDLMixerXBackend::SDLMixerXBackend() {
Mix_Init(MIX_INIT_FLAC|MIX_INIT_MID|MIX_INIT_MOD|MIX_INIT_MP3|MIX_INIT_OGG|MIX_INIT_OPUS|MIX_INIT_WAVPACK);
}
SDLMixerXBackend::~SDLMixerXBackend() {
Mix_Quit();
}
2024-08-08 13:12:37 -07:00
void SDLMixerXBackend::cleanup() {
streams.clear();
2024-09-28 10:31:06 -07:00
position = 0.0;
rate = 0.0;
memset(&spec, 0, sizeof(spec));
current_file = "";
current_title = "";
length = 0.0;
if (music != nullptr) {
Mix_HaltMusicStream(music);
Mix_FreeMusic(music);
}
Mix_SetSoundFonts(NULL);
mixer = nullptr;
music = nullptr;
2024-08-08 13:12:37 -07:00
delete file;
file = nullptr;
open = false;
2024-09-28 10:31:06 -07:00
Mix_CloseAudio();
2024-08-08 13:12:37 -07:00
}
2024-11-12 14:53:44 -08:00
void SDLMixerXBackend::add_licenses() {
auto &license_data = get_license_data();
auto smx = LicenseData("SDL Mixer X", "Zlib");
LOAD_LICENSE(smx, sdl_mixer_x);
license_data.insert(smx);
}