@ziplayer/plugin 0.0.4 → 0.1.0

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/src/TTSPlugin.ts CHANGED
@@ -3,19 +3,26 @@ import { Readable } from "stream";
3
3
  import { getTTSUrls } from "@zibot/zitts";
4
4
  import axios from "axios";
5
5
 
6
+ /**
7
+ * Configuration options for the TTSPlugin.
8
+ */
6
9
  export interface TTSPluginOptions {
7
- defaultLang?: string; // e.g., "vi" | "en"
10
+ /** Default language code for TTS (e.g., "vi", "en", "en-US") */
11
+ defaultLang?: string;
12
+ /** Whether to use slow speech rate */
8
13
  slow?: boolean;
9
14
  /**
10
15
  * Optional custom TTS hook. If provided, it will be used to
11
16
  * create the audio stream for the given text instead of the
12
17
  * built-in Google TTS wrapper.
13
18
  *
14
- * Return one of:
19
+ * @param text - The text to convert to speech
20
+ * @param ctx - Context information including language, speed, and track
21
+ * @returns One of:
15
22
  * - Node Readable (preferred)
16
23
  * - HTTP(S) URL string or URL object
17
24
  * - Buffer / Uint8Array / ArrayBuffer
18
- \t * - Or an object with { stream, type } | { url, type }
25
+ * - Or an object with { stream, type } | { url, type }
19
26
  */
20
27
  createStream?: (
21
28
  text: string,
@@ -30,17 +37,86 @@ export interface TTSPluginOptions {
30
37
  | ArrayBuffer;
31
38
  }
32
39
 
40
+ /**
41
+ * Internal configuration for TTS processing.
42
+ */
33
43
  interface TTSConfig {
44
+ /** The text to convert to speech */
34
45
  text: string;
46
+ /** The language code for TTS */
35
47
  lang: string;
48
+ /** Whether to use slow speech rate */
36
49
  slow: boolean;
37
50
  }
38
51
 
52
+ /**
53
+ * A plugin for Text-to-Speech (TTS) functionality.
54
+ *
55
+ * This plugin provides support for:
56
+ * - Converting text to speech using Google TTS
57
+ * - Custom TTS providers via the createStream hook
58
+ * - Multiple language support
59
+ * - Configurable speech rate (normal/slow)
60
+ * - TTS query parsing with language and speed options
61
+ *
62
+ * @example
63
+ * const ttsPlugin = new TTSPlugin({
64
+ * defaultLang: "en",
65
+ * slow: false
66
+ * });
67
+ *
68
+ * // Add to PlayerManager
69
+ * const manager = new PlayerManager({
70
+ * plugins: [ttsPlugin]
71
+ * });
72
+ *
73
+ * // Search for TTS content
74
+ * const result = await ttsPlugin.search("tts:Hello world", "user123");
75
+ * const stream = await ttsPlugin.getStream(result.tracks[0]);
76
+ *
77
+ * @example
78
+ * // Custom TTS provider
79
+ * const customTTSPlugin = new TTSPlugin({
80
+ * defaultLang: "en",
81
+ * createStream: async (text, ctx) => {
82
+ * // Custom TTS implementation
83
+ * return customTTSProvider.synthesize(text, ctx.lang);
84
+ * }
85
+ * });
86
+ *
87
+ * @since 1.0.0
88
+ */
39
89
  export class TTSPlugin extends BasePlugin {
40
90
  name = "tts";
41
91
  version = "1.0.0";
42
92
  private opts: { defaultLang: string; slow: boolean; createStream?: TTSPluginOptions["createStream"] };
43
93
 
94
+ /**
95
+ * Creates a new TTSPlugin instance.
96
+ *
97
+ * @param opts - Configuration options for the TTS plugin
98
+ * @param opts.defaultLang - Default language code for TTS (default: "vi")
99
+ * @param opts.slow - Whether to use slow speech rate (default: false)
100
+ * @param opts.createStream - Optional custom TTS provider function
101
+ *
102
+ * @example
103
+ * // Basic TTS with Vietnamese as default
104
+ * const ttsPlugin = new TTSPlugin();
105
+ *
106
+ * // TTS with English as default and slow speech
107
+ * const slowTTSPlugin = new TTSPlugin({
108
+ * defaultLang: "en",
109
+ * slow: true
110
+ * });
111
+ *
112
+ * // TTS with custom provider
113
+ * const customTTSPlugin = new TTSPlugin({
114
+ * defaultLang: "en",
115
+ * createStream: async (text, ctx) => {
116
+ * return await myCustomTTS.synthesize(text, ctx.lang);
117
+ * }
118
+ * });
119
+ */
44
120
  constructor(opts?: TTSPluginOptions) {
45
121
  super();
46
122
  this.opts = {
@@ -50,12 +126,46 @@ export class TTSPlugin extends BasePlugin {
50
126
  };
51
127
  }
52
128
 
129
+ /**
130
+ * Determines if this plugin can handle the given query.
131
+ *
132
+ * @param query - The search query to check
133
+ * @returns `true` if the query starts with "tts:" or "say ", `false` otherwise
134
+ *
135
+ * @example
136
+ * plugin.canHandle("tts:Hello world"); // true
137
+ * plugin.canHandle("say Hello world"); // true
138
+ * plugin.canHandle("youtube.com/watch?v=123"); // false
139
+ */
53
140
  canHandle(query: string): boolean {
54
141
  if (!query) return false;
55
142
  const q = query.trim().toLowerCase();
56
143
  return q.startsWith("tts:") || q.startsWith("say ");
57
144
  }
58
145
 
146
+ /**
147
+ * Creates a TTS track from the given query.
148
+ *
149
+ * This method parses TTS queries and creates a track that can be played as audio.
150
+ * It supports various query formats including language and speed specifications.
151
+ *
152
+ * @param query - The TTS query to process
153
+ * @param requestedBy - The user ID who requested the TTS
154
+ * @returns A SearchResult containing a single TTS track
155
+ *
156
+ * @example
157
+ * // Basic TTS
158
+ * const result = await plugin.search("tts:Hello world", "user123");
159
+ *
160
+ * // TTS with specific language
161
+ * const result2 = await plugin.search("tts:en:Hello world", "user123");
162
+ *
163
+ * // TTS with language and slow speed
164
+ * const result3 = await plugin.search("tts:en:true:Hello world", "user123");
165
+ *
166
+ * // Using "say" prefix
167
+ * const result4 = await plugin.search("say Hello world", "user123");
168
+ */
59
169
  async search(query: string, requestedBy: string): Promise<SearchResult> {
60
170
  if (!this.canHandle(query)) {
61
171
  return { tracks: [] };
@@ -79,6 +189,23 @@ export class TTSPlugin extends BasePlugin {
79
189
  return { tracks: [track] };
80
190
  }
81
191
 
192
+ /**
193
+ * Generates an audio stream for a TTS track.
194
+ *
195
+ * This method converts the text in the track to speech using either the custom
196
+ * TTS provider (if configured) or the built-in Google TTS service. It handles
197
+ * various return types from custom providers and ensures proper stream formatting.
198
+ *
199
+ * @param track - The TTS track to convert to audio
200
+ * @returns A StreamInfo object containing the audio stream
201
+ * @throws {Error} If TTS generation fails or no audio URLs are returned
202
+ *
203
+ * @example
204
+ * const track = { id: "tts-123", title: "TTS: Hello world", ... };
205
+ * const streamInfo = await plugin.getStream(track);
206
+ * console.log(streamInfo.type); // "arbitrary"
207
+ * console.log(streamInfo.stream); // Readable stream with audio
208
+ */
82
209
  async getStream(track: Track): Promise<StreamInfo> {
83
210
  const cfg = this.extractConfig(track);
84
211