Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hark.bundle.js is broken #47

Open
roschler opened this issue Mar 28, 2024 · 0 comments
Open

hark.bundle.js is broken #47

roschler opened this issue Mar 28, 2024 · 0 comments

Comments

@roschler
Copy link

The bundle wrapper is doing something wrong. hark.bundle.js is broken. Take a look at the source and you immediately see that getMaxVolume() is not being called anywhere else in the source. I went back to the unbundled version and modernized the variable scope prefixes while I was at it, and it works now. Note: My version is formatted to be used as a module and I am importing wildemitted.js from a a sibling events directory, also as a module.

Here is my "unbundled" version. Tested and it works fine:

// Import dependencies (ensure WildEmitter is available as an ES6 module)

import WildEmitter from '../events/wildemitter.mjs';

export function getMaxVolume(analyser, fftBins) {
  let maxVolume = -Infinity;
  analyser.getFloatFrequencyData(fftBins);

  for (let i = 4, ii = fftBins.length; i < ii; i++) {
    if (fftBins[i] > maxVolume && fftBins[i] < 0) {
      maxVolume = fftBins[i];
    }
  }
  return maxVolume;
}

let audioContextType;
if (typeof window !== 'undefined') {
  audioContextType = window.AudioContext || window.webkitAudioContext;
}

let audioContext = null;

export function hark(stream, options) {
  let harker = new WildEmitter(); // Ensure WildEmitter is correctly imported or implemented

  if (!audioContextType) return harker;

  options = options || {};
  let smoothing = options.smoothing || 0.1,
    interval = options.interval || 50,
    threshold = options.threshold,
    play = options.play,
    history = options.history || 10;

  audioContext = options.audioContext || audioContext || new audioContextType();

  let analyser = audioContext.createAnalyser();
  analyser.fftSize = 512;
  analyser.smoothingTimeConstant = smoothing;
  let fftBins = new Float32Array(analyser.frequencyBinCount);

  let sourceNode;
  if (stream.jquery) stream = stream[0];
  if (stream instanceof HTMLAudioElement || stream instanceof HTMLVideoElement) {
    sourceNode = audioContext.createMediaElementSource(stream);
    if (typeof play === 'undefined') play = true;
    threshold = threshold || -50;
  } else {
    sourceNode = audioContext.createMediaStreamSource(stream);
    threshold = threshold || -50;
  }

  sourceNode.connect(analyser);
  if (play) analyser.connect(audioContext.destination);

  // Implement harker logic...

  return harker;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant