@iam-protocol/pulse-sdk 0.2.1 → 0.2.2

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
@@ -12967,7 +12967,8 @@ async function captureAudio(options = {}) {
12967
12967
  const {
12968
12968
  signal,
12969
12969
  minDurationMs = MIN_CAPTURE_MS,
12970
- maxDurationMs = MAX_CAPTURE_MS
12970
+ maxDurationMs = MAX_CAPTURE_MS,
12971
+ onAudioLevel
12971
12972
  } = options;
12972
12973
  const stream = await navigator.mediaDevices.getUserMedia({
12973
12974
  audio: {
@@ -12987,7 +12988,13 @@ async function captureAudio(options = {}) {
12987
12988
  const bufferSize = 4096;
12988
12989
  const processor = ctx.createScriptProcessor(bufferSize, 1, 1);
12989
12990
  processor.onaudioprocess = (e2) => {
12990
- chunks.push(new Float32Array(e2.inputBuffer.getChannelData(0)));
12991
+ const data = e2.inputBuffer.getChannelData(0);
12992
+ chunks.push(new Float32Array(data));
12993
+ if (onAudioLevel) {
12994
+ let sum = 0;
12995
+ for (let i = 0; i < data.length; i++) sum += data[i] * data[i];
12996
+ onAudioLevel(Math.sqrt(sum / data.length));
12997
+ }
12991
12998
  };
12992
12999
  source.connect(processor);
12993
13000
  processor.connect(ctx.destination);
@@ -13857,13 +13864,14 @@ var PulseSession = class {
13857
13864
  this.touchElement = touchElement;
13858
13865
  }
13859
13866
  // --- Audio ---
13860
- async startAudio() {
13867
+ async startAudio(onAudioLevel) {
13861
13868
  if (this.audioStageState !== "idle")
13862
13869
  throw new Error("Audio capture already started");
13863
13870
  this.audioStageState = "capturing";
13864
13871
  this.audioController = new AbortController();
13865
13872
  this.audioPromise = captureAudio({
13866
- signal: this.audioController.signal
13873
+ signal: this.audioController.signal,
13874
+ onAudioLevel
13867
13875
  }).catch(() => null);
13868
13876
  }
13869
13877
  async stopAudio() {