-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVST-utils.js
124 lines (100 loc) · 3.66 KB
/
VST-utils.js
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
var myName;
function loadFile(fileHandler) { // Callback event assigned in main page
console.log("File to open:" ,fileHandler);
myName = fileHandler.name;
const reader = new FileReader();
reader.addEventListener('load', (event) => {
rawFileContents = event.target.result;
console.log("Loaded: ", event, rawFileContents.length);
fileContentsUInt8 = new Uint8Array(rawFileContents); // Extract from the generic ArrayBuffer an array of Unsigned Integers (0..255)
//console.log(fileContentsUInt8);
document.getElementById("status").innerHTML = "File loaded. Click to save:";
return {rawFileContents, fileContentsUInt8};
});
reader.readAsArrayBuffer(fileHandler); // Read as arrayBuffer as "readAsBinaryString" is deprecated but we don't want Javascript to interpret the file at its own wish...
console.log("Reading process initiated...");
}
function saveFile(fileContent, filename) {
myBlob = new Blob([fileContent], {type: "application/octet-stream"});
var url = window.URL.createObjectURL(myBlob);
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
anchor.click();
window.URL.revokeObjectURL(url);
}
function sliceToFloat32(rawFileContents, chunkStart) {
const view = new DataView(rawFileContents); // Create access point to ArrayBuffer
floatNum = view.getFloat32(chunkStart, true); // Extract float32 from access point
// console.log(typeof floatNum, floatNum);
return floatNum;
}
function sliceToString(UInt8Slice, chunkStart, chunkEnd) {
sliceUInt8 = UInt8Slice.slice(chunkStart, chunkEnd); // Slice will contain data from index chunkStart to index chunkEnd-1
//console.log(typeof sliceUInt8, sliceUInt8);
UTF16string = String.fromCharCode(...sliceUInt8); // Interpret byte array as a string of UTF16 characters
//console.log(typeof UTF16string, UTF16string);
UTF8decoder = new TextDecoder();
UTF8string = UTF8decoder.decode(sliceUInt8); // Interpret byte array as a string of UTF8 characters
//console.log(typeof UTF8string, UTF8string);
return {UTF8string, UTF16string};
}
function sliceToHexString(UInt8Slice, chunkStart, chunkEnd) {
finalString = "";
sliceUInt8 = UInt8Slice.slice(chunkStart, chunkEnd);
sliceUInt8.forEach( (element) => {
finalString += stringToHex(element);
});
return finalString;
}
function getValFromString(rawContents, offset, L ) {
// Note: offset in bytes, non in array indexes
//console.log(rawContents);
finalVal = 0;
for (i = offset; i < offset + L; i++) {
byt = rawContents[i];
finalVal = finalVal + byt * (256 ** ((i-offset)));
//console.log(i, byt, i-offset, 256 ** (i-offset), byt * 256 ** (i-offset), finalVal);
}
return finalVal
}
function stringToHex(s) {
temp = "";
intVal = 0;
for (i = 0; i < s.length; i++) {
chVal = s[i];
chValHex = numHex(chVal);
temp = temp + " " + chValHex;
//console.log(i, i.toString(16), chVal, chValHex)
}
return temp
}
function stringToDecInv(s) {
byteVal = 0;
intVal = 0
for (i = s.length-1; i >= 0; i--) {
if (s.length <= 8) {
byteVal = s[i];
intVal = intVal + (byteVal * (256 **(i)));
//console.log(i, byteVal, intVal);
}
}
return intVal
}
function stringToHexInv(s) {
temp = "";
intVal = 0;
for (i = s.length-1; i >= 0; i--) {
chVal = s[i];
chValHex = numHex(chVal)
temp = temp + chValHex;
}
return temp
}
function numHex(s){
var a = s.toString(16);
if ((a.length % 2) > 0) {
a = "0" + a;
}
return a;
}