Skip to content

Commit

Permalink
revives naudio
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Jul 27, 2024
1 parent 75d2621 commit 41f198b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 8 deletions.
116 changes: 116 additions & 0 deletions OpenUtau.Core/Audio/NAudioOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using NAudio.Wave;
using OpenUtau.Core.Util;

namespace OpenUtau.Audio {
#if !WINDOWS
public class NAudioOutput : DummyAudioOutput { }
#else
public class NAudioOutput : IAudioOutput {
const int Channels = 2;

private readonly object lockObj = new object();
private WaveOutEvent waveOutEvent;
private int deviceNumber;

public NAudioOutput() {
if (Guid.TryParse(Preferences.Default.PlaybackDevice, out var guid)) {
SelectDevice(guid, Preferences.Default.PlaybackDeviceNumber);
} else {
SelectDevice(new Guid(), 0);
}
}

public PlaybackState PlaybackState {
get {
lock (lockObj) {
return waveOutEvent == null ? PlaybackState.Stopped : waveOutEvent.PlaybackState;
}
}
}

public int DeviceNumber => deviceNumber;

public long GetPosition() {
lock (lockObj) {
return waveOutEvent == null
? 0
: waveOutEvent.GetPosition() / Channels;
}
}

public void Init(ISampleProvider sampleProvider) {
lock (lockObj) {
if (waveOutEvent != null) {
waveOutEvent.Stop();
waveOutEvent.Dispose();
}
waveOutEvent = new WaveOutEvent() {
DeviceNumber = deviceNumber,
};
waveOutEvent.Init(sampleProvider);
}
}

public void Pause() {
lock (lockObj) {
if (waveOutEvent != null) {
waveOutEvent.Pause();
}
}
}

public void Play() {
lock (lockObj) {
if (waveOutEvent != null) {
waveOutEvent.Play();
}
}
}

public void Stop() {
lock (lockObj) {
if (waveOutEvent != null) {
waveOutEvent.Stop();
waveOutEvent.Dispose();
waveOutEvent = null;
}
}
}

public void SelectDevice(Guid guid, int deviceNumber) {
Preferences.Default.PlaybackDevice = guid.ToString();
Preferences.Default.PlaybackDeviceNumber = deviceNumber;
Preferences.Save();
// Product guid may not be unique. Use device number first.
if (deviceNumber < WaveOut.DeviceCount && WaveOut.GetCapabilities(deviceNumber).ProductGuid == guid) {
this.deviceNumber = deviceNumber;
return;
}
// If guid does not match, device number may have changed. Search guid instead.
this.deviceNumber = 0;
for (int i = 0; i < WaveOut.DeviceCount; ++i) {
if (WaveOut.GetCapabilities(i).ProductGuid == guid) {
this.deviceNumber = i;
break;
}
}
}

public List<AudioOutputDevice> GetOutputDevices() {
var outDevices = new List<AudioOutputDevice>();
for (int i = 0; i < WaveOut.DeviceCount; ++i) {
var capability = WaveOut.GetCapabilities(i);
outDevices.Add(new AudioOutputDevice {
api = "WaveOut",
name = capability.ProductName,
deviceNumber = i,
guid = capability.ProductGuid,
});
}
return outDevices;
}
}
#endif
}
4 changes: 4 additions & 0 deletions OpenUtau.Core/OpenUtau.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<PackageReference Include="NetMQ" Version="4.0.1.13" />
</ItemGroup>
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.15.0" />
</ItemGroup>
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'false'">
Expand Down Expand Up @@ -67,4 +68,7 @@
<LastGenOutput>VogenRes.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">
<DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup>
</Project>
9 changes: 7 additions & 2 deletions OpenUtau.Test/OpenUtau.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFramework>net6.0-windows</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
16 changes: 12 additions & 4 deletions OpenUtau/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ public static void InitOpenUtau() {

public static void InitAudio() {
Log.Information("Initializing audio.");
try {
PlaybackManager.Inst.AudioOutput = new Audio.MiniAudioOutput();
} catch (Exception e) {
Log.Error(e, "Failed to Initialize audio");
if (!OS.IsWindows() || Core.Util.Preferences.Default.PreferPortAudio) {
try {
PlaybackManager.Inst.AudioOutput = new Audio.MiniAudioOutput();
} catch (Exception e1) {
Log.Error(e1, "Failed to init MiniAudio");
}
} else {
try {
PlaybackManager.Inst.AudioOutput = new Audio.NAudioOutput();
} catch (Exception e2) {
Log.Error(e2, "Failed to init NAudio");
}
}
Log.Information("Initialized audio.");
}
Expand Down
9 changes: 7 additions & 2 deletions OpenUtau/OpenUtau.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFramework>net6.0-windows</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
<Nullable>enable</Nullable>
Expand Down

0 comments on commit 41f198b

Please sign in to comment.