Events::Callback now a class

on_event method renamed to trigger

events example updated to reflect changes to Events module
This commit is contained in:
onyx-and-iris 2023-07-17 08:34:34 +01:00
parent 6d080af041
commit ac7d9c0d03
4 changed files with 20 additions and 15 deletions

View File

@ -1,13 +1,13 @@
require_relative "../../lib/voicemeeter" require_relative "../../lib/voicemeeter"
class Main class Main
def initialize def initialize(vm)
@vm = Voicemeeter::Remote.new(:potato, pdirty: true, ldirty: true) @vm = vm
@vm.register(method(:on_pdirty), method(:on_ldirty)) @vm.callback.register(method(:on_pdirty), method(:on_mdirty), method(:on_midi), method(:on_ldirty))
end end
def run def run
@vm.run { exit if gets.chomp.empty? } exit if gets.chomp.empty?
end end
def on_pdirty def on_pdirty
@ -30,4 +30,8 @@ class Main
end end
end end
Main.new.run if $0 == __FILE__ if $0 == __FILE__
Voicemeeter::Remote.new(:potato, pdirty: true, mdirty: true, midi: true, ldirty: true).run do |vm|
Main.new(vm).run
end
end

View File

@ -11,9 +11,8 @@ module Voicemeeter
class Base class Base
include Logging include Logging
include Worker include Worker
include Events::Callbacks
attr_reader :kind, :midi, :event, :running attr_reader :kind, :midi, :event, :running, :callback
attr_accessor :cache attr_accessor :cache
RATELIMIT = 0.033 RATELIMIT = 0.033
@ -27,7 +26,7 @@ module Voicemeeter
Events::Tracker.new( Events::Tracker.new(
**(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k }) **(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k })
) )
@callbacks = [] @callback = Events::Callback.new
@que = Queue.new @que = Queue.new
@cache = {strip_mode: 0} @cache = {strip_mode: 0}
end end

View File

@ -2,8 +2,10 @@ require_relative "logger"
module Voicemeeter module Voicemeeter
module Events module Events
module Callbacks class Callback
attr_reader :callbacks def initialize
@callbacks = []
end
def register(*args) def register(*args)
args.each { |callback| @callbacks.append callback } args.each { |callback| @callbacks.append callback }
@ -13,7 +15,7 @@ module Voicemeeter
@callbacks.reject! { |c| args.include? c } @callbacks.reject! { |c| args.include? c }
end end
def on_event(event) def trigger(event)
@callbacks.each do |callback| @callbacks.each do |callback|
if callback.respond_to? :on_update if callback.respond_to? :on_update
callback.on_update { event.to_s[3..] } callback.on_update { event.to_s[3..] }

View File

@ -31,15 +31,15 @@ module Voicemeeter
logger.debug("closing worker thread") logger.debug("closing worker thread")
break break
end end
on_event :on_pdirty if e_from_que == :pdirty && pdirty? callback.trigger :on_pdirty if e_from_que == :pdirty && pdirty?
on_event :on_mdirty if e_from_que == :mdirty && mdirty? callback.trigger :on_mdirty if e_from_que == :mdirty && mdirty?
on_event :on_midi if e_from_que == :midi && get_midi_message callback.trigger :on_midi if e_from_que == :midi && get_midi_message
if e_from_que == :ldirty && ldirty? if e_from_que == :ldirty && ldirty?
cache[:strip_comp] = cache[:strip_level].zip(cache[:strip_buf]).map { |a, b| a != b } cache[:strip_comp] = cache[:strip_level].zip(cache[:strip_buf]).map { |a, b| a != b }
cache[:bus_comp] = cache[:bus_level].zip(cache[:bus_buf]).map { |a, b| a != b } cache[:bus_comp] = cache[:bus_level].zip(cache[:bus_buf]).map { |a, b| a != b }
cache[:strip_level] = cache[:strip_buf] cache[:strip_level] = cache[:strip_buf]
cache[:bus_level] = cache[:bus_buf] cache[:bus_level] = cache[:bus_buf]
on_event :on_ldirty callback.trigger :on_ldirty
end end
end end
end end