-
Notifications
You must be signed in to change notification settings - Fork 8
/
MemoryBuffer.pas
84 lines (65 loc) · 1.84 KB
/
MemoryBuffer.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{$mode objfpc}
unit MemoryBuffer;
interface
uses
JS;
type
TMemoryBuffer = class
private
byteBuffer: TJSUint8Array;
public
constructor Create (size: integer);
{ UInt8 }
procedure AddBytes (count: integer; data: array of byte);
{ Float32 }
procedure AddFloats (count: integer; data: array of single);
procedure AddFloat (data: single);
{ UInt16 }
procedure AddWords(count: integer; data: array of word);
property GetBytes: TJSUint8Array read byteBuffer;
private
byteOffset: integer;
floatBuffer: TJSFloat32Array;
wordBuffer: TJSUInt16Array;
end;
implementation
constructor TMemoryBuffer.Create (size: integer);
begin
byteBuffer := TJSUint8Array.New(size);
end;
procedure TMemoryBuffer.AddBytes (count: integer; data: array of byte);
begin
//writeln('AddBytes: @', byteOffset, ' -> ', data);
byteBuffer._set(data, byteOffset);
byteOffset := byteOffset + (count * 1);
end;
procedure TMemoryBuffer.AddFloat (data: single);
begin
AddFloats(1, [data]);
end;
procedure TMemoryBuffer.AddFloats (count: integer; data: array of single);
const
kElementSize = 4;
var
floatOffset: integer;
begin
floatOffset := byteOffset div kElementSize;
//writeln('AddFloats: @', byteOffset, '/', floatOffset, ' -> ', data);
if floatBuffer = nil then
floatBuffer := TJSFloat32Array.New(byteBuffer.buffer, 0, byteBuffer.byteLength div kElementSize);
floatBuffer._set(data, floatOffset);
byteOffset := byteOffset + (count * kElementSize);
end;
procedure TMemoryBuffer.AddWords(count: integer; data: array of word);
const
kElementSize = 2;
var
wordOffset: integer;
begin
wordOffset := byteOffset div kElementSize;
if wordBuffer = nil then
wordBuffer := TJSUInt16Array.New(byteBuffer.buffer, 0, byteBuffer.byteLength div kElementSize);
wordBuffer._set(data, wordOffset);
byteOffset := byteOffset + (count * kElementSize);
end;
end.