How to use ScriptProcessorNode

500 views Asked by At

I am trying to create a Web Audio script processor in Dart. I noticed this question that shows using a "javascriptnode":ScriptProcessorNode

This doesn't seem to be present in the current Dart build. Does anyone have an example of how to use it. I am trying to create a BitCrusher processor.

It seems the latest api doesn't have the "onAudioProcess" method and I am guessing I should be using the "addEventListener" method but I don't know what the message type should be. I even tried the "setEventListener" method but I still don't get events.

_script = _context.createScriptProcessor(2048, 1, 1);

_script.addEventListener("message", _onProcess);
_script.setEventListener(_onProcess);   //<--- this doesn't work either.

void _onProcess(Event e) {            //<---- This is never called
  // Perform bit crush algorithm
}
1

There are 1 answers

1
alextk On

In the mean time (i.e. assuming that soon you could use directly _script.onAudioProcess) you can use the following stub

// stub as onAudioProcess has disappeared from sdk 1.7
Stream<AudioProcessingEvent> onAudioProcess(ScriptProcessorNode node) {
  return node.on['audioprocess'];
}

and use it like this

onAudioProcess(_script).listen((AudioProcessingEvent event) {

  // example to get output data (for playback)
  Float32List data = event.outputBuffer.getChannelData(0);

  // example to get input data (for recording)
  Float32List data = event.inputBuffer.getChannelData(0);

});