2022-04-28 00:21:10 +02:00
|
|
|
require "common.class"
|
|
|
|
|
|
|
|
---@type AudioSample
|
|
|
|
local exclusiveAudioSample = nil
|
|
|
|
|
|
|
|
---@class AudioSample
|
|
|
|
---@field path string
|
|
|
|
---@field exclusive boolean
|
|
|
|
---@field loop boolean
|
2022-05-06 01:02:30 +02:00
|
|
|
local AudioSample = {
|
|
|
|
__name = "AudioSample"
|
|
|
|
}
|
2022-04-28 00:21:10 +02:00
|
|
|
|
|
|
|
---Create new AudioSample instance
|
2022-05-06 01:02:30 +02:00
|
|
|
---@param params AudioSample
|
2022-04-28 00:21:10 +02:00
|
|
|
---@return AudioSample
|
2022-05-06 01:02:30 +02:00
|
|
|
function AudioSample.new(params)
|
|
|
|
assert(params.path, "AudioSample.new() did not receive path to audio sample")
|
|
|
|
params.exclusive = params.exclusive or false
|
|
|
|
params.loop = params.loop or false
|
2022-04-28 00:21:10 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
game.LoadSkinSample(params.path)
|
2022-04-28 00:21:10 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
return CreateInstance(AudioSample, params)
|
2022-04-28 00:21:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function AudioSample:play()
|
2022-06-29 00:23:16 +02:00
|
|
|
if not game.IsSamplePlaying(self.path) then
|
2022-04-28 00:21:10 +02:00
|
|
|
if self.exclusive then
|
|
|
|
if exclusiveAudioSample and self ~= exclusiveAudioSample then
|
|
|
|
exclusiveAudioSample:stop()
|
|
|
|
end
|
|
|
|
exclusiveAudioSample = self
|
|
|
|
end
|
|
|
|
game.PlaySample(self.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function AudioSample:stop()
|
2022-06-29 00:23:16 +02:00
|
|
|
if game.IsSamplePlaying(self.path) then
|
2022-04-28 00:21:10 +02:00
|
|
|
if self.exclusive and self == exclusiveAudioSample then
|
|
|
|
exclusiveAudioSample = nil
|
|
|
|
end
|
|
|
|
game.StopSample(self.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return AudioSample
|