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9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import { client } from "./client.mjs";
import { html, create, styled } from "./misc.mjs";

const sample_texts = [
  {
    text: "天气预报显示,今天会有小雨,请大家出门时记得带伞。降温的天气也提醒我们要适时添衣保暖。",
  },
  {
    text: "公司的年度总结会议将在下周三举行,请各部门提前准备好相关材料,确保会议顺利进行。",
  },
  {
    text: "今天的午餐菜单包括烤鸡、沙拉和蔬菜汤,大家可以根据自己的口味选择适合的菜品。",
  },
  {
    text: "请注意,电梯将在下午两点进行例行维护,预计需要一个小时的时间,请大家在此期间使用楼梯。",
  },
  {
    text: "图书馆新到了一批书籍,涵盖了文学、科学和历史等多个领域,欢迎大家前来借阅。",
  },
];

let history_index = 0;

const useStore = create((set, get) => ({
  tts: {
    text: "你好,这里是一段ChatTTS Forge项目的示例文本。",
    spk: "female2",
    style: "chat",
    temperature: 0.3,
    top_P: 1,
    top_K: 20,
    seed: -1,
    format: "mp3",
    prompt1: "",
    prompt2: "",
    prefix: "",
  },
  styles: [],
  speakers: [],

  ui: {
    loading: false,
    // 历史生成结果 { audio: Blob, url: string, params: object }
    history: [],
  },

  async synthesizeTTS() {
    const params = structuredClone(get().tts);
    const blob = await client.synthesizeTTS({
      ...params,
    });
    const blob_url = URL.createObjectURL(blob);
    set({
      ui: {
        ...get().ui,
        history: [
          ...get().ui.history,
          {
            id: history_index++,
            audio: blob,
            url: blob_url,
            params: params,
          },
        ],
      },
    });
  },
  setStyles(styles) {
    set({ styles });
  },
  setSpeakers(speakers) {
    set({ speakers });
  },
  setTTS(tts) {
    set({
      tts: {
        ...get().tts,
        ...tts,
      },
    });
  },
  setUI(ui) {
    set({
      ui: {
        ...get().ui,
        ...ui,
      },
    });
  },
}));

window.addEventListener("load", async () => {
  const styles = await client.listStyles();
  const speakers = await client.listSpeakers();
  console.log("styles:", styles);
  console.log("speakers:", speakers);
  useStore.get().setStyles(styles.data);
  useStore.get().setSpeakers(speakers.data);
});

const TTSPageContainer = styled.div`
  h1 {
    margin-bottom: 1rem;
  }

  p {
    margin-bottom: 1rem;
  }

  #app {
    margin-top: 1rem;
  }

  textarea {
    width: 100%;
    height: 10rem;
    margin-bottom: 1rem;

    min-height: 10rem;

    resize: vertical;
  }

  button {
    padding: 0.5rem 1rem;
    background-color: #007bff;
    color: white;
    border: none;
    cursor: pointer;
  }

  button:hover {
    background-color: #0056b3;
  }

  button:disabled {
    background-color: #6c757d;
    cursor: not-allowed;
  }

  fieldset {
    margin-top: 1rem;
    padding: 1rem;
    border: 1px solid #333;
  }

  legend {
    font-weight: bold;
  }

  label {
    display: block;
    margin-bottom: 0.5rem;
  }

  select,
  input[type="range"],
  input[type="number"] {
    width: 100%;
    margin-top: 0.25rem;
  }

  input[type="range"] {
    width: calc(100% - 2rem);
  }

  input[type="number"] {
    width: calc(100% - 2rem);
    padding: 0.5rem;
  }

  input[type="text"] {
    width: 100%;
    padding: 0.5rem;
  }

  audio {
    margin-top: 1rem;
  }

  textarea,
  input,
  select {
    background-color: #333;
    color: white;
    border: 1px solid #333;
    border-radius: 0.25rem;
    padding: 0.5rem;
  }

  table {
    width: 100%;
    border-collapse: collapse;
  }

  th,
  td {
    padding: 0.5rem;
    border: 1px solid #333;
  }

  th {
    background-color: #333;
    color: white;
  }

  th:nth-child(2),
  td:nth-child(2) {
    width: 60%;
  }

  .content-body {
    display: flex;
    gap: 1rem;
  }

  .content-left {
    flex: 1;
  }

  .content-right {
    flex: 4;
  }

  h1 small {
    font-weight: 100;
    font-size: 0.5em;
    font-weight: normal;
  }

  .btn-synthesize {
    background-color: #007bff;
    color: white;
    border: none;
    cursor: pointer;
    padding: 0.5rem 1rem;
  }

  .btn-synthesize:hover {
    background-color: #0056b3;
  }

  .btn-synthesize:disabled {
    background-color: #6c757d;
    cursor: not-allowed;
  }

  .btn-clear {
    background-color: #dc3545;
    color: white;
    border: none;
    cursor: pointer;
    padding: 0.5rem 1rem;
  }

  .btn-clear:hover {
    background-color: #bd2130;
  }

  .btn-clear:disabled {
    background-color: #6c757d;
    cursor: not-allowed;
  }

  .btn-random {
    background-color: #28a745;
    color: white;
    border: none;
    cursor: pointer;
    padding: 0.5rem 1rem;
  }

  .btn-random:hover {
    background-color: #218838;
  }

  pre {
    white-space: pre-wrap;
  }

  .sample-texts {
    width: unset;
    display: inline-block;
    padding: 0.5rem;
    margin-bottom: 1rem;
  }
`;

export const TTSPage = () => {
  const { tts, setTTS, synthesizeTTS, ui, setUI, speakers, styles } =
    useStore();

  const request = async () => {
    if (ui.loading) {
      return;
    }
    setUI({ loading: true });
    try {
      await synthesizeTTS();
    } catch (error) {
      console.error("Error synthesizing TTS:", error);
    } finally {
      setUI({ loading: false });
    }
  };

  return html`
    <${TTSPageContainer}>
      <textarea
        value=${tts.text}
        onInput=${(e) => setTTS({ text: e.target.value })}
      ></textarea>
      <button class="btn-synthesize" disabled=${ui.loading} onClick=${request}>
        ${ui.loading ? "Synthesizing..." : "Synthesize"}
      </button>
      <button
        class="btn-clear"
        disabled=${ui.loading}
        onClick=${() => setUI({ history: [] })}
      >
        Clear History
      </button>

      <select
        placeholder="Sample Text"
        class="sample-texts"
        value=${tts.text}
        onChange=${(e) => setTTS({ text: e.target.value })}
      >
        ${sample_texts.map(
          (item, index) => html`
            <option key=${index} value=${item.text}>
              Sample ${index + 1}: ${item.text.slice(0, 10) + "..."}
            </option>
          `
        )}
      </select>

      <div class="content-body">
        <fieldset class="content-left">
          <legend>Options</legend>
          <label>
            Speaker:
            <select
              value=${tts.spk}
              onChange=${(e) => setTTS({ spk: e.target.value })}
            >
              <option value="-1">*random</option>
              ${speakers.map(
                (spk) => html`
                  <option key=${spk.index} value=${spk.name}>
                    ${spk.name}
                  </option>
                `
              )}
            </select>
          </label>
          <label>
            Style:
            <select
              value=${tts.style}
              onChange=${(e) => setTTS({ style: e.target.value })}
            >
              <option value="">*auto</option>
              ${styles.map(
                (style) => html`
                  <option key=${style.id} value=${style.name}>
                    ${style.name}
                  </option>
                `
              )}
            </select>
          </label>
          <label>
            Temperature:
            <input
              type="range"
              min="0.01"
              max="2"
              step="0.01"
              value=${tts.temperature}
              onInput=${(e) => setTTS({ temperature: e.target.value })}
            />
            ${tts.temperature}
          </label>
          <label>
            Top P:
            <input
              type="range"
              min="0.01"
              max="1"
              step="0.01"
              value=${tts.top_P}
              onInput=${(e) => setTTS({ top_P: e.target.value })}
            />
            ${tts.top_P}
          </label>
          <label>
            Top K:
            <input
              type="range"
              min="1"
              max="50"
              step="1"
              value=${tts.top_K}
              onInput=${(e) => setTTS({ top_K: e.target.value })}
            />
            ${tts.top_K}
          </label>
          <label>
            Seed:
            <input
              type="number"
              value=${tts.seed}
              onInput=${(e) => setTTS({ seed: e.target.value })}
            />
            <button
              class="btn-random"
              onClick=${() =>
                setTTS({ seed: Math.floor(Math.random() * 2 ** 32 - 1) })}
            >
              Random
            </button>
          </label>
          <label>
            Format
            <select
              value=${tts.format}
              onChange=${(e) => setTTS({ format: e.target.value })}
            >
              <option value="mp3">MP3</option>
              <option value="wav">WAV</option>
            </select>
          </label>
          <label>
            Prompt1:
            <input
              type="text"
              value=${tts.prompt1}
              onInput=${(e) => setTTS({ prompt1: e.target.value })}
            />
          </label>
          <label>
            Prompt2:
            <input
              type="text"
              value=${tts.prompt2}
              onInput=${(e) => setTTS({ prompt2: e.target.value })}
            />
          </label>
          <label>
            Prefix:
            <input
              type="text"
              value=${tts.prefix}
              onInput=${(e) => setTTS({ prefix: e.target.value })}
            />
          </label>
        </fieldset>

        <fieldset class="content-right">
          <legend>History</legend>
          <table>
            <thead>
              <tr>
                <th>id</th>
                <th>Params</th>
                <th>Audio</th>
              </tr>
            </thead>
            <tbody>
              ${[...ui.history].reverse().map(
                (item, index) => html`
                  <tr key=${item.id}>
                    <td>${item.id}</td>
                    <td>
                      <pre>${JSON.stringify(item.params, null, 2)}</pre>
                    </td>
                    <td>
                      <audio controls>
                        <source
                          src=${item.url}
                          type="audio/${item.params.format}"
                        />
                      </audio>
                    </td>
                  </tr>
                `
              )}
            </tbody>
          </table>
        </fieldset>
      </div>
    <//>
  `;
};