add U16view, I16view, U32view, I32view classes
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user