Skip to content

Commit

Permalink
feature: Output API setting
Browse files Browse the repository at this point in the history
  • Loading branch information
geloczi committed Apr 1, 2023
1 parent ef4c825 commit 6bd65d7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MusicPlayback/Mp3StreamPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace MusicPlayback
{
public class Mp3StreamPlayer : WavStreamPlayer
{
public Mp3StreamPlayer(int bufferSizeInSeconds)
: base(bufferSizeInSeconds)
public Mp3StreamPlayer(OutputApiType outputType, int bufferSizeInSeconds)
: base(outputType, bufferSizeInSeconds)
{
}

Expand Down
2 changes: 2 additions & 0 deletions MusicPlayback/MusicPlayback.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NAudio.Asio" Version="2.1.0" />
<PackageReference Include="NAudio.Core" Version="2.1.0" />
<PackageReference Include="NAudio.Wasapi" Version="2.1.0" />
<PackageReference Include="NAudio.WinMM" Version="2.1.0" />
</ItemGroup>

Expand Down
10 changes: 10 additions & 0 deletions MusicPlayback/OutputApiType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MusicPlayback
{
public enum OutputApiType
{
DirectSound,
WaveOut,
Wasapi,
ASIO
}
}
24 changes: 22 additions & 2 deletions MusicPlayback/WavStreamPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class WavStreamPlayer : IAudioStreamPlayer
private IWavePlayer _outputDevice;
protected readonly object _lock = new object();
protected readonly MyBufferedWaveProvider _bufferedWaveProvider;
private readonly OutputApiType _outputType;
#endregion Fields

#region Events
Expand Down Expand Up @@ -65,8 +66,9 @@ public double Volume
/// <summary>
/// Creates a BufferedWavPlayer with standard WAV format.
/// </summary>
public WavStreamPlayer(int bufferSizeInSeconds)
public WavStreamPlayer(OutputApiType outputType, int bufferSizeInSeconds)
{
_outputType = outputType;
_bufferedWaveProvider = new MyBufferedWaveProvider(new WaveFormat(), bufferSizeInSeconds);
}
#endregion Constructor
Expand Down Expand Up @@ -186,7 +188,25 @@ private void CreateOutputDevice()
{
if (!(_outputDevice is null))
throw new InvalidOperationException("Output device already created.");
_outputDevice = new WaveOutEvent();


switch (_outputType)
{
case OutputApiType.ASIO:
_outputDevice = new AsioOut();
break;
case OutputApiType.DirectSound:
_outputDevice = new DirectSoundOut();
break;
case OutputApiType.Wasapi:
_outputDevice = new WasapiOut();
break;
case OutputApiType.WaveOut:
_outputDevice = new WaveOutEvent();
break;
default:
throw new NotSupportedException($"{_outputType}");
}
_outputDevice.Volume = (float)_volume;
_outputDevice.PlaybackStopped += OutputDevice_PlaybackStopped;
_outputDevice.Init(_bufferedWaveProvider);
Expand Down
9 changes: 6 additions & 3 deletions SynAudio/MediaPlayer/MusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class MusicPlayer : ViewModelBase, IDisposable
#region [Properties]
public IAudioStreamPlayer Player;

public OutputApiType OutputApi { get; set; } = OutputApiType.DirectSound;

private PlaybackStateType _playbackState = PlaybackStateType.Stopped;
public PlaybackStateType PlaybackState
{
Expand Down Expand Up @@ -93,6 +95,7 @@ public void Dispose()
{
if (_disposed)
return;
CleanUpPlayback();
_disposed = true;
}

Expand Down Expand Up @@ -223,9 +226,9 @@ private void StartSongStreamingInternal(SongModel song, TranscodeMode transcode,
if (transcode == TranscodeMode.None)
throw new NotSupportedException();
else if (transcode == TranscodeMode.WAV)
Player = new WavStreamPlayer(BufferLengthInSeconds);
Player = new WavStreamPlayer(OutputApi, BufferLengthInSeconds);
else
Player = new Mp3StreamPlayer(BufferLengthInSeconds);
Player = new Mp3StreamPlayer(OutputApi, BufferLengthInSeconds);

Player.Volume = _volume;
Player.PlaybackStarted += Player_PlaybackStarted;
Expand Down Expand Up @@ -287,7 +290,7 @@ private void Seek(TimeSpan position)

if (_song is null)
return;

if (position > Length - TimeSpan.FromSeconds(1))
position = Length > TimeSpan.FromSeconds(1) ? Length - TimeSpan.FromSeconds(1) : TimeSpan.Zero;

Expand Down
3 changes: 3 additions & 0 deletions SynAudio/Models/Config/SettingsModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MusicPlayback;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SynologyDotNet.AudioStation;
Expand Down Expand Up @@ -26,6 +27,8 @@ public class SettingsModel

public bool SavePassword { get; set; }

[JsonConverter(typeof(StringEnumConverter))]
public OutputApiType OutputApi { get; set; } = OutputApiType.DirectSound;

[JsonConverter(typeof(StringEnumConverter))]
public TranscodeMode Transcoding { get; set; } = TranscodeMode.WAV;
Expand Down
2 changes: 1 addition & 1 deletion SynAudio/SynAudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- General -->
<PropertyGroup>
<Product>SynAudio</Product>
<Version>0.6.3</Version>
<Version>0.6.4</Version>
<Description>Stream music from your Synology NAS.</Description>
<RepositoryUrl>https://github.com/geloczigeri/synologydotnet-audiostation-wpf</RepositoryUrl>
<Authors>Gergő Gelóczi</Authors>
Expand Down
3 changes: 2 additions & 1 deletion SynAudio/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ public async Task Open()
{
Player = new MusicPlayer(Library)
{
Volume = PercentageToRatio(Settings.Volume)
Volume = PercentageToRatio(Settings.Volume),
OutputApi = Settings.OutputApi
};
Player.PlaybackStateChanged += Player_PlaybackStateChanged;

Expand Down
3 changes: 3 additions & 0 deletions SynAudio/ViewModels/SettingsDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SynAudio.Models.Config;
using SynAudio.ViewModels;
using System;
using MusicPlayback;

namespace SynAudio.Views
{
Expand All @@ -21,6 +22,8 @@ public class SettingsDialogModel : ViewModelBase

public Styles.Theme[] ThemeItems { get; } = Enum.GetValues<Styles.Theme>();

public OutputApiType[] OutputApiItems { get; } = Enum.GetValues<OutputApiType>();

public SettingsDialogModel(MainWindowViewModel main)
{
Main = main;
Expand Down
11 changes: 9 additions & 2 deletions SynAudio/Views/SettingsDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,17 @@
</Border>
</TabItem>

<!--<TabItem Header="Audio">
<TabItem Header="Audio">
<Border Style="{StaticResource TabContentBorder}">
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
<ComboBox ItemsSource="{Binding OutputApiItems, Mode=OneTime}"
SelectedItem="{Binding Settings.OutputApi}"
HorizontalAlignment="Left"
MinWidth="100"/>
<Label>Please restart the application to apply the selected Output API!</Label>
</StackPanel>
</Border>
</TabItem>-->
</TabItem>

<TabItem Header="Theme">
<Border Style="{StaticResource TabContentBorder}">
Expand Down

0 comments on commit 6bd65d7

Please sign in to comment.