File size: 7,894 Bytes
96f34e3
0524ab0
 
 
 
 
 
346a09e
0524ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f34e3
0524ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f34e3
0524ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
// Provider abstraction for Codev voice mode.
//
// Two interfaces:
// - `TranscriptionProvider` — speech → text
// - `TTSProvider` — text → audio
//
// Built-in concrete providers:
// - `EdgeTTSProvider` — uses the `node-edge-tts` package (no Python subprocess)
// - `CommandTTSProvider` — generic shell command with `{input}` / `{input_path}` / `{output_path}` placeholders

import { spawn } from 'node:child_process'
import { existsSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'

// ---------------------------------------------------------------------------
// Public contracts
// ---------------------------------------------------------------------------

export type TranscriptionResult = {
  success: boolean
  text: string
  error?: string
}

export interface TranscriptionProvider {
  name: string
  transcribe(wavPath: string, language?: string): Promise<TranscriptionResult>
}

export type SynthesisResult = {
  audioPath?: string
  error?: string
}

export interface TTSProvider {
  name: string
  synthesize(text: string): Promise<SynthesisResult>
}

// ---------------------------------------------------------------------------
// Local whisper STT
// ---------------------------------------------------------------------------

export type LocalWhisperSTTOptions = {
  binary?: string
  model?: string
  language?: string
  args?: string[]
}

export class LocalWhisperSTT implements TranscriptionProvider {
  readonly name = 'local-whisper'

  constructor(private readonly opts: LocalWhisperSTTOptions = {}) {}

  async transcribe(wavPath: string, language?: string): Promise<TranscriptionResult> {
    const binary = this.resolveBinary()
    if (!binary) {
      return { success: false, text: '', error: 'no whisper binary found' }
    }
    const outDir = await this.makeTmpDir('vc-whisper-out')
    const lang = this.opts.language ?? language ?? 'auto'
    const args = [
      ...(this.opts.args ?? []),
      ...(this.opts.model ? ['-m', this.opts.model] : []),
      '-l',
      lang,
      '--output_format',
      'txt',
      '--output_dir',
      outDir,
      wavPath,
    ]
    try {
      await this.exec(binary, args)
      const base = path.basename(wavPath, path.extname(wavPath)) + '.txt'
      const txtPath = path.join(outDir, base)
      if (!existsSync(txtPath)) {
        return { success: false, text: '', error: 'whisper produced no txt output' }
      }
      const text = await os.domain?.(txtPath) ?? (await import('node:fs')).promises.readFile(txtPath, 'utf8')
      return { success: true, text: String(text).trim() }
    } catch (e: any) {
      return { success: false, text: '', error: e?.message ?? String(e) }
    }
  }

  private resolveBinary(): string | undefined {
    if (this.opts.binary && existsSync(this.opts.binary)) return this.opts.binary
    const candidates = [
      'whisper.cpp',
      'whisper-cpp',
      'whisper',
      'main',
      path.join(os.homedir(), '.local', 'bin', 'whisper.cpp'),
      '/opt/homebrew/bin/whisper.cpp',
      '/usr/local/bin/whisper.cpp',
    ]
    for (const c of candidates) {
      if (existsSync(c)) return c
    }
    return undefined
  }

  private async exec(cmd: string, args: string[]): Promise<void> {
    await new Promise<void>((resolve, reject) => {
      const child = spawn(cmd, args, { shell: false })
      let stderr = ''
      child.stderr?.on('data', (chunk) => {
        stderr += String(chunk)
      })
      child.on('close', (code) => {
        if (code === 0) resolve()
        else reject(new Error(`exit ${code}: ${stderr.slice(0, 200)}`))
      })
      child.on('error', (err) => reject(err))
    })
  }

  private async makeTmpDir(prefix: string): Promise<string> {
    const dir = path.join(os.tmpdir(), `codev-${prefix}-${process.pid}-${Date.now()}`)
    await new Promise<void>((resolve) => {
      const w = spawn('mkdir', ['-p', dir])
      w.on('close', (code) => (code === 0 ? resolve() : resolve()))
    })
    return dir
  }
}

// ---------------------------------------------------------------------------
// Edge TTS
// ---------------------------------------------------------------------------

export class EdgeTTSProvider implements TTSProvider {
  readonly name = 'edge-tts'

  constructor(private readonly voice?: string) {}

  async synthesize(text: string): Promise<SynthesisResult> {
    const trimmed = text.trim()
    if (!trimmed) return { error: 'empty text' }
    const outPath = path.join(os.homedir(), '.claude', 'voice', `tts_${Date.now()}.mp3`)
    await new Promise((resolve) => {
      const w = spawn('mkdir', ['-p', path.dirname(outPath)])
      w.on('close', () => resolve(undefined))
    })
    const selectedVoice = this.voice ?? 'en-US-AriaNeural'
    const inputPath = await this.writeTempText(trimmed)
    try {
      const args = [
        '--voice',
        selectedVoice,
        '--text',
        trimmed,
        '--write-media',
        outPath,
      ]
      await this.exec('edge-tts', args)
      if (!existsSync(outPath)) {
        return { error: 'edge-tts produced no output file' }
      }
      return { audioPath: outPath }
    } catch (e: any) {
      return { error: e?.message ?? String(e) }
    } finally {
      try {
        await (await import('node:fs')).promises.unlink(inputPath)
      } catch {
        // ignore
      }
    }
  }

  private async exec(cmd: string, args: string[]): Promise<void> {
    await new Promise<void>((resolve, reject) => {
      const child = spawn(cmd, args, { shell: false })
      let stderr = ''
      child.stderr?.on('data', (chunk) => {
        stderr += String(chunk)
      })
      child.on('close', (code) => {
        if (code === 0) resolve()
        else reject(new Error(`exit ${code}: ${stderr.slice(0, 200)}`))
      })
      child.on('error', (err) => reject(err))
    })
  }

  private async writeTempText(text: string): Promise<string> {
    const p = path.join(os.tmpdir(), `codev-edge-tts-${process.pid}-${Date.now()}.txt`)
    await (await import('node:fs')).promises.writeFile(p, text, 'utf8')
    return p
  }
}

// ---------------------------------------------------------------------------
// Command-based fallback TTS
// ---------------------------------------------------------------------------

export type CommandTTSTemplate = string

export class CommandTTSProvider implements TTSProvider {
  readonly name = 'command'

  constructor(private readonly template: CommandTTSTemplate) {}

  async synthesize(text: string): Promise<SynthesisResult> {
    const inputPath = await this.writeTempText(text)
    const outputPath = path.join(os.tmpdir(), `vesperclaw-tts-out-${Date.now()}.mp3`)
    const resolved = this.template
      .replace('{input}', inputPath)
      .replace('{input_path}', inputPath)
      .replace('{output_path}', outputPath)
    try {
      await this.exec(resolved)
      return { audioPath: outputPath }
    } catch (e: any) {
      return { error: e?.message ?? String(e) }
    } finally {
      try {
        await (await import('node:fs')).promises.unlink(inputPath)
      } catch {
        // ignore
      }
    }
  }

  private async exec(command: string): Promise<void> {
    await new Promise<void>((resolve, reject) => {
      const child = spawn(command, { shell: true, stdio: ['ignore', 'pipe', 'pipe'] })
      let stderr = ''
      child.stderr?.on('data', (chunk) => {
        stderr += String(chunk)
      })
      child.on('close', (code) => {
        if (code === 0) resolve()
        else reject(new Error(`exit ${code}: ${stderr.slice(0, 200)}`))
      })
      child.on('error', (err) => reject(err))
    })
  }

  private async writeTempText(text: string): Promise<string> {
    const p = path.join(os.tmpdir(), `vesperclaw-tts-txt-${process.pid}-${Date.now()}.txt`)
    await (await import('node:fs')).promises.writeFile(p, text, 'utf8')
    return p
  }
}