FileReader each method added

yields, identifier and data read for each configfile

register moved into Loader
This commit is contained in:
onyx-and-iris 2023-08-02 12:35:18 +01:00
parent f4be09be2a
commit fdfadaafac

View File

@ -7,7 +7,78 @@ require_relative "errors"
module Voicemeeter module Voicemeeter
module Configs module Configs
private class TOMLStrBuilder
def self.run(kind)
aouts = (0...kind.phys_out).to_h { |i| ["A#{i + 1}".to_sym, false] }
bouts = (0...kind.virt_out).to_h { |i| ["B#{i + 1}".to_sym, false] }
strip_bools = %i[mute mono solo].to_h { |param| [param, false] }
gain = [:gain].to_h { |param| [param, 0.0] }
phys_float =
%i[comp gate denoiser].to_h { |param| [param, {knob: 0.0}] }
eq = [:eq].to_h { |param| [param, {on: false}] }
overrides = {B1: true}
# physical strip params
phys_strip =
(0...kind.phys_in).to_h do |i|
[
"strip-#{i}".to_sym,
{**aouts, **bouts, **strip_bools, **gain, **phys_float, **eq, **overrides}
]
end
overrides = {A1: true}
# virtual strip params
virt_strip =
(kind.phys_in...kind.phys_in + kind.virt_in).to_h do |i|
[
"strip-#{i}".to_sym,
{**aouts, **bouts, **strip_bools, **gain, **overrides}
]
end
bus_bools = %i[mute mono].to_h { |param| [param, false] }
bus =
(0...kind.num_bus).to_h do |i|
["bus-#{i}".to_sym, {**bus_bools, **gain, **eq}]
end
{**phys_strip, **virt_strip, **bus}
end
end
class FileReader
include Logging
def initialize(loader, kind)
@loader = loader
@configpaths = [
Pathname.getwd.join("configs", kind.name.to_s),
Pathname.new(Dir.home).join(".config", "voicemeeter-rb", kind.name.to_s),
Pathname.new(Dir.home).join("Documents", "Voicemeeter", "configs", kind.name.to_s)
]
end
def each
@configpaths.each do |configpath|
if configpath.exist?
logger.debug "checking #{configpath} for configs"
filepaths = configpath.glob("*.yml")
filepaths.each do |filepath|
@filename = (filepath.basename.sub_ext "").to_s.to_sym
yield @filename, YAML.load_file(
filepath,
symbolize_names: true
)
end
end
end
rescue Psych::SyntaxError => e
logger.error "#{e.class.name}: #{e.message}"
end
end
class Loader class Loader
include Logging include Logging
@ -19,111 +90,32 @@ module Voicemeeter
@configs = Hash.new do |hash, key| @configs = Hash.new do |hash, key|
raise Errors::VMError.new "unknown config #{key}. known configs: #{hash.keys}" raise Errors::VMError.new "unknown config #{key}. known configs: #{hash.keys}"
end end
@yml_reader = FileReader.new(self, kind) @filereader = FileReader.new(self, kind)
end end
def to_s def to_s
"Loader #{@kind}" "Loader #{@kind}"
end end
private private def register(identifier, data)
if configs.key? identifier
def build_reset_profile logger.debug "config with name '#{identifier}' already in memory, skipping..."
aouts = (0...@kind.phys_out).to_h { |i| ["A#{i + 1}".to_sym, false] }
bouts = (0...@kind.virt_out).to_h { |i| ["B#{i + 1}".to_sym, false] }
strip_bools = %i[mute mono solo].to_h { |param| [param, false] }
gain = [:gain].to_h { |param| [param, 0.0] }
phys_float =
%i[comp gate denoiser].to_h { |param| [param, {knob: 0.0}] }
eq = [:eq].to_h { |param| [param, {on: false}] }
overrides = {B1: true}
# physical strip params
phys_strip =
(0...@kind.phys_in).to_h do |i|
[
"strip-#{i}".to_sym,
{**aouts, **bouts, **strip_bools, **gain, **phys_float, **eq, **overrides}
]
end
overrides = {A1: true}
# virtual strip params
virt_strip =
(@kind.phys_in...@kind.phys_in + @kind.virt_in).to_h do |i|
[
"strip-#{i}".to_sym,
{**aouts, **bouts, **strip_bools, **gain, **overrides}
]
end
bus_bools = %i[mute mono].to_h { |param| [param, false] }
bus =
(0...@kind.num_bus).to_h do |i|
["bus-#{i}".to_sym, {**bus_bools, **gain, **eq}]
end
{**phys_strip, **virt_strip, **bus}
end
def read_from_yml = @yml_reader.read
public
def run
logger.debug "Running #{self}"
configs[:reset] = build_reset_profile
read_from_yml
self
end
end
class FileReader
include Logging
def initialize(loader, kind)
@loader = loader
@kind = kind
@configpaths = [
Pathname.getwd.join("configs", kind.name.to_s),
Pathname.new(Dir.home).join(".config", "voicemeeter-rb", kind.name.to_s),
Pathname.new(Dir.home).join("Documents", "Voicemeeter", "configs", kind.name.to_s)
]
end
def read
@configpaths.each do |configpath|
if configpath.exist?
logger.debug "checking #{configpath} for configs"
filepaths = configpath.glob("*.yml")
filepaths.each do |filepath|
register(filepath)
end
end
end
end
private
def register(filepath)
filename = (filepath.basename.sub_ext "").to_s.to_sym
if @loader.configs.key? filename
logger.debug "config with name '#{filename}' already in memory, skipping..."
return return
end end
@loader.configs[filename] = YAML.load_file( configs[identifier] = data
filepath, logger.info "#{@kind.name}/#{identifier} loaded into memory"
symbolize_names: true
)
logger.info "#{@kind.name}/#{filename} loaded into memory"
rescue Psych::SyntaxError => e
logger.error "#{e.class.name}: #{e.message}"
end
end end
public def run
logger.debug "Running #{self}"
configs[:reset] = TOMLStrBuilder.run(@kind)
@filereader.each do |identifier, data|
register(identifier, data)
end
self
end
end
def get(kind_id) def get(kind_id)
unless defined? @loaders unless defined? @loaders