@vapi-ai/web 1.0.136 → 1.0.138

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/player.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import EventEmitter from "events";
3
3
  export declare class ContinuousPlayer extends EventEmitter {
4
- private atomic;
5
- private audio;
4
+ private audioCtx;
5
+ private source;
6
6
  constructor();
7
7
  start(): void;
8
- playChunk(audioData: ArrayBuffer): void;
8
+ playChunk(audioData: ArrayBuffer): Promise<void>;
9
9
  clear(): void;
10
10
  }
package/dist/player.js CHANGED
@@ -1,38 +1,37 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
5
14
  Object.defineProperty(exports, "__esModule", { value: true });
6
15
  exports.ContinuousPlayer = void 0;
7
- const AtomicMediaSource_1 = __importDefault(require("./AtomicMediaSource"));
8
16
  const events_1 = __importDefault(require("events"));
9
17
  class ContinuousPlayer extends events_1.default {
10
18
  constructor() {
11
19
  super();
12
- this.audio = document.createElement("audio");
13
- this.audio.addEventListener("playing", () => this.emit("speech-start"));
14
- this.audio.addEventListener("play", () => this.emit("speech-start"));
15
- this.audio.addEventListener("canplay", () => {
16
- this.audio.play();
17
- });
18
- this.audio.addEventListener("waiting", () => this.emit("speech-end"));
19
- this.atomic = new AtomicMediaSource_1.default();
20
- this.audio.src = URL.createObjectURL(this.atomic.mediaSource);
21
- document.body.appendChild(this.audio);
22
- }
23
- start() {
24
- this.audio.play();
20
+ this.source = null;
21
+ this.audioCtx = new AudioContext();
25
22
  }
23
+ start() { }
26
24
  playChunk(audioData) {
27
- var _a;
28
- (_a = this.atomic.sourceBuffer) === null || _a === void 0 ? void 0 : _a.addEventListener("updateend", () => {
29
- this.audio.play().catch((error) => console.error("Play error:", error));
30
- }, { once: true });
31
- this.atomic.appendBuffer(audioData);
32
- }
33
- clear() {
34
- this.audio.pause();
35
- this.atomic.clearBuffer();
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const audioBuffer = yield this.audioCtx.decodeAudioData(audioData);
27
+ this.source = this.audioCtx.createBufferSource();
28
+ this.source.buffer = audioBuffer;
29
+ this.source.connect(this.audioCtx.destination);
30
+ this.source.start();
31
+ this.emit("speech-start");
32
+ this.source.onended = () => this.emit("speech-end");
33
+ });
36
34
  }
35
+ clear() { }
37
36
  }
38
37
  exports.ContinuousPlayer = ContinuousPlayer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vapi-ai/web",
3
- "version": "1.0.136",
3
+ "version": "1.0.138",
4
4
  "description": "",
5
5
  "main": "dist/vapi.js",
6
6
  "types": "dist/vapi.d.ts",
package/player.ts CHANGED
@@ -2,45 +2,26 @@ import AtomicMediaSource from "./AtomicMediaSource";
2
2
  import EventEmitter from "events";
3
3
 
4
4
  export class ContinuousPlayer extends EventEmitter {
5
- private atomic: AtomicMediaSource;
6
- private audio: HTMLAudioElement;
5
+ private audioCtx: AudioContext;
6
+ private source: AudioBufferSourceNode | null = null;
7
7
 
8
8
  constructor() {
9
9
  super();
10
10
 
11
- this.audio = document.createElement("audio");
12
-
13
- this.audio.addEventListener("playing", () => this.emit("speech-start"));
14
- this.audio.addEventListener("play", () => this.emit("speech-start"));
15
- this.audio.addEventListener("canplay", () => {
16
- this.audio.play();
17
- });
18
-
19
- this.audio.addEventListener("waiting", () => this.emit("speech-end"));
20
-
21
- this.atomic = new AtomicMediaSource();
22
-
23
- this.audio.src = URL.createObjectURL(this.atomic.mediaSource);
24
- document.body.appendChild(this.audio);
11
+ this.audioCtx = new AudioContext();
25
12
  }
26
13
 
27
- start() {
28
- this.audio.play();
29
- }
14
+ start() {}
30
15
 
31
- playChunk(audioData: ArrayBuffer) {
32
- this.atomic.sourceBuffer?.addEventListener(
33
- "updateend",
34
- () => {
35
- this.audio.play().catch((error) => console.error("Play error:", error));
36
- },
37
- { once: true }
38
- );
39
- this.atomic.appendBuffer(audioData);
16
+ async playChunk(audioData: ArrayBuffer) {
17
+ const audioBuffer = await this.audioCtx.decodeAudioData(audioData);
18
+ this.source = this.audioCtx.createBufferSource();
19
+ this.source.buffer = audioBuffer;
20
+ this.source.connect(this.audioCtx.destination);
21
+ this.source.start();
22
+ this.emit("speech-start");
23
+ this.source.onended = () => this.emit("speech-end");
40
24
  }
41
25
 
42
- clear() {
43
- this.audio.pause();
44
- this.atomic.clearBuffer();
45
- }
26
+ clear() {}
46
27
  }