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

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()

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

View File

@ -211,11 +211,6 @@ entities.get_all = function(uids)
end
end
local bytearray = require "core:internal/bytearray"
Bytearray = bytearray.FFIBytearray
Bytearray_as_string = bytearray.FFIBytearray_as_string
Bytearray_construct = function(...) return Bytearray(...) end
__vc_scripts_registry = require "core:internal/scripts_registry"
file.open = require "core:internal/stream_providers/file"
@ -424,6 +419,9 @@ end
local __post_runnables = {}
local fn_audio_reset_fetch_buffer = audio.__reset_fetch_buffer
audio.__reset_fetch_buffer = nil
function __process_post_runnables()
if #__post_runnables then
for _, func in ipairs(__post_runnables) do
@ -449,6 +447,7 @@ function __process_post_runnables()
__vc_named_coroutines[name] = nil
end
fn_audio_reset_fetch_buffer()
debug.pull_events()
network.__process_events()
block.__process_register_events()

View File

@ -265,6 +265,12 @@ require "core:internal/extensions/math"
require "core:internal/extensions/file"
require "core:internal/extensions/table"
require "core:internal/extensions/string"
local bytearray = require "core:internal/bytearray"
Bytearray = bytearray.FFIBytearray
Bytearray_as_string = bytearray.FFIBytearray_as_string
Bytearray_construct = function(...) return Bytearray(...) end
bit.compile = require "core:bitwise/compiler"
bit.execute = require "core:bitwise/executor"

View File

@ -476,7 +476,7 @@ const luaL_Reg audiolib[] = {
{"get_velocity", lua::wrap<l_audio_get_velocity>},
{"count_speakers", lua::wrap<l_audio_count_speakers>},
{"count_streams", lua::wrap<l_audio_count_streams>},
{"fetch_input", lua::wrap<l_audio_fetch_input>},
{"__fetch_input", lua::wrap<l_audio_fetch_input>},
{"get_input_devices_names", lua::wrap<l_audio_get_input_devices_names>},
{"get_output_devices_names", lua::wrap<l_audio_get_output_devices_names>},
{"set_input_device", lua::wrap<l_audio_set_input_device>},