Skip to content

Commit

Permalink
Switch audio playback to NAudio; clarify Mode function names; fix jso…
Browse files Browse the repository at this point in the history
…n reader/writer to suport float type
  • Loading branch information
joekolodz committed Nov 7, 2024
1 parent a7313e5 commit 469510f
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/CustomSierraJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void SerializeAxis(HOTASAxis axis)
if (prop.Name == nameof(axis.IsDirectional) && (bool)prop.GetValue(axis) == true) continue;
if (prop.Name == nameof(axis.IsMultiAction) && (bool)prop.GetValue(axis) == false) continue;
if (prop.Name == nameof(axis.SoundFileName) && string.IsNullOrEmpty((string)prop.GetValue(axis))) continue;
if (prop.Name == nameof(axis.SoundVolume) && Math.Abs((double)prop.GetValue(axis) - 1.0d) < 0.01) continue;
if (prop.Name == nameof(axis.SoundVolume) && Math.Abs((float)prop.GetValue(axis) - 1.0d) < 0.01) continue;

Serializer.WriteKeyValue(prop.Name, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/HOTASAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HOTASAxis : IHotasBaseMap
public bool IsDirectional { get; set; } = true;
public bool IsMultiAction { get; set; } = false;
public string SoundFileName { get; set; }
public double SoundVolume { get; set; } = 1.0d;
public float SoundVolume { get; set; } = 1.0f;
public ObservableCollection<Segment> Segments { get; set; }

[SierraJsonIgnore]
Expand Down
9 changes: 5 additions & 4 deletions src/Models/HOTASDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void OverlayAllModesToDevice()
}

Modes = mergedModes;
_hotasQueue?.SetModes(Modes);
_hotasQueue?.SetModesCollection(Modes);
}

/// <summary>
Expand All @@ -305,7 +305,7 @@ public void OverlayAllModesToDevice(Dictionary<int, ObservableCollection<IHotasB
}

Modes = mergedModes;
_hotasQueue?.SetModes(Modes);
_hotasQueue?.SetModesCollection(Modes);
}

/// <summary>
Expand Down Expand Up @@ -366,7 +366,7 @@ public void SetMode(int mode)
return;
}
ButtonMap = Modes[mode];
_hotasQueue.SetMode(mode);
_hotasQueue.ActivateMode(mode);
}

public void SetModeActivation(Dictionary<int, ModeActivationItem> modeActivationButtons)
Expand Down Expand Up @@ -562,7 +562,8 @@ public void Reset()
_modeActivationButtons.Clear();

//Modes = new Dictionary<int, ObservableCollection<IHotasBaseMap>>();
_hotasQueue.SetModes(Modes);
_hotasQueue.SetModesCollection(Modes);
_hotasQueue.ActivateMode(1);
}

private void ClearButtonMap()
Expand Down
4 changes: 2 additions & 2 deletions src/Models/HOTASQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,13 @@ public void SetButtonMap(ObservableCollection<IHotasBaseMap> buttonMap)
_buttonMap = buttonMap;
}

public void SetMode(int mode)
public void ActivateMode(int mode)
{
_mode = mode;
_buttonMap = _modes[_mode];
}

public void SetModes(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes)
public void SetModesCollection(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes)
{
_modes = modes;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/IHOTASQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IHOTASQueue
void Stop();
IHotasBaseMap GetMap(int buttonOffset);
void SetButtonMap(ObservableCollection<IHotasBaseMap> buttonMap);
void SetMode(int mode);
void SetModes(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes);
void ActivateMode(int mode);
void SetModesCollection(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes);
}
}
9 changes: 3 additions & 6 deletions src/Models/IMediaPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Windows.Threading;

namespace SierraHOTAS.Models
{
public interface IMediaPlayer
{
void Play();
void Close();
void Open(Uri source);
Dispatcher Dispatcher { get; }
bool IsMuted { get; set; }
double Volume { get; set; }
TimeSpan Position { get; set; }
void Open(string sourceFilePath);
float Volume { get; set; }
long Position { get; set; }
}
}
41 changes: 16 additions & 25 deletions src/Models/MediaPlayerWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
using System;
using NAudio.Wave;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Media;
using System.Windows.Threading;

namespace SierraHOTAS.Models
{
[ExcludeFromCodeCoverage]
public class MediaPlayerWrapper : IMediaPlayer
{
private readonly MediaPlayer _mediaPlayer;
private readonly WaveOutEvent _outputDevice;
private AudioFileReader _audioFile;

public MediaPlayerWrapper(MediaPlayer mediaPlayer)
public MediaPlayerWrapper(WaveOutEvent outputDevice)
{
_mediaPlayer = mediaPlayer;
_outputDevice = outputDevice;
}

public void Play()
{
_mediaPlayer.Play();
_outputDevice.Play();
}

public void Close()
{
_mediaPlayer.Close();
_audioFile.Close();
}

public void Open(Uri source)
public void Open(string sourceFilePath)
{
_mediaPlayer.Open(source);
_audioFile = new AudioFileReader(sourceFilePath);
_outputDevice.Init(_audioFile);
}

public Dispatcher Dispatcher => _mediaPlayer.Dispatcher;


public bool IsMuted
{
get => _mediaPlayer.IsMuted;
set => _mediaPlayer.IsMuted = value;
}

public double Volume
public float Volume
{
get => _mediaPlayer.Volume;
set => _mediaPlayer.Volume = value;
get => _outputDevice.Volume;
set => _outputDevice.Volume = value;
}

public TimeSpan Position
public long Position
{
get => _mediaPlayer.Position;
set => _mediaPlayer.Position = value;
get => _audioFile.Position;
set => _audioFile.Position = value;
}
}
}
4 changes: 4 additions & 0 deletions src/Models/Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public int Value
}
}

public Segment()
{
}

public Segment(int id, int value)
{
Id = id;
Expand Down
22 changes: 16 additions & 6 deletions src/Serializer/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static string TokenizeString()
return s.ToString();
}

private static Int32 TokenizeNumber()
private static string TokenizeNumber()
{
StringBuilder s = new StringBuilder();

Expand All @@ -226,7 +226,7 @@ private static Int32 TokenizeNumber()
}

index--; //roll back one index otherwise the for loop will advance over the comma which is being pointed at currently
return Convert.ToInt32(s.ToString());
return s.ToString();
}

private static void TokenizeBoolTrue()
Expand Down Expand Up @@ -261,7 +261,7 @@ private static void TokenizeNull()
}


static object ParseValue(JsonToken token)
static object ParseValue(JsonToken token, Type type)
{
switch (token.Token)
{
Expand All @@ -284,7 +284,9 @@ static object ParseValue(JsonToken token)
break;

case Tokens.NUMBER:
return Convert.ToInt32(token.Value);
if(int.TryParse(token.Value, out var i)) return i;
if(float.TryParse(token.Value, out var f)) return f;
throw new InvalidCastException($"ParseValue not implemented to handle type {type}.");

case Tokens.COMMA:
break;
Expand All @@ -311,7 +313,15 @@ private static object ParseObject(Type type)
}

//treat as class
var obj = Activator.CreateInstance(type);
object obj = null;
try
{
obj = Activator.CreateInstance(type);
}
catch (Exception e)
{
Debug.WriteLine("can't instantiate");
}
return ParseObject(obj);
}

Expand Down Expand Up @@ -398,7 +408,7 @@ private static object ParseObject(object obj)
value = TryCustomConverter(propInfo.PropertyType, tok.Value);
if (value == null)
{
value = ParseValue(tok);
value = ParseValue(tok, propInfo.PropertyType);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Serializer/Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public static void WriteValue(object obj)
case double d:
WriteDouble(d);
break;
case float f:
WriteFloat(f);
break;
case bool b:
WriteBool(b);
break;
Expand Down Expand Up @@ -135,6 +138,8 @@ private static bool HideValue(object value)
return i == 0;
case double d:
return Math.Abs(d - 1.0d) < 0.01;
case float f:
return Math.Abs(f - 1.0d) < 0.01;
case bool b:
return b == false;
case string s:
Expand Down Expand Up @@ -169,6 +174,11 @@ private static void WriteDouble(double d)
_resultJSON.Append(d);
}

private static void WriteFloat(float f)
{
_resultJSON.Append(f);
}

private static void WriteBool(bool b)
{
_resultJSON.Append(b.ToString().ToLower());
Expand Down
3 changes: 3 additions & 0 deletions src/SierraHOTAS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@
<PackageReference Include="Hardcodet.NotifyIcon.Wpf">
<Version>1.0.8</Version>
</PackageReference>
<PackageReference Include="NAudio">
<Version>2.2.1</Version>
</PackageReference>
<PackageReference Include="NLog">
<Version>4.6.7</Version>
</PackageReference>
Expand Down
19 changes: 7 additions & 12 deletions src/ViewModels/AxisMapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public string SoundFileName
}
}

public double SoundVolume
public float SoundVolume
{
get => _hotasAxis.SoundVolume;
set
Expand Down Expand Up @@ -176,13 +176,9 @@ public AxisMapViewModel(IDispatcher dispatcher, MediaPlayerFactory mediaPlayerFa
_mediaPlayer = mediaPlayerFactory.CreateMediaPlayer();
_mediaPlayer.Volume = 0f;

if (string.IsNullOrWhiteSpace(SoundFileName))
if (!string.IsNullOrWhiteSpace(SoundFileName))
{
_mediaPlayer.IsMuted = true;
}
else
{
_mediaPlayer.Open(new Uri(map.SoundFileName, UriKind.Relative));
_mediaPlayer.Open(map.SoundFileName);
}
RebuildAllButtonMapViewModels();
}
Expand Down Expand Up @@ -341,9 +337,9 @@ private void OnAxisSegmentChanged(object sender, AxisSegmentChangedEventArgs e)
{
_appDispatcher?.Invoke(() =>
{
_mediaPlayer.Volume = axisMap.SoundVolume;
_mediaPlayer.Volume = axisMap.SoundVolume > 1.0f ? 1.0f : axisMap.SoundVolume;
_mediaPlayer.Position = 0;
_mediaPlayer.Play();
_mediaPlayer.Position = TimeSpan.Zero;
});
}
}
Expand Down Expand Up @@ -428,14 +424,13 @@ private void LoadNewSound()
if (string.IsNullOrWhiteSpace(soundFileName)) return;
SoundFileName = soundFileName;
_mediaPlayer.Close();
_mediaPlayer.Open(new Uri(SoundFileName, UriKind.Relative));
_mediaPlayer.IsMuted = false;
_mediaPlayer.Open(SoundFileName);
_mediaPlayer.Play();
}

private void RemoveSound()
{
SoundFileName = string.Empty;
_mediaPlayer.IsMuted = true;
}
}
}
2 changes: 0 additions & 2 deletions src/ViewModels/HOTASCollectionViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using SierraHOTAS.Annotations;
using SierraHOTAS.Controls;
using SierraHOTAS.Factories;
using SierraHOTAS.Models;
using SierraHOTAS.ViewModels.Commands;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
Expand Down
7 changes: 3 additions & 4 deletions tests/AxisMapViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace SierraHOTAS.Tests
{
using NSubstitute;
using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using SierraHOTAS.Factories;
using SierraHOTAS.Models;
using SierraHOTAS.ViewModels;
using System;
using System.Collections.ObjectModel;
using Xunit;

public class AxisMapViewModelTests
Expand Down Expand Up @@ -158,7 +157,7 @@ public void constructor_test_no_sound_file()
public void constructor_test_valid_sound_file_from_deserialization()
{
var map = new HOTASAxis();
map.SoundFileName = "file name";
map.SoundFileName = "D:\\Development\\SierraHOTAS\\src\\Sounds\\click05.mp3";
CreateAxisMapViewModel(out var subMediaPlayer, map);
map.SoundFileName = "bob";

Expand Down
2 changes: 1 addition & 1 deletion tests/HOTASQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void button_pressed_use_inherited_parent()
queue.Listen(joystick, modes, activationList);

//inherited key should be active
queue.SetMode(2);
queue.ActivateMode(2);

var mre = new ManualResetEventSlim();
queue.KeystrokeDownSent += (sender, e) =>
Expand Down

0 comments on commit 469510f

Please sign in to comment.