export class OpusWrapper { public static encode(blob: Blob | File): Promise { return new Promise((resolve, reject) => { const opusWorker = new Worker('/tp-audio-editor/opus/EmsWorkerProxy.js'); const reader = new FileReader(); reader.onloadend = () => { const args = [ 'in', 'out' ]; const inData = {}; inData['in'] = new Uint8Array(reader.result); const outData = { 'out': { 'MIME': 'audio/ogg' } }; opusWorker.onmessage = e => { if (!e.data) { return; } if (e.data.reply === 'done') { opusWorker.terminate(); resolve(e.data.values['out'].blob); } else if (e.data.reply === 'err') { opusWorker.terminate(); reject('Error occured while encoding to opus'); } }; opusWorker.onerror = e => { opusWorker.terminate(); reject(e); }; opusWorker.postMessage({ command: 'encode', args: args, outData: outData, fileData: inData }); }; reader.readAsArrayBuffer(blob); }); } }