Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix: a cloned voice recorded too slowly is caught and flagged, so your translated voice no longer drags

wquguru8e27bb0429fc
diff --git a/Resources/en.lproj/Localizable.strings b/Resources/en.lproj/Localizable.strings
index 23e860f..c6f761a 100644
--- a/Resources/en.lproj/Localizable.strings
+++ b/Resources/en.lproj/Localizable.strings
@@ -998,6 +998,7 @@
"用你的声音说%@" = "Your voice speaking %@";
"对方会听到这个" = "They'll hear this";
"用这个声音" = "Use This Voice";
+"读得偏慢" = "Read slowly";
/* Voices */
"美式" = "American";
@@ -1020,3 +1021,4 @@
"无法打开麦克风:%@" = "Can't open the microphone: %@";
"录音太短了,请至少朗读 %lld 秒" = "Too short; read for at least %lld seconds";
"没听清录音内容,请换个安静的地方再录一次" = "Couldn't make out the recording; try again somewhere quiet";
+"读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次" = "Read a bit slowly: your cloned voice would be just as slow. Record again at your usual pace";
diff --git a/Resources/ja.lproj/Localizable.strings b/Resources/ja.lproj/Localizable.strings
index 1a431b9..4142291 100644
--- a/Resources/ja.lproj/Localizable.strings
+++ b/Resources/ja.lproj/Localizable.strings
@@ -998,6 +998,7 @@
"用你的声音说%@" = "あなたの声で%@を話す";
"对方会听到这个" = "相手にはこう聞こえます";
"用这个声音" = "この声を使う";
+"读得偏慢" = "読むのが遅め";
/* Voices */
"美式" = "アメリカ英語";
@@ -1020,3 +1021,4 @@
"无法打开麦克风:%@" = "マイクを開けません: %@";
"录音太短了,请至少朗读 %lld 秒" = "録音が短すぎます。%lld秒以上読み上げてください";
"没听清录音内容,请换个安静的地方再录一次" = "録音を聞き取れませんでした。静かな場所でもう一度録音してください";
+"读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次" = "読むのが少し遅いため、クローンの声も同じく遅くなります。普段の話す速さでもう一度録音してください";
diff --git a/Resources/ko.lproj/Localizable.strings b/Resources/ko.lproj/Localizable.strings
index 081457d..a9028f8 100644
--- a/Resources/ko.lproj/Localizable.strings
+++ b/Resources/ko.lproj/Localizable.strings
@@ -998,6 +998,7 @@
"用你的声音说%@" = "내 목소리로 %@ 말하기";
"对方会听到这个" = "상대에게 이렇게 들립니다";
"用这个声音" = "이 음성 사용";
+"读得偏慢" = "느리게 읽음";
/* Voices */
"美式" = "미국식";
@@ -1020,3 +1021,4 @@
"无法打开麦克风:%@" = "마이크를 열 수 없습니다: %@";
"录音太短了,请至少朗读 %lld 秒" = "녹음이 너무 짧습니다. 최소 %lld초 이상 읽어 주세요";
"没听清录音内容,请换个安静的地方再录一次" = "녹음 내용을 알아듣지 못했습니다. 조용한 곳에서 다시 녹음하세요";
+"读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次" = "조금 느리게 읽어서 복제한 목소리도 똑같이 느려집니다. 평소 말하는 속도로 다시 녹음하세요";
diff --git a/Resources/zh-Hans.lproj/Localizable.strings b/Resources/zh-Hans.lproj/Localizable.strings
index dc8ccdb..866700e 100644
--- a/Resources/zh-Hans.lproj/Localizable.strings
+++ b/Resources/zh-Hans.lproj/Localizable.strings
@@ -998,6 +998,7 @@
"用你的声音说%@" = "用你的声音说%@";
"对方会听到这个" = "对方会听到这个";
"用这个声音" = "用这个声音";
+"读得偏慢" = "读得偏慢";
/* Voices */
"美式" = "美式";
@@ -1020,3 +1021,4 @@
"无法打开麦克风:%@" = "无法打开麦克风:%@";
"录音太短了,请至少朗读 %lld 秒" = "录音太短了,请至少朗读 %lld 秒";
"没听清录音内容,请换个安静的地方再录一次" = "没听清录音内容,请换个安静的地方再录一次";
+"读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次" = "读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次";
diff --git a/Sources/LiveSubs/AppModel.swift b/Sources/LiveSubs/AppModel.swift
index 8c15b95..13e9552 100644
--- a/Sources/LiveSubs/AppModel.swift
+++ b/Sources/LiveSubs/AppModel.swift
@@ -1385,7 +1385,8 @@ final class AppModel: ObservableObject {
(try? f.read(into: buf)) != nil else { debugLog("voiceCloneFile: unreadable"); return }
let text = await LoopbackTest.transcribe([buf], locale: settings.speakFromLang.locale)
do { try VoiceReference.save(wav: url, text: text) } catch { debugLog("voiceCloneFile: \(error)") }
- debugLog("voiceCloneFile: saved, transcript \(text.count) chars")
+ let pace = VoiceReference.pace(of: url, text: text)
+ debugLog("voiceCloneFile: saved, transcript \(text.count) chars, pace \(pace.map { String(format: "%.1f", $0) } ?? "-")/s slow=\(pace.map { VoiceReference.isSlow(text: text, pace: $0) } ?? false)")
}
case "voicePreview":
// voicePreview:<lang>=<id>
diff --git a/Sources/LiveSubs/VoicePicker.swift b/Sources/LiveSubs/VoicePicker.swift
index cc09fd7..bc1e4ea 100644
--- a/Sources/LiveSubs/VoicePicker.swift
+++ b/Sources/LiveSubs/VoicePicker.swift
@@ -209,7 +209,7 @@ struct VoiceSection: View {
Text("我自己的声音").font(.system(size: 13, weight: .semibold))
if has {
HStack(spacing: 4) {
- Text("已录 \(Int(VoiceReference.duration.rounded())) 秒 · \(cloneNote)")
+ Text("已录 \(Int(VoiceReference.duration.rounded())) 秒 · \(VoiceReference.savedIsSlow ? L("读得偏慢") : cloneNote)")
Text("·")
Button("重录") { recording = true }.buttonStyle(.plain).underline()
Button("删除") {
diff --git a/Sources/LiveSubs/Voices.swift b/Sources/LiveSubs/Voices.swift
index 4764f41..312f665 100644
--- a/Sources/LiveSubs/Voices.swift
+++ b/Sources/LiveSubs/Voices.swift
@@ -172,6 +172,68 @@ enum VoiceReference {
try? FileManager.default.removeItem(at: folder)
}
+ // MARK: Pace
+
+ /// Ming copies the recording's pace into every sentence: a recital at 1.7 characters a second
+ /// made each sentence about 4× as long as the built-in voice says it. Below these (characters
+ /// for Chinese, Japanese and Korean, words otherwise, per second of speech) the clone is too
+ /// slow for a meeting; reading the passage at an ordinary pace gives 3.5–5 and 2.3–3.
+ static let slowCharacters = 2.5, slowWords = 1.6
+
+ /// How many characters or words a second the recording is read at, silence at both ends left
+ /// out; nil when there is no speech to measure.
+ static func pace(text: String, samples: [Float], sampleRate: Double) -> Double? {
+ let units = SpeakScore.tokens(text).count
+ guard units > 0, let span = speechSpan(samples, sampleRate: sampleRate), span > 0.5 else { return nil }
+ return Double(units) / span
+ }
+
+ static func isSlow(text: String, pace: Double) -> Bool {
+ let tokens = SpeakScore.tokens(text)
+ let characters = tokens.filter { $0.unicodeScalars.contains(where: SpeakScore.isCJK) }.count
+ return pace < (characters * 2 >= tokens.count ? slowCharacters : slowWords)
+ }
+
+ /// Seconds from the first to the last 20 ms window within 35 dB of the loudest one.
+ static func speechSpan(_ samples: [Float], sampleRate: Double) -> Double? {
+ let size = max(1, Int(sampleRate * 0.02))
+ var levels: [Float] = []
+ var i = 0
+ while i < samples.count {
+ let end = min(samples.count, i + size)
+ var sum: Float = 0
+ for k in i..<end { sum += samples[k] * samples[k] }
+ levels.append((sum / Float(end - i)).squareRoot())
+ i = end
+ }
+ guard let loudest = levels.max(), loudest > 0.003 else { return nil }
+ let floor = loudest * 0.0178
+ guard let first = levels.firstIndex(where: { $0 >= floor }),
+ let last = levels.lastIndex(where: { $0 >= floor }) else { return nil }
+ return Double(last - first + 1) * Double(size) / sampleRate
+ }
+
+ /// The pace of a WAV and what it says.
+ static func pace(of url: URL, text: String) -> Double? {
+ guard let f = try? AVAudioFile(forReading: url),
+ let buf = AVAudioPCMBuffer(pcmFormat: f.processingFormat, frameCapacity: AVAudioFrameCount(f.length)),
+ (try? f.read(into: buf)) != nil, let ch = buf.floatChannelData?[0] else { return nil }
+ return pace(text: text, samples: Array(UnsafeBufferPointer(start: ch, count: Int(buf.frameLength))),
+ sampleRate: f.processingFormat.sampleRate)
+ }
+
+ private static var savedPace: (date: Date, slow: Bool)?
+
+ /// The saved recording is read too slowly (worked out once per recording).
+ static var savedIsSlow: Bool {
+ guard let date = (try? audioURL.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate,
+ let text else { return false }
+ if let c = savedPace, c.date == date { return c.slow }
+ let slow = pace(of: audioURL, text: text).map { isSlow(text: text, pace: $0) } ?? false
+ savedPace = (date, slow)
+ return slow
+ }
+
/// The passage to read, in the user's own language.
static func passage(for locale: String) -> String {
switch VoiceCatalog.key(for: locale) {
@@ -277,7 +339,14 @@ final class VoiceRecorder: ObservableObject {
Task {
let text = await Self.transcribe(url, locale: locale)
self.transcript = text
- self.phase = text.count >= 6 ? .ready : .failed(L("没听清录音内容,请换个安静的地方再录一次"))
+ guard text.count >= 6 else { self.phase = .failed(L("没听清录音内容,请换个安静的地方再录一次")); return }
+ let pace = VoiceReference.pace(of: url, text: text)
+ debugLog("voice clone: \(text.count) chars, pace \(pace.map { String(format: "%.1f", $0) } ?? "-")/s")
+ if let pace, VoiceReference.isSlow(text: text, pace: pace) {
+ self.phase = .failed(L("读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次"))
+ return
+ }
+ self.phase = .ready
}
}
diff --git a/Tests/LiveSubsTests/VoiceReferenceTests.swift b/Tests/LiveSubsTests/VoiceReferenceTests.swift
new file mode 100644
index 0000000..1fc6496
--- /dev/null
+++ b/Tests/LiveSubsTests/VoiceReferenceTests.swift
@@ -0,0 +1,45 @@
+import Foundation
+import Testing
+@testable import LiveSubs
+
+@Suite struct VoiceReferenceTests {
+ /// `seconds` of silence, then `speech` seconds of a 200 Hz tone, then silence.
+ private func clip(lead: Double, speech: Double, tail: Double, rate: Double = 16_000) -> [Float] {
+ let quiet = { (s: Double) in [Float](repeating: 0.0005, count: Int(s * rate)) }
+ let tone = (0..<Int(speech * rate)).map { Float(sin(2 * Double.pi * 200 * Double($0) / rate)) * 0.3 }
+ return quiet(lead) + tone + quiet(tail)
+ }
+
+ @Test func speechSpanLeavesOutSilenceAtBothEnds() throws {
+ let span = try #require(VoiceReference.speechSpan(clip(lead: 1.5, speech: 6, tail: 2), sampleRate: 16_000))
+ #expect(abs(span - 6) < 0.05)
+ }
+
+ @Test func silenceHasNoSpanOrPace() {
+ let quiet = [Float](repeating: 0.0005, count: 16_000 * 3)
+ #expect(VoiceReference.speechSpan(quiet, sampleRate: 16_000) == nil)
+ #expect(VoiceReference.pace(text: "大家好", samples: quiet, sampleRate: 16_000) == nil)
+ }
+
+ @Test func paceCountsChineseCharactersPerSecondOfSpeech() throws {
+ // 19 characters over 10 s of speech: the recital that made the clone 4× slow.
+ let text = "贤文慧汝尊尊集运增广多见多闻观今一见古。"
+ let pace = try #require(VoiceReference.pace(text: text, samples: clip(lead: 0.5, speech: 10, tail: 0.5), sampleRate: 16_000))
+ #expect(abs(pace - 1.9) < 0.05)
+ #expect(VoiceReference.isSlow(text: text, pace: pace))
+ }
+
+ @Test func theChinesePassageAtAnOrdinaryPaceIsNotSlow() {
+ let text = VoiceReference.passage(for: "zh-CN")
+ let units = Double(SpeakScore.tokens(text).count)
+ #expect(!VoiceReference.isSlow(text: text, pace: units / 15))
+ #expect(VoiceReference.isSlow(text: text, pace: units / 30))
+ }
+
+ @Test func englishIsMeasuredInWords() {
+ let text = VoiceReference.passage(for: "en-US")
+ let words = Double(SpeakScore.tokens(text).count)
+ #expect(!VoiceReference.isSlow(text: text, pace: words / 16))
+ #expect(VoiceReference.isSlow(text: text, pace: words / 30))
+ }
+}
diff --git a/site/changelog.json b/site/changelog.json
index e77bda8..10fc8e7 100644
--- a/site/changelog.json
+++ b/site/changelog.json
@@ -6,6 +6,11 @@
"summary": "实时纪要窗口重新排版,纪要落后时说清楚还差多少;说话人的名字在实时纪要里就能改。",
"image": null,
"items": [
+ {
+ "kind": "fixed",
+ "title": "录“我自己的声音”时读得太慢会提醒重录,克隆的声音不再拖得很慢。",
+ "text": "克隆会照搬录音的语速:太慢的录音会让对方每句都多等好几秒。已经录好的偏慢录音,在声音列表里会标出“读得偏慢”。"
+ },
{
"kind": "fixed",
"title": "同传偶尔在第一句话卡住、之后整场都没声音的问题已修好。",