-
Notifications
You must be signed in to change notification settings - Fork 17
/
Alignment.ts
302 lines (213 loc) · 9.44 KB
/
Alignment.ts
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
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
import { extendDeep } from "../utilities/ObjectUtilities.js"
import { logToStderr } from "../utilities/Utilities.js"
import { AudioSourceParam, RawAudio, downmixToMonoAndNormalize, ensureRawAudio, getRawAudioDuration, normalizeAudioLevel, trimAudioEnd } from "../audio/AudioUtilities.js"
import { Logger } from "../utilities/Logger.js"
import { resampleAudioSpeex } from "../dsp/SpeexResampler.js"
import * as API from "./API.js"
import { Timeline, addTimeOffsetToTimeline, addWordTextOffsetsToTimeline, wordTimelineToSegmentSentenceTimeline } from "../utilities/Timeline.js"
import { formatLanguageCodeWithName, getDefaultDialectForLanguageCodeIfPossible, getShortLanguageCode, normalizeLanguageCode } from "../utilities/Locale.js"
import { WhisperOptions, whisperOptionsDefaults } from "../recognition/WhisperSTT.js"
import chalk from "chalk"
import { createAlignmentReferenceUsingEspeakPreprocessed } from "../alignment/SpeechAlignment.js"
import { loadLexiconsForLanguage } from "../nlp/Lexicon.js"
import { SubtitlesConfig, defaultSubtitlesConfig } from "../subtitles/Subtitles.js"
import { type MfccOptions } from "../dsp/MFCC.js"
const log = logToStderr
export async function align(input: AudioSourceParam, transcript: string, options: AlignmentOptions): Promise<AlignmentResult> {
const logger = new Logger()
const startTimestamp = logger.getTimestamp()
logger.start("Prepare for alignment")
const inputRawAudio = await ensureRawAudio(input)
let sourceRawAudio = await ensureRawAudio(inputRawAudio, 16000, 1)
sourceRawAudio = normalizeAudioLevel(sourceRawAudio)
sourceRawAudio.audioChannels[0] = trimAudioEnd(sourceRawAudio.audioChannels[0], 0, -40)
options = extendDeep(defaultAlignmentOptions, options)
const dtwWindowDuration = options.dtw!.windowDuration!
let language: string
if (options.language) {
language = normalizeLanguageCode(options.language!)
} else {
logger.start("No language specified. Detecting language")
const { detectedLanguage } = await API.detectTextLanguage(transcript, options.languageDetection || {})
language = detectedLanguage
logger.end()
logger.logTitledMessage('Language detected', formatLanguageCodeWithName(detectedLanguage))
}
language = getDefaultDialectForLanguageCodeIfPossible(language)
logger.start("Get espeak voice list and select best matching voice")
const { bestMatchingVoice } = await API.requestVoiceList({ engine: "espeak", language })
if (!bestMatchingVoice) {
throw new Error("No matching voice found")
}
const espeakVoice = bestMatchingVoice.name
logger.end()
logger.logTitledMessage('Selected voice', `'${espeakVoice}' (${formatLanguageCodeWithName(bestMatchingVoice.languages[0], 2)})`)
logger.start("Load alignment module")
const { alignUsingDtwWithRecognition, alignUsingDtw } = await import("../alignment/SpeechAlignment.js")
async function getAlignmentReference() {
logger.start("Create alignment reference with eSpeak")
const espeakLanguage = bestMatchingVoice.languages[0]
const lexicons = await loadLexiconsForLanguage(espeakLanguage, options.customLexiconPaths)
let { rawAudio: referenceRawAudio, timeline: referenceTimeline } = await createAlignmentReferenceUsingEspeakPreprocessed(transcript, espeakLanguage, espeakVoice, lexicons)
referenceRawAudio = await resampleAudioSpeex(referenceRawAudio, 16000)
referenceRawAudio = downmixToMonoAndNormalize(referenceRawAudio)
return { referenceRawAudio, referenceTimeline }
}
let mappedTimeline: Timeline
switch (options.engine) {
case "dtw": {
const { referenceRawAudio, referenceTimeline } = await getAlignmentReference()
logger.end()
const mfccOptions = getMfccOptionsForGranularity(options.dtw!.granularity!)
mappedTimeline = await alignUsingDtw(sourceRawAudio, referenceRawAudio, referenceTimeline, dtwWindowDuration, mfccOptions)
break
}
case "dtw-ra": {
/*
const promptWords = (await splitToWords(prompt, language)).filter(word => isWord(word))
shuffleArrayInPlace(promptWords, this.randomGen)
//promptWords.reverse()
prompt = promptWords.join(" ")
*/
const recognitionOptionsDefaults: API.RecognitionOptions = {
engine: "whisper",
language,
}
const recognitionOptions: API.RecognitionOptions = extendDeep(recognitionOptionsDefaults, options.recognition || {})
logger.end()
const { wordTimeline: recognitionTimeline } = await API.recognize(sourceRawAudio, recognitionOptions)
const { referenceRawAudio, referenceTimeline } = await getAlignmentReference()
logger.end()
const phoneAlignmentMethod = options.dtw!.phoneAlignmentMethod!
const mfccOptions = getMfccOptionsForGranularity(options.dtw!.granularity!)
mappedTimeline = await alignUsingDtwWithRecognition(sourceRawAudio, referenceRawAudio, referenceTimeline, recognitionTimeline, espeakVoice, phoneAlignmentMethod, dtwWindowDuration, mfccOptions)
break
}
case "whisper": {
const WhisperSTT = await import("../recognition/WhisperSTT.js")
const whisperOptions = options.whisper!
const shortLanguageCode = getShortLanguageCode(language)
const { modelName, modelDir, tokenizerDir } = await WhisperSTT.loadPackagesAndGetPaths(whisperOptions.model, language)
if (modelName.endsWith(".en") && shortLanguageCode != "en") {
throw new Error(`The model '${modelName}' is English only and cannot transcribe language '${shortLanguageCode}'`)
}
if (getRawAudioDuration(sourceRawAudio) > 30) {
throw new Error("Whisper based alignment currently only supports audio inputs that are 30s or less")
}
logger.end()
mappedTimeline = await WhisperSTT.align(sourceRawAudio, transcript, modelName, modelDir, tokenizerDir, shortLanguageCode)
break
}
default: {
throw new Error(`Engine '${options.engine}' is not supported`)
}
}
addWordTextOffsetsToTimeline(mappedTimeline, transcript)
const { segmentTimeline } = await wordTimelineToSegmentSentenceTimeline(mappedTimeline, transcript, language, options.plainText?.paragraphBreaks, options.plainText?.whitespace)
logger.end()
logger.logDuration(`Total alignment time`, startTimestamp, chalk.magentaBright)
return {
timeline: segmentTimeline,
wordTimeline: mappedTimeline,
inputRawAudio,
transcript,
language
}
}
export async function alignSegments(sourceRawAudio: RawAudio, segmentTimeline: Timeline, alignmentOptions: AlignmentOptions) {
const timeline: Timeline = []
for (const segmentEntry of segmentTimeline) {
const segmentText = segmentEntry.text
const segmentStartTime = segmentEntry.startTime
const segmentEndTime = segmentEntry.endTime
const segmentStartSampleIndex = Math.floor(segmentStartTime * sourceRawAudio.sampleRate)
const segmentEndSampleIndex = Math.floor(segmentEndTime * sourceRawAudio.sampleRate)
const segmentAudioSamples = sourceRawAudio.audioChannels[0].slice(segmentStartSampleIndex, segmentEndSampleIndex)
const segmentRawAudio: RawAudio = {
audioChannels: [segmentAudioSamples],
sampleRate: sourceRawAudio.sampleRate
}
const { wordTimeline: mappedTimeline } = await align(segmentRawAudio, segmentText, alignmentOptions)
const segmentTimelineWithOffset = addTimeOffsetToTimeline(mappedTimeline, segmentStartTime)
timeline.push(...segmentTimelineWithOffset)
}
return timeline
}
function getMfccOptionsForGranularity(granularity: DtwGranularity) {
let result: MfccOptions
if (granularity == 'high') {
result = { windowDuration: 0.025, hopDuration: 0.010, fftOrder: 512 }
} else if (granularity == 'medium') {
result = { windowDuration: 0.050, hopDuration: 0.025, fftOrder: 1024 }
} else if (granularity == 'low') {
result = { windowDuration: 0.100, hopDuration: 0.050, fftOrder: 2048 }
} else {
throw new Error(`Invalid granularity setting: '${granularity}'`)
}
return result
}
export interface AlignmentResult {
timeline: Timeline
wordTimeline: Timeline
transcript: string
language: string
inputRawAudio: RawAudio
}
export type AlignmentEngine = "dtw" | "dtw-ra" | "whisper"
export type PhoneAlignmentMethod = "interpolation" | "dtw"
export type DtwGranularity = 'high' | 'medium' | 'low'
export interface AlignmentOptions {
engine?: AlignmentEngine
language?: string
languageDetection?: API.TextLanguageDetectionOptions
customLexiconPaths?: string[]
plainText?: API.PlainTextOptions
subtitles?: SubtitlesConfig
dtw?: {
windowDuration?: number,
phoneAlignmentMethod?: PhoneAlignmentMethod,
granularity: DtwGranularity
}
recognition?: API.RecognitionOptions
whisper?: WhisperOptions
}
export const defaultAlignmentOptions: AlignmentOptions = {
engine: "dtw",
language: undefined,
languageDetection: {
},
customLexiconPaths: undefined,
plainText: {
paragraphBreaks: 'double',
whitespace: 'collapse'
},
subtitles: defaultSubtitlesConfig,
dtw: {
windowDuration: 120,
phoneAlignmentMethod: 'dtw',
granularity: 'high'
},
recognition: {
},
whisper: whisperOptionsDefaults
}
export const alignmentEngines: API.EngineMetadata[] = [
{
id: 'dtw',
name: 'Dynamic Time Warping',
description: 'Makes use of synthesis to find the best mapping between the original audio and its transcript.',
type: 'local'
},
{
id: 'dtw-ra',
name: 'Dynamic Time Warping with Recognition Assist',
description: 'Makes use of both synthesis and recognition to find the best mapping between the original audio and its transcript.',
type: 'local'
},
{
id: 'whisper',
name: 'OpenAI Whisper',
description: 'Extracts timestamps from the internal state of the Whisper recognition model (note: currently limited to a maximum of 30s audio length).',
type: 'local'
}
]