diff --git a/ButterSTT/AudioUtils.cs b/ButterSTT/AudioUtils.cs new file mode 100644 index 0000000..077a805 --- /dev/null +++ b/ButterSTT/AudioUtils.cs @@ -0,0 +1,65 @@ +using NAudio.CoreAudioApi; +using NAudio.Wave; + +namespace ButterSTT +{ + public static class AudioUtils + { + public static IEnumerable<( + int index, + WaveInCapabilities device, + MMDevice? mmDevice + )> EnumerateWaveInDevices() + { + using var enumerator = new MMDeviceEnumerator(); + for (var i = -1; i < WaveInEvent.DeviceCount; i++) + { + (int index, WaveInCapabilities device, MMDevice? mmDevice) device = ( + i, + WaveInEvent.GetCapabilities(i), + null + ); + + if (i >= 0) + { + var mmDevices = enumerator.EnumerateAudioEndPoints( + DataFlow.Capture, + DeviceState.Active + ); + + // Try the most likely index first, this makes it more likely to get the + // correct name if multiple are named only slightly differently + if (mmDevices.Count > i) + { + try + { + var likelyDevice = mmDevices[i]; + if (likelyDevice.FriendlyName.StartsWith(device.device.ProductName)) + device.mmDevice = likelyDevice; + } + catch { } + } + + // If the device isn't in the same index, search all devices + if (device.mmDevice == null) + { + foreach (var mmDevice in mmDevices) + { + try + { + if (mmDevice.FriendlyName.StartsWith(device.device.ProductName)) + { + device.mmDevice = mmDevice; + break; + } + } + catch { } + } + } + } + + yield return device; + } + } + } +} diff --git a/ButterSTT/Constants.cs b/ButterSTT/Constants.cs new file mode 100644 index 0000000..f57bd9e --- /dev/null +++ b/ButterSTT/Constants.cs @@ -0,0 +1,9 @@ +namespace ButterSTT +{ + public static class Constants + { + public static readonly string Name = "ButterSTT"; + public static readonly string Version = "0.2.3"; + public static readonly string Url = "https://github.com/ButterscotchV/ButterSTT"; + } +} diff --git a/ButterSTT/Program.cs b/ButterSTT/Program.cs index 0193e96..7482bb4 100644 --- a/ButterSTT/Program.cs +++ b/ButterSTT/Program.cs @@ -4,6 +4,18 @@ try { + // Print program info + Console.WriteLine($"Starting {Constants.Name} v{Constants.Version} ({Constants.Url})...\n"); + + // Print available audio devices + var audioDevices = string.Join( + '\n', + AudioUtils + .EnumerateWaveInDevices() + .Select(d => $" - [{d.index}]: {d.mmDevice?.FriendlyName ?? d.device.ProductName}") + ); + Console.WriteLine($"Available audio devices:\n{audioDevices}\n"); + // Load config JsonConfigHandler configHandler = new("config.json", JsonConfigHandler.Context.STTConfig);