feat: multiple audio fetchers support

This commit is contained in:
MihailRis
2025-11-01 23:06:26 +03:00
parent 5a65cbe071
commit 86a4060a68
5 changed files with 47 additions and 12 deletions
+17 -4
View File
@@ -5,21 +5,34 @@ local _gui_confirm = gui.confirm
local _base64_encode_urlsafe = base64.encode_urlsafe
local _random_bytes = random.bytes
local _debug_pack_by_frame = debug.get_pack_by_frame
local _audio_fetch_input = audio.fetch_input
local _audio_fetch_input = audio.__fetch_input
audio.__fetch_input = nil
local MAX_FETCH = 44100 * 4
local total_fetch = Bytearray()
function audio.__reset_fetch_buffer()
total_fetch:clear()
end
function audio.fetch_input(token, size)
size = size or MAX_FETCH
if audio_input_tokens_store[token] then
return _audio_fetch_input(size)
if #total_fetch >= size then
return total_fetch:sub(1, size)
end
total_fetch:append(_audio_fetch_input(size - #total_fetch))
return total_fetch:sub()
end
error("access denied")
end
local GRAND_PERMISSION_MSG = "Grant '%{0}' pack audio recording permission?"
local GRANT_PERMISSION_MSG = "Grant '%{0}' pack audio recording permission?"
function audio.input.request_open(callback)
local token = _base64_encode_urlsafe(_random_bytes(18))
local caller = _debug_pack_by_frame(1)
_gui_confirm(gui.str(GRAND_PERMISSION_MSG):gsub("%%{0}", caller), function()
_gui_confirm(gui.str(GRANT_PERMISSION_MSG):gsub("%%{0}", caller), function()
audio_input_tokens_store[token] = caller
callback(token)
menu:reset()
+19 -2
View File
@@ -14,6 +14,8 @@ FFI.cdef[[
local malloc = FFI.C.malloc
local free = FFI.C.free
local FFIBytearray
local bytearray_type
local function grow_buffer(self, elems)
local new_capacity = math.ceil(self.capacity / 0.75 + elems)
@@ -119,6 +121,20 @@ local function get_capacity(self)
return self.capacity
end
local function sub(self, offset, length)
offset = offset or 1
length = length or (self.size - offset + 1)
if offset < 1 or offset > self.size then
return FFIBytearray(0)
end
if offset + length - 1 > self.size then
length = self.size - offset + 1
end
local buffer = malloc(length)
FFI.copy(buffer, self.bytes + (offset - 1), length)
return bytearray_type(buffer, length, length)
end
local bytearray_methods = {
append=append,
insert=insert,
@@ -127,6 +143,7 @@ local bytearray_methods = {
clear=clear,
reserve=reserve,
get_capacity=get_capacity,
sub=sub,
}
local bytearray_mt = {
@@ -168,9 +185,9 @@ local bytearray_mt = {
}
bytearray_mt.__pairs = bytearray_mt.__ipairs
local bytearray_type = FFI.metatype("bytearray_t", bytearray_mt)
bytearray_type = FFI.metatype("bytearray_t", bytearray_mt)
local FFIBytearray = {
FFIBytearray = {
__call = function (self, n)
local t = type(n)
if t == "string" then