wangzhibo
2026-08-30 c8515170fd399b4d2dfaebb59128d4674e58915e
1
2
3
4
5
6
7
8
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
/** @param {Record<string, any>} voice */
export function hasVoicePreview(voice) {
  return Boolean(voice?.isClone || voice?.voiceDemo || voice?.voice_demo)
}
 
export function createVoicePreviewRequestGate() {
  let sequence = 0
 
  return {
    begin() {
      sequence += 1
      return sequence
    },
    invalidate() {
      sequence += 1
    },
    isCurrent(requestId) {
      return requestId === sequence
    },
  }
}
 
/**
 * @param {{ id: string, isClone?: boolean, voiceDemo?: string | null }} voice
 * @param {(cloneId: string) => Promise<string>} getCloneAudioId
 * @param {string} baseUrl
 */
export async function resolveVoicePreviewUrl(voice, getCloneAudioId, baseUrl) {
  if (!voice?.isClone) {
    return typeof voice?.voiceDemo === 'string' ? voice.voiceDemo : ''
  }
  if (!voice.id) {
    return ''
  }
 
  const uuid = await getCloneAudioId(voice.id)
  if (!uuid) {
    return ''
  }
 
  return `${baseUrl.replace(/\/+$/, '')}/voiceClone/play/${encodeURIComponent(uuid)}`
}