|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using NAudio.Wave; |
| 4 | +using OpenUtau.Core.Util; |
| 5 | + |
| 6 | +namespace OpenUtau.Audio { |
| 7 | +#if !WINDOWS |
| 8 | + public class NAudioOutput : DummyAudioOutput { } |
| 9 | +#else |
| 10 | + public class NAudioOutput : IAudioOutput { |
| 11 | + const int Channels = 2; |
| 12 | + |
| 13 | + private readonly object lockObj = new object(); |
| 14 | + private WaveOutEvent waveOutEvent; |
| 15 | + private int deviceNumber; |
| 16 | + |
| 17 | + public NAudioOutput() { |
| 18 | + if (Guid.TryParse(Preferences.Default.PlaybackDevice, out var guid)) { |
| 19 | + SelectDevice(guid, Preferences.Default.PlaybackDeviceNumber); |
| 20 | + } else { |
| 21 | + SelectDevice(new Guid(), 0); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public PlaybackState PlaybackState { |
| 26 | + get { |
| 27 | + lock (lockObj) { |
| 28 | + return waveOutEvent == null ? PlaybackState.Stopped : waveOutEvent.PlaybackState; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public int DeviceNumber => deviceNumber; |
| 34 | + |
| 35 | + public long GetPosition() { |
| 36 | + lock (lockObj) { |
| 37 | + return waveOutEvent == null |
| 38 | + ? 0 |
| 39 | + : waveOutEvent.GetPosition() / Channels; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void Init(ISampleProvider sampleProvider) { |
| 44 | + lock (lockObj) { |
| 45 | + if (waveOutEvent != null) { |
| 46 | + waveOutEvent.Stop(); |
| 47 | + waveOutEvent.Dispose(); |
| 48 | + } |
| 49 | + waveOutEvent = new WaveOutEvent() { |
| 50 | + DeviceNumber = deviceNumber, |
| 51 | + }; |
| 52 | + waveOutEvent.Init(sampleProvider); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void Pause() { |
| 57 | + lock (lockObj) { |
| 58 | + if (waveOutEvent != null) { |
| 59 | + waveOutEvent.Pause(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void Play() { |
| 65 | + lock (lockObj) { |
| 66 | + if (waveOutEvent != null) { |
| 67 | + waveOutEvent.Play(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void Stop() { |
| 73 | + lock (lockObj) { |
| 74 | + if (waveOutEvent != null) { |
| 75 | + waveOutEvent.Stop(); |
| 76 | + waveOutEvent.Dispose(); |
| 77 | + waveOutEvent = null; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void SelectDevice(Guid guid, int deviceNumber) { |
| 83 | + Preferences.Default.PlaybackDevice = guid.ToString(); |
| 84 | + Preferences.Default.PlaybackDeviceNumber = deviceNumber; |
| 85 | + Preferences.Save(); |
| 86 | + // Product guid may not be unique. Use device number first. |
| 87 | + if (deviceNumber < WaveOut.DeviceCount && WaveOut.GetCapabilities(deviceNumber).ProductGuid == guid) { |
| 88 | + this.deviceNumber = deviceNumber; |
| 89 | + return; |
| 90 | + } |
| 91 | + // If guid does not match, device number may have changed. Search guid instead. |
| 92 | + this.deviceNumber = 0; |
| 93 | + for (int i = 0; i < WaveOut.DeviceCount; ++i) { |
| 94 | + if (WaveOut.GetCapabilities(i).ProductGuid == guid) { |
| 95 | + this.deviceNumber = i; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public List<AudioOutputDevice> GetOutputDevices() { |
| 102 | + var outDevices = new List<AudioOutputDevice>(); |
| 103 | + for (int i = 0; i < WaveOut.DeviceCount; ++i) { |
| 104 | + var capability = WaveOut.GetCapabilities(i); |
| 105 | + outDevices.Add(new AudioOutputDevice { |
| 106 | + api = "WaveOut", |
| 107 | + name = capability.ProductName, |
| 108 | + deviceNumber = i, |
| 109 | + guid = capability.ProductGuid, |
| 110 | + }); |
| 111 | + } |
| 112 | + return outDevices; |
| 113 | + } |
| 114 | + } |
| 115 | +#endif |
| 116 | +} |
0 commit comments