From 5726426ece7a443ea2b2dccb0cfb526ad677a1d1 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Mon, 11 Nov 2024 14:58:51 +0100 Subject: [PATCH] Fix some bugs related to parsing effect parameters Closes #23. There is still some sus-ness in there, but it's fine with test tracks, so I'm leaving it for another day. --- example/gui/TunesView.gd | 1 + src/effector/si_effect_stream.cpp | 59 +++++++++++++++++++------------ src/effector/si_effect_stream.h | 2 +- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/example/gui/TunesView.gd b/example/gui/TunesView.gd index ee1d4e5..efd765b 100644 --- a/example/gui/TunesView.gd +++ b/example/gui/TunesView.gd @@ -45,6 +45,7 @@ func _play_selected() -> void: var selected_tune := _tunes[selected_items[0]] Controller.music_player.play_tune(selected_tune.mml_string) + if selected_tune.author.is_empty(): _tune_status.text = "Now Playing: %s" % [ selected_tune.title ] else: diff --git a/src/effector/si_effect_stream.cpp b/src/effector/si_effect_stream.cpp index a6d1e94..bbb231c 100644 --- a/src/effector/si_effect_stream.cpp +++ b/src/effector/si_effect_stream.cpp @@ -114,9 +114,7 @@ int SiEffectStream::process(int p_start_idx, int p_length, bool p_write_in_strea // void SiEffectStream::_add_effect(String p_cmd, Vector p_args, int p_argc) { - if (p_argc == 0) { - return; - } + ERR_FAIL_COND_MSG(p_cmd.is_empty(), "SiEffectStream: Trying to add an effect with no name."); Ref effect = SiEffector::get_effect_instance(p_cmd); if (effect.is_valid()) { @@ -125,15 +123,13 @@ void SiEffectStream::_add_effect(String p_cmd, Vector p_args, int p_argc } } -void SiEffectStream::_set_volume(int p_slot, String p_cmd, Vector p_args, int p_argc) { - if (p_argc == 0) { - return; - } +void SiEffectStream::_set_postfix_param(int p_slot, String p_cmd, Vector p_args, int p_argc) { + ERR_FAIL_COND_MSG(p_cmd.is_empty(), vformat("SiEffectStream: Trying to set an effect param with no name in slot %d.", p_slot)); if (p_cmd == "p") { - _pan = (((int)p_args[0]) << 4) - 64; + set_pan((((int)p_args[0]) << 4) - 64); } else if (p_cmd == "@p") { - _pan = (int)p_args[0]; + set_pan((int)p_args[0]); } else if (p_cmd == "@v") { double value = ((int)p_args[0]) * 0.0078125; set_stream_send(0, CLAMP(value, 0, 1)); @@ -147,28 +143,40 @@ void SiEffectStream::_set_volume(int p_slot, String p_cmd, Vector p_args value = ((int)p_args[i]) * 0.0078125; set_stream_send(i + p_slot, CLAMP(value, 0, 1)); } + } else { + ERR_PRINT(vformat("SiEffectStream: Trying to set an unknown effect param (%s) in slot %d.", p_cmd, p_slot)); } } void SiEffectStream::parse_mml(int p_slot, String p_mml, String p_postfix) { const int max_argc = 16; + // SUS: Slot number is only used to set postfix params, but not the effect itself. + // It is possible that the given slot number is incorrect and thus the effect is + // added in a different position than the params are set in. + + String command; int argc = 0; Vector args; args.resize_zeroed(max_argc); #define CLEAR_ARGS() \ + command = ""; \ for (int a = 0; a < max_argc; a++) { \ args.write[a] = NAN; \ } \ argc = 0; +#define CONSUME_ARG(m_index) \ + if (res->get_string(m_index).is_valid_float()) { \ + args.write[argc] = res->get_string(m_index).to_float(); \ + } \ + argc++; + // Reset and clear everything. initialize(0); CLEAR_ARGS(); - String command; - Ref re_mml = RegEx::create_from_string("([a-zA-Z_]+|,)\\s*([.\\-\\d]+)?"); Ref re_postfix = RegEx::create_from_string("(p|@p|@v|,)\\s*([.\\-\\d]+)?"); @@ -179,19 +187,21 @@ void SiEffectStream::parse_mml(int p_slot, String p_mml, String p_postfix) { Ref res = matches[i]; if (res->get_string(1) == ",") { - args.write[argc] = res->get_string(2).to_float(); - argc++; + CONSUME_ARG(2); } else { - _add_effect(command, args, argc); + if (!command.is_empty()) { + _add_effect(command, args, argc); + } CLEAR_ARGS(); command = res->get_string(1); - args.write[0] = res->get_string(2).to_float(); - argc = 1; + CONSUME_ARG(2); } } - _add_effect(command, args, argc); + if (!command.is_empty()) { + _add_effect(command, args, argc); + } CLEAR_ARGS(); // Parse the postfix. @@ -201,22 +211,25 @@ void SiEffectStream::parse_mml(int p_slot, String p_mml, String p_postfix) { Ref res = matches[i]; if (res->get_string(1) == ",") { - args.write[argc] = res->get_string(2).to_float(); - argc++; + CONSUME_ARG(2); } else { - _set_volume(p_slot, command, args, argc); + if (!command.is_empty()) { + _set_postfix_param(p_slot, command, args, argc); + } CLEAR_ARGS(); command = res->get_string(1); - args.write[0] = res->get_string(2).to_float(); - argc = 1; + CONSUME_ARG(2); } } - _set_volume(p_slot, command, args, argc); + if (!command.is_empty()) { + _set_postfix_param(p_slot, command, args, argc); + } CLEAR_ARGS(); #undef CLEAR_ARGS +#undef CONSUME_ARG } void SiEffectStream::initialize(int p_depth) { diff --git a/src/effector/si_effect_stream.h b/src/effector/si_effect_stream.h index c6a4be1..d3e6dfc 100644 --- a/src/effector/si_effect_stream.h +++ b/src/effector/si_effect_stream.h @@ -32,7 +32,7 @@ class SiEffectStream { Vector _output_streams; void _add_effect(String p_cmd, Vector p_args, int p_argc); - void _set_volume(int p_slot, String p_cmd, Vector p_args, int p_argc); + void _set_postfix_param(int p_slot, String p_cmd, Vector p_args, int p_argc); public: List> get_chain() const { return _chain; }