obsws-ruby/examples/levels/main.rb

46 lines
1.1 KiB
Ruby
Raw Normal View History

2023-07-21 06:37:14 +01:00
require_relative "../../lib/obsws"
2023-07-19 15:13:26 +01:00
require "yaml"
2022-10-27 06:45:52 +01:00
module LevelTypes
VU = 0
POSTFADER = 1
PREFADER = 2
end
2023-07-19 15:13:26 +01:00
class Main
2023-07-21 07:55:50 +01:00
DEVICE = "Desktop Audio"
2022-10-27 06:45:52 +01:00
def initialize(**kwargs)
2023-07-19 15:13:26 +01:00
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
@e_client.on(:input_mute_state_changed) do |data|
if data.input_name == DEVICE
puts "#{DEVICE} mute toggled"
end
end
@e_client.on(:input_volume_meters) do |data|
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.call(left[LevelTypes::POSTFADER])}, R: #{fget.call(right[LevelTypes::POSTFADER])}]"
end
end
end
2022-10-27 06:45:52 +01:00
end
2023-07-19 15:13:26 +01:00
def run
puts "press <Enter> to quit"
2023-07-21 07:55:50 +01:00
loop { break if gets.chomp.empty? }
2023-07-19 15:13:26 +01:00
end
2022-10-27 06:45:52 +01:00
end
2023-07-19 15:13:26 +01:00
def conn_from_yaml
YAML.load_file("obs.yml", symbolize_names: true)[:connection]
2022-10-27 06:45:52 +01:00
end
2023-07-21 06:37:14 +01:00
Main.new(**conn_from_yaml).run if $PROGRAM_NAME == __FILE__