From 86a4060a689e133d645429f08fcc217f847c13a9 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 1 Nov 2025 23:06:26 +0300 Subject: [PATCH] feat: multiple audio fetchers support --- res/modules/internal/audio_input.lua | 21 +++++++++++++++++---- res/modules/internal/bytearray.lua | 21 +++++++++++++++++++-- res/scripts/stdlib.lua | 9 ++++----- res/scripts/stdmin.lua | 6 ++++++ src/logic/scripting/lua/libs/libaudio.cpp | 2 +- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/res/modules/internal/audio_input.lua b/res/modules/internal/audio_input.lua index 3540e88b..c0aa638e 100644 --- a/res/modules/internal/audio_input.lua +++ b/res/modules/internal/audio_input.lua @@ -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() diff --git a/res/modules/internal/bytearray.lua b/res/modules/internal/bytearray.lua index 56795499..7d5d250e 100644 --- a/res/modules/internal/bytearray.lua +++ b/res/modules/internal/bytearray.lua @@ -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 diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 71a8256e..19c17384 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -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() diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 7e8678e5..49714b99 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -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" diff --git a/src/logic/scripting/lua/libs/libaudio.cpp b/src/logic/scripting/lua/libs/libaudio.cpp index ed2e3db6..37c26549 100644 --- a/src/logic/scripting/lua/libs/libaudio.cpp +++ b/src/logic/scripting/lua/libs/libaudio.cpp @@ -476,7 +476,7 @@ const luaL_Reg audiolib[] = { {"get_velocity", lua::wrap}, {"count_speakers", lua::wrap}, {"count_streams", lua::wrap}, - {"fetch_input", lua::wrap}, + {"__fetch_input", lua::wrap}, {"get_input_devices_names", lua::wrap}, {"get_output_devices_names", lua::wrap}, {"set_input_device", lua::wrap},