Skip to content

Commit

Permalink
julianxhokaxhiu#129 Allow multiple extensions for external_sfx/music…
Browse files Browse the repository at this point in the history
…/voice
  • Loading branch information
myst6re authored and julianxhokaxhiu committed Nov 15, 2020
1 parent ba3d079 commit 0c320ff
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 46 deletions.
6 changes: 6 additions & 0 deletions misc/FFNx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@

#[EXTERNAL SFX EXTENSION]
# The type of file to search for. By default is ogg.
# You can use a list to specify multiple extensions, ordered by priority.
# Example: ["ogg", "flac"]
# Supported extensions:
# - ogg, mp3, flac, wav
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -131,6 +133,8 @@

#[EXTERNAL MUSIC EXTENSION]
# The type of file to search for. By default is ogg.
# You can use a list to specify multiple extensions, ordered by priority.
# Example: ["minipsf", "ogg"]
# Supported extensions:
# - https://github.com/losnoco/vgmstream#supported-file-types
# - psf, psf2, minipsf, minipsf2 (PSF and PSF2) if you provide the Highly Experimental BIOS
Expand All @@ -152,6 +156,8 @@

#[EXTERNAL VOICE EXTENSION]
# The type of file to search for. By default is ogg.
# You can use a list to specify multiple extensions, ordered by priority.
# Example: ["ogg", "flac"]
# Supported extensions:
# - ogg, mp3, flac, wav
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
61 changes: 36 additions & 25 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,43 @@ void NxAudioEngine::loadConfig()
}

template <class T>
void NxAudioEngine::getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type)
bool NxAudioEngine::getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type)
{
std::vector<std::string> extensions;

switch(_type)
{
case NxAudioEngineLayer::NXAUDIOENGINE_SFX:
sprintf(_out, "%s/%s/%d.%s", basedir, external_sfx_path.c_str(), _key, external_sfx_ext.c_str());
extensions = external_sfx_ext;
break;
case NxAudioEngineLayer::NXAUDIOENGINE_MUSIC:
extensions = external_music_ext;
break;
case NxAudioEngineLayer::NXAUDIOENGINE_VOICE:
extensions = external_voice_ext;
break;
}

for (auto extension: extensions) {
switch (_type)
{
case NxAudioEngineLayer::NXAUDIOENGINE_SFX:
sprintf(_out, "%s/%s/%d.%s", basedir, external_sfx_path.c_str(), _key, extension.c_str());
break;
case NxAudioEngineLayer::NXAUDIOENGINE_MUSIC:
sprintf(_out, "%s/%s/%s.%s", basedir, external_music_path.c_str(), _key, external_music_ext.c_str());
sprintf(_out, "%s/%s/%s.%s", basedir, external_music_path.c_str(), _key, extension.c_str());
break;
case NxAudioEngineLayer::NXAUDIOENGINE_VOICE:
sprintf(_out, "%s/%s/%s.%s", basedir, external_voice_path.c_str(), _key, external_voice_ext.c_str());
sprintf(_out, "%s/%s/%s.%s", basedir, external_voice_path.c_str(), _key, extension.c_str());
break;
}

if (fileExists(_out)) {
return true;
}
}

return false;
}

bool NxAudioEngine::fileExists(const char* filename)
Expand Down Expand Up @@ -142,13 +165,9 @@ void NxAudioEngine::cleanup()
// SFX
bool NxAudioEngine::canPlaySFX(int id)
{
struct stat dummy;

char filename[MAX_PATH];

getFilenameFullPath<int>(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX);

return (stat(filename, &dummy) == 0);
return getFilenameFullPath<int>(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX);
}

void NxAudioEngine::loadSFX(int id)
Expand All @@ -161,11 +180,11 @@ void NxAudioEngine::loadSFX(int id)
{
char filename[MAX_PATH];

getFilenameFullPath<int>(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX);
bool exists = getFilenameFullPath<int>(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX);

if (trace_all || trace_sfx) trace("NxAudioEngine::%s: %s\n", __func__, filename);

if (fileExists(filename))
if (exists)
{
SoLoud::Wav* sfx = new SoLoud::Wav();

Expand Down Expand Up @@ -251,25 +270,21 @@ void NxAudioEngine::setSFXSpeed(float speed, int channel)
// Music
bool NxAudioEngine::canPlayMusic(const char* name)
{
struct stat dummy;

char filename[MAX_PATH];

getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC);

return (stat(filename, &dummy) == 0);
return getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC);
}

SoLoud::AudioSource* NxAudioEngine::loadMusic(const char* name)
{
SoLoud::AudioSource* music = nullptr;
char filename[MAX_PATH];

getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC);
bool exists = getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC);

if (trace_all || trace_music) trace("NxAudioEngine::%s: %s\n", __func__, filename);

if (fileExists(filename))
if (exists)
{
if (_openpsf_loaded) {
SoLoud::OpenPsf* openpsf = new SoLoud::OpenPsf();
Expand Down Expand Up @@ -589,24 +604,20 @@ void NxAudioEngine::setMusicLooping(bool looping)
// Voice
bool NxAudioEngine::canPlayVoice(const char* name)
{
struct stat dummy;

char filename[MAX_PATH];

getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE);

return (stat(filename, &dummy) == 0);
return getFilenameFullPath<const char*>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE);
}

void NxAudioEngine::playVoice(const char* name)
{
char filename[MAX_PATH];

getFilenameFullPath<const char *>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE);
bool exists = getFilenameFullPath<const char *>(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE);

if (trace_all || trace_voice) trace("NxAudioEngine::%s: %s\n", __func__, filename);

if (fileExists(filename))
if (exists)
{
SoLoud::WavStream* voice = new SoLoud::WavStream();

Expand Down
3 changes: 2 additions & 1 deletion src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ class NxAudioEngine
SoLoud::handle _voiceHandle = NXAUDIOENGINE_INVALID_HANDLE;

// MISC
// Returns false if the file does not exist
template <class T>
void getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type);
bool getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type);

bool fileExists(const char* filename);

Expand Down
5 changes: 0 additions & 5 deletions src/audio/openpsf/openpsf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ namespace SoLoud
return FILE_NOT_FOUND;
}

if (!Psf::is_our_path(aFilename, external_music_ext.c_str())) {
error("Incompatible file extension %s\n", external_music_ext.c_str());
return FILE_LOAD_FAILED;
}

stop();

stream = new Psf(PsfFlags::PsfDefaults, 0);
Expand Down
40 changes: 28 additions & 12 deletions src/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ bool enable_ffmpeg_videos;
std::string ffmpeg_video_ext;
bool use_external_sfx;
std::string external_sfx_path;
std::string external_sfx_ext;
std::vector<std::string> external_sfx_ext;
bool use_external_music;
bool external_music_resume;
std::string external_music_path;
std::string external_music_ext;
std::vector<std::string> external_music_ext;
std::string he_bios_path;
std::string external_voice_path;
std::string external_voice_ext;
std::vector<std::string> external_voice_ext;
bool enable_voice_music_fade;
long external_voice_music_fade_volume;
bool save_textures;
Expand Down Expand Up @@ -90,6 +90,22 @@ double speedhack_max;
double speedhack_min;
bool enable_animated_textures;

std::vector<std::string> get_string_or_array_of_strings(const toml::node_view<toml::node> &node)
{
if (node.is_array()) {
toml::array* a = node.as_array();
if (a && a->is_homogeneous(toml::node_type::string)) {
std::vector<std::string> ret(a->size());
for (toml::array::iterator it = a->begin(); it != a->end(); ++it) {
ret.push_back((*it).value_or(""));
}
return ret;
}
}

return std::vector<std::string>(1, node.value_or(""));
}

void read_cfg()
{
toml::parse_result config;
Expand All @@ -116,14 +132,14 @@ void read_cfg()
ffmpeg_video_ext = config["ffmpeg_video_ext"].value_or("");
use_external_sfx = config["use_external_sfx"].value_or(false);
external_sfx_path = config["external_sfx_path"].value_or("");
external_sfx_ext = config["external_sfx_ext"].value_or("");
external_sfx_ext = get_string_or_array_of_strings(config["external_sfx_ext"]);
use_external_music = config["use_external_music"].value_or(false);
external_music_resume = config["external_music_resume"].value_or(true);
external_music_path = config["external_music_path"].value_or("");
external_music_ext = config["external_music_ext"].value_or("");
external_music_ext = get_string_or_array_of_strings(config["external_music_ext"]);
he_bios_path = config["he_bios_path"].value_or("");
external_voice_path = config["external_voice_path"].value_or("");
external_voice_ext = config["external_voice_ext"].value_or("");
external_voice_ext = get_string_or_array_of_strings(config["external_voice_ext"]);
enable_voice_music_fade = config["enable_voice_music_fade"].value_or(false);
external_voice_music_fade_volume = config["external_voice_music_fade_volume"].value_or(25);
save_textures = config["save_textures"].value_or(false);
Expand Down Expand Up @@ -281,20 +297,20 @@ void read_cfg()
external_sfx_path = "sfx";

// EXTERNAL SFX EXTENSION
if (external_sfx_ext.empty())
external_sfx_ext = "ogg";
if (external_sfx_ext.empty() || external_sfx_ext.front().empty())
external_sfx_ext = std::vector<std::string>(1, "ogg");

// EXTERNAL MUSIC EXTENSION
if (external_music_ext.empty())
external_music_ext = "ogg";
if (external_music_ext.empty() || external_music_ext.front().empty())
external_music_ext = std::vector<std::string>(1, "ogg");

// EXTERNAL VOICE PATH
if (external_voice_path.empty())
external_voice_path = "voice";

// EXTERNAL VOICE EXTENSION
if (external_voice_ext.empty())
external_voice_ext = "ogg";
if (external_voice_ext.empty() || external_voice_ext.front().empty())
external_voice_ext = std::vector<std::string>(1, "ogg");

// MOD PATH
if (mod_path.empty())
Expand Down
7 changes: 4 additions & 3 deletions src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <string>
#include <vector>
#include "log.h"

#define RENDERER_BACKEND_AUTO 0
Expand All @@ -36,14 +37,14 @@ extern bool enable_ffmpeg_videos;
extern std::string ffmpeg_video_ext;
extern bool use_external_sfx;
extern std::string external_sfx_path;
extern std::string external_sfx_ext;
extern std::vector<std::string> external_sfx_ext;
extern bool use_external_music;
extern bool external_music_resume;
extern std::string external_music_path;
extern std::string external_music_ext;
extern std::vector<std::string> external_music_ext;
extern std::string he_bios_path;
extern std::string external_voice_path;
extern std::string external_voice_ext;
extern std::vector<std::string> external_voice_ext;
extern bool enable_voice_music_fade;
extern long external_voice_music_fade_volume;
extern bool save_textures;
Expand Down

0 comments on commit 0c320ff

Please sign in to comment.