Skip to content

Commit

Permalink
Add startup info and microphone device listing
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Feb 1, 2024
1 parent 09961d9 commit e3446e6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
65 changes: 65 additions & 0 deletions ButterSTT/AudioUtils.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
9 changes: 9 additions & 0 deletions ButterSTT/Constants.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
12 changes: 12 additions & 0 deletions ButterSTT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<STTConfig> configHandler =
new("config.json", JsonConfigHandler<STTConfig>.Context.STTConfig);
Expand Down

0 comments on commit e3446e6

Please sign in to comment.