Skip to content

Commit

Permalink
Some more LuaLS documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuAuahDark committed Oct 4, 2024
1 parent 9a3a7d9 commit b95d433
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 17 deletions.
28 changes: 25 additions & 3 deletions audio_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local AudioManager = {
}

-- must be called before any audio is loaded or the behaviour is undefined
---@param rate integer
function AudioManager.setRenderFramerate(rate)
if rate > 0 then
local smpPerFrame = 48000/rate
Expand Down Expand Up @@ -120,28 +121,36 @@ function AudioManager.newAudio(path, kind)
end
end

---@param data love.SoundData|love.Decoder|love.Data|love.File
---@param kind string
---@return livesim2.AudioManager.Object
function AudioManager.newAudioDirect(data, kind)
--kind = kind or "master"
---@class livesim2.AudioManager.Object
local obj = {
pos = 0,
size = 0,
volume = Volume.get(kind),
volumeKind = kind,
playing = false,
looping = false,
---@type love.SoundData|nil
soundData = nil,
soundDataPointer = nil,
---@type love.SoundData|nil
originalSoundData = nil,
---@type integer
channelCount = nil,
---@type love.Source
source = nil,
}
if AudioManager.renderRate > 0 then
-- render mode requires 48000Hz
if type(data) == "userdata" and not(data:typeOf("SoundData")) then
if Util.isLOVEType(data) and not Util.isLOVEType(data, "SoundData") then
local sdAsync = Async.syncLily(lily.newSoundData(data))
data = sdAsync:getValues() -- automatically sync
end

---@cast data love.SoundData
obj.channelCount = Util.getChannelCount(data)

-- check sample rate
Expand Down Expand Up @@ -169,7 +178,8 @@ function AudioManager.newAudioDirect(data, kind)
obj.soundDataPointer = ffi.cast("short*", data:getPointer())
return obj
else
if type(data) == "userdata" and data:typeOf("SoundData") then
if Util.isLOVEType(data, "SoundData") then
---@cast data love.SoundData
obj.soundData = data
obj.source = love.audio.newSource(data)
else
Expand All @@ -181,7 +191,9 @@ function AudioManager.newAudioDirect(data, kind)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.clone(obj)
---@class livesim2.AudioManager.Object
local x = {
pos = 0,
size = obj.size,
Expand All @@ -200,6 +212,7 @@ function AudioManager.clone(obj)
return x
end

---@param obj livesim2.AudioManager.Object
function AudioManager.play(obj)
if AudioManager.renderRate > 0 then
if obj.playing then return end
Expand All @@ -210,6 +223,7 @@ function AudioManager.play(obj)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.pause(obj)
if AudioManager.renderRate > 0 then
if not(obj.playing) then return end
Expand All @@ -226,6 +240,7 @@ function AudioManager.pause(obj)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.stop(obj)
if AudioManager.renderRate > 0 then
AudioManager.pause(obj)
Expand All @@ -235,6 +250,7 @@ function AudioManager.stop(obj)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.isLooping(obj)
if AudioManager.renderRate > 0 then
return obj.looping
Expand All @@ -243,6 +259,7 @@ function AudioManager.isLooping(obj)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.setLooping(obj, loop)
if AudioManager.renderRate > 0 then
obj.looping = loop
Expand All @@ -251,6 +268,7 @@ function AudioManager.setLooping(obj, loop)
end
end

---@param obj livesim2.AudioManager.Object
function AudioManager.isPlaying(obj)
if AudioManager.renderRate > 0 then
return obj.playing
Expand All @@ -259,6 +277,8 @@ function AudioManager.isPlaying(obj)
end
end

---@param obj livesim2.AudioManager.Object
---@param vol number
function AudioManager.setVolume(obj, vol)
vol = Volume.get(obj.volumeKind, vol)
if AudioManager.renderRate > 0 then
Expand All @@ -268,6 +288,8 @@ function AudioManager.setVolume(obj, vol)
end
end

---@param obj livesim2.AudioManager.Object
---@param seconds number
function AudioManager.seek(obj, seconds)
if AudioManager.renderRate > 0 then
obj.pos = 48000 * seconds
Expand Down
1 change: 1 addition & 0 deletions color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ local function hexCache(hexcode)
return v
end

---@class color:{[string]:number[]}
local color = setmetatable({}, {
__index = function(color, var)
if var:find("hex", 1, true) == 1 then
Expand Down
2 changes: 1 addition & 1 deletion fixmrt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- This script patches LOVE rendering pipeline to workaround MRT bandwidth problem in some GPU drivers.
-- See: https://github.com/love2d/love/commit/1896f82f5d40d33a551f7c5fd9662db3b4ccdf31

if love.getVersion() < 11 or love._version >= "11.3" then
if love.getVersion() ~= 11 or love._version >= "11.3" then
-- LOVE pre-11.0 does not have this problem.
-- LOVE 11.3 already have this problem worked around.
return true
Expand Down
16 changes: 16 additions & 0 deletions game/bgm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ local AudioManager = require("audio_manager")
local Util = require("util")

local BGM = {}
---@class livesim2.BGM
local BGMClass = Luaoop.class("livesim2.BGM")

---@param sd love.SoundData|love.Decoder|love.Data|love.File
function BGMClass:__construct(sd)
if not(sd:typeOf("SoundData")) then
sd = love.sound.newSoundData(sd)
end

---@cast sd love.SoundData
self.audio = AudioManager.newAudioDirect(sd, "music")
self.channel = Util.getChannelCount(sd)
self.soundData = sd
Expand All @@ -33,11 +36,17 @@ function BGMClass:rewind()
return AudioManager.play(self.audio)
end

---@param sd love.SoundData
---@param pos integer
function BGMClass._getSampleSafe(sd, pos)
local s, v = pcall(sd.getSample, sd, pos)
return s and v or 0
end

---@param output number[]
---@param sd love.SoundData
---@param pos integer
---@param amount integer
function BGMClass:_populateSample(output, sd, pos, amount)
if self.channel == 1 then
-- mono
Expand All @@ -55,6 +64,8 @@ function BGMClass:_populateSample(output, sd, pos, amount)
end
end

---@param output number[]
---@param amount integer
function BGMClass:_getSamplesRender(output, amount)
-- Use original sound data
local sd, pos
Expand All @@ -71,6 +82,8 @@ function BGMClass:_getSamplesRender(output, amount)
end

-- interleaved samples: {l, r, l, r, l, r, ...}
---@param amount integer
---@return number[]
function BGMClass:getSamples(amount)
local output = {}
if AudioManager.renderRate > 0 then
Expand All @@ -82,6 +95,7 @@ function BGMClass:getSamples(amount)
return output
end

---@param timepos number
function BGMClass:seek(timepos)
return AudioManager.seek(self.audio, timepos)
end
Expand All @@ -106,6 +120,8 @@ function BGMClass:getSampleRate()
end
end

---@param decoder love.FileData|love.File|string
---@return livesim2.BGM
function BGM.newSong(decoder)
return BGMClass(Util.newDecoder(decoder))
end
Expand Down
6 changes: 5 additions & 1 deletion game/color_theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ local currentColor, currentColorDark, currentColorDarker

-- colid: 1 = μ's, 2 = Aqours, 3 = NijiGaku, 4 = Liella, 5 = Hasunosora
-- 6 = Musical, 7 = Yohane
---@param colid integer
function ColorTheme.init(colid)
if currentColor then return end
return ColorTheme.set(colid)
end

---@param colid integer
function ColorTheme.set(colid)
if ColorTheme[colid] == nil then
error("unknown color id "..colid)
Expand All @@ -81,6 +83,7 @@ function ColorTheme.set(colid)
currentColorDarker = ColorTheme[colid].currentColorDarker
end

---@param opacity number
function ColorTheme.get(opacity)
assert(currentColor, "forgot to call colorTheme.init()")
if opacity then
Expand All @@ -90,6 +93,7 @@ function ColorTheme.get(opacity)
end
end

---@param opacity number
function ColorTheme.getDark(opacity)
assert(currentColorDark, "forgot to call colorTheme.init()")
if opacity then
Expand All @@ -99,7 +103,7 @@ function ColorTheme.getDark(opacity)
end
end


---@param opacity number
function ColorTheme.getDarker(opacity)
assert(currentColorDarker, "forgot to call colorTheme.init()")
if opacity then
Expand Down
4 changes: 2 additions & 2 deletions game/live/lyrics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local MainFont = require("main_font")
local Lyrics = Luaoop.class("Livesim2.Lyrics")
local perZeroThree = 1/0.3

---@param srt Livesim2.SrtParseData[]
---@param srt livesim2.SrtParseData[]
function Lyrics:__construct(srt)
local font1, font2 = MainFont.get(24, 16)
local timings = {}
Expand Down Expand Up @@ -79,5 +79,5 @@ function Lyrics:draw()
end
end

---@cast Lyrics +fun(srt:Livesim2.SrtParseData[]):Livesim2.Lyrics
---@cast Lyrics +fun(srt:livesim2.SrtParseData[]):Livesim2.Lyrics
return Lyrics
8 changes: 3 additions & 5 deletions game/md5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ local Util = require("util")
local md5impl
local md5implfb

local function isData(t)
return type(t) == "userdata" and t:typeOf("Data")
end

function md5implfb(code)
if type(code) == "userdata" and code:typeOf("Data") then
return md5fb.sum(code:getString())
Expand All @@ -29,8 +25,10 @@ if Util.compareLOVEVersion(11, 0) >= 0 then
end
end

---@param code string
---@return string
return function(code)
local len = isData(code) and code:getSize() or #code
local len = Util.isLOVEType(code, "Data") and code:getSize() or #code
-- https://bitbucket.org/rude/love/issues/1453
-- https://bitbucket.org/rude/love/pull-requests/117
-- LOVE 11.2 and earlier produces wrong hash for
Expand Down
2 changes: 1 addition & 1 deletion game/srt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

---@param iter fun():(string|nil)
return function(iter)
---@type Livesim2.SrtParseData[]
---@type livesim2.SrtParseData[]
local res = {}

-- ignore sub number, unnecessary
Expand Down
2 changes: 1 addition & 1 deletion type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end
---@field public star integer
local SummaryInfo = {}

---@class Livesim2.SrtParseData
---@class livesim2.SrtParseData
---@field public start number
---@field public stop number
---@field public text1 string
Expand Down
9 changes: 9 additions & 0 deletions util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function Util.releaseObject(obj)
end

---@param sounddata love.SoundData
---@return integer
function Util.getChannelCount(sounddata)
if version11 then return sounddata:getChannelCount()
else return sounddata:getChannels() end
Expand Down Expand Up @@ -277,6 +278,8 @@ function Util.isMobile()
return love._os == "iOS" or love._os == "Android"
end

---@param path love.FileData|love.File|string
---@return love.Decoder
function Util.newDecoder(path)
if hasLVEP then
local s, v = pcall(love.sound.newDecoder, path)
Expand Down Expand Up @@ -538,6 +541,12 @@ function Util.split(text, delim, removeempty)
return t
end

---@param obj any
---@param lovetype string?
---@return boolean
function Util.isLOVEType(obj, lovetype)
return type(obj) == "userdata" and obj.typeOf and obj:typeOf(lovetype or "Object")
end

if version11 then
local fontDPIScale = 1
Expand Down
11 changes: 8 additions & 3 deletions volume.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ local Volume = {
list = {master = 0.8}
}

-- volume default initialized to 0.8
---@param name string
---@param value number
function Volume.define(name, value)
assert(Volume.list[name] == nil, "name already defined")
Volume.list[name] = assert(tonumber(value), "invalid default volume")
end

-- value default to 1 if not specified
---value default to 1 if not specified
---@param name string
---@param value number?
function Volume.get(name, value)
assert(Volume.list[name], "name doesn't exist")
value = value or 1
Expand All @@ -23,7 +26,9 @@ function Volume.get(name, value)
end
end

-- usually settings set the volume
---usually settings set the volume
---@param name string
---@param value number
function Volume.set(name, value)
Volume.list[name] = assert(tonumber(value), "invalid volume")
end
Expand Down

0 comments on commit b95d433

Please sign in to comment.