From 834ff43d5ee98349e7348ec6ef7bbd3a10eb5668 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 9 May 2023 15:59:14 +0200 Subject: [PATCH] bmd_option: allow setting of floating point props this may be useful for things like bmdDeckLinkConfigAnalogAudioOutputScaleChannel1 ('aos1') --- src/blackmagic_common.cpp | 45 +++++++++++++++++++++++++++++++-------- src/blackmagic_common.hpp | 4 +++- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/blackmagic_common.cpp b/src/blackmagic_common.cpp index ebbcd1fdc..d9c3e1f8b 100644 --- a/src/blackmagic_common.cpp +++ b/src/blackmagic_common.cpp @@ -604,7 +604,12 @@ std::ostream &operator<<(std::ostream &output, const bmd_option &b) { output << b.get_int(); } break; - + case bmd_option::type_tag::t_float: { + auto flags = output.flags(); + output << fixed << b.m_val.f; + output.flags(flags); + break; + } } return output; } @@ -618,6 +623,11 @@ void bmd_option::set_int(int64_t val_) { m_type = type_tag::t_int; m_user_specified = true; } +void bmd_option::set_float(double val_) { + m_val.f = val_; + m_type = type_tag::t_float; + m_user_specified = true; +} void bmd_option::set_keep() { m_type = type_tag::t_keep; } @@ -673,16 +683,29 @@ bool bmd_option::parse(const char *val) return true; } - // check int - bool all_digit = true; + // check number + bool is_number = true; + bool decimal_point = false; for (size_t i = 0; i < strlen(val); ++i) { - all_digit = all_digit && isxdigit(val[i]); - if (i == 0 && val[i] == '-') { - all_digit = true; + if (val[i] == '.') { + if (decimal_point) { // there was already decimal point + is_number = false; + break; + } + decimal_point = true; + continue; + } + is_number = is_number && isxdigit(val[i]); + if (i == 0 && (val[i] == '-' || val[i] == '+')) { + is_number = true; } } - if (all_digit) { - set_int(stoi(val, nullptr, 0)); + if (is_number) { + if (decimal_point) { + set_float(stod(val)); + } else { + set_int(stoi(val, nullptr, 0)); + } return true; } if (strlen(val) <= 4) { @@ -703,7 +726,11 @@ bool bmd_option::option_write(IDeckLinkConfiguration *deckLinkConfiguration, BMD case type_tag::t_int: res = deckLinkConfiguration->SetInt(opt, get_int()); break; - default: + case type_tag::t_float: + res = deckLinkConfiguration->SetFloat(opt, m_val.f); + break; + case type_tag::t_keep: + case type_tag::t_default: return true; } ostringstream value_oss; diff --git a/src/blackmagic_common.hpp b/src/blackmagic_common.hpp index 515adc795..a16e85964 100644 --- a/src/blackmagic_common.hpp +++ b/src/blackmagic_common.hpp @@ -80,10 +80,11 @@ static std::vector> uv_to_bmd_codec_map = { struct bmd_option { private: - enum class type_tag : int { t_default, t_keep, t_flag, t_int } m_type = type_tag::t_default; + enum class type_tag : int { t_default, t_keep, t_flag, t_int, t_float } m_type = type_tag::t_default; union { bool b; int64_t i; + double f; } m_val{}; bool m_user_specified = true; ///< ignore errors if set to false friend std::ostream &operator<<(std::ostream &output, const bmd_option &b); @@ -100,6 +101,7 @@ public: bool parse(const char *); void set_flag(bool val_); void set_int(int64_t val_); + void set_float(double val_); bool option_write(IDeckLinkConfiguration *deckLinkConfiguration, BMDDeckLinkConfigurationID opt) const; };