2023-07-07 02:30:49 +01:00
|
|
|
require_relative "install"
|
|
|
|
require_relative "cbindings"
|
|
|
|
require_relative "kinds"
|
|
|
|
require_relative "midi"
|
2023-07-14 01:49:26 +01:00
|
|
|
require_relative "event"
|
|
|
|
require_relative "worker"
|
2023-07-09 05:48:08 +01:00
|
|
|
require "easy_logging"
|
2023-07-07 02:30:49 +01:00
|
|
|
|
|
|
|
module Voicemeeter
|
|
|
|
class Base
|
2023-07-14 11:44:49 +01:00
|
|
|
include EasyLogging
|
2023-07-14 01:49:26 +01:00
|
|
|
include Worker
|
|
|
|
include Events::Callbacks
|
2023-07-09 05:48:08 +01:00
|
|
|
|
2023-07-14 01:49:26 +01:00
|
|
|
attr_reader :kind, :midi, :event, :running
|
|
|
|
attr_accessor :cache
|
|
|
|
|
|
|
|
RATELIMIT = 0.033
|
2023-07-07 02:30:49 +01:00
|
|
|
|
|
|
|
def initialize(kind, **kwargs)
|
|
|
|
@kind = kind
|
|
|
|
@sync = kwargs[:sync] || false
|
2023-07-14 01:49:26 +01:00
|
|
|
@ratelimit = kwargs[:ratelimit] || RATELIMIT
|
2023-07-07 02:30:49 +01:00
|
|
|
@midi = Midi.new
|
2023-07-14 01:49:26 +01:00
|
|
|
@event =
|
|
|
|
Events::Tracker.new(
|
|
|
|
**(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k })
|
|
|
|
)
|
2023-07-14 11:44:49 +01:00
|
|
|
@callbacks = []
|
2023-07-14 01:49:26 +01:00
|
|
|
@que = Queue.new
|
2023-07-14 11:44:49 +01:00
|
|
|
@cache = {strip_mode: 0}
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
2023-07-09 05:48:08 +01:00
|
|
|
def to_s
|
2023-07-14 01:49:26 +01:00
|
|
|
"Voicemeeter #{kind}"
|
2023-07-09 05:48:08 +01:00
|
|
|
end
|
|
|
|
|
2023-07-07 02:30:49 +01:00
|
|
|
def login
|
2023-07-14 11:44:49 +01:00
|
|
|
runvm if CBindings.call(:bind_login, ok: [0, 1]) == 1
|
2023-07-07 02:30:49 +01:00
|
|
|
clear_dirty
|
2023-07-09 05:48:08 +01:00
|
|
|
logger.info "Successfully logged into #{self} version #{version}"
|
2023-07-14 01:49:26 +01:00
|
|
|
if event.any?
|
|
|
|
init_worker(@que)
|
|
|
|
init_producer(@que)
|
|
|
|
end
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
2023-07-14 01:49:26 +01:00
|
|
|
@running = false
|
2023-07-07 02:30:49 +01:00
|
|
|
sleep(0.1)
|
|
|
|
CBindings.call(:bind_logout)
|
2023-07-09 05:48:08 +01:00
|
|
|
logger.info "Sucessfully logged out of #{self}"
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def pdirty?
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_is_parameters_dirty, ok: [0, 1]) == 1
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def mdirty?
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_macro_button_is_dirty, ok: [0, 1]) == 1
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
2023-07-14 01:49:26 +01:00
|
|
|
def ldirty?
|
|
|
|
cache[:strip_buf], cache[:bus_buf] = _get_levels
|
|
|
|
!(
|
|
|
|
cache[:strip_level] == cache[:strip_buf] &&
|
|
|
|
@cache[:bus_level] == cache[:bus_buf]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-07-07 02:30:49 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def clear_dirty
|
|
|
|
while pdirty? || mdirty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def runvm
|
|
|
|
kinds = {
|
|
|
|
basic: Kinds::KindEnum::BASIC,
|
|
|
|
banana: Kinds::KindEnum::BANANA,
|
2023-07-14 11:44:49 +01:00
|
|
|
potato: (Install::OS_BITS == 64) ? 6 : Kinds::KindEnum::POTATO
|
2023-07-07 02:30:49 +01:00
|
|
|
}
|
2023-07-14 01:49:26 +01:00
|
|
|
CBindings.call(:bind_run_voicemeeter, kinds[kind.name])
|
2023-07-07 02:30:49 +01:00
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
def type
|
|
|
|
ckind = FFI::MemoryPointer.new(:long, 1)
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_get_voicemeeter_type, ckind)
|
2023-07-07 02:30:49 +01:00
|
|
|
kinds = {
|
|
|
|
Kinds::KindEnum::BASIC => :basic,
|
|
|
|
Kinds::KindEnum::BANANA => :banana,
|
|
|
|
Kinds::KindEnum::POTATO => :potato
|
|
|
|
}
|
|
|
|
kinds[ckind.read_long]
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
cver = FFI::MemoryPointer.new(:long, 1)
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_get_voicemeeter_version, cver)
|
2023-07-07 02:30:49 +01:00
|
|
|
[
|
|
|
|
(cver.read_long & 0xFF000000) >> 24,
|
|
|
|
(cver.read_long & 0x00FF0000) >> 16,
|
|
|
|
(cver.read_long & 0x0000FF00) >> 8,
|
|
|
|
cver.read_long & 0x000000FF
|
|
|
|
].join(".")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(name, is_string = false)
|
|
|
|
clear_dirty if @sync
|
|
|
|
if is_string
|
|
|
|
c_get = FFI::MemoryPointer.new(:string, 512, true)
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_get_parameter_string_a, name, c_get)
|
2023-07-07 02:30:49 +01:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set(name, value)
|
|
|
|
if value.is_a? String
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_set_parameter_string_a, name, value)
|
2023-07-07 02:30:49 +01:00
|
|
|
else
|
|
|
|
CBindings.call(:bind_set_parameter_float, name, value.to_f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_buttonstatus(id, mode)
|
|
|
|
clear_dirty if @sync
|
|
|
|
c_get = FFI::MemoryPointer.new(:float, 1)
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_macro_button_get_status, id, c_get, mode)
|
2023-07-07 02:30:49 +01:00
|
|
|
c_get.read_float.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_buttonstatus(id, state, mode)
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_macro_button_set_status, id, state, mode)
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
|
2023-07-14 01:49:26 +01:00
|
|
|
def get_level(mode, index)
|
2023-07-07 02:30:49 +01:00
|
|
|
cget = FFI::MemoryPointer.new(:float, 1)
|
2023-07-14 01:49:26 +01:00
|
|
|
CBindings.call(:bind_get_level, mode, index, cget)
|
2023-07-07 02:30:49 +01:00
|
|
|
cget.read_float
|
|
|
|
end
|
|
|
|
|
2023-07-14 01:49:26 +01:00
|
|
|
def _get_levels
|
|
|
|
[
|
|
|
|
(0...kind.num_strip_levels).map do |i|
|
|
|
|
get_level(cache[:strip_mode], i)
|
|
|
|
end,
|
|
|
|
(0...kind.num_bus_levels).map { |i| get_level(3, i) }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-07-07 02:30:49 +01:00
|
|
|
def get_num_devices(dir)
|
|
|
|
unless %i[in out].include? dir
|
|
|
|
raise VMError.new("dir got: #{dir}, expected in or out")
|
|
|
|
end
|
|
|
|
if dir == :in
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_input_get_device_number, exp: ->(x) { x >= 0 })
|
2023-07-07 02:30:49 +01:00
|
|
|
else
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(:bind_output_get_device_number, exp: ->(x) { x >= 0 })
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_device_description(index, dir)
|
|
|
|
unless %i[in out].include > dir
|
|
|
|
raise VMError.new("dir got: #{dir}, expected in or out")
|
|
|
|
end
|
|
|
|
ctype = FFI::MemoryPointer.new(:long, 1)
|
|
|
|
cname = FFI::MemoryPointer.new(:string, 256, true)
|
|
|
|
chwid = FFI::MemoryPointer.new(:string, 256, true)
|
|
|
|
if dir == :in
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(
|
|
|
|
:bind_input_get_device_desc_a,
|
|
|
|
index,
|
|
|
|
ctype,
|
|
|
|
cname,
|
|
|
|
chwid
|
|
|
|
)
|
2023-07-07 02:30:49 +01:00
|
|
|
else
|
2023-07-09 23:48:35 +01:00
|
|
|
CBindings.call(
|
|
|
|
:bind_output_get_device_desc_a,
|
|
|
|
index,
|
|
|
|
ctype,
|
|
|
|
cname,
|
|
|
|
chwid
|
|
|
|
)
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
[cname.read_string, ctype.read_long, chwid.read_string]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_midi_message
|
|
|
|
cmsg = FFI::MemoryPointer.new(:string, 1024, true)
|
|
|
|
res =
|
|
|
|
CBindings.call(
|
|
|
|
:bind_get_midi_message,
|
|
|
|
cmsg,
|
|
|
|
1024,
|
2023-07-14 01:49:26 +01:00
|
|
|
ok: [-5, -6],
|
2023-07-07 02:30:49 +01:00
|
|
|
exp: ->(x) { x >= 0 }
|
|
|
|
)
|
|
|
|
if (got_midi = res > 0)
|
|
|
|
vals = cmsg.read_string_bytes
|
|
|
|
vals.each_slice(3) do |ch, key, velocity|
|
2023-07-14 01:49:26 +01:00
|
|
|
midi.channel = ch if midi.channel.nil? || midi.channel != ch
|
|
|
|
midi.current = key.to_i
|
|
|
|
midi.set(key.to_i, velocity.to_i)
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
got_midi
|
|
|
|
end
|
2023-07-09 05:48:08 +01:00
|
|
|
|
|
|
|
def apply(data)
|
|
|
|
data.each do |key, val|
|
|
|
|
kls, index, *rem = key.to_s.split("-")
|
|
|
|
if rem.empty?
|
2023-07-14 01:49:26 +01:00
|
|
|
target = send(kls)
|
2023-07-09 05:48:08 +01:00
|
|
|
else
|
|
|
|
dir = "#{index.chomp("stream")}stream"
|
|
|
|
index = rem[0]
|
2023-07-14 01:49:26 +01:00
|
|
|
target = vban.send(dir)
|
2023-07-09 05:48:08 +01:00
|
|
|
end
|
|
|
|
target[index.to_i].apply(val)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_config(name)
|
2023-07-14 01:49:26 +01:00
|
|
|
apply(configs[name])
|
2023-07-09 23:48:35 +01:00
|
|
|
logger.info "profile #{name} applied!"
|
2023-07-09 05:48:08 +01:00
|
|
|
end
|
2023-07-07 02:30:49 +01:00
|
|
|
end
|
|
|
|
end
|