2023-07-16 11:08:24 +01:00
|
|
|
require_relative "logger"
|
2023-07-13 21:57:32 +01:00
|
|
|
|
|
|
|
module Voicemeeter
|
|
|
|
module Worker
|
2023-07-27 10:58:26 +01:00
|
|
|
# Event threads, provides updates to observers
|
|
|
|
|
2023-07-16 11:08:24 +01:00
|
|
|
include Logging
|
2023-07-13 21:57:32 +01:00
|
|
|
|
2023-08-03 18:45:42 +01:00
|
|
|
private
|
|
|
|
|
2023-07-13 21:57:32 +01:00
|
|
|
def init_producer(que)
|
2023-08-05 23:08:21 +01:00
|
|
|
@producer = Thread.new do
|
2023-08-03 11:04:13 +01:00
|
|
|
Thread.current.name = "producer"
|
2023-08-05 23:08:21 +01:00
|
|
|
Thread.current[:running] = true
|
|
|
|
while Thread.current[:running]
|
2023-07-13 21:57:32 +01:00
|
|
|
que << :pdirty if event.pdirty
|
|
|
|
que << :mdirty if event.mdirty
|
|
|
|
que << :midi if event.midi
|
|
|
|
que << :ldirty if event.ldirty
|
|
|
|
sleep(@ratelimit)
|
|
|
|
end
|
2023-08-03 11:04:13 +01:00
|
|
|
logger.debug "closing #{Thread.current.name} thread"
|
2023-08-05 23:08:21 +01:00
|
|
|
que << Thread.current[:running]
|
2023-07-13 21:57:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_worker(que)
|
|
|
|
logger.info "Listening for #{event.get.join(", ")} events"
|
2023-08-06 18:02:48 +01:00
|
|
|
cache[:strip_level], cache[:bus_level] = _get_levels
|
|
|
|
cache[:strip_comp] = Array.new(kind.num_strip_levels, false)
|
|
|
|
cache[:bus_comp] = Array.new(kind.num_bus_levels, false)
|
2023-07-13 21:57:32 +01:00
|
|
|
Thread.new do
|
2023-08-03 11:04:13 +01:00
|
|
|
Thread.current.name = "worker"
|
2023-08-03 18:45:42 +01:00
|
|
|
while (event = que.pop)
|
|
|
|
trigger :pdirty if event == :pdirty && pdirty?
|
|
|
|
trigger :mdirty if event == :mdirty && mdirty?
|
|
|
|
trigger :midi if event == :midi && get_midi_message
|
|
|
|
if event == :ldirty && ldirty?
|
2023-07-14 21:03:40 +01:00
|
|
|
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 }
|
2023-07-14 01:46:25 +01:00
|
|
|
cache[:strip_level] = cache[:strip_buf]
|
|
|
|
cache[:bus_level] = cache[:bus_buf]
|
2023-08-03 13:13:01 +01:00
|
|
|
trigger :ldirty
|
2023-07-13 21:57:32 +01:00
|
|
|
end
|
|
|
|
end
|
2023-08-03 18:45:42 +01:00
|
|
|
logger.debug "closing #{Thread.current.name} thread"
|
2023-07-13 21:57:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-03 18:45:42 +01:00
|
|
|
public
|
|
|
|
|
2023-08-10 14:40:22 +01:00
|
|
|
def running?
|
|
|
|
@producer&.alive? # safe navigation
|
|
|
|
end
|
|
|
|
|
2023-07-21 14:37:17 +01:00
|
|
|
def init_event_threads
|
2023-08-03 18:45:42 +01:00
|
|
|
que = Queue.new
|
2023-08-03 18:50:25 +01:00
|
|
|
init_worker(que) and init_producer(que)
|
2023-07-21 14:37:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def end_event_threads
|
2023-08-10 20:37:55 +01:00
|
|
|
if running?
|
2023-08-05 23:08:21 +01:00
|
|
|
@producer[:running] = false
|
|
|
|
@producer.join
|
|
|
|
end
|
2023-07-13 21:57:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|