snakecase, camelcase functions moved into

Util::String

module Cache added.
This commit is contained in:
onyx-and-iris 2023-07-17 14:13:08 +01:00
parent eb4920f4a5
commit 2efd07b3d5
4 changed files with 50 additions and 28 deletions

View File

@ -5,12 +5,14 @@ require_relative "midi"
require_relative "event" require_relative "event"
require_relative "worker" require_relative "worker"
require_relative "errors" require_relative "errors"
require_relative "util"
require_relative "logger" require_relative "logger"
module Voicemeeter module Voicemeeter
class Base class Base
include Logging include Logging
include Worker include Worker
include Util::Cache
attr_reader :kind, :midi, :event, :running, :callback attr_reader :kind, :midi, :event, :running, :callback
attr_accessor :cache attr_accessor :cache
@ -106,15 +108,16 @@ module Voicemeeter
end end
def get(name, is_string = false) def get(name, is_string = false)
clear_dirty if @sync polling(:get, name: name) do
if is_string if is_string
c_get = FFI::MemoryPointer.new(:string, 512, true) cget = FFI::MemoryPointer.new(:string, 512, true)
CBindings.call(:bind_get_parameter_string_a, name, c_get) CBindings.call(:bind_get_parameter_string_a, name, cget)
c_get.read_string cget.read_string
else else
c_get = FFI::MemoryPointer.new(:float, 1) cget = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_get_parameter_float, name, c_get) CBindings.call(:bind_get_parameter_float, name, cget)
c_get.read_float.round(1) cget.read_float.round(1)
end
end end
end end
@ -124,17 +127,20 @@ module Voicemeeter
else else
CBindings.call(:bind_set_parameter_float, name, value.to_f) CBindings.call(:bind_set_parameter_float, name, value.to_f)
end end
cache.store(name, value)
end end
def get_buttonstatus(id, mode) def get_buttonstatus(id, mode)
clear_dirty if @sync polling(:get_buttonstatus, id: id, mode: mode) do
c_get = FFI::MemoryPointer.new(:float, 1) cget = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_macro_button_get_status, id, c_get, mode) CBindings.call(:bind_macro_button_get_status, id, cget, mode)
c_get.read_float.to_i cget.read_float.to_i
end
end end
def set_buttonstatus(id, state, mode) def set_buttonstatus(id, mode, state)
CBindings.call(:bind_macro_button_set_status, id, state, mode) CBindings.call(:bind_macro_button_set_status, id, state, mode)
cache.store("mb_#{id}_#{mode}", state)
end end
def get_level(mode, index) def get_level(mode, index)

View File

@ -16,7 +16,7 @@ module Voicemeeter
ffi_convention :stdcall ffi_convention :stdcall
private_class_method def self.attach_function(c_name, args, returns) 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) super(ruby_name, c_name, args, returns)
end end

View File

@ -19,7 +19,7 @@ module Voicemeeter
end end
def fn_name 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 end
end end

View File

@ -1,5 +1,6 @@
module Voicemeeter module Voicemeeter
module Util module Util
module String
def snakecase(string) def snakecase(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2')
@ -16,4 +17,19 @@ module Voicemeeter
module_function :snakecase, :camelcase module_function :snakecase, :camelcase
end 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]
clear_dirty if @sync
yield
end
end
end
end end