tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

silence-detector.js (1389B)


      1 // AudioWorkletProcessor that detects silence and notifies the main thread on state change.
      2 class SilenceDetector extends AudioWorkletProcessor {
      3  constructor() {
      4    super();
      5    // Assume silence and no input until the first buffer is processed.
      6    this.isSilent = true;
      7    this.hasInput = false;
      8  }
      9 
     10  process(inputs, outputs, parameters) {
     11    const input = inputs[0];
     12    const currentHasInput = input && input.length > 0;
     13 
     14    // Detect silence state (no input counts as silence)
     15    let isCurrentlySilent = true;
     16    if (currentHasInput) {
     17      const channel = input[0];
     18      for (let i = 0; i < channel.length; i++) {
     19        if (channel[i] !== 0) {
     20          isCurrentlySilent = false;
     21          break;
     22        }
     23      }
     24    }
     25 
     26    // Check if state changed
     27    const hasInputChanged = this.hasInput !== currentHasInput;
     28    const isSilentChanged = this.isSilent !== isCurrentlySilent;
     29 
     30    // Update state
     31    this.hasInput = currentHasInput;
     32    this.isSilent = isCurrentlySilent;
     33 
     34    // Send notifications
     35    if (hasInputChanged || isSilentChanged) {
     36      this.port.postMessage({
     37        type: "stateChanged",
     38        isSilent: this.isSilent,
     39        hasInput: this.hasInput,
     40        hasInputChanged: hasInputChanged,
     41        isSilentChanged: isSilentChanged,
     42      });
     43    }
     44 
     45    return currentHasInput;
     46  }
     47 }
     48 
     49 registerProcessor("silence-detector", SilenceDetector);