mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2026-04-18 13:33:31 +00:00
examples refactored
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
require "obsws"
|
||||
require "perfect_toml"
|
||||
require "yaml"
|
||||
|
||||
OBSWS::LOGGER.info!
|
||||
DEVICE = "Desktop Audio"
|
||||
@@ -10,44 +10,39 @@ module LevelTypes
|
||||
PREFADER = 2
|
||||
end
|
||||
|
||||
class Observer
|
||||
attr_reader :running
|
||||
|
||||
class Main
|
||||
def initialize(**kwargs)
|
||||
kwargs[:subs] = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
|
||||
@e_client = OBSWS::Events::Client.new(**kwargs)
|
||||
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
|
||||
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
|
||||
@e_client.add_observer(self)
|
||||
end
|
||||
|
||||
|
||||
def run
|
||||
puts "press <Enter> to quit"
|
||||
exit if gets.chomp.empty?
|
||||
end
|
||||
|
||||
def on_input_mute_state_changed(data)
|
||||
"""An input's mute state has changed."""
|
||||
if data.input_name == DEVICE
|
||||
puts "#{DEVICE} mute toggled"
|
||||
end
|
||||
end
|
||||
|
||||
def on_input_volume_meters(data)
|
||||
def fget(x) = x > 0 ? (20 * Math.log(x, 10)).round(1) : -200.0
|
||||
fget = ->(x) { (x > 0) ? (20 * Math.log(x, 10)).round(1) : -200.0 }
|
||||
|
||||
data.inputs.each do |d|
|
||||
name = d[:inputName]
|
||||
if name == DEVICE && !d[:inputLevelsMul].empty?
|
||||
left, right = d[:inputLevelsMul]
|
||||
puts "#{name} [L: #{fget(left[LevelTypes::POSTFADER])}, R: #{fget(right[LevelTypes::POSTFADER])}]"
|
||||
puts "#{name} [L: #{fget.call(left[LevelTypes::POSTFADER])}, R: #{fget.call(right[LevelTypes::POSTFADER])}]"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def conn_from_toml
|
||||
PerfectTOML.load_file("obs.toml", symbolize_names: true)[:connection]
|
||||
def conn_from_yaml
|
||||
YAML.load_file("obs.yml", symbolize_names: true)[:connection]
|
||||
end
|
||||
|
||||
def main
|
||||
o = Observer.new(**conn_from_toml)
|
||||
|
||||
puts "press <Enter> to quit"
|
||||
loop { exit if gets.chomp.empty? }
|
||||
end
|
||||
|
||||
main if $0 == __FILE__
|
||||
Main.new(**conn_from_yaml).run if $0 == __FILE__
|
||||
|
||||
Reference in New Issue
Block a user