cube-tube/app/util/music.rb

87 lines
2 KiB
Ruby
Raw Permalink Normal View History

2023-03-23 05:16:35 +00:00
# frozen_string_literal: true
# Module for managing and interacting with music.
module Music
class << self
def queue
@queue ||= {}
@queue
end
def queue_up(key, channel = 0)
@queue[channel] ||= []
@queue[channel].push(MUSIC.fetch(key))
end
def for(key)
MUSIC.fetch(key)
end
def play(args, key, opts = { channel: 0 })
MUSIC.fetch(key).play_music(args, opts)
end
def stopped?(args, channel = nil)
channel_names(args, channel).none? do |c|
args.audio.key?(c)
end
2023-03-23 05:16:35 +00:00
end
def paused?(args, channel = nil)
channel_names(args, channel).all? do |c|
args.audio.key?(c) && args.audio[c].paused
end
2023-03-23 05:16:35 +00:00
end
def stop(args, channel = nil)
channel_names(args, channel).each do |c|
args.audio.delete(c)
end
2023-03-23 05:16:35 +00:00
end
def pause(args, channel = nil)
channel_names(args, channel).each do |c|
args.audio[c].paused = true unless stopped?(args, c)
end
2023-03-23 05:16:35 +00:00
end
def resume(args, channel = nil)
channel_names(args, channel).each do |c|
args.audio[c].paused = false unless stopped?(args, c)
end
2023-03-23 05:16:35 +00:00
end
def set_volume(args, volume, channel = nil)
channel_names(args, channel).each do |c|
args.audio[c].gain = volume
end
end
def channel_names(args, channel = nil)
if channel.nil?
args.audio.keys.select do |key|
key.start_with? 'MUSIC_CHANNEL_'
end
elsif channel.is_a?(String)
[channel]
elsif channel.is_a?(Numeric)
["MUSIC_CHANNEL_#{channel}"]
else
channel
end
2023-03-23 05:16:35 +00:00
end
def tick(args)
queue.each do |channel, value|
next if value.empty?
if args.audio["MUSIC_CHANNEL_#{channel}"]
args.audio["MUSIC_CHANNEL_#{channel}"].looping = false if args.audio["MUSIC_CHANNEL_#{channel}"].looping
else
value.shift.play_music(args, { channel: channel, gain: args.state.setting.music ? 0.8 : 0.0, paused: false })
2023-03-23 05:16:35 +00:00
end
end
end
end
end