forked from jummbus/jummbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBufferWriter.ts
More file actions
145 lines (129 loc) · 4.54 KB
/
ArrayBufferWriter.ts
File metadata and controls
145 lines (129 loc) · 4.54 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
function transfer(source: ArrayBuffer, length: number): ArrayBuffer {
const dest: ArrayBuffer = new ArrayBuffer(length);
let nextOffset = 0;
let leftBytes = Math.min(source.byteLength, dest.byteLength);
const wordSizes = [8, 4, 2, 1];
for (const wordSize of wordSizes) {
if (leftBytes >= wordSize) {
const done = transferWith(wordSize, source, dest, nextOffset, leftBytes);
nextOffset = done.nextOffset;
leftBytes = done.leftBytes;
}
}
return dest;
function transferWith(wordSize: number, source: ArrayBuffer, dest: ArrayBuffer, nextOffset: number, leftBytes: number) {
let ViewClass: any = Uint8Array;
switch (wordSize) {
case 8:
ViewClass = Float64Array;
break;
case 4:
ViewClass = Float32Array;
break;
case 2:
ViewClass = Uint16Array;
break;
case 1:
ViewClass = Uint8Array;
break;
default:
ViewClass = Uint8Array;
break;
}
const view_source = new ViewClass(source, nextOffset, (leftBytes / wordSize) | 0);
const view_dest = new ViewClass(dest, nextOffset, (leftBytes / wordSize) | 0);
for (let i: number = 0; i < view_dest.length; i++) {
view_dest[i] = view_source[i];
}
return {
nextOffset: view_source.byteOffset + view_source.byteLength,
leftBytes: leftBytes - view_dest.length * wordSize,
}
}
}
// Note: All methods are big endian.
export class ArrayBufferWriter {
private _writeIndex: number = 0;
private _fileSize: number = 0;
private _arrayBuffer: ArrayBuffer;
private _data: DataView;
constructor(initialCapacity: number) {
this._arrayBuffer = new ArrayBuffer(initialCapacity);
this._data = new DataView(this._arrayBuffer);
}
private _addBytes(numBytes: number): void {
this._fileSize += numBytes;
if (this._fileSize > this._arrayBuffer.byteLength) {
this._arrayBuffer = transfer(this._arrayBuffer, Math.max(this._arrayBuffer.byteLength * 2, this._fileSize));
this._data = new DataView(this._arrayBuffer);
}
}
public getWriteIndex(): number {
return this._writeIndex;
}
public rewriteUint32(index: number, value: number): void {
this._data.setUint32(index, value >>> 0, false);
}
public writeUint32(value: number): void {
value = value >>> 0;
this._addBytes(4);
this._data.setUint32(this._writeIndex, value, false);
this._writeIndex = this._fileSize;
}
public writeUint24(value: number): void {
value = value >>> 0;
this._addBytes(3);
this._data.setUint8(this._writeIndex, (value >> 16) & 0xff);
this._data.setUint8(this._writeIndex + 1, (value >> 8) & 0xff);
this._data.setUint8(this._writeIndex + 2, (value) & 0xff);
this._writeIndex = this._fileSize;
}
public writeUint16(value: number): void {
value = value >>> 0;
this._addBytes(2);
this._data.setUint16(this._writeIndex, value, false);
this._writeIndex = this._fileSize;
}
public writeUint8(value: number): void {
value = value >>> 0;
this._addBytes(1);
this._data.setUint8(this._writeIndex, value);
this._writeIndex = this._fileSize;
}
public writeInt8(value: number): void {
value = value | 0;
this._addBytes(1);
this._data.setInt8(this._writeIndex, value);
this._writeIndex = this._fileSize;
}
public writeMidi7Bits(value: number): void {
value = value >>> 0;
if (value >= 0x80) throw new Error("7 bit value contained 8th bit!");
this._addBytes(1);
this._data.setUint8(this._writeIndex, value);
this._writeIndex = this._fileSize;
}
public writeMidiVariableLength(value: number): void {
value = value >>> 0;
if (value > 0x0fffffff) throw new Error("writeVariableLength value too big.");
let startWriting: boolean = false;
for (let i: number = 0; i < 4; i++) {
const shift: number = 21 - i * 7;
const bits: number = (value >>> shift) & 0x7f;
if (bits != 0 || i == 3) startWriting = true; // skip leading zero bytes, but always write the last byte even if it's zero.
if (startWriting) this.writeUint8((i == 3 ? 0x00 : 0x80) | bits);
}
}
public writeMidiAscii(string: string): void {
this.writeMidiVariableLength(string.length);
for (let i: number = 0; i < string.length; i++) {
const charCode: number = string.charCodeAt(i);
if (charCode > 0x7f) throw new Error("Trying to write unicode character as ascii.");
this.writeUint8(charCode); // technically charCodeAt returns 2 byte values, but this string should contain exclusively 1 byte values.
}
}
public toCompactArrayBuffer(): ArrayBuffer {
return transfer(this._arrayBuffer, this._fileSize);
}
}