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(windows): the cloned voice no longer comes out silent when the recording ends in a pause

wquguru920c82f3408d
diff --git a/windows/src/Cicada.Cli/Program.cs b/windows/src/Cicada.Cli/Program.cs
index b49aa6a..b219dca 100644
--- a/windows/src/Cicada.Cli/Program.cs
+++ b/windows/src/Cicada.Cli/Program.cs
@@ -8,7 +8,8 @@
// cicada-cli check the AI service's key check: state, hears, catalog sizes
// cicada-cli speak <text> <out.wav> the service's voice for <text>, as a 24 kHz WAV; --voice <id>,
// --lang <code> (Ming's language_id), --clone <reference.wav> [--clone-text <what it says>]
-// [--from <my language>] for the user's own voice (Ling only)
+// [--from <my language>] for the user's own voice (Ling only), kept as the app keeps it
+// (quiet ends trimmed) unless --raw sends the file as it is
// cicada-cli segments <file.wav> where the segmenter cuts (compare with ming_asr.py)
// cicada-cli roundtrip <session.json>… reads and writes records, says if anything changed
//
@@ -86,13 +87,23 @@ switch (args_[0])
if (text.Length == 0)
{
var me = Opt("--from", "zh-CN");
- var (outcome, heard) = await VoiceCloneCheck.Check(refWav,
+ var (outcome, heard, trimmed) = await VoiceCloneCheck.Check(refWav,
async w => (await new Translator(ling).Hear(w, SpeakEngine.TargetFor("room", "en", me), me, Priority.Speak))?.Original);
- Console.WriteLine($"reference: {(refWav.Length - 44) / 32000.0:F1} s, {outcome}, heard «{heard}»");
+ Console.WriteLine($"reference: {(refWav.Length - 44) / 32000.0:F1} s, {(trimmed.Length - 44) / 32000.0:F1} s trimmed, {outcome}, heard «{heard}»");
if (outcome != VoiceCloneCheck.Outcome.Ready) return 1;
text = heard;
+ refWav = trimmed;
+ }
+ if (args_.Contains("--raw")) reference = ("data:audio/wav;base64," + Convert.ToBase64String(refWav), text);
+ else
+ {
+ // Kept and read back as the app keeps it (VoiceReference.Save trims the quiet ends).
+ var kept = new VoiceReference(new PlainProtector(), Path.Combine(Path.GetTempPath(), $"cicada-cli-voice-{Environment.ProcessId}"));
+ kept.Save(refWav, text);
+ reference = kept.Reference();
+ Console.WriteLine($"reference kept: {kept.Duration:F1} s");
+ kept.Remove();
}
- reference = ("data:audio/wav;base64," + Convert.ToBase64String(refWav), text);
}
var lang = Opt("--lang", "");
var started = DateTime.UtcNow;
diff --git a/windows/src/Cicada.Core/VoiceClone.cs b/windows/src/Cicada.Core/VoiceClone.cs
index c60a984..984e7b9 100644
--- a/windows/src/Cicada.Core/VoiceClone.cs
+++ b/windows/src/Cicada.Core/VoiceClone.cs
@@ -60,8 +60,10 @@ public sealed class VoiceReference(IKeyProtector protector, string? folder = nul
string Seal(byte[] data) => Sealed + Convert.ToBase64String(protector.Tag == "plain" ? data : protector.Protect(data));
+ /// Keeps `wav` trimmed of the quiet at its ends (a trailing pause makes Ming's clone silent).
public void Save(byte[] wav, string text)
{
+ wav = VoicePace.Trim(wav);
lock (gate)
{
Directory.CreateDirectory(Folder);
@@ -126,7 +128,31 @@ public static class VoicePace
}
/// Seconds from the first to the last 20 ms window within 35 dB of the loudest one.
- public static double? SpeechSpan(float[] samples, double sampleRate)
+ public static double? SpeechSpan(float[] samples, double sampleRate) =>
+ SpeechWindows(samples, sampleRate) is var (first, last, size) ? (last - first + 1) * (double)size / sampleRate : null;
+
+ /// Quiet kept each side of the reading when trimming.
+ public const double TrimPad = 0.2;
+
+ /// A recording (16 kHz mono WAV) without the quiet before and after the reading, 0.2 s kept each
+ /// side (linux voice_reference.trim). Ming copies the clip's ending: a second of silence after
+ /// the last word (the pause before 读完了) made every cloned sentence come back silent
+ /// (measured 2026-11 against Ling: 8.2 s of nothing instead of 3.5 s of voice).
+ public static byte[] Trim(byte[] wav, int rate = 16000)
+ {
+ var start = wav.Length >= 44 && wav[0] == 'R' && wav[1] == 'I' ? 44 : 0;
+ if (SpeechWindows(Samples(wav), rate) is not var (first, last, size)) return wav;
+ var pad = (int)(TrimPad * rate);
+ var n = (wav.Length - start) / 2;
+ int from = Math.Max(0, first * size - pad), to = Math.Min(n, (last + 1) * size + pad);
+ var pcm = new short[to - from];
+ Buffer.BlockCopy(wav, start + from * 2, pcm, 0, pcm.Length * 2);
+ return Cicada.Wav.Bytes(pcm, rate);
+ }
+
+ /// The first and last 20 ms windows within 35 dB of the loudest one, and the window size; null
+ /// when it is all quiet.
+ static (int first, int last, int size)? SpeechWindows(float[] samples, double sampleRate)
{
var size = Math.Max(1, (int)(sampleRate * 0.02));
var levels = new List<float>();
@@ -141,9 +167,7 @@ public static class VoicePace
var loudest = levels.Max();
if (loudest <= 0.003f) return null;
var floor = loudest * 0.0178f;
- var first = levels.FindIndex(l => l >= floor);
- var last = levels.FindLastIndex(l => l >= floor);
- return (last - first + 1) * (double)size / sampleRate;
+ return (levels.FindIndex(l => l >= floor), levels.FindLastIndex(l => l >= floor), size);
}
/// Words, and each CJK character on its own (SpeakTest.swift › SpeakScore.tokens).
@@ -181,7 +205,16 @@ public static class VoiceCloneCheck
{
public enum Outcome { Ready, TooShort, NotHeard, Slow }
- public static async Task<(Outcome outcome, string text)> Check(byte[] wav, Func<byte[], Task<string?>> transcribe)
+ /// `wav` comes back trimmed of the quiet at its ends (VoicePace.Trim): that is what is kept, and
+ /// the 10 s minimum is of the reading itself, not of the pause before 读完了.
+ public static async Task<(Outcome outcome, string text, byte[] wav)> Check(byte[] recorded, Func<byte[], Task<string?>> transcribe)
+ {
+ var wav = VoicePace.Trim(recorded);
+ var (outcome, text) = await CheckTrimmed(wav, transcribe);
+ return (outcome, text, wav);
+ }
+
+ static async Task<(Outcome outcome, string text)> CheckTrimmed(byte[] wav, Func<byte[], Task<string?>> transcribe)
{
var seconds = Math.Max(0, wav.Length - 44) / 32000.0;
if (seconds < VoiceReference.MinSeconds) return (Outcome.TooShort, "");
diff --git a/windows/src/Cicada/App.cs b/windows/src/Cicada/App.cs
index bf5f012..b5dff47 100644
--- a/windows/src/Cicada/App.cs
+++ b/windows/src/Cicada/App.cs
@@ -1582,9 +1582,9 @@ static class DebugServer
var target = app.Speak.Target;
_ = Task.Run(async () =>
{
- var (outcome, text) = await VoiceCloneCheck.Check(wav, w => given is not null ? Task.FromResult<string?>(given) : app.Transcribe(w));
+ var (outcome, text, trimmed) = await VoiceCloneCheck.Check(wav, w => given is not null ? Task.FromResult<string?>(given) : app.Transcribe(w));
Log.Debug($"ownvoice: {outcome}");
- if (outcome == VoiceCloneCheck.Outcome.Ready) app.Dispatcher().Invoke(() => app.SaveOwnVoice(wav, text, target));
+ if (outcome == VoiceCloneCheck.Outcome.Ready) app.Dispatcher().Invoke(() => app.SaveOwnVoice(trimmed, text, target));
});
break;
}
diff --git a/windows/src/Cicada/VoiceCloneDialog.cs b/windows/src/Cicada/VoiceCloneDialog.cs
index d6a8e1f..aca3b6d 100644
--- a/windows/src/Cicada/VoiceCloneDialog.cs
+++ b/windows/src/Cicada/VoiceCloneDialog.cs
@@ -160,7 +160,10 @@ sealed class VoiceCloneDialog
seconds = Math.Max(0, recorded.Length - 44) / 32000.0;
phase = Phase.Checking;
Render();
- var (outcome, text) = await VoiceCloneCheck.Check(recorded, app.Transcribe);
+ var (outcome, text, trimmed) = await VoiceCloneCheck.Check(recorded, app.Transcribe);
+ // What is played back, previewed and kept: the reading without the quiet at its ends.
+ wav = trimmed;
+ seconds = Math.Max(0, trimmed.Length - 44) / 32000.0;
transcript = text;
Log.Debug($"voice clone: {outcome}");
switch (outcome)
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index c992156..5de0931 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -3017,8 +3017,32 @@ static class Tests
Equal(VoiceCloneCheck.Outcome.NotHeard, (await VoiceCloneCheck.Check(Spoken(12), _ => Task.FromResult<string?>("嗯"))).outcome, "nothing recognisable");
Equal(VoiceCloneCheck.Outcome.NotHeard, (await VoiceCloneCheck.Check(Spoken(12), _ => throw new LingException(LingException.Kind.Network))).outcome, "the hearing failed");
Equal(VoiceCloneCheck.Outcome.Slow, (await VoiceCloneCheck.Check(Spoken(14), _ => Task.FromResult<string?>("大家好我是主持人今天讨论"))).outcome, "12 characters in 13 s");
- var (ok, text) = await VoiceCloneCheck.Check(Spoken(12), _ => Task.FromResult<string?>(" " + passage + " "));
+ var (ok, text, kept) = await VoiceCloneCheck.Check(Spoken(12), _ => Task.FromResult<string?>(" " + passage + " "));
Equal((VoiceCloneCheck.Outcome.Ready, passage), (ok, text), "an ordinary reading is kept with its words");
+ Equal(11.4, Math.Round((kept.Length - 44) / 32000.0, 2), "and without its quiet ends (11 s read, 0.2 s kept each side)");
+ // 9.5 s read and a 3 s pause before 读完了: 12.5 s recorded, but under 10 s of reading.
+ var paused = Spoken(10.5).Concat(new byte[16000 * 3 * 2]).ToArray();
+ BitConverter.GetBytes(paused.Length - 44).CopyTo(paused, 40);
+ Equal(VoiceCloneCheck.Outcome.TooShort, (await VoiceCloneCheck.Check(paused, _ => Task.FromResult<string?>(passage))).outcome, "the minimum is of the reading");
+ }
+
+ public static void TrimsTheQuietEndsOfTheRecording()
+ {
+ // 1 s quiet, 3 s of reading, 1 s quiet (the pause before 读完了 made Ming's clone silent).
+ var pcm = new short[16000 * 5];
+ for (var i = 16000; i < 16000 * 4; i++) pcm[i] = (short)(Math.Sin(i * 0.05) * 9000);
+ var trimmed = VoicePace.Trim(Wav.Bytes(pcm));
+ Equal(3.4, Math.Round((trimmed.Length - 44) / 32000.0, 2), "the reading and 0.2 s each side");
+ Check(trimmed[..4].SequenceEqual("RIFF"u8.ToArray()) && BitConverter.ToInt32(trimmed, 40) == trimmed.Length - 44, "still a WAV with the right length");
+ Equal(0, (int)BitConverter.ToInt16(trimmed, 44 + 2 * 3199), "quiet kept before the first word");
+ Check(BitConverter.ToInt16(trimmed, 44 + 2 * 3300) != 0, "the reading starts 0.2 s in");
+ var silent = Wav.Bytes(new short[16000]);
+ Check(VoicePace.Trim(silent).SequenceEqual(silent), "all quiet: left as it is");
+ var folder = Path.Combine(Paths.Root, "voice-trim-test");
+ var r = new VoiceReference(new PlainProtector(), folder);
+ r.Save(Wav.Bytes(pcm), "大家好");
+ Equal(3.4, Math.Round(r.Duration, 2), "kept trimmed");
+ r.Remove();
}
public static async Task HearingGoesToTheChosenHearModel()