Multiple improvments

This commit is contained in:
Stone_Red
2024-01-02 14:11:31 +01:00
parent 888c017a82
commit ab64e2c987
4 changed files with 84 additions and 18 deletions
+65 -9
View File
@@ -1,8 +1,11 @@
using BeatDetector.AudioProcessing;
using NAudio.Wave;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
@@ -15,6 +18,19 @@ namespace BeatDetector;
/// </summary>
public partial class MainWindow : Window
{
private readonly Brush[] brushes = new Brush[]
{
Brushes.Red,
Brushes.Orange,
Brushes.Plum,
Brushes.Purple,
Brushes.Blue,
Brushes.Green,
};
private readonly IWaveIn audioStream;
private int lightAmountFactor = 3;
private AudioBeatDetector? audioBeatDetector;
public ObservableCollection<BeatLight> BeatLights { get; } = new ObservableCollection<BeatLight>();
public MainWindow()
@@ -22,24 +38,64 @@ public partial class MainWindow : Window
DataContext = this;
InitializeComponent();
for (int i = 0; i < 4; i++)
{
BeatLights.Add(new BeatLight());
}
audioStream = new WasapiLoopbackCapture();
audioStream.StartRecording();
AudioBeatDetector audioBeatDetector = new AudioBeatDetector(new TimeSpan(0, 0, 0, 0, 100), 1024 * 2, BeatLights.Count);
audioBeatDetector.OnBeat += AudioBeatDetector_OnBeat;
audioBeatDetector.OnNoBeat += AudioBeatDetector_OnNoBeat;
SetupAudioBeatDetector();
}
private void AudioBeatDetector_OnBeat(object? sender, BeatDetectorEventArgs e)
{
Dispatcher.Invoke(() => BeatLights[e.ChunkIndex].Color = Brushes.Red);
Dispatcher.Invoke(() =>
{
double index = (brushes.Length - 1) / (double)BeatLights.Count * BeatLights.Count(x => x.Color != Brushes.White);
BeatLights[e.ChunkIndex].Color = brushes[(int)index];
});
}
private void AudioBeatDetector_OnNoBeat(object? sender, BeatDetectorEventArgs e)
{
Dispatcher.Invoke(() => BeatLights[e.ChunkIndex].Color = Brushes.White);
_ = Dispatcher.Invoke(() => BeatLights[e.ChunkIndex].Color = Brushes.White);
}
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Add && lightAmountFactor < 10)
{
lightAmountFactor++;
audioBeatDetector?.Dispose();
SetupAudioBeatDetector();
}
else if (e.Key == System.Windows.Input.Key.Subtract && lightAmountFactor > 1)
{
lightAmountFactor--;
audioBeatDetector?.Dispose();
SetupAudioBeatDetector();
}
}
private void SetupAudioBeatDetector()
{
BeatLights.Clear();
for (int i = 0; i < Math.Pow(lightAmountFactor, 2); i++)
{
BeatLights.Add(new BeatLight());
}
if (audioBeatDetector is not null)
{
audioBeatDetector.OnBeat -= AudioBeatDetector_OnBeat;
audioBeatDetector.OnNoBeat -= AudioBeatDetector_OnNoBeat;
}
audioBeatDetector = new AudioBeatDetector(audioStream, TimeSpan.FromMilliseconds(0), 1024 * 2, BeatLights.Count);
audioBeatDetector.OnBeat += AudioBeatDetector_OnBeat;
audioBeatDetector.OnNoBeat += AudioBeatDetector_OnNoBeat;
Width = (Math.Sqrt(BeatLights.Count) * 110) + 15;
Height = (Math.Sqrt(BeatLights.Count) * 110) + 40;
}
}