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): with the microphone open, the meeting recording keeps your own voice too (#19)

wquguru677d2c756e5e
diff --git a/site/changelog.json b/site/changelog.json
index 9020d3b..937a149 100644
--- a/site/changelog.json
+++ b/site/changelog.json
@@ -10,6 +10,7 @@
"kind": "fixed",
"platforms": [
"mac",
+ "windows",
"linux"
],
"title": "打开麦克风时,会议录音里也有自己的声音。",
diff --git a/windows/src/Cicada.Cli/Program.cs b/windows/src/Cicada.Cli/Program.cs
index 2be5b15..f0472bb 100644
--- a/windows/src/Cicada.Cli/Program.cs
+++ b/windows/src/Cicada.Cli/Program.cs
@@ -143,7 +143,7 @@ switch (args_[0])
mic.Ended += _ => done.TrySetResult();
roomEnded = done.Task;
roomEngine.Changed += show;
- roomEngine.Join(mic, engine.SessionId!, store.Get(engine.SessionId!)!.StartedAt);
+ roomEngine.Join(mic, engine.SessionId!, store.Get(engine.SessionId!)!.StartedAt, engine.Mix);
}
await Task.WhenAll(ended.Task, roomEnded);
await Task.Delay(TimeSpan.FromSeconds(1.5 + (roomEngine is null ? 0 : EchoMatch.Hold)));
diff --git a/windows/src/Cicada.Core/ListenEngine.cs b/windows/src/Cicada.Core/ListenEngine.cs
index 9d447ec..7c4890c 100644
--- a/windows/src/Cicada.Core/ListenEngine.cs
+++ b/windows/src/Cicada.Core/ListenEngine.cs
@@ -23,6 +23,10 @@ public sealed class ListenEngine
long began;
/// 保存会议录音: the main side writes what it hears to the meeting's system.aac.
IAudioWriter? recorder;
+ /// The main side's recording, which 电脑和会议室's room side adds itself into (`Join`).
+ public RoomMix? Mix => recorder as RoomMix;
+ /// The room side: the main side's recording, where what the room mic hears goes too.
+ RoomMix? roomMix;
Task? worker;
Segmenter? segmenter;
readonly Dictionary<int, Heard?> done = [];
@@ -157,7 +161,7 @@ public sealed class ListenEngine
try
{
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
- recorder = files.Open(path);
+ recorder = files.Open(path) is { } file ? new RoomMix(file) : null;
}
catch (Exception e) { Log.Debug($"listen: recording not started {e.GetType().Name}"); }
if (recorder is null) Log.Debug("listen: no recording, the encoder refused");
@@ -170,11 +174,13 @@ public sealed class ListenEngine
Changed?.Invoke();
}
- /// The room side joins the meeting `sessionId`, which began at `startedAt`.
- public void Join(IAudioSource mic, string sessionId, DateTimeOffset startedAt)
+ /// The room side joins the meeting `sessionId`, which began at `startedAt`; what it hears goes
+ /// into the meeting's recording `mix` (the main side's `Mix`) too.
+ public void Join(IAudioSource mic, string sessionId, DateTimeOffset startedAt, RoomMix? mix = null)
{
if (Listening || !RoomSide) return;
SessionId = sessionId;
+ roomMix = mix;
Room = true;
store.MarkRoom(sessionId);
Begin(mic, true, Math.Max(0, (DateTimeOffset.Now - startedAt).TotalSeconds));
@@ -214,10 +220,10 @@ public sealed class ListenEngine
{
var silence = new short[gap];
segmenter.Feed(silence);
- Record(silence);
+ Record(silence, fed);
fed += gap;
}
- Record(buf);
+ Record(buf, fed);
fed += buf.Length;
Level = Rms(buf);
// The computer is playing speech: the room mic would hear it from the speakers.
@@ -254,8 +260,14 @@ public sealed class ListenEngine
void OnSamples(short[] buf) => audio?.Writer.TryWrite((buf, System.Diagnostics.Stopwatch.GetElapsedTime(began).TotalSeconds));
- void Record(short[] pcm)
+ /// `fed`: samples this side gave before these, so the room's land at their place in the meeting.
+ void Record(short[] pcm, long fed)
{
+ if (RoomSide)
+ {
+ roomMix?.Room(pcm, (long)Math.Round(offset * Segmenter.Rate) + fed);
+ return;
+ }
if (recorder is null) return;
try { recorder.Write(pcm); }
catch (Exception e)
@@ -309,6 +321,7 @@ public sealed class ListenEngine
Task[] waiting;
lock (gate) waiting = inFlight.ToArray();
CloseRecording();
+ roomMix = null;
await Task.WhenAny(Task.WhenAll(waiting), Task.Delay(15_000));
var id = SessionId!;
if (RoomSide)
diff --git a/windows/src/Cicada.Core/Recording.cs b/windows/src/Cicada.Core/Recording.cs
index 7f3d53f..4852245 100644
--- a/windows/src/Cicada.Core/Recording.cs
+++ b/windows/src/Cicada.Core/Recording.cs
@@ -20,6 +20,98 @@ public interface IAudioWriter
void Close();
}
+/// 电脑和会议室 keeps one recording, as the Mac does: the room side's samples are added into the
+/// PC's at the same place on the meeting's clock (each side counts the samples it gave, gaps
+/// filled). The PC's audio waits `Delay` before it is written, so a room buffer that arrives a
+/// little after the PC's is still mixed in; what the room says after the PC's last sample follows.
+public sealed class RoomMix(IAudioWriter writer) : IAudioWriter
+{
+ const int Delay = Segmenter.Rate; // 1 s
+ const int Limit = 30 * Segmenter.Rate; // the PC's audio stalled: older room audio goes
+ readonly object gate = new();
+ readonly List<short> pc = [], room = [];
+ long at, roomAt; // where on the clock pc[0] and room[0] are (everything before `at` is written)
+ bool closed;
+
+ /// The PC's audio (the main side), in order.
+ public void Write(ReadOnlySpan<short> pcm)
+ {
+ short[] output;
+ lock (gate)
+ {
+ if (closed) return;
+ pc.AddRange(pcm);
+ output = Take(pc.Count - Delay);
+ }
+ if (output.Length > 0) writer.Write(output);
+ }
+
+ /// The room side's audio; `start`: its first sample's place on the clock.
+ public void Room(ReadOnlySpan<short> pcm, long start)
+ {
+ lock (gate)
+ {
+ if (closed) return;
+ var end = roomAt + room.Count;
+ if (room.Count == 0) roomAt = start;
+ else if (start > end) room.AddRange(new short[start - end]); // the room side stopped a while
+ else if (start < end) pcm = pcm[(int)Math.Min(end - start, pcm.Length)..];
+ room.AddRange(pcm);
+ Drop(at);
+ if (room.Count > Limit) { roomAt += room.Count - Limit; room.RemoveRange(0, room.Count - Limit); }
+ }
+ }
+
+ /// Everything still held goes into the file, then the file closes.
+ public void Close()
+ {
+ List<short> output;
+ lock (gate)
+ {
+ if (closed) return;
+ closed = true;
+ output = [.. Take(pc.Count)];
+ if (room.Count > 0 && roomAt >= at)
+ {
+ output.AddRange(new short[roomAt - at]);
+ output.AddRange(room);
+ room.Clear();
+ }
+ }
+ if (output.Count > 0) writer.Write(output.ToArray());
+ writer.Close();
+ }
+
+ /// Room audio for a place already written: too late to mix.
+ void Drop(long upto)
+ {
+ var late = (int)Math.Min(room.Count, upto - roomAt);
+ if (late <= 0) return;
+ room.RemoveRange(0, late);
+ roomAt += late;
+ }
+
+ /// The next `n` samples of the PC's audio with the room's added.
+ short[] Take(int n)
+ {
+ if (n <= 0) return [];
+ var output = pc.GetRange(0, n).ToArray();
+ pc.RemoveRange(0, n);
+ var start = at;
+ at += n;
+ Drop(start);
+ if (room.Count > 0 && roomAt < at)
+ {
+ var off = (int)(roomAt - start);
+ var k = (int)Math.Min(room.Count, at - roomAt);
+ for (int i = 0; i < k; i++) output[off + i] = (short)Math.Clamp(output[off + i] + room[i], short.MinValue, short.MaxValue);
+ room.RemoveRange(0, k);
+ roomAt += k;
+ }
+ return output;
+ }
+}
+
public static class Recording
{
public static string SystemAudio(string sessionId) => Path.Combine(Paths.Sessions, sessionId, "system.m4a");
diff --git a/windows/src/Cicada/App.cs b/windows/src/Cicada/App.cs
index a500e02..61a080a 100644
--- a/windows/src/Cicada/App.cs
+++ b/windows/src/Cicada/App.cs
@@ -452,7 +452,7 @@ sealed class Controller
});
};
HybridMicDenied = false;
- RoomListen.Join(mic, id, session.StartedAt);
+ RoomListen.Join(mic, id, session.StartedAt, Listen.Mix);
Log.Debug("hybrid: room side started");
}
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index 3dc4d4f..5a12790 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -417,6 +417,58 @@ static class Tests
public void Stop() { }
}
+ static short Loudest(List<short> pcm, double from, double to)
+ {
+ short m = 0;
+ for (int i = (int)(from * 16000); i < Math.Min(pcm.Count, (int)(to * 16000)); i++) m = Math.Max(m, Math.Abs(pcm[i]));
+ return m;
+ }
+
+ /// 电脑和会议室: the room mic goes into the meeting's one recording at its place on the clock (#19).
+ public static void TheRoomIsMixedIntoTheRecordingWhereItWasHeard()
+ {
+ var audio = new FakeAudio();
+ var mix = new RoomMix(audio.Open(Path.Combine(Path.GetTempPath(), $"mix-{Guid.NewGuid()}.aac"))!);
+ for (int i = 0; i < 30; i++) // 3 s of the PC at 1000; the room at 500 from 1 s to 2 s, half a second late
+ {
+ mix.Write(Enumerable.Repeat((short)1000, 1600).ToArray());
+ if (i >= 15 && i < 25) mix.Room(Enumerable.Repeat((short)500, 1600).ToArray(), (i - 5) * 1600);
+ }
+ mix.Write(Enumerable.Repeat((short)30000, 1600).ToArray());
+ mix.Room(Enumerable.Repeat((short)30000, 1600).ToArray(), 3 * 16000);
+ mix.Room(Enumerable.Repeat((short)30000, 1600).ToArray(), 4 * 16000); // after the PC's last sample
+ mix.Close();
+ Check(audio.Closed, "closed");
+ Equal(4 * 16000 + 1600, audio.Samples.Count, "the PC's 3.1 s, then silence up to the room's last words");
+ Equal((short)1000, Loudest(audio.Samples, 0, 1), "the PC alone");
+ Equal((short)1500, audio.Samples.Skip(16000).Take(16000).Min(), "both, every sample of that second");
+ Equal((short)1000, Loudest(audio.Samples, 2, 3), "the PC alone again");
+ Equal(short.MaxValue, audio.Samples[3 * 16000], "loud on loud clips instead of wrapping");
+ Equal((short)0, Loudest(audio.Samples, 3.2, 4), "nothing between");
+ Equal((short)30000, audio.Samples[^1], "the room after the PC ended");
+ }
+
+ public static async Task BothSidesOfAMeetingGoIntoItsRecording()
+ {
+ var ling = Client(new Fake(_ => Reply("{}")));
+ var audio = new FakeAudio();
+ var store = new SessionStore { Audio = audio };
+ var settings = new AppSettings { Target = "zh-CN", SpeakerVoices = false };
+ var main = new ListenEngine(ling, new Translator(ling), store, new NotesEngine(ling, store), settings);
+ var room = new ListenEngine(ling, new Translator(ling), store, new NotesEngine(ling, store), settings, roomSide: true);
+ var pc = new GappySource((0.5, true), (2.5, false));
+ var mic = new GappySource((2.0, true)); // a mic sends all the time, quiet or not
+ var ended = new TaskCompletionSource();
+ pc.Ended += _ => ended.TrySetResult();
+ main.Start(pc);
+ room.Join(mic, main.SessionId!, store.Get(main.SessionId!)!.StartedAt, main.Mix);
+ await ended.Task;
+ await room.Stop();
+ await main.Stop();
+ Check(audio.Closed, "closed when the meeting ends");
+ Check(Loudest(audio.Samples, 1.0, 1.8) > 0, "the room's sound, after the PC's ended, is in the recording");
+ }
+
public static void SilenceGoesInOnlyOnceTheStreamIsASecondBehind()
{
Equal(0, Recording.Behind(0.5, 0, 1600), "a packet a little late: nothing");