@iam-protocol/pulse-sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -12966,7 +12966,8 @@ async function captureAudio(options = {}) {
12966
12966
  const {
12967
12967
  signal,
12968
12968
  minDurationMs = MIN_CAPTURE_MS,
12969
- maxDurationMs = MAX_CAPTURE_MS
12969
+ maxDurationMs = MAX_CAPTURE_MS,
12970
+ onAudioLevel
12970
12971
  } = options;
12971
12972
  const stream = await navigator.mediaDevices.getUserMedia({
12972
12973
  audio: {
@@ -12986,7 +12987,13 @@ async function captureAudio(options = {}) {
12986
12987
  const bufferSize = 4096;
12987
12988
  const processor = ctx.createScriptProcessor(bufferSize, 1, 1);
12988
12989
  processor.onaudioprocess = (e2) => {
12989
- chunks.push(new Float32Array(e2.inputBuffer.getChannelData(0)));
12990
+ const data = e2.inputBuffer.getChannelData(0);
12991
+ chunks.push(new Float32Array(data));
12992
+ if (onAudioLevel) {
12993
+ let sum = 0;
12994
+ for (let i = 0; i < data.length; i++) sum += data[i] * data[i];
12995
+ onAudioLevel(Math.sqrt(sum / data.length));
12996
+ }
12990
12997
  };
12991
12998
  source.connect(processor);
12992
12999
  processor.connect(ctx.destination);
@@ -13803,13 +13810,14 @@ var PulseSession = class {
13803
13810
  this.touchElement = touchElement;
13804
13811
  }
13805
13812
  // --- Audio ---
13806
- async startAudio() {
13813
+ async startAudio(onAudioLevel) {
13807
13814
  if (this.audioStageState !== "idle")
13808
13815
  throw new Error("Audio capture already started");
13809
13816
  this.audioStageState = "capturing";
13810
13817
  this.audioController = new AbortController();
13811
13818
  this.audioPromise = captureAudio({
13812
- signal: this.audioController.signal
13819
+ signal: this.audioController.signal,
13820
+ onAudioLevel
13813
13821
  }).catch(() => null);
13814
13822
  }
13815
13823
  async stopAudio() {