1

I want to create an audio object in the blob but i couldn't.

const blobCode = () => {
    return `
    var sound      = document.createElement('audio');
    sound.id       = 'audio-player';
    sound.controls = 'controls';
    sound.src      = 'media/Blue Browne.mp3';
    sound.type     = 'audio/mpeg';
    document.body.appendChild(sound);
    class WhiteNoiseProcessor extends AudioWorkletProcessor {
    process (inputs, outputs, parameters) {
      const output = outputs[0]
      output.forEach(channel => {
        for (let i = 0; i < channel.length; i++) {
          channel[i] = Math.random() * 2 - 1
        }
      })
      return true
    }
  }
  
  registerProcessor('white-noise-processor', WhiteNoiseProcessor);`
}


const audioContext = new OfflineAudioContext(1, 128, 300000);


var blob_url = new Blob([blobCode()], {
    type: "text/javascript"
});
var blob_url_create = URL.createObjectURL(blob_url);
await audioContext.audioWorklet.addModule(blob_url_create).then(async () => {
    var wa = new AudioWorkletNode(audioContext, "white-noise-processor");
});

If i set the Blob type text/javascript get the error Uncaught ReferenceError: document is not defined

var blob_url = new Blob([blobCode()], {type: "text/javascript"});

If i set the Blob type text/html get the error Uncaught DOMException: The user aborted a request

var blob_url = new Blob([blobCode()], {type: "text/html"});

How do i resolve this error?

wejopa5747
  • 21
  • 1
  • 2
  • 1
    I'm trying to make it work but I miss the `AudioWorkletProcessor`. Can you update your code above so I can try to find a solution? – Alex Dec 12 '21 at 00:31
  • @Alex try this https://jsfiddle.net/camoti6818/tfqr7azh/1/ if i add worklet document.createElement('audio'); and blob type it application/javascript i get ReferenceError, if I make it blob type application/html i get "DOMException: The user aborted a request" – wejopa5747 Dec 12 '21 at 07:37
  • Unfortunately not much hope here. As MDN say in [their](https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor#examples) doc, if you want to define an `AudioWorkletProcessor `, it _should be done in a separate file_. So it looks like your user case, which I understand is to have the code in the same file, is not supported. They mention that a separate thread is involved, which is probably why. – Alex Dec 13 '21 at 20:08

0 Answers0