@spatialwalk/avatarkit 1.0.0-beta.2 → 1.0.0-beta.21

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +340 -0
  2. package/README.md +501 -194
  3. package/dist/StreamingAudioPlayer-DEXcuhRW.js +334 -0
  4. package/dist/StreamingAudioPlayer-DEXcuhRW.js.map +1 -0
  5. package/dist/animation/AnimationWebSocketClient.d.ts +7 -4
  6. package/dist/animation/AnimationWebSocketClient.d.ts.map +1 -1
  7. package/dist/audio/AnimationPlayer.d.ts +12 -0
  8. package/dist/audio/AnimationPlayer.d.ts.map +1 -1
  9. package/dist/audio/StreamingAudioPlayer.d.ts +11 -0
  10. package/dist/audio/StreamingAudioPlayer.d.ts.map +1 -1
  11. package/dist/avatar_core_wasm-BPIbbUx_.js +1664 -0
  12. package/dist/avatar_core_wasm-BPIbbUx_.js.map +1 -0
  13. package/dist/avatar_core_wasm.wasm +0 -0
  14. package/dist/config/app-config.d.ts +3 -7
  15. package/dist/config/app-config.d.ts.map +1 -1
  16. package/dist/config/constants.d.ts +19 -3
  17. package/dist/config/constants.d.ts.map +1 -1
  18. package/dist/config/sdk-config-loader.d.ts.map +1 -1
  19. package/dist/core/Avatar.d.ts +0 -8
  20. package/dist/core/Avatar.d.ts.map +1 -1
  21. package/dist/core/AvatarController.d.ts +112 -65
  22. package/dist/core/AvatarController.d.ts.map +1 -1
  23. package/dist/core/AvatarDownloader.d.ts +1 -20
  24. package/dist/core/AvatarDownloader.d.ts.map +1 -1
  25. package/dist/core/AvatarKit.d.ts +8 -15
  26. package/dist/core/AvatarKit.d.ts.map +1 -1
  27. package/dist/core/AvatarManager.d.ts +1 -4
  28. package/dist/core/AvatarManager.d.ts.map +1 -1
  29. package/dist/core/AvatarView.d.ts +65 -53
  30. package/dist/core/AvatarView.d.ts.map +1 -1
  31. package/dist/core/NetworkLayer.d.ts +8 -0
  32. package/dist/core/NetworkLayer.d.ts.map +1 -0
  33. package/dist/index-ChKhyUK4.js +6437 -0
  34. package/dist/index-ChKhyUK4.js.map +1 -0
  35. package/dist/index.d.ts +0 -1
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +14 -15
  38. package/dist/renderer/RenderSystem.d.ts +9 -76
  39. package/dist/renderer/RenderSystem.d.ts.map +1 -1
  40. package/dist/renderer/webgl/reorderData.d.ts.map +1 -1
  41. package/dist/renderer/webgl/webglRenderer.d.ts.map +1 -1
  42. package/dist/types/character.d.ts +0 -11
  43. package/dist/types/character.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +18 -6
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/dist/utils/cls-tracker.d.ts +17 -0
  47. package/dist/utils/cls-tracker.d.ts.map +1 -0
  48. package/dist/utils/{reqId.d.ts → conversationId.d.ts} +6 -6
  49. package/dist/utils/conversationId.d.ts.map +1 -0
  50. package/dist/utils/logger.d.ts +2 -10
  51. package/dist/utils/logger.d.ts.map +1 -1
  52. package/dist/vanilla/vite.config.d.ts +3 -0
  53. package/dist/vanilla/vite.config.d.ts.map +1 -0
  54. package/dist/wasm/avatarCoreAdapter.d.ts +58 -9
  55. package/dist/wasm/avatarCoreAdapter.d.ts.map +1 -1
  56. package/dist/wasm/avatarCoreMemory.d.ts +5 -1
  57. package/dist/wasm/avatarCoreMemory.d.ts.map +1 -1
  58. package/package.json +10 -4
  59. package/dist/StreamingAudioPlayer-CMEiGwxE.js +0 -288
  60. package/dist/StreamingAudioPlayer-CMEiGwxE.js.map +0 -1
  61. package/dist/avatar_core_wasm-DmkU6dYn.js +0 -1666
  62. package/dist/avatar_core_wasm-DmkU6dYn.js.map +0 -1
  63. package/dist/index-CNhquYUE.js +0 -9712
  64. package/dist/index-CNhquYUE.js.map +0 -1
  65. package/dist/utils/posthog-tracker.d.ts +0 -82
  66. package/dist/utils/posthog-tracker.d.ts.map +0 -1
  67. package/dist/utils/reqId.d.ts.map +0 -1
@@ -1,288 +0,0 @@
1
- var c = Object.defineProperty;
2
- var g = (h, t, e) => t in h ? c(h, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[t] = e;
3
- var s = (h, t, e) => g(h, typeof t != "symbol" ? t + "" : t, e);
4
- import { l } from "./index-CNhquYUE.js";
5
- class k {
6
- constructor(t) {
7
- // AudioContext is managed internally
8
- s(this, "audioContext", null);
9
- s(this, "sampleRate");
10
- s(this, "channelCount");
11
- s(this, "debug");
12
- // Session-level state
13
- s(this, "sessionId");
14
- s(this, "sessionStartTime", 0);
15
- // AudioContext time when session started
16
- s(this, "pausedTimeOffset", 0);
17
- // Accumulated paused time
18
- s(this, "pausedAt", 0);
19
- // Time when paused
20
- s(this, "scheduledTime", 0);
21
- // Next chunk schedule time in AudioContext time
22
- // Playback state
23
- s(this, "isPlaying", !1);
24
- s(this, "isPaused", !1);
25
- // Audio buffer queue
26
- s(this, "audioChunks", []);
27
- s(this, "scheduledChunks", 0);
28
- // Number of chunks already scheduled
29
- s(this, "activeSources", []);
30
- // Event callbacks
31
- s(this, "onEndedCallback");
32
- this.sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, this.sampleRate = (t == null ? void 0 : t.sampleRate) ?? 24e3, this.channelCount = (t == null ? void 0 : t.channelCount) ?? 1, this.debug = (t == null ? void 0 : t.debug) ?? !1;
33
- }
34
- /**
35
- * Initialize audio context (create and ensure it's ready)
36
- */
37
- async initialize() {
38
- this.audioContext || (this.audioContext = new AudioContext({
39
- sampleRate: this.sampleRate
40
- }), this.audioContext.state === "suspended" && await this.audioContext.resume(), this.log("AudioContext initialized", {
41
- sessionId: this.sessionId,
42
- sampleRate: this.audioContext.sampleRate,
43
- state: this.audioContext.state
44
- }));
45
- }
46
- /**
47
- * Add audio chunk (16-bit PCM)
48
- */
49
- addChunk(t, e = !1) {
50
- if (!this.audioContext) {
51
- l.error("AudioContext not initialized");
52
- return;
53
- }
54
- this.audioChunks.push({ data: t, isLast: e }), this.log(`Added chunk ${this.audioChunks.length}`, {
55
- size: t.length,
56
- totalChunks: this.audioChunks.length,
57
- isLast: e,
58
- isPlaying: this.isPlaying,
59
- scheduledChunks: this.scheduledChunks
60
- }), !this.isPlaying && this.audioChunks.length > 0 ? (this.log("[StreamingAudioPlayer] Auto-starting playback from addChunk"), this.startPlayback()) : this.isPlaying ? (this.log("[StreamingAudioPlayer] Already playing, scheduling next chunk"), this.scheduleNextChunk()) : this.log("[StreamingAudioPlayer] Not playing and no chunks, waiting for more chunks");
61
- }
62
- /**
63
- * Start new session (stop current and start fresh)
64
- */
65
- async startNewSession(t) {
66
- this.stop(), this.sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, this.audioChunks = [], this.scheduledChunks = 0, this.pausedTimeOffset = 0, this.pausedAt = 0, this.log("Starting new session", {
67
- chunks: t.length
68
- });
69
- for (const e of t)
70
- this.addChunk(e.data, e.isLast);
71
- }
72
- /**
73
- * Start playback
74
- */
75
- startPlayback() {
76
- if (!this.audioContext) {
77
- this.log("[StreamingAudioPlayer] Cannot start playback: AudioContext not initialized");
78
- return;
79
- }
80
- if (this.isPlaying) {
81
- this.log("[StreamingAudioPlayer] Cannot start playback: Already playing");
82
- return;
83
- }
84
- this.isPlaying = !0, this.sessionStartTime = this.audioContext.currentTime, this.scheduledTime = this.sessionStartTime, this.log("[StreamingAudioPlayer] Starting playback", {
85
- sessionStartTime: this.sessionStartTime,
86
- bufferedChunks: this.audioChunks.length,
87
- scheduledChunks: this.scheduledChunks,
88
- activeSources: this.activeSources.length
89
- }), this.scheduleAllChunks();
90
- }
91
- /**
92
- * Schedule all pending chunks
93
- */
94
- scheduleAllChunks() {
95
- for (; this.scheduledChunks < this.audioChunks.length; )
96
- this.scheduleNextChunk();
97
- }
98
- /**
99
- * Schedule next audio chunk
100
- */
101
- scheduleNextChunk() {
102
- if (!this.audioContext) {
103
- this.log("[StreamingAudioPlayer] Cannot schedule chunk: AudioContext not initialized");
104
- return;
105
- }
106
- if (!this.isPlaying) {
107
- this.log("[StreamingAudioPlayer] Cannot schedule chunk: Not playing");
108
- return;
109
- }
110
- const t = this.scheduledChunks;
111
- if (t >= this.audioChunks.length) {
112
- this.log(`[StreamingAudioPlayer] No more chunks to schedule (chunkIndex: ${t}, totalChunks: ${this.audioChunks.length})`);
113
- return;
114
- }
115
- const e = this.audioChunks[t];
116
- if (e.data.length === 0 && !e.isLast) {
117
- this.scheduledChunks++;
118
- return;
119
- }
120
- const r = e.data, d = e.isLast, n = this.pcmToAudioBuffer(r);
121
- if (!n) {
122
- l.error("Failed to create AudioBuffer from PCM data");
123
- return;
124
- }
125
- try {
126
- const i = this.audioContext.createBufferSource();
127
- i.buffer = n, i.connect(this.audioContext.destination), i.start(this.scheduledTime), this.activeSources.push(i), i.onended = () => {
128
- const u = this.activeSources.indexOf(i);
129
- u >= 0 && this.activeSources.splice(u, 1), d && this.activeSources.length === 0 && (this.log("Last audio chunk ended, marking playback as ended"), this.markEnded());
130
- }, this.scheduledTime += n.duration, this.scheduledChunks++, this.log(`[StreamingAudioPlayer] Scheduled chunk ${t + 1}/${this.audioChunks.length}`, {
131
- startTime: this.scheduledTime - n.duration,
132
- duration: n.duration,
133
- nextScheduleTime: this.scheduledTime,
134
- isLast: d,
135
- activeSources: this.activeSources.length
136
- });
137
- } catch (i) {
138
- l.errorWithError("Failed to schedule audio chunk:", i);
139
- }
140
- }
141
- /**
142
- * Convert PCM data to AudioBuffer
143
- * Input: 16-bit PCM (int16), Output: AudioBuffer (float32 [-1, 1])
144
- */
145
- pcmToAudioBuffer(t) {
146
- if (!this.audioContext)
147
- return null;
148
- if (t.length === 0) {
149
- const u = Math.floor(this.sampleRate * 0.01), a = this.audioContext.createBuffer(
150
- this.channelCount,
151
- u,
152
- this.sampleRate
153
- );
154
- for (let o = 0; o < this.channelCount; o++)
155
- a.getChannelData(o).fill(0);
156
- return a;
157
- }
158
- const e = new Uint8Array(t), r = new Int16Array(e.buffer, 0, e.length / 2), d = r.length / this.channelCount, n = this.audioContext.createBuffer(
159
- this.channelCount,
160
- d,
161
- this.sampleRate
162
- );
163
- for (let i = 0; i < this.channelCount; i++) {
164
- const u = n.getChannelData(i);
165
- for (let a = 0; a < d; a++) {
166
- const o = a * this.channelCount + i;
167
- u[a] = r[o] / 32768;
168
- }
169
- }
170
- return n;
171
- }
172
- /**
173
- * Get current playback time (seconds)
174
- */
175
- getCurrentTime() {
176
- if (!this.audioContext || !this.isPlaying)
177
- return 0;
178
- if (this.isPaused)
179
- return this.pausedAt;
180
- const e = this.audioContext.currentTime - this.sessionStartTime - this.pausedTimeOffset;
181
- return Math.max(0, e);
182
- }
183
- /**
184
- * Pause playback
185
- */
186
- pause() {
187
- !this.isPlaying || this.isPaused || (this.isPaused = !0, this.pausedAt = this.getCurrentTime(), this.log("Playback paused", {
188
- pausedAt: this.pausedAt
189
- }));
190
- }
191
- /**
192
- * Resume playback
193
- */
194
- async resume() {
195
- if (!this.isPaused || !this.audioContext)
196
- return;
197
- const t = this.audioContext.currentTime - (this.sessionStartTime + this.pausedAt);
198
- this.pausedTimeOffset += t, this.isPaused = !1, this.log("Playback resumed", {
199
- pauseDuration: t,
200
- totalPausedOffset: this.pausedTimeOffset
201
- });
202
- }
203
- /**
204
- * Stop playback
205
- */
206
- stop() {
207
- if (this.audioContext) {
208
- this.isPlaying = !1, this.isPaused = !1, this.sessionStartTime = 0, this.scheduledTime = 0;
209
- for (const t of this.activeSources) {
210
- t.onended = null;
211
- try {
212
- t.stop(0);
213
- } catch {
214
- }
215
- try {
216
- t.disconnect();
217
- } catch {
218
- }
219
- }
220
- this.activeSources = [], this.audioChunks = [], this.scheduledChunks = 0, this.log("[StreamingAudioPlayer] Playback stopped, state reset");
221
- }
222
- }
223
- /**
224
- * Mark playback as ended
225
- */
226
- markEnded() {
227
- var t;
228
- this.log("Playback ended"), this.isPlaying = !1, (t = this.onEndedCallback) == null || t.call(this);
229
- }
230
- /**
231
- * Set ended callback
232
- */
233
- onEnded(t) {
234
- this.onEndedCallback = t;
235
- }
236
- /**
237
- * Check if playing
238
- */
239
- isPlayingNow() {
240
- return this.isPlaying && !this.isPaused;
241
- }
242
- /**
243
- * Get total duration of buffered audio
244
- */
245
- getBufferedDuration() {
246
- if (!this.audioContext)
247
- return 0;
248
- let t = 0;
249
- for (const e of this.audioChunks)
250
- t += e.data.length / 2 / this.channelCount;
251
- return t / this.sampleRate;
252
- }
253
- /**
254
- * Get remaining duration (buffered - played) in seconds
255
- */
256
- getRemainingDuration() {
257
- const t = this.getBufferedDuration(), e = this.getCurrentTime();
258
- return Math.max(0, t - e);
259
- }
260
- /**
261
- * Dispose and cleanup
262
- */
263
- dispose() {
264
- this.stop(), this.audioContext && (this.audioContext.close(), this.audioContext = null), this.audioChunks = [], this.scheduledChunks = 0, this.sessionStartTime = 0, this.pausedTimeOffset = 0, this.pausedAt = 0, this.scheduledTime = 0, this.onEndedCallback = void 0, this.log("StreamingAudioPlayer disposed");
265
- }
266
- /**
267
- * Flush buffered audio
268
- * - hard: stops all playing sources and clears all chunks
269
- * - soft (default): clears UNSCHEDULED chunks only
270
- */
271
- flush(t) {
272
- if ((t == null ? void 0 : t.hard) === !0) {
273
- this.stop(), this.audioChunks = [], this.scheduledChunks = 0, this.sessionStartTime = 0, this.pausedAt = 0, this.scheduledTime = 0, this.log("Flushed (hard)");
274
- return;
275
- }
276
- this.scheduledChunks < this.audioChunks.length && this.audioChunks.splice(this.scheduledChunks), this.log("Flushed (soft)", { remainingScheduled: this.scheduledChunks });
277
- }
278
- /**
279
- * Debug logging
280
- */
281
- log(t, e) {
282
- this.debug && l.log(`[StreamingAudioPlayer] ${t}`, e || "");
283
- }
284
- }
285
- export {
286
- k as StreamingAudioPlayer
287
- };
288
- //# sourceMappingURL=StreamingAudioPlayer-CMEiGwxE.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StreamingAudioPlayer-CMEiGwxE.js","sources":["../audio/StreamingAudioPlayer.ts"],"sourcesContent":["/**\n * Streaming Audio Player\n * Implements real-time audio playback using Web Audio API\n * Supports dynamic PCM chunk addition without Workers\n */\n\nimport { logger } from '../utils/logger'\n\nexport interface StreamingAudioPlayerOptions {\n sampleRate?: number // PCM sample rate (default: 24000, backend returns 24kHz)\n channelCount?: number // Number of channels (default: 1)\n debug?: boolean\n}\n\nexport class StreamingAudioPlayer {\n // AudioContext is managed internally\n private audioContext: AudioContext | null = null\n private sampleRate: number\n private channelCount: number\n private debug: boolean\n\n // Session-level state\n private sessionId: string\n private sessionStartTime = 0 // AudioContext time when session started\n private pausedTimeOffset = 0 // Accumulated paused time\n private pausedAt = 0 // Time when paused\n private scheduledTime = 0 // Next chunk schedule time in AudioContext time\n\n // Playback state\n private isPlaying = false\n private isPaused = false\n\n // Audio buffer queue\n private audioChunks: Array<{ data: Uint8Array, isLast: boolean }> = []\n private scheduledChunks = 0 // Number of chunks already scheduled\n private activeSources: AudioBufferSourceNode[] = []\n\n // Event callbacks\n private onEndedCallback?: () => void\n\n constructor(options?: StreamingAudioPlayerOptions) {\n this.sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`\n this.sampleRate = options?.sampleRate ?? 24000 // Backend returns 24kHz by default\n this.channelCount = options?.channelCount ?? 1\n this.debug = options?.debug ?? false\n }\n\n /**\n * Initialize audio context (create and ensure it's ready)\n */\n async initialize(): Promise<void> {\n if (this.audioContext) {\n return\n }\n\n // Create AudioContext\n this.audioContext = new AudioContext({\n sampleRate: this.sampleRate,\n })\n\n // Resume context (required for some browsers)\n if (this.audioContext.state === 'suspended') {\n await this.audioContext.resume()\n }\n\n this.log('AudioContext initialized', {\n sessionId: this.sessionId,\n sampleRate: this.audioContext.sampleRate,\n state: this.audioContext.state,\n })\n }\n\n /**\n * Add audio chunk (16-bit PCM)\n */\n addChunk(pcmData: Uint8Array, isLast: boolean = false): void {\n if (!this.audioContext) {\n logger.error('AudioContext not initialized')\n return\n }\n\n // Store chunk with metadata\n this.audioChunks.push({ data: pcmData, isLast })\n\n // Track buffer underrun warning\n if (this.isPlaying && this.audioChunks.length === this.scheduledChunks) {\n // Buffer underrun detected - chunks consumed faster than added\n }\n\n this.log(`Added chunk ${this.audioChunks.length}`, {\n size: pcmData.length,\n totalChunks: this.audioChunks.length,\n isLast,\n isPlaying: this.isPlaying,\n scheduledChunks: this.scheduledChunks,\n })\n\n // Auto-start if we have any audio chunks\n if (!this.isPlaying && this.audioChunks.length > 0) {\n this.log('[StreamingAudioPlayer] Auto-starting playback from addChunk')\n this.startPlayback()\n }\n // Schedule next chunk if already playing\n else if (this.isPlaying) {\n this.log('[StreamingAudioPlayer] Already playing, scheduling next chunk')\n this.scheduleNextChunk()\n } else {\n this.log('[StreamingAudioPlayer] Not playing and no chunks, waiting for more chunks')\n }\n }\n\n /**\n * Start new session (stop current and start fresh)\n */\n async startNewSession(audioChunks: Array<{ data: Uint8Array, isLast: boolean }>): Promise<void> {\n // Stop current session if playing\n this.stop()\n\n // Generate new session ID to prevent data mixing\n this.sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`\n\n // Reset session state\n this.audioChunks = []\n this.scheduledChunks = 0\n this.pausedTimeOffset = 0\n this.pausedAt = 0\n // Don't set sessionStartTime or scheduledTime here - let startPlayback() set them\n this.log('Starting new session', {\n chunks: audioChunks.length,\n })\n\n // Add audio chunks with their metadata\n for (const chunk of audioChunks) {\n this.addChunk(chunk.data, chunk.isLast)\n }\n }\n\n /**\n * Start playback\n */\n private startPlayback(): void {\n if (!this.audioContext) {\n this.log('[StreamingAudioPlayer] Cannot start playback: AudioContext not initialized')\n return\n }\n if (this.isPlaying) {\n this.log('[StreamingAudioPlayer] Cannot start playback: Already playing')\n return\n }\n\n this.isPlaying = true\n this.sessionStartTime = this.audioContext.currentTime\n this.scheduledTime = this.sessionStartTime\n\n this.log('[StreamingAudioPlayer] Starting playback', {\n sessionStartTime: this.sessionStartTime,\n bufferedChunks: this.audioChunks.length,\n scheduledChunks: this.scheduledChunks,\n activeSources: this.activeSources.length,\n })\n\n // Schedule all available chunks\n this.scheduleAllChunks()\n }\n\n /**\n * Schedule all pending chunks\n */\n private scheduleAllChunks(): void {\n while (this.scheduledChunks < this.audioChunks.length) {\n this.scheduleNextChunk()\n }\n }\n\n /**\n * Schedule next audio chunk\n */\n private scheduleNextChunk(): void {\n if (!this.audioContext) {\n this.log('[StreamingAudioPlayer] Cannot schedule chunk: AudioContext not initialized')\n return\n }\n if (!this.isPlaying) {\n this.log('[StreamingAudioPlayer] Cannot schedule chunk: Not playing')\n return\n }\n\n const chunkIndex = this.scheduledChunks\n if (chunkIndex >= this.audioChunks.length) {\n this.log(`[StreamingAudioPlayer] No more chunks to schedule (chunkIndex: ${chunkIndex}, totalChunks: ${this.audioChunks.length})`)\n return\n }\n\n const chunk = this.audioChunks[chunkIndex]\n\n // 当音频块为空且不是最后一个块时,跳过调度\n if (chunk.data.length === 0 && !chunk.isLast) {\n this.scheduledChunks++\n return\n }\n\n const pcmData = chunk.data\n const isLast = chunk.isLast\n const audioBuffer = this.pcmToAudioBuffer(pcmData)\n\n if (!audioBuffer) {\n const errorMessage = 'Failed to create AudioBuffer from PCM data'\n logger.error(errorMessage)\n return\n }\n\n try {\n // Create and configure source node\n const source = this.audioContext.createBufferSource()\n source.buffer = audioBuffer\n source.connect(this.audioContext.destination)\n\n // Schedule playback\n source.start(this.scheduledTime)\n\n // Track active source for hard-cancel\n this.activeSources.push(source)\n source.onended = () => {\n // Remove from active list when it ends\n const idx = this.activeSources.indexOf(source)\n if (idx >= 0)\n this.activeSources.splice(idx, 1)\n\n // Check if this was the last chunk and all sources have ended\n if (isLast && this.activeSources.length === 0) {\n this.log('Last audio chunk ended, marking playback as ended')\n this.markEnded()\n }\n }\n\n // Update scheduled time for next chunk\n this.scheduledTime += audioBuffer.duration\n\n this.scheduledChunks++\n\n this.log(`[StreamingAudioPlayer] Scheduled chunk ${chunkIndex + 1}/${this.audioChunks.length}`, {\n startTime: this.scheduledTime - audioBuffer.duration,\n duration: audioBuffer.duration,\n nextScheduleTime: this.scheduledTime,\n isLast,\n activeSources: this.activeSources.length,\n })\n }\n catch (err) {\n logger.errorWithError('Failed to schedule audio chunk:', err)\n }\n }\n\n /**\n * Convert PCM data to AudioBuffer\n * Input: 16-bit PCM (int16), Output: AudioBuffer (float32 [-1, 1])\n */\n private pcmToAudioBuffer(pcmData: Uint8Array): AudioBuffer | null {\n if (!this.audioContext) {\n return null\n }\n\n // Handle empty PCM data (e.g., when isLast is true)\n if (pcmData.length === 0) {\n // For empty chunks (typically the last chunk), create minimal silence\n // Use a very short duration to avoid playback stuttering\n const silenceDuration = 0.01 // 1ms - minimal silence to maintain timing\n const numSamples = Math.floor(this.sampleRate * silenceDuration)\n\n const audioBuffer = this.audioContext.createBuffer(\n this.channelCount,\n numSamples,\n this.sampleRate,\n )\n\n // Fill with silence (all zeros)\n for (let channel = 0; channel < this.channelCount; channel++) {\n const channelData = audioBuffer.getChannelData(channel)\n channelData.fill(0) // Fill with silence\n }\n\n return audioBuffer\n }\n\n // Create aligned copy to avoid byte offset issues\n // Int16Array requires byteOffset to be a multiple of 2\n const alignedData = new Uint8Array(pcmData)\n const int16Array = new Int16Array(alignedData.buffer, 0, alignedData.length / 2)\n\n // Calculate number of samples\n const numSamples = int16Array.length / this.channelCount\n\n // Create AudioBuffer\n const audioBuffer = this.audioContext.createBuffer(\n this.channelCount,\n numSamples,\n this.sampleRate,\n )\n\n // Convert int16 to float32 [-1, 1]\n for (let channel = 0; channel < this.channelCount; channel++) {\n const channelData = audioBuffer.getChannelData(channel)\n\n for (let i = 0; i < numSamples; i++) {\n const sampleIndex = i * this.channelCount + channel\n // Normalize int16 (-32768 to 32767) to float32 (-1 to 1)\n channelData[i] = int16Array[sampleIndex] / 32768.0\n }\n }\n\n return audioBuffer\n }\n\n /**\n * Get current playback time (seconds)\n */\n getCurrentTime(): number {\n if (!this.audioContext || !this.isPlaying) {\n return 0\n }\n\n if (this.isPaused) {\n return this.pausedAt\n }\n\n // Calculate elapsed time using session start time and paused offset\n const currentAudioTime = this.audioContext.currentTime\n const elapsed = currentAudioTime - this.sessionStartTime - this.pausedTimeOffset\n\n return Math.max(0, elapsed)\n }\n\n /**\n * Pause playback\n */\n pause(): void {\n if (!this.isPlaying || this.isPaused) {\n return\n }\n\n this.isPaused = true\n this.pausedAt = this.getCurrentTime()\n\n // Don't suspend AudioContext to maintain time continuity\n // Just mark as paused and stop scheduling new chunks\n this.log('Playback paused', {\n pausedAt: this.pausedAt,\n })\n }\n\n /**\n * Resume playback\n */\n async resume(): Promise<void> {\n if (!this.isPaused || !this.audioContext) {\n return\n }\n\n // Calculate paused duration and add to offset\n const pauseDuration = this.audioContext.currentTime - (this.sessionStartTime + this.pausedAt)\n this.pausedTimeOffset += pauseDuration\n\n this.isPaused = false\n\n this.log('Playback resumed', {\n pauseDuration,\n totalPausedOffset: this.pausedTimeOffset,\n })\n }\n\n /**\n * Stop playback\n */\n stop(): void {\n if (!this.audioContext) {\n return\n }\n\n this.isPlaying = false\n this.isPaused = false\n this.sessionStartTime = 0 // Reset session start time\n this.scheduledTime = 0 // Reset scheduled time for next session\n\n // Hard stop all scheduled sources immediately\n for (const source of this.activeSources) {\n source.onended = null\n try {\n source.stop(0)\n }\n catch {}\n try {\n source.disconnect()\n }\n catch {}\n }\n this.activeSources = []\n\n // 清理音频块和调度状态,确保下次播放时状态干净\n this.audioChunks = []\n this.scheduledChunks = 0\n\n this.log('[StreamingAudioPlayer] Playback stopped, state reset')\n\n // Note: Individual source nodes will stop automatically\n // We just reset our state\n }\n\n /**\n * Mark playback as ended\n */\n markEnded(): void {\n this.log('Playback ended')\n this.isPlaying = false\n this.onEndedCallback?.()\n }\n\n /**\n * Set ended callback\n */\n onEnded(callback: () => void): void {\n this.onEndedCallback = callback\n }\n\n /**\n * Check if playing\n */\n isPlayingNow(): boolean {\n return this.isPlaying && !this.isPaused\n }\n\n /**\n * Get total duration of buffered audio\n */\n getBufferedDuration(): number {\n if (!this.audioContext) {\n return 0\n }\n\n let totalSamples = 0\n for (const chunk of this.audioChunks) {\n totalSamples += chunk.data.length / 2 / this.channelCount // 16-bit = 2 bytes per sample\n }\n\n return totalSamples / this.sampleRate\n }\n\n /**\n * Get remaining duration (buffered - played) in seconds\n */\n getRemainingDuration(): number {\n const total = this.getBufferedDuration()\n const played = this.getCurrentTime()\n return Math.max(0, total - played)\n }\n\n\n /**\n * Dispose and cleanup\n */\n dispose(): void {\n this.stop()\n\n // Close AudioContext\n if (this.audioContext) {\n this.audioContext.close()\n this.audioContext = null\n }\n\n // Clear session state\n this.audioChunks = []\n this.scheduledChunks = 0\n this.sessionStartTime = 0\n this.pausedTimeOffset = 0\n this.pausedAt = 0\n this.scheduledTime = 0\n this.onEndedCallback = undefined\n\n this.log('StreamingAudioPlayer disposed')\n }\n\n /**\n * Flush buffered audio\n * - hard: stops all playing sources and clears all chunks\n * - soft (default): clears UNSCHEDULED chunks only\n */\n flush(options?: { hard?: boolean }): void {\n const hard = options?.hard === true\n if (hard) {\n this.stop()\n this.audioChunks = []\n this.scheduledChunks = 0\n this.sessionStartTime = 0\n this.pausedAt = 0\n this.scheduledTime = 0\n this.log('Flushed (hard)')\n return\n }\n\n // Soft flush: drop unscheduled region\n if (this.scheduledChunks < this.audioChunks.length) {\n this.audioChunks.splice(this.scheduledChunks)\n }\n this.log('Flushed (soft)', { remainingScheduled: this.scheduledChunks })\n }\n\n /**\n * Debug logging\n */\n private log(message: string, data?: unknown): void {\n if (this.debug) {\n logger.log(`[StreamingAudioPlayer] ${message}`, data || '')\n }\n }\n}\n"],"names":["StreamingAudioPlayer","options","__publicField","pcmData","isLast","logger","audioChunks","chunk","chunkIndex","audioBuffer","source","idx","err","numSamples","channel","alignedData","int16Array","channelData","i","sampleIndex","elapsed","pauseDuration","_a","callback","totalSamples","total","played","message","data"],"mappings":";;;;AAcO,MAAMA,EAAqB;AAAA,EA0BhC,YAAYC,GAAuC;AAxB3C;AAAA,IAAAC,EAAA,sBAAoC;AACpC,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;A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