add U16view, I16view, U32view, I32view classes

This commit is contained in:
MihailRis 2025-11-02 01:17:56 +03:00
parent 86a4060a68
commit aa54e900d9
4 changed files with 69 additions and 9 deletions

View File

@ -19,10 +19,10 @@ function audio.fetch_input(token, size)
size = size or MAX_FETCH
if audio_input_tokens_store[token] then
if #total_fetch >= size then
return total_fetch:sub(1, size)
return total_fetch:slice(1, size)
end
total_fetch:append(_audio_fetch_input(size - #total_fetch))
return total_fetch:sub()
return total_fetch:slice()
end
error("access denied")
end

View File

@ -121,7 +121,7 @@ local function get_capacity(self)
return self.capacity
end
local function sub(self, offset, length)
local function slice(self, offset, length)
offset = offset or 1
length = length or (self.size - offset + 1)
if offset < 1 or offset > self.size then
@ -131,6 +131,9 @@ local function sub(self, offset, length)
length = self.size - offset + 1
end
local buffer = malloc(length)
if not buffer then
error("malloc(" .. length .. ") returned NULL")
end
FFI.copy(buffer, self.bytes + (offset - 1), length)
return bytearray_type(buffer, length, length)
end
@ -143,7 +146,7 @@ local bytearray_methods = {
clear=clear,
reserve=reserve,
get_capacity=get_capacity,
sub=sub,
slice=slice,
}
local bytearray_mt = {
@ -227,7 +230,59 @@ local function FFIBytearray_as_string(bytes)
end
end
local function create_FFIview_class(name, typename, typesize)
local FFIU16view_mt = {
__index = function(self, key)
if key <= 0 or key > self.size then
return
end
return self.ptr[key - 1]
end,
__newindex = function(self, key, value)
if key == self.size + 1 then
return append(self, value)
elseif key <= 0 or key > self.size then
return
end
self.ptr[key - 1] = value
end,
__len = function(self)
return self.size
end,
__tostring = function(self)
return string.format(name .. "[%s]{...}", tonumber(self.size))
end,
__ipairs = function(self)
local i = 0
return function()
i = i + 1
if i <= self.size then
return i, self.ptr[i - 1]
end
end
end
}
return function (bytes)
local ptr = FFI.cast(typename .. "*", bytes.bytes)
local x = setmetatable({
bytes=bytes,
ptr=ptr,
size=math.floor(bytes.size / typesize),
}, FFIU16view_mt)
return x
end
end
local FFII16view = create_FFIview_class("FFII16view", "int16_t", 2)
local FFIU16view = create_FFIview_class("FFIU16view", "uint16_t", 2)
local FFII32view = create_FFIview_class("FFII32view", "int32_t", 4)
local FFIU32view = create_FFIview_class("FFIU32view", "uint32_t", 4)
return {
FFIBytearray = setmetatable(FFIBytearray, FFIBytearray),
FFIBytearray_as_string = FFIBytearray_as_string
FFIBytearray_as_string = FFIBytearray_as_string,
FFIU16view = FFIU16view,
FFII16view = FFII16view,
FFIU32view = FFIU32view,
FFII32view = FFII32view,
}

View File

@ -124,10 +124,11 @@ function on_hud_open()
streamid = audio.play_stream_2d("test-stream", 2.0, 1.0, "ui")
s = audio.PCMStream(44100, 1, 8)
local buffer = Bytearray(44100)
for i=1, #buffer do
buffer[i] = math.random(1, 8)
s = audio.PCMStream(44100, 1, 16)
local buffer = Bytearray(44100 * 4)
local view = U16view(buffer)
for i=1, view.size do
view[i] = 16000 + math.sin(i / 200.0 * (1 + i * 0.0001)) * 2000
end
s:feed(buffer)
s:create_sound("test-sound")

View File

@ -269,6 +269,10 @@ require "core:internal/extensions/string"
local bytearray = require "core:internal/bytearray"
Bytearray = bytearray.FFIBytearray
Bytearray_as_string = bytearray.FFIBytearray_as_string
U16view = bytearray.FFIU16view
I16view = bytearray.FFII16view
U32view = bytearray.FFIU32view
I32view = bytearray.FFII32view
Bytearray_construct = function(...) return Bytearray(...) end
bit.compile = require "core:bitwise/compiler"