From 2bb7dac554484290afbb03c5070d34824fe1604f Mon Sep 17 00:00:00 2001 From: nicko88 Date: Tue, 23 Nov 2021 23:11:41 -0600 Subject: [PATCH] Fixed loading settings bug, upgraded AudioSync code (needs more testing), misc tweaks, v0.2.0 --- HTFanControl/HTFanControl.csproj | 14 ++-- HTFanControl/Players/AudioSync.cs | 103 +++++++++++++++--------------- HTFanControl/Settings.cs | 2 +- HTFanControl/WebUI.cs | 12 ++-- HTFanControl/html/settings.html | 10 +-- 5 files changed, 72 insertions(+), 69 deletions(-) diff --git a/HTFanControl/HTFanControl.csproj b/HTFanControl/HTFanControl.csproj index 9bbf4be..4e391d2 100644 --- a/HTFanControl/HTFanControl.csproj +++ b/HTFanControl/HTFanControl.csproj @@ -6,9 +6,9 @@ htfancontrol.ico Debug;ReleaseWin;ReleaseLinux true - 0.1.1.0 - 0.1.1.0 - 0.1.1 + 0.2.0.0 + 0.2.0.0 + 0.2.0 4D Theater Wind Effect - DIY Home Theater Project nicko88 @@ -72,12 +72,12 @@ - + - + - - + + diff --git a/HTFanControl/Players/AudioSync.cs b/HTFanControl/Players/AudioSync.cs index 00dbcdc..67baf24 100644 --- a/HTFanControl/Players/AudioSync.cs +++ b/HTFanControl/Players/AudioSync.cs @@ -11,6 +11,7 @@ using OpenTK.Audio.OpenAL; using System.IO; using System.Net.Http; +using System.Linq; namespace HTFanControl { @@ -24,7 +25,7 @@ class AudioSync private InMemoryModelService _modelService; private CancellationTokenSource tokenSource; - private BlockingCollection _blockingCollection; + private BlockingCollection _realtimeSource; private List _float32Buffer = new List(); //private Timer _pause; @@ -77,7 +78,7 @@ public void Stop() _state = ""; _modelService = null; - _blockingCollection = null; + _realtimeSource = null; _float32Buffer = new List(); try @@ -108,7 +109,7 @@ private void LoadFingerprint(string fileName) private async void StartMatching(CancellationToken cancellationToken) { - _blockingCollection = new BlockingCollection(); + _realtimeSource = new BlockingCollection(); Console.WriteLine("Start Listening..."); //Log.WriteLine("Start Listening..."); @@ -116,38 +117,43 @@ private async void StartMatching(CancellationToken cancellationToken) try { - double queryResult = await GetBestMatchForStream(new BlockingRealtimeCollection(_blockingCollection), cancellationToken); + _ = await GetBestMatchForStream(_realtimeSource, cancellationToken); } catch { } } - private void FoundMatch(ResultEntry resultEntry) + private void FoundMatch(AVQueryResult aVQueryResult) { - _timeJump = false; - TimeSpan matchTime = TimeSpan.FromSeconds(resultEntry.TrackMatchStartsAt + resultEntry.QueryLength - resultEntry.QueryMatchStartsAt + 0.65); - - if(matchTime > _lastMatchTime.Add(TimeSpan.FromMinutes(5)) || matchTime < _lastMatchTime.Subtract(TimeSpan.FromMinutes(5))) + if (aVQueryResult.ContainsMatches) { - _timeJump = true; - _lastMatchTime = matchTime; - Console.WriteLine("Time Jump Detected"); - //Log.WriteLine("Time Jump Detected"); - } + ResultEntry resultEntry = aVQueryResult.ResultEntries.First().Audio; - if (!_timeJump) - { - Console.WriteLine($"Match Found: {matchTime:G}"); - //Log.WriteLine($"Match Found: {matchTime:G}"); - _hTFanControl._currentVideoTime = Convert.ToInt64(matchTime.TotalMilliseconds); - _hTFanControl.UpdateTime(); - _lastMatchTime = matchTime; - } + _timeJump = false; + TimeSpan matchTime = TimeSpan.FromSeconds(resultEntry.TrackMatchStartsAt + resultEntry.QueryLength + TimeSpan.FromMilliseconds(aVQueryResult.QueryCommandStats.Audio.TotalDurationMilliseconds).TotalSeconds + 0.12); - //_pause.Change(10000, Timeout.Infinite); + if (matchTime > _lastMatchTime.Add(TimeSpan.FromMinutes(5)) || matchTime < _lastMatchTime.Subtract(TimeSpan.FromMinutes(5))) + { + _timeJump = true; + _lastMatchTime = matchTime; + Console.WriteLine("Time Jump Detected"); + //Log.WriteLine("Time Jump Detected"); + } - if (verifyAccuracy) - { - VerifyAccuracy(matchTime); + if (!_timeJump) + { + Console.WriteLine($"Match Found: {matchTime:G}"); + //Log.WriteLine($"Match Found: {matchTime:G}"); + _hTFanControl._currentVideoTime = Convert.ToInt64(matchTime.TotalMilliseconds); + _hTFanControl.UpdateTime(); + _lastMatchTime = matchTime; + } + + //_pause.Change(10000, Timeout.Infinite); + + if (verifyAccuracy) + { + VerifyAccuracy(matchTime); + } } } @@ -199,21 +205,18 @@ private void RecordOpenTK(object cancellationToken) { int samplesAvailable = ALC.GetAvailableSamples(captureDevice); - short[] samples = new short[samplesAvailable]; - ALC.CaptureSamples(captureDevice, ref samples[0], samplesAvailable); - - for (int i = 0; i < samples.Length; i += 2) + if (samplesAvailable > 0) { - float fSample = samples[i] / 32768f; - _float32Buffer.Add(fSample); + short[] samples = new short[samplesAvailable]; + ALC.CaptureSamples(captureDevice, ref samples[0], samplesAvailable); - if (_float32Buffer.Count == 10240) + for (int i = 0; i < samples.Length; i += 2) { - AudioSamples audioSamples = new AudioSamples(_float32Buffer.ToArray(), "", 5512); - _blockingCollection.Add(audioSamples); - - _float32Buffer = new List(); + _float32Buffer.Add(samples[i] / 32767f); } + + _realtimeSource.Add(new AudioSamples(_float32Buffer.ToArray(), string.Empty, 5512)); + _float32Buffer = new List(); } } else @@ -231,20 +234,20 @@ private void RecordOpenTK(object cancellationToken) } } - private async Task GetBestMatchForStream(BlockingRealtimeCollection audioSamples, CancellationToken token) + public async Task GetBestMatchForStream(BlockingCollection audioSamples, CancellationToken token) { - var queryResult = await QueryCommandBuilder.Instance - .BuildRealtimeQueryCommand() - .From(audioSamples) - .WithRealtimeQueryConfig(config => - { - config.ResultEntryFilter = new TrackMatchLengthEntryFilter(1d); - config.SuccessCallback = FoundMatch; - return config; - }) - .UsingServices(_modelService) - .Query(token); - return queryResult; + double seconds = await QueryCommandBuilder.Instance + .BuildRealtimeQueryCommand() + .From(new BlockingRealtimeCollection(audioSamples)) + .WithRealtimeQueryConfig(config => + { + config.ResultEntryFilter = new TrackMatchLengthEntryFilter(3d); + config.SuccessCallback = result => FoundMatch(result); + return config; + }) + .UsingServices(_modelService) + .Query(token); + return seconds; } } } \ No newline at end of file diff --git a/HTFanControl/Settings.cs b/HTFanControl/Settings.cs index 076602c..3a23b37 100644 --- a/HTFanControl/Settings.cs +++ b/HTFanControl/Settings.cs @@ -52,7 +52,7 @@ public static Settings LoadSettings() options.WriteIndented = true; string jsonSettings = File.ReadAllText(Path.Combine(_rootPath, "HTFanControlSettings.json")); - settings = JsonSerializer.Deserialize(Path.Combine(_rootPath, "HTFanControlSettings.json"), options); + settings = JsonSerializer.Deserialize(jsonSettings, options); } catch { diff --git a/HTFanControl/WebUI.cs b/HTFanControl/WebUI.cs index 368fd48..91a2e35 100644 --- a/HTFanControl/WebUI.cs +++ b/HTFanControl/WebUI.cs @@ -476,10 +476,10 @@ private void SaveSettings(HttpListenerRequest request) _HTFanCtrl._settings.ControllerType = data.RootElement.GetProperty("Controller").GetString(); _HTFanCtrl._settings.LIRC_IP = data.RootElement.GetProperty("LircIP").GetString(); - _HTFanCtrl._settings.LIRC_Port = Convert.ToInt32(data.RootElement.GetProperty("LircPort").GetString()); + _HTFanCtrl._settings.LIRC_Port = int.TryParse(data.RootElement.GetProperty("LircPort").GetString(), out int lircPort) ? lircPort : 8765; _HTFanCtrl._settings.LIRC_Remote = data.RootElement.GetProperty("LircRemote").GetString(); _HTFanCtrl._settings.MQTT_IP = data.RootElement.GetProperty("MqttIP").GetString(); - _HTFanCtrl._settings.MQTT_Port = Convert.ToInt32(data.RootElement.GetProperty("MqttPort").GetString()); + _HTFanCtrl._settings.MQTT_Port = int.TryParse(data.RootElement.GetProperty("MqttPort").GetString(), out int MqttPort) ? MqttPort : 1883; _HTFanCtrl._settings.MQTT_OFF_Topic = data.RootElement.GetProperty("MqttOFFtopic").GetString(); _HTFanCtrl._settings.MQTT_OFF_Payload = data.RootElement.GetProperty("MqttOFFpayload").GetString(); _HTFanCtrl._settings.MQTT_ECO_Topic = data.RootElement.GetProperty("MqttECOtopic").GetString(); @@ -491,10 +491,10 @@ private void SaveSettings(HttpListenerRequest request) _HTFanCtrl._settings.MQTT_HIGH_Topic = data.RootElement.GetProperty("MqttHIGHtopic").GetString(); _HTFanCtrl._settings.MQTT_HIGH_Payload = data.RootElement.GetProperty("MqttHIGHpayload").GetString(); _HTFanCtrl._settings.MediaPlayerIP = data.RootElement.GetProperty("MediaPlayerIP").GetString(); - _HTFanCtrl._settings.MediaPlayerPort = Convert.ToInt32(data.RootElement.GetProperty("MediaPlayerPort").GetString()); - _HTFanCtrl._settings.GlobalOffsetMS = Convert.ToInt32(data.RootElement.GetProperty("GlobalOffset").GetString()); - _HTFanCtrl._settings.SpinupOffsetMS = Convert.ToInt32(data.RootElement.GetProperty("SpinupOffset").GetString()); - _HTFanCtrl._settings.SpindownOffsetMS = Convert.ToInt32(data.RootElement.GetProperty("SpindownOffset").GetString()); + _HTFanCtrl._settings.MediaPlayerPort = int.TryParse(data.RootElement.GetProperty("MediaPlayerPort").GetString(), out int MediaPlayerPort) ? MediaPlayerPort : 0; + _HTFanCtrl._settings.GlobalOffsetMS = int.TryParse(data.RootElement.GetProperty("GlobalOffset").GetString(), out int GlobalOffset) ? GlobalOffset : 0; + _HTFanCtrl._settings.SpinupOffsetMS = int.TryParse(data.RootElement.GetProperty("SpinupOffset").GetString(), out int SpinupOffset) ? SpinupOffset : 0; + _HTFanCtrl._settings.SpindownOffsetMS = int.TryParse(data.RootElement.GetProperty("SpindownOffset").GetString(), out int SpindownOffset) ? SpindownOffset : 0; _HTFanCtrl._settings.MediaPlayerType = data.RootElement.GetProperty("MediaPlayer").GetString(); _HTFanCtrl._settings.PlexToken = data.RootElement.GetProperty("PlexToken").GetString(); diff --git a/HTFanControl/html/settings.html b/HTFanControl/html/settings.html index 8dbabbd..b3cbfa9 100644 --- a/HTFanControl/html/settings.html +++ b/HTFanControl/html/settings.html @@ -167,7 +167,7 @@ padding-left: 4px; padding-right: 4px; margin-bottom: 8px; - display: table; + display: block; } .nrow { @@ -219,7 +219,7 @@

Settings

-
+
Fan Control Type @@ -268,7 +268,7 @@

Settings