Skip to content

Why use a deprecated API for recording sound (createScriptProcessor)? #125

@tpaulshippy

Description

@tpaulshippy

We are attempting to replicate some of these samples, but are using a subclass of AudioWorkletProcessor for the recording instead of the audioContext.createScriptProcessor method that is used in these samples.

Here's what we have:

export const audioRecorderProcessorCode = `
class AudioRecorderProcessor extends AudioWorkletProcessor {
  constructor() { 
    super(); 
    this.recording = false; 
    
    this.port.onmessage = event => {
      this.recording = event.data.type === 'start-recording';
    }; 
  }
  
  process(inputs) {
    const inputSamples = inputs[0]?.[0]; // First channel of the first input
    
    if (!this.recording || !inputSamples?.length) return true;
    
    const pcm = Int16Array.from(inputSamples, sample => 
      Math.max(-1, Math.min(1, sample)) * 0x7FFF
    );
    
    this.port.postMessage(
      { type: 'audio-data', audioData: pcm.buffer }, 
      [pcm.buffer]
    );
    
    return true;
  }
}

registerProcessor('audio-recorder-processor', AudioRecorderProcessor);
`;


  async initializeAudioWorklet(ctx) {
    const processorBlob = new Blob([audioRecorderProcessorCode], {
      type: "application/javascript",
    });
    await ctx.audioWorklet.addModule(URL.createObjectURL(processorBlob));
  }

Is there some reason these samples use the deprecated API?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions