Skip to content

Commit 41f198b

Browse files
committed
revives naudio
1 parent 75d2621 commit 41f198b

File tree

5 files changed

+146
-8
lines changed

5 files changed

+146
-8
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

OpenUtau.Core/OpenUtau.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<PackageReference Include="NetMQ" Version="4.0.1.13" />
3232
</ItemGroup>
3333
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">
34+
<PackageReference Include="NAudio" Version="2.2.1" />
3435
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.15.0" />
3536
</ItemGroup>
3637
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'false'">
@@ -67,4 +68,7 @@
6768
<LastGenOutput>VogenRes.Designer.cs</LastGenOutput>
6869
</EmbeddedResource>
6970
</ItemGroup>
71+
<PropertyGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">
72+
<DefineConstants>WINDOWS</DefineConstants>
73+
</PropertyGroup>
7074
</Project>

OpenUtau.Test/OpenUtau.Test.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
3+
<TargetFramework>net6.0-windows</TargetFramework>
4+
</PropertyGroup>
5+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
36
<TargetFramework>net6.0</TargetFramework>
7+
</PropertyGroup>
8+
<PropertyGroup>
49
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
510
<IsPackable>false</IsPackable>
611
</PropertyGroup>

OpenUtau/App.axaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,18 @@ public static void InitOpenUtau() {
124124

125125
public static void InitAudio() {
126126
Log.Information("Initializing audio.");
127-
try {
128-
PlaybackManager.Inst.AudioOutput = new Audio.MiniAudioOutput();
129-
} catch (Exception e) {
130-
Log.Error(e, "Failed to Initialize audio");
127+
if (!OS.IsWindows() || Core.Util.Preferences.Default.PreferPortAudio) {
128+
try {
129+
PlaybackManager.Inst.AudioOutput = new Audio.MiniAudioOutput();
130+
} catch (Exception e1) {
131+
Log.Error(e1, "Failed to init MiniAudio");
132+
}
133+
} else {
134+
try {
135+
PlaybackManager.Inst.AudioOutput = new Audio.NAudioOutput();
136+
} catch (Exception e2) {
137+
Log.Error(e2, "Failed to init NAudio");
138+
}
131139
}
132140
Log.Information("Initialized audio.");
133141
}

OpenUtau/OpenUtau.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
3+
<TargetFramework>net6.0-windows</TargetFramework>
4+
</PropertyGroup>
5+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
36
<TargetFramework>net6.0</TargetFramework>
7+
</PropertyGroup>
8+
<PropertyGroup>
49
<OutputType>WinExe</OutputType>
510
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
611
<Nullable>enable</Nullable>

0 commit comments

Comments
 (0)