-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
79 lines (62 loc) · 3.35 KB
/
worker.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
let wasmModule;
let loadedModule;
onmessage = function(e) {
if (e.data.msg === 'LOAD_WASM') {
loadWASMModule(e.data.scriptName);
} else if (e.data.msg === 'PROCESS_AUDIO') {
const inputData = new Float32Array(e.data.inputData); // Convert back from ArrayBuffer
const length = e.data.length; // Use the correct length
console.log('Running inference with WASM...');
console.log('length:', length);
// Allocate memory in WASM and copy input data into the WASM memory
const audioPointer = loadedModule._malloc(inputData.length * inputData.BYTES_PER_ELEMENT);
const wasmInputArray = new Float32Array(loadedModule.HEAPF32.buffer, audioPointer, inputData.length);
wasmInputArray.set(inputData);
// Allocate memory for MIDI data pointer and size
const midiDataPointer = loadedModule._malloc(4); // Allocate 4 bytes for the pointer (uint8_t*)
const midiSizePointer = loadedModule._malloc(4); // Allocate 4 bytes for the size (int)
// Call the WASM function with the audio buffer, length, and pointers for the MIDI data and size
loadedModule._convertToMidi(audioPointer, length, midiDataPointer, midiSizePointer);
// Retrieve the MIDI data pointer and size from WASM memory
const midiData = loadedModule.getValue(midiDataPointer, 'i32'); // Get the pointer to MIDI data
const midiSize = loadedModule.getValue(midiSizePointer, 'i32'); // Get the size of the MIDI data
// If valid MIDI data was returned
if (midiData !== 0 && midiSize > 0) {
// Access the MIDI data from WASM memory
const midiBytes = new Uint8Array(loadedModule.HEAPU8.buffer, midiData, midiSize);
// Create a Blob from the MIDI data
const blob = new Blob([midiBytes], { type: 'audio/midi' });
// Optionally, create a URL from the Blob
const blobUrl = URL.createObjectURL(blob);
// Send the Blob (or the Blob URL) back to the main thread
postMessage({
msg: 'PROCESSING_DONE',
blob: blob, // Send the Blob directly
blobUrl: blobUrl // Alternatively, send the Blob URL
});
// Free the memory allocated for the MIDI data in WASM
loadedModule._free(midiData);
} else {
console.error('Failed to generate MIDI data.');
console.log('midiData:', midiData);
console.log('midiSize:', midiSize);
postMessage({ msg: 'PROCESSING_FAILED' });
}
// Free the memory allocated in WASM for the input audio and the MIDI pointer/size
loadedModule._free(audioPointer);
loadedModule._free(midiDataPointer);
loadedModule._free(midiSizePointer);
}
};
function loadWASMModule(scriptName) {
//importScripts(scriptName); // Load the WASM glue code
//<script src="basicpitch.js?v=<?= time() ?>"></script>
importScripts(`${scriptName}?v=${new Date().getTime()}`); // Load the WASM glue code w/ cache busting
// Initialize the WASM module (which should set `Module`)
wasmModule = libbasicpitch(); // Module is created in the glue code
wasmModule.then((loaded_module) => {
console.log('WASM module loaded:', loaded_module);
postMessage({ msg: 'WASM_READY' });
loadedModule = loaded_module;
});
}