@iam-protocol/pulse-sdk 0.3.4 → 0.3.5

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
@@ -30,9 +30,10 @@ async function captureAudio(options = {}) {
30
30
  signal,
31
31
  minDurationMs = MIN_CAPTURE_MS,
32
32
  maxDurationMs = MAX_CAPTURE_MS,
33
- onAudioLevel
33
+ onAudioLevel,
34
+ stream: preAcquiredStream
34
35
  } = options;
35
- const stream = await navigator.mediaDevices.getUserMedia({
36
+ const stream = preAcquiredStream ?? await navigator.mediaDevices.getUserMedia({
36
37
  audio: {
37
38
  sampleRate: TARGET_SAMPLE_RATE,
38
39
  channelCount: 1,
@@ -42,6 +43,7 @@ async function captureAudio(options = {}) {
42
43
  }
43
44
  });
44
45
  const ctx = new AudioContext({ sampleRate: TARGET_SAMPLE_RATE });
46
+ await ctx.resume();
45
47
  const capturedSampleRate = ctx.sampleRate;
46
48
  const source = ctx.createMediaStreamSource(stream);
47
49
  const chunks = [];
@@ -118,7 +120,7 @@ async function captureMotion(options = {}) {
118
120
  minDurationMs = MIN_CAPTURE_MS,
119
121
  maxDurationMs = MAX_CAPTURE_MS
120
122
  } = options;
121
- const hasPermission = await requestMotionPermission();
123
+ const hasPermission = options.permissionGranted ?? await requestMotionPermission();
122
124
  if (!hasPermission) return [];
123
125
  const samples = [];
124
126
  const startTime = performance.now();
@@ -1499,12 +1501,25 @@ var PulseSession = class {
1499
1501
  async startAudio(onAudioLevel) {
1500
1502
  if (this.audioStageState !== "idle")
1501
1503
  throw new Error("Audio capture already started");
1504
+ const stream = await navigator.mediaDevices.getUserMedia({
1505
+ audio: {
1506
+ sampleRate: 16e3,
1507
+ channelCount: 1,
1508
+ echoCancellation: false,
1509
+ noiseSuppression: false,
1510
+ autoGainControl: false
1511
+ }
1512
+ });
1502
1513
  this.audioStageState = "capturing";
1503
1514
  this.audioController = new AbortController();
1504
1515
  this.audioPromise = captureAudio({
1505
1516
  signal: this.audioController.signal,
1506
- onAudioLevel
1507
- }).catch(() => null);
1517
+ onAudioLevel,
1518
+ stream
1519
+ }).catch(() => {
1520
+ stream.getTracks().forEach((t) => t.stop());
1521
+ return null;
1522
+ });
1508
1523
  }
1509
1524
  async stopAudio() {
1510
1525
  if (this.audioStageState !== "capturing")
@@ -1520,10 +1535,16 @@ var PulseSession = class {
1520
1535
  async startMotion() {
1521
1536
  if (this.motionStageState !== "idle")
1522
1537
  throw new Error("Motion capture already started");
1538
+ const hasPermission = await requestMotionPermission();
1539
+ if (!hasPermission) {
1540
+ this.motionStageState = "skipped";
1541
+ return;
1542
+ }
1523
1543
  this.motionStageState = "capturing";
1524
1544
  this.motionController = new AbortController();
1525
1545
  this.motionPromise = captureMotion({
1526
- signal: this.motionController.signal
1546
+ signal: this.motionController.signal,
1547
+ permissionGranted: true
1527
1548
  }).catch(() => []);
1528
1549
  }
1529
1550
  async stopMotion() {
@@ -1539,6 +1560,9 @@ var PulseSession = class {
1539
1560
  throw new Error("Motion capture already started");
1540
1561
  this.motionStageState = "skipped";
1541
1562
  }
1563
+ isMotionCapturing() {
1564
+ return this.motionStageState === "capturing";
1565
+ }
1542
1566
  // --- Touch ---
1543
1567
  async startTouch() {
1544
1568
  if (this.touchStageState !== "idle")
@@ -1621,12 +1645,13 @@ var PulseSDK = class {
1621
1645
  }
1622
1646
  try {
1623
1647
  await session.startMotion();
1648
+ } catch {
1649
+ }
1650
+ if (session.isMotionCapturing()) {
1624
1651
  stopPromises.push(
1625
1652
  new Promise((r) => setTimeout(r, DEFAULT_CAPTURE_MS)).then(() => session.stopMotion()).then(() => {
1626
1653
  })
1627
1654
  );
1628
- } catch {
1629
- session.skipMotion();
1630
1655
  }
1631
1656
  if (touchElement) {
1632
1657
  try {