From 2efd07b3d5d7bb41bb72bcbd071fd627db243199 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 17 Jul 2023 14:13:08 +0100 Subject: [PATCH] snakecase, camelcase functions moved into Util::String module Cache added. --- lib/voicemeeter/base.rb | 34 +++++++++++++++++------------- lib/voicemeeter/cbindings.rb | 2 +- lib/voicemeeter/errors.rb | 2 +- lib/voicemeeter/util.rb | 40 +++++++++++++++++++++++++----------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lib/voicemeeter/base.rb b/lib/voicemeeter/base.rb index 3ef135e..c4c0ef1 100644 --- a/lib/voicemeeter/base.rb +++ b/lib/voicemeeter/base.rb @@ -5,12 +5,14 @@ require_relative "midi" require_relative "event" require_relative "worker" require_relative "errors" +require_relative "util" require_relative "logger" module Voicemeeter class Base include Logging include Worker + include Util::Cache attr_reader :kind, :midi, :event, :running, :callback attr_accessor :cache @@ -106,15 +108,16 @@ module Voicemeeter end def get(name, is_string = false) - clear_dirty if @sync - if is_string - c_get = FFI::MemoryPointer.new(:string, 512, true) - CBindings.call(:bind_get_parameter_string_a, name, c_get) - c_get.read_string - else - c_get = FFI::MemoryPointer.new(:float, 1) - CBindings.call(:bind_get_parameter_float, name, c_get) - c_get.read_float.round(1) + polling(:get, name: name) do + if is_string + cget = FFI::MemoryPointer.new(:string, 512, true) + CBindings.call(:bind_get_parameter_string_a, name, cget) + cget.read_string + else + cget = FFI::MemoryPointer.new(:float, 1) + CBindings.call(:bind_get_parameter_float, name, cget) + cget.read_float.round(1) + end end end @@ -124,17 +127,20 @@ module Voicemeeter else CBindings.call(:bind_set_parameter_float, name, value.to_f) end + cache.store(name, value) end def get_buttonstatus(id, mode) - clear_dirty if @sync - c_get = FFI::MemoryPointer.new(:float, 1) - CBindings.call(:bind_macro_button_get_status, id, c_get, mode) - c_get.read_float.to_i + polling(:get_buttonstatus, id: id, mode: mode) do + cget = FFI::MemoryPointer.new(:float, 1) + CBindings.call(:bind_macro_button_get_status, id, cget, mode) + cget.read_float.to_i + end end - def set_buttonstatus(id, state, mode) + def set_buttonstatus(id, mode, state) CBindings.call(:bind_macro_button_set_status, id, state, mode) + cache.store("mb_#{id}_#{mode}", state) end def get_level(mode, index) diff --git a/lib/voicemeeter/cbindings.rb b/lib/voicemeeter/cbindings.rb index a7cb2fd..390de12 100644 --- a/lib/voicemeeter/cbindings.rb +++ b/lib/voicemeeter/cbindings.rb @@ -16,7 +16,7 @@ module Voicemeeter ffi_convention :stdcall private_class_method def self.attach_function(c_name, args, returns) - ruby_name = "bind_#{Util.snakecase(c_name.to_s.delete_prefix("VBVMR_"))}".to_sym + ruby_name = "bind_#{Util::String.snakecase(c_name.to_s.delete_prefix("VBVMR_"))}".to_sym super(ruby_name, c_name, args, returns) end diff --git a/lib/voicemeeter/errors.rb b/lib/voicemeeter/errors.rb index dcb09e9..dac5f4d 100644 --- a/lib/voicemeeter/errors.rb +++ b/lib/voicemeeter/errors.rb @@ -19,7 +19,7 @@ module Voicemeeter end def fn_name - "VBVMR_#{Util.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}" + "VBVMR_#{Util::String.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}" end end end diff --git a/lib/voicemeeter/util.rb b/lib/voicemeeter/util.rb index 10dd97a..e793530 100644 --- a/lib/voicemeeter/util.rb +++ b/lib/voicemeeter/util.rb @@ -1,19 +1,35 @@ module Voicemeeter module Util - def snakecase(string) - string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') - .gsub(/([a-z\d])([A-Z])/, '\1_\2') - .tr("-", "_") - .gsub(/\s/, "_") - .gsub(/__+/, "_") - .downcase + module String + def snakecase(string) + string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') + .tr("-", "_") + .gsub(/\s/, "_") + .gsub(/__+/, "_") + .downcase + end + + def camelcase(string) + string if string !~ /_/ && string =~ /[A-Z]+.*/ + string.split("_").map { |e| e.capitalize }.join + end + + module_function :snakecase, :camelcase end - def camelcase(string) - string if string !~ /_/ && string =~ /[A-Z]+.*/ - string.split("_").map { |e| e.capitalize }.join - end + module Cache + def polling(func, **kwargs) + params = { + get: kwargs[:name], + get_buttonstatus: "mb_#{kwargs[:id]}_#{kwargs[:mode]}" + } + return cache.delete(params[func]) if cache.key? params[func] - module_function :snakecase, :camelcase + clear_dirty if @sync + + yield + end + end end end