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.

3.1 KB
Raw Blame
using System.Runtime.InteropServices;

namespace Cicada.App;

sealed class MicSource : IAudioSource
{
    readonly Func<Endpoints.Device?> pick;
    Thread? thread;
    volatile bool running;
    public event Action<short[]>? Samples;
    public event Action<string?>? Ended;
    public string? DeviceName { get; private set; }

    public MicSource(Func<Endpoints.Device?> pick) { this.pick = pick; }

    public void Start()
    {
        running = true;
        thread = new Thread(Run) { IsBackground = true, Name = "MicSource", Priority = ThreadPriority.AboveNormal };
        thread.SetApartmentState(ApartmentState.MTA);
        thread.Start();
    }

    public void Stop()
    {
        running = false;
        thread?.Join(1000);
    }

    void Run()
    {
        int failures = 0;
        while (running)
        {
            if (pick() is not { } device) { Log.Debug("mic: no microphone"); Ended?.Invoke("none"); return; }
            DeviceName = device.Name;
            var started = DateTime.UtcNow;
            try { Capture(device.Id); return; }
            catch (Exception e) when (e.HResult == unchecked((int)0x80070005))
            {
                Log.Debug("mic: denied by Windows' microphone privacy setting");
                Ended?.Invoke("denied");
                return;
            }
            catch (Exception e)
            {
                Log.Debug($"mic: {device.Name} stopped 0x{e.HResult:X8}, picking the room mic again");
                failures = DateTime.UtcNow - started > TimeSpan.FromSeconds(10) ? 1 : failures + 1;
                if (failures > 5) { Ended?.Invoke(e.Message); return; }
                Thread.Sleep(300 * failures);
            }
        }
    }

    void Capture(string id)
    {
        IAudioClient? client = null;
        try
        {
            client = Endpoints.Activate(id);
            var fmt = Endpoints.Pcm16(16000);
            int hr = client.Initialize(0, Endpoints.AutoConvert, 10_000_000, 0, fmt, IntPtr.Zero);
            Marshal.FreeHGlobal(fmt);
            Marshal.ThrowExceptionForHR(hr);
            var iid = typeof(IAudioCaptureClient).GUID;
            Marshal.ThrowExceptionForHR(client.GetService(ref iid, out var s));
            var cap = (IAudioCaptureClient)s;
            Marshal.ThrowExceptionForHR(client.Start());
            Log.Debug($"mic: capturing {DeviceName}");
            while (running)
            {
                Thread.Sleep(20);
                while (true)
                {
                    Marshal.ThrowExceptionForHR(cap.GetNextPacketSize(out var n));
                    if (n == 0) break;
                    Marshal.ThrowExceptionForHR(cap.GetBuffer(out var data, out var frames, out var flags, out _, out _));
                    var buf = new short[frames];
                    if ((flags & 0x2) == 0) Marshal.Copy(data, buf, 0, (int)frames);
                    cap.ReleaseBuffer(frames);
                    if (frames > 0) Samples?.Invoke(buf);
                }
            }
            client.Stop();
        }
        finally { if (client is not null) Marshal.ReleaseComObject(client); }
    }
}