Skip to content

Commit

Permalink
Update to save/look up by device id
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEqualsCoder committed Jan 7, 2024
1 parent 94f94f1 commit b4eaf49
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Randomizer.App/Windows/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ComboBox SelectedItem="{Binding PushToTalkDevice}"
<ComboBox SelectedValue="{Binding PushToTalkDevice}"
ItemsSource="{Binding PushToTalkDevices, ElementName=Self}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
MinWidth="75">
</ComboBox>
</Grid>
Expand Down
18 changes: 16 additions & 2 deletions src/Randomizer.App/Windows/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public partial class OptionsWindow : Window, INotifyPropertyChanged
private GeneralOptions _options = new();
private bool _canLogIn = true;
private ICollection<string> _availableProfiles;

private Dictionary<string, string> _availableInputDevices = new() { { "Default", "Default" } };
private SourceRomValidationService _sourceRomValidationService;

public OptionsWindow(IChatAuthenticationService chatAuthenticationService,
Expand All @@ -41,6 +43,11 @@ public OptionsWindow(IChatAuthenticationService chatAuthenticationService,
IGitHubSpriteDownloaderService gitHubSpriteDownloaderService,
IMicrophoneService microphoneService)
{
foreach (var device in microphoneService.GetDeviceDetails())
{
_availableInputDevices[device.Key] = device.Value;
}

InitializeComponent();
_trackerConfigProvider = configProvider;
_chatAuthenticationService = chatAuthenticationService;
Expand All @@ -49,7 +56,6 @@ public OptionsWindow(IChatAuthenticationService chatAuthenticationService,
_gitHubConfigDownloaderService = gitHubConfigDownloaderService;
_gitHubSpriteDownloaderService = gitHubSpriteDownloaderService;
_availableProfiles = configProvider.GetAvailableProfiles();
PushToTalkDevices.AddRange(microphoneService.GetDeviceNames());
PropertyChanged?.Invoke(this, new(nameof(DisabledProfiles)));
}

Expand Down Expand Up @@ -86,7 +92,15 @@ public bool IsLoggingIn

public bool IsValidToken => !string.IsNullOrEmpty(Options.TwitchOAuthToken);

public List<string> PushToTalkDevices { get; set; } = new() { "Default" };
public Dictionary<string, string> PushToTalkDevices
{
get => _availableInputDevices;
set
{
_availableInputDevices = value;
PropertyChanged?.Invoke(this, new(nameof(PushToTalkDevices)));
}
}

public ICollection<string> AvailableProfiles
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IMicrophoneService : IDisposable

Stream? StopRecording();

ICollection<string> GetDeviceNames();
Dictionary<string, string> GetDeviceDetails();

string? DesiredAudioDevice { get; set; }
}
14 changes: 7 additions & 7 deletions src/Randomizer.SMZ3.Tracking/Services/MicrophoneService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,36 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public ICollection<string> GetDeviceNames()
public Dictionary<string, string> GetDeviceDetails()
{
var toReturn = new List<string>();
var toReturn = new Dictionary<string, string>();
var enumerator = new MMDeviceEnumerator();
foreach (var wasapi in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
toReturn.Add(wasapi.DeviceFriendlyName);
toReturn[wasapi.ID] = wasapi.DeviceFriendlyName;
}

return toReturn;
}

protected virtual MMDevice? GetInputDeviceByName(string? deviceName)
protected virtual MMDevice? GetInputDeviceById(string? id)
{
if (string.IsNullOrEmpty(deviceName) || "Default" == deviceName)
if (string.IsNullOrEmpty(id) || "Default" == id)
{
return WasapiCapture.GetDefaultCaptureDevice();
}

var enumerator = new MMDeviceEnumerator();
return enumerator
.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active)
.FirstOrDefault(wasapi => wasapi.DeviceFriendlyName == deviceName);
.FirstOrDefault(wasapi => wasapi.ID == id);
}

protected virtual MMDevice? GetInputDevice()
{
try
{
return GetInputDeviceByName(DesiredAudioDevice) ?? WasapiCapture.GetDefaultCaptureDevice();
return GetInputDeviceById(DesiredAudioDevice) ?? WasapiCapture.GetDefaultCaptureDevice();
}
catch (Exception e)
{
Expand Down

0 comments on commit b4eaf49

Please sign in to comment.