Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OpenUtau.Core/Audio/DummyAudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OpenUtau.Audio {
public class DummyAudioOutput : IAudioOutput {
public PlaybackState PlaybackState => PlaybackState.Stopped;
public int DeviceNumber => 0;
public event EventHandler DevicesChanged;
public List<AudioOutputDevice> GetOutputDevices() => new List<AudioOutputDevice>();
public long GetPosition() => 0;
public void Init(ISampleProvider sampleProvider) { }
Expand Down
3 changes: 3 additions & 0 deletions OpenUtau.Core/Audio/IAudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public interface IAudioOutput {
PlaybackState PlaybackState { get; }
int DeviceNumber { get; }

// Raised when available output devices list changes (plug/unplug).
event EventHandler DevicesChanged;

void SelectDevice(Guid guid, int deviceNumber);
void Init(ISampleProvider sampleProvider);
void Pause();
Expand Down
57 changes: 53 additions & 4 deletions OpenUtau.Core/Audio/MiniAudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System.Threading;
using OpenUtau.Core.Util;
using Serilog;

Expand All @@ -20,12 +21,24 @@ public class MiniAudioOutput : IAudioOutput, IDisposable {
private bool eof;

private List<AudioOutputDevice> devices = new List<AudioOutputDevice>();
private readonly object devicesLock = new object();
private Timer devicePollTimer = null;
public event EventHandler DevicesChanged;
private IntPtr callbackPtr = IntPtr.Zero;
private IntPtr nativeContext = IntPtr.Zero;
private Guid selectedDevice = Guid.Empty;

public MiniAudioOutput() {
UpdateDeviceList();
// Start device polling timer to detect plug/unplug on platforms
// where native library does not emit events.
devicePollTimer = new Timer(_ => {
try {
PollDeviceChange();
} catch (Exception e) {
Log.Warning(e, "Device poll error");
}
}, null, 2000, 2000);
unsafe {
var f = (ou_audio_data_callback_t)DataCallback;
GCHandle.Alloc(f);
Expand All @@ -49,9 +62,8 @@ public MiniAudioOutput() {
}
}
}

private void UpdateDeviceList() {
devices.Clear();
private List<AudioOutputDevice> GetDeviceListFromNative() {
var list = new List<AudioOutputDevice>();
unsafe {
const int kMaxCount = 128;
ou_audio_device_info_t* device_infos = stackalloc ou_audio_device_info_t[kMaxCount];
Expand All @@ -73,7 +85,7 @@ private void UpdateDeviceList() {
string name = (OS.IsWindows() && api != "WASAPI")
? Marshal.PtrToStringAnsi(device_infos[i].name)
: Marshal.PtrToStringUTF8(device_infos[i].name);
devices.Add(new AudioOutputDevice {
list.Add(new AudioOutputDevice {
name = name,
api = api,
deviceNumber = i,
Expand All @@ -82,6 +94,37 @@ private void UpdateDeviceList() {
}
ou_free_audio_device_infos(device_infos, count);
}
return list;
}

private void UpdateDeviceList() {
var newList = GetDeviceListFromNative();
lock (devicesLock) {
devices = newList;
}
}

private void PollDeviceChange() {
var newList = GetDeviceListFromNative();
bool changed = false;
lock (devicesLock) {
if (newList.Count != devices.Count) {
changed = true;
} else {
for (int i = 0; i < newList.Count; i++) {
if (newList[i].guid != devices[i].guid || newList[i].name != devices[i].name) {
changed = true;
break;
}
}
}
if (changed) {
devices = newList;
}
}
if (changed) {
DevicesChanged?.Invoke(this, EventArgs.Empty);
}
}

public void Init(ISampleProvider sampleProvider) {
Expand Down Expand Up @@ -234,6 +277,12 @@ protected virtual void Dispose(bool disposing) {
}

// free unmanaged resources (unmanaged objects) and override finalizer
if (devicePollTimer != null) {
try {
devicePollTimer.Dispose();
} catch { }
devicePollTimer = null;
}
if (nativeContext != IntPtr.Zero) {
ou_free_audio_device(nativeContext);
nativeContext = IntPtr.Zero;
Expand Down
1 change: 1 addition & 0 deletions OpenUtau/NAudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace OpenUtau.App {
public class NAudioOutput : DummyAudioOutput { }
#else
public class NAudioOutput : IAudioOutput {
public event EventHandler DevicesChanged;
const int Channels = 2;

private readonly object lockObj = new object();
Expand Down
17 changes: 17 additions & 0 deletions OpenUtau/ViewModels/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ public PreferencesViewModel() {
if (device != null) {
AudioOutputDevice = device;
}
// Subscribe to device list changes to refresh UI when devices are plugged/unplugged.
try {
audioOutput.DevicesChanged += (s, e) => {
Avalonia.Threading.Dispatcher.UIThread.Post(() => {
try {
AudioOutputDevices = PlaybackManager.Inst.AudioOutput.GetOutputDevices();
int curDeviceNumber = PlaybackManager.Inst.AudioOutput.DeviceNumber;
var cur = AudioOutputDevices.FirstOrDefault(d => d.deviceNumber == curDeviceNumber);
if (cur != null) {
AudioOutputDevice = cur;
}
} catch (Exception ex) {
Log.Warning(ex, "Failed to update audio device list on DevicesChanged");
}
});
};
} catch { }
}
PreferPortAudio = Preferences.Default.PreferPortAudio ? 1 : 0;
PlaybackAutoScroll = Preferences.Default.PlaybackAutoScroll;
Expand Down