Skip to content

Commit

Permalink
Fixed loading settings bug, upgraded AudioSync code (needs more testi…
Browse files Browse the repository at this point in the history
…ng), misc tweaks, v0.2.0
  • Loading branch information
nicko88 committed Nov 24, 2021
1 parent ca574ed commit 2bb7dac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
14 changes: 7 additions & 7 deletions HTFanControl/HTFanControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<ApplicationIcon>htfancontrol.ico</ApplicationIcon>
<Configurations>Debug;ReleaseWin;ReleaseLinux</Configurations>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<FileVersion>0.1.1.0</FileVersion>
<Version>0.1.1</Version>
<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
<Version>0.2.0</Version>
<Description>4D Theater Wind Effect - DIY Home Theater Project</Description>
<Copyright>nicko88</Copyright>
</PropertyGroup>
Expand Down Expand Up @@ -72,12 +72,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.37" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.38" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="MQTTnet" Version="3.0.17" />
<PackageReference Include="MQTTnet" Version="3.1.0" />
<PackageReference Include="OpenTK.OpenAL" Version="4.6.7" />
<PackageReference Include="SoundFingerprinting" Version="7.9.6" />
<PackageReference Include="SoundFingerprinting.Emy" Version="7.9.6" />
<PackageReference Include="SoundFingerprinting" Version="7.15.0" />
<PackageReference Include="SoundFingerprinting.Emy" Version="7.15.5" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseWin|AnyCPU' Or '$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
103 changes: 53 additions & 50 deletions HTFanControl/Players/AudioSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using OpenTK.Audio.OpenAL;
using System.IO;
using System.Net.Http;
using System.Linq;

namespace HTFanControl
{
Expand All @@ -24,7 +25,7 @@ class AudioSync
private InMemoryModelService _modelService;

private CancellationTokenSource tokenSource;
private BlockingCollection<AudioSamples> _blockingCollection;
private BlockingCollection<AudioSamples> _realtimeSource;
private List<float> _float32Buffer = new List<float>();

//private Timer _pause;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void Stop()
_state = "";

_modelService = null;
_blockingCollection = null;
_realtimeSource = null;
_float32Buffer = new List<float>();

try
Expand Down Expand Up @@ -108,46 +109,51 @@ private void LoadFingerprint(string fileName)

private async void StartMatching(CancellationToken cancellationToken)
{
_blockingCollection = new BlockingCollection<AudioSamples>();
_realtimeSource = new BlockingCollection<AudioSamples>();

Console.WriteLine("Start Listening...");
//Log.WriteLine("Start Listening...");
_state = "(listening...)";

try
{
double queryResult = await GetBestMatchForStream(new BlockingRealtimeCollection<AudioSamples>(_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);
}
}
}

Expand Down Expand Up @@ -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<float>();
_float32Buffer.Add(samples[i] / 32767f);
}

_realtimeSource.Add(new AudioSamples(_float32Buffer.ToArray(), string.Empty, 5512));
_float32Buffer = new List<float>();
}
}
else
Expand All @@ -231,20 +234,20 @@ private void RecordOpenTK(object cancellationToken)
}
}

private async Task<double> GetBestMatchForStream(BlockingRealtimeCollection<AudioSamples> audioSamples, CancellationToken token)
public async Task<double> GetBestMatchForStream(BlockingCollection<AudioSamples> 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>(audioSamples))
.WithRealtimeQueryConfig(config =>
{
config.ResultEntryFilter = new TrackMatchLengthEntryFilter(3d);
config.SuccessCallback = result => FoundMatch(result);
return config;
})
.UsingServices(_modelService)
.Query(token);
return seconds;
}
}
}
2 changes: 1 addition & 1 deletion HTFanControl/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static Settings LoadSettings()
options.WriteIndented = true;

string jsonSettings = File.ReadAllText(Path.Combine(_rootPath, "HTFanControlSettings.json"));
settings = JsonSerializer.Deserialize<Settings>(Path.Combine(_rootPath, "HTFanControlSettings.json"), options);
settings = JsonSerializer.Deserialize<Settings>(jsonSettings, options);
}
catch
{
Expand Down
12 changes: 6 additions & 6 deletions HTFanControl/WebUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();

Expand Down
10 changes: 5 additions & 5 deletions HTFanControl/html/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
padding-left: 4px;
padding-right: 4px;
margin-bottom: 8px;
display: table;
display: block;
}

.nrow {
Expand Down Expand Up @@ -219,7 +219,7 @@
</ul>

<h3>Settings</h3>
<div class="form-row ngroup">
<div class="ngroup">
<div class="nrow">
<div class="nitem" style="flex-grow: 1;">
<b>Fan Control Type</b>
Expand Down Expand Up @@ -268,7 +268,7 @@ <h3>Settings</h3>

<div id="mqttDiv" style="display: none; padding-bottom: 5px;">
<div class="nrow">
<div class="nitem" style="flex-grow: 2;"><b>MQTT IP</b><input type="text" class="form-control" id="MqttIP" value="{MqttIP}"></div>
<div class="nitem" style="flex-grow: 2;"><b>MQTT Broker IP</b><input type="text" class="form-control" id="MqttIP" value="{MqttIP}"></div>
<div class="nitem" style="flex-grow: 1;"><b>Port (optional)</b><input type="number" class="form-control" id="MqttPort" value="{MqttPort}"></div>
</div>
<div class="nrow">
Expand Down Expand Up @@ -343,8 +343,8 @@ <h3>Settings</h3>

<div class="nrow">
<div class="nitem" style="flex-grow: 1;"><b>Global Offset (ms)</b><input type="number" class="form-control" id="GlobalOffset" value="{GlobalOffset}"></div>
<div class="nitem" style="flex-grow: 1;"><b>Spin Up Offset (ms)</b><input type="number" class="form-control" id="SpinupOffset" value="{SpinupOffset}"></div>
<div class="nitem" style="flex-grow: 1;"><b>Spin Down Offset (ms)</b><input type="number" class="form-control" id="SpindownOffset" value="{SpindownOffset}"></div>
<div class="nitem" style="flex-grow: 1;"><b>SpinUp Offset (ms)</b><input type="number" class="form-control" id="SpinupOffset" value="{SpinupOffset}"></div>
<div class="nitem" style="flex-grow: 1;"><b>SpinDown Offset (ms)</b><input type="number" class="form-control" id="SpindownOffset" value="{SpindownOffset}"></div>
</div>

</div>
Expand Down

0 comments on commit 2bb7dac

Please sign in to comment.