@vapi-ai/web 1.0.50 → 1.0.52

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
@@ -4,8 +4,8 @@ export declare class ContinuousPlayer extends EventEmitter {
4
4
  private mediaSource;
5
5
  private sourceBuffer;
6
6
  private audio;
7
- private audioQueue;
8
7
  private nextTimestamp;
8
+ private audioQueue;
9
9
  constructor();
10
10
  start(): void;
11
11
  playChunk(audioData: ArrayBuffer): void;
package/dist/player.js CHANGED
@@ -4,28 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ContinuousPlayer = void 0;
7
+ const async_1 = __importDefault(require("async"));
7
8
  const events_1 = __importDefault(require("events"));
8
9
  class ContinuousPlayer extends events_1.default {
9
10
  constructor() {
10
11
  super();
11
12
  this.sourceBuffer = null;
12
- this.audioQueue = [];
13
13
  this.nextTimestamp = 0;
14
14
  this.mediaSource = new MediaSource();
15
15
  this.audio = document.createElement("audio");
16
16
  this.audio.src = URL.createObjectURL(this.mediaSource);
17
17
  document.body.appendChild(this.audio);
18
+ this.audioQueue = async_1.default.queue((task, callback) => {
19
+ this.appendNextChunk(task, callback);
20
+ }, 1);
18
21
  this.mediaSource.addEventListener("sourceopen", () => {
19
22
  this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/webm; codecs="opus"');
20
23
  this.sourceBuffer.addEventListener("updateend", () => {
21
- if (!this.sourceBuffer)
22
- return;
23
- if (this.sourceBuffer.buffered.length > 0) {
24
- const lastBufferedIndex = this.sourceBuffer.buffered.length - 1;
25
- this.sourceBuffer.timestampOffset =
26
- this.sourceBuffer.buffered.end(lastBufferedIndex);
24
+ if (this.sourceBuffer && this.sourceBuffer.buffered.length > 0) {
25
+ this.sourceBuffer.timestampOffset = this.sourceBuffer.buffered.end(this.sourceBuffer.buffered.length - 1);
27
26
  }
28
- this.appendNextChunk();
27
+ this.audioQueue.resume();
29
28
  });
30
29
  });
31
30
  }
@@ -34,18 +33,14 @@ class ContinuousPlayer extends events_1.default {
34
33
  }
35
34
  playChunk(audioData) {
36
35
  this.audioQueue.push(audioData);
37
- this.appendNextChunk();
38
36
  }
39
- appendNextChunk() {
40
- if (this.sourceBuffer &&
41
- !this.sourceBuffer.updating &&
42
- this.audioQueue.length > 0) {
43
- const chunk = this.audioQueue.shift();
44
- if (chunk) {
45
- console.log("appending", this.sourceBuffer.timestampOffset);
46
- this.sourceBuffer.appendBuffer(chunk);
47
- }
37
+ appendNextChunk(chunk, callback) {
38
+ if (this.sourceBuffer && !this.sourceBuffer.updating) {
39
+ console.log("appending", this.sourceBuffer.timestampOffset);
40
+ this.sourceBuffer.appendBuffer(chunk);
41
+ this.audioQueue.pause();
48
42
  }
43
+ callback(null);
49
44
  }
50
45
  clear() {
51
46
  if (this.sourceBuffer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vapi-ai/web",
3
- "version": "1.0.50",
3
+ "version": "1.0.52",
4
4
  "description": "",
5
5
  "main": "dist/vapi.js",
6
6
  "types": "dist/vapi.d.ts",
package/player.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import async, { QueueObject } from "async";
2
+
1
3
  import EventEmitter from "events";
2
4
 
3
5
  export class ContinuousPlayer extends EventEmitter {
4
6
  private mediaSource: MediaSource;
5
7
  private sourceBuffer: SourceBuffer | null = null;
6
8
  private audio: HTMLAudioElement;
7
- private audioQueue: ArrayBuffer[] = [];
8
9
  private nextTimestamp: number = 0;
10
+ private audioQueue: QueueObject<ArrayBuffer>;
9
11
 
10
12
  constructor() {
11
13
  super();
@@ -15,19 +17,22 @@ export class ContinuousPlayer extends EventEmitter {
15
17
  this.audio.src = URL.createObjectURL(this.mediaSource);
16
18
  document.body.appendChild(this.audio);
17
19
 
20
+ this.audioQueue = async.queue((task, callback) => {
21
+ this.appendNextChunk(task, callback);
22
+ }, 1);
23
+
18
24
  this.mediaSource.addEventListener("sourceopen", () => {
19
25
  this.sourceBuffer = this.mediaSource.addSourceBuffer(
20
26
  'audio/webm; codecs="opus"'
21
27
  );
22
28
 
23
29
  this.sourceBuffer.addEventListener("updateend", () => {
24
- if (!this.sourceBuffer) return;
25
- if (this.sourceBuffer.buffered.length > 0) {
26
- const lastBufferedIndex = this.sourceBuffer.buffered.length - 1;
27
- this.sourceBuffer.timestampOffset =
28
- this.sourceBuffer.buffered.end(lastBufferedIndex);
30
+ if (this.sourceBuffer && this.sourceBuffer.buffered.length > 0) {
31
+ this.sourceBuffer.timestampOffset = this.sourceBuffer.buffered.end(
32
+ this.sourceBuffer.buffered.length - 1
33
+ );
29
34
  }
30
- this.appendNextChunk();
35
+ this.audioQueue.resume();
31
36
  });
32
37
  });
33
38
  }
@@ -38,22 +43,18 @@ export class ContinuousPlayer extends EventEmitter {
38
43
 
39
44
  playChunk(audioData: ArrayBuffer) {
40
45
  this.audioQueue.push(audioData);
41
-
42
- this.appendNextChunk();
43
46
  }
44
47
 
45
- private appendNextChunk(): void {
46
- if (
47
- this.sourceBuffer &&
48
- !this.sourceBuffer.updating &&
49
- this.audioQueue.length > 0
50
- ) {
51
- const chunk = this.audioQueue.shift();
52
- if (chunk) {
53
- console.log("appending", this.sourceBuffer.timestampOffset);
54
- this.sourceBuffer.appendBuffer(chunk);
55
- }
48
+ private appendNextChunk(
49
+ chunk: ArrayBuffer,
50
+ callback: async.ErrorCallback<Error>
51
+ ): void {
52
+ if (this.sourceBuffer && !this.sourceBuffer.updating) {
53
+ console.log("appending", this.sourceBuffer.timestampOffset);
54
+ this.sourceBuffer.appendBuffer(chunk);
55
+ this.audioQueue.pause();
56
56
  }
57
+ callback(null);
57
58
  }
58
59
 
59
60
  clear(): void {