diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs index fc7a865..9227651 100644 --- a/Correlator/DataService/AudioServiceImpl.cs +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using Correlator.SensorHubTag; using Correlator.Util; using NAudio.Wave; @@ -37,7 +36,6 @@ _waveFileWriter = new WaveFileWriter(fileName, _waveFormat); - //异步执行听音,不然会卡住界面 waveOut.Play(); } @@ -60,5 +58,7 @@ _waveProvider.ClearBuffer(); _waveFileWriter.Dispose(); } + + // public AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs index fc7a865..9227651 100644 --- a/Correlator/DataService/AudioServiceImpl.cs +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using Correlator.SensorHubTag; using Correlator.Util; using NAudio.Wave; @@ -37,7 +36,6 @@ _waveFileWriter = new WaveFileWriter(fileName, _waveFormat); - //异步执行听音,不然会卡住界面 waveOut.Play(); } @@ -60,5 +58,7 @@ _waveProvider.ClearBuffer(); _waveFileWriter.Dispose(); } + + // public AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs index 74de79b..83609b2 100644 --- a/Correlator/DataService/IAudioService.cs +++ b/Correlator/DataService/IAudioService.cs @@ -18,5 +18,7 @@ /// 结束听音 /// void Stop(); + + // AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs index fc7a865..9227651 100644 --- a/Correlator/DataService/AudioServiceImpl.cs +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using Correlator.SensorHubTag; using Correlator.Util; using NAudio.Wave; @@ -37,7 +36,6 @@ _waveFileWriter = new WaveFileWriter(fileName, _waveFormat); - //异步执行听音,不然会卡住界面 waveOut.Play(); } @@ -60,5 +58,7 @@ _waveProvider.ClearBuffer(); _waveFileWriter.Dispose(); } + + // public AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs index 74de79b..83609b2 100644 --- a/Correlator/DataService/IAudioService.cs +++ b/Correlator/DataService/IAudioService.cs @@ -18,5 +18,7 @@ /// 结束听音 /// void Stop(); + + // AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index 7bc889d..a01d4d1 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -245,10 +245,6 @@ RuntimeCache.CorrelatorData = _dataModel; _eventAggregator.GetEvent().Publish(); } - else - { - "SerialPortServiceImpl".WriteLog("加速度计数据未收集完整,无法计算"); - } } } @@ -285,7 +281,7 @@ spectrumData[i] = BitConverter.ToSingle(pcm, i * 4); } - _audioService.AudioVisualizer.PushSampleData(spectrumData); + // _audioService.AudioVisualizer.PushSampleData(spectrumData); } } diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs index fc7a865..9227651 100644 --- a/Correlator/DataService/AudioServiceImpl.cs +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using Correlator.SensorHubTag; using Correlator.Util; using NAudio.Wave; @@ -37,7 +36,6 @@ _waveFileWriter = new WaveFileWriter(fileName, _waveFormat); - //异步执行听音,不然会卡住界面 waveOut.Play(); } @@ -60,5 +58,7 @@ _waveProvider.ClearBuffer(); _waveFileWriter.Dispose(); } + + // public AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs index 74de79b..83609b2 100644 --- a/Correlator/DataService/IAudioService.cs +++ b/Correlator/DataService/IAudioService.cs @@ -18,5 +18,7 @@ /// 结束听音 /// void Stop(); + + // AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index 7bc889d..a01d4d1 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -245,10 +245,6 @@ RuntimeCache.CorrelatorData = _dataModel; _eventAggregator.GetEvent().Publish(); } - else - { - "SerialPortServiceImpl".WriteLog("加速度计数据未收集完整,无法计算"); - } } } @@ -285,7 +281,7 @@ spectrumData[i] = BitConverter.ToSingle(pcm, i * 4); } - _audioService.AudioVisualizer.PushSampleData(spectrumData); + // _audioService.AudioVisualizer.PushSampleData(spectrumData); } } diff --git a/Correlator/Dialog/SimplyAuditionDialog.xaml b/Correlator/Dialog/SimplyAuditionDialog.xaml index 4983d55..5fd8124 100644 --- a/Correlator/Dialog/SimplyAuditionDialog.xaml +++ b/Correlator/Dialog/SimplyAuditionDialog.xaml @@ -11,6 +11,8 @@ Width="600" Height="400" d:DataContext="{d:DesignInstance Type=vm:SimplyAuditionDialogViewModel}" + Loaded="SimplyAuditionDialog_OnLoaded" + Unloaded="SimplyAuditionDialog_OnUnloaded" mc:Ignorable="d"> @@ -42,8 +44,9 @@ - + + @@ -182,6 +185,13 @@ + + + + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs index fc7a865..9227651 100644 --- a/Correlator/DataService/AudioServiceImpl.cs +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using Correlator.SensorHubTag; using Correlator.Util; using NAudio.Wave; @@ -37,7 +36,6 @@ _waveFileWriter = new WaveFileWriter(fileName, _waveFormat); - //异步执行听音,不然会卡住界面 waveOut.Play(); } @@ -60,5 +58,7 @@ _waveProvider.ClearBuffer(); _waveFileWriter.Dispose(); } + + // public AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs index 74de79b..83609b2 100644 --- a/Correlator/DataService/IAudioService.cs +++ b/Correlator/DataService/IAudioService.cs @@ -18,5 +18,7 @@ /// 结束听音 /// void Stop(); + + // AudioVisualizer AudioVisualizer { get; set; } } } \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index 7bc889d..a01d4d1 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -245,10 +245,6 @@ RuntimeCache.CorrelatorData = _dataModel; _eventAggregator.GetEvent().Publish(); } - else - { - "SerialPortServiceImpl".WriteLog("加速度计数据未收集完整,无法计算"); - } } } @@ -285,7 +281,7 @@ spectrumData[i] = BitConverter.ToSingle(pcm, i * 4); } - _audioService.AudioVisualizer.PushSampleData(spectrumData); + // _audioService.AudioVisualizer.PushSampleData(spectrumData); } } diff --git a/Correlator/Dialog/SimplyAuditionDialog.xaml b/Correlator/Dialog/SimplyAuditionDialog.xaml index 4983d55..5fd8124 100644 --- a/Correlator/Dialog/SimplyAuditionDialog.xaml +++ b/Correlator/Dialog/SimplyAuditionDialog.xaml @@ -11,6 +11,8 @@ Width="600" Height="400" d:DataContext="{d:DesignInstance Type=vm:SimplyAuditionDialogViewModel}" + Loaded="SimplyAuditionDialog_OnLoaded" + Unloaded="SimplyAuditionDialog_OnUnloaded" mc:Ignorable="d"> @@ -42,8 +44,9 @@ - + + @@ -182,6 +185,13 @@ + + + + diff --git a/Correlator/Dialog/SimplyAuditionDialog.xaml.cs b/Correlator/Dialog/SimplyAuditionDialog.xaml.cs index 723929c..3747ce0 100644 --- a/Correlator/Dialog/SimplyAuditionDialog.xaml.cs +++ b/Correlator/Dialog/SimplyAuditionDialog.xaml.cs @@ -1,4 +1,11 @@ -using System.Windows.Controls; +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Shapes; +using System.Windows.Threading; +using Correlator.DataService; +using Correlator.Util; namespace Correlator.Dialog { @@ -7,9 +14,158 @@ /// public partial class SimplyAuditionDialog : UserControl { - public SimplyAuditionDialog() + private readonly AudioVisualizer _visualizer; // 可视化 + private double[] _spectrumData; // 频谱数据 + private int _colorIndex; + private readonly Color[] _allColors; + + private readonly DispatcherTimer _dataTimer = new DispatcherTimer + { + Interval = new TimeSpan(0, 0, 0, 0, 30) + }; + + private readonly DispatcherTimer _drawingTimer = new DispatcherTimer + { + Interval = new TimeSpan(0, 0, 0, 0, 30) + }; + + public SimplyAuditionDialog(IApplicationDataService dataService, IAudioService audioService) { InitializeComponent(); + + // _visualizer = dataService.GetAudioVisualizer(256); + // _allColors = dataService.GetAllHsvColors(); // 获取所有的渐变颜色 (HSV 颜色) + // + // audioService.AudioVisualizer = _visualizer; + // + // _dataTimer.Tick += DataTimer_Tick; + // _drawingTimer.Tick += DrawingTimer_Tick; + } + + private void SimplyAuditionDialog_OnLoaded(object sender, RoutedEventArgs e) + { + // _dataTimer.Start(); + // _drawingTimer.Start(); + } + + private void SimplyAuditionDialog_OnUnloaded(object sender, RoutedEventArgs e) + { + // _drawingTimer.Stop(); + // _dataTimer.Stop(); + } + + /// + /// 刷新频谱数据以及实现频谱数据缓动 + /// + /// + /// + private void DataTimer_Tick(object sender, EventArgs e) + { + var newSpectrumData = _visualizer.GetSpectrumData(); // 从可视化器中获取频谱数据 + newSpectrumData = AudioVisualizer.MakeSmooth(newSpectrumData, 2); // 平滑频谱数据 + + if (_spectrumData == null) // 如果已经存储的频谱数据为空, 则把新的频谱数据直接赋值上去 + { + _spectrumData = newSpectrumData; + return; + } + + for (var i = 0; i < newSpectrumData.Length; i++) // 计算旧频谱数据和新频谱数据之间的 "中间值" + { + var oldData = _spectrumData[i]; + var newData = newSpectrumData[i]; + // 每一次执行, 频谱值会向目标值移动 20% (如果太大, 缓动效果不明显, 如果太小, 频谱会有延迟的感觉) + var lerpData = oldData + (newData - oldData) * .2f; + _spectrumData[i] = lerpData; + } + } + + private void DrawingTimer_Tick(object sender, EventArgs e) + { + if (_spectrumData == null) + { + return; + } + + _colorIndex++; + + var color1 = _allColors[_colorIndex % _allColors.Length]; + var color2 = _allColors[(_colorIndex + 200) % _allColors.Length]; + + DrawGradientStrips( + StripsPath, color1, color2, + _spectrumData, _spectrumData.Length, + StripsPath.ActualWidth, 0, StripsPath.ActualHeight, + 2, -StripsPath.ActualHeight * 200 + ); + } + + /// + /// 绘制渐变的条形 + /// + /// 绘图目标 + /// 下方颜色 + /// 上方颜色 + /// 频谱数据 + /// 条形的数量 + /// 绘图的宽度 + /// 绘图的起始 X 坐标 + /// 绘图的起始 Y 坐标 + /// 条形与条形之间的间隔(像素) + /// 控制波形图波峰高度 + private void DrawGradientStrips( + Path stripsPath, Color bottomColor, Color topColor, double[] spectrumData, + int stripCount, double drawingWidth, double xOffset, double yOffset, double spacing, double scale + ) + { + //竖条宽度 + var stripWidth = (drawingWidth - spacing * stripCount) / stripCount; + var pointArray = new Point[stripCount]; + + for (var i = 0; i < stripCount; i++) + { + var x = stripWidth * i + spacing * i + xOffset; + var y = spectrumData[i * spectrumData.Length / stripCount] * scale; // height + //给所有频谱位置赋值 + pointArray[i] = new Point(x, y); + } + + //生成一系列频谱竖条 + var geometry = new GeometryGroup(); + foreach (var point in pointArray) + { + var height = point.Y; + + if (height < 0) + { + height = -height; + } + + //每根竖条的四个角坐标 + var endPoints = new[] + { + new Point(point.X, point.Y + yOffset), //左下角 + new Point(point.X, point.Y + height + yOffset), //左上角 + new Point(point.X + stripWidth, point.Y + height + yOffset), //右上角 + new Point(point.X + stripWidth, point.Y + yOffset) //右下角 + }; + + var figure = new PathFigure + { + StartPoint = endPoints[0] + }; + + figure.Segments.Add(new PolyLineSegment(endPoints, false)); + geometry.Children.Add(new PathGeometry { Figures = { figure } }); + } + + stripsPath.Data = geometry; + + //设置频谱竖条的渐变色 + var linearGradientBrush = new LinearGradientBrush( + bottomColor, topColor, new Point(0, 0), new Point(0, 1) + ); + stripsPath.Fill = linearGradientBrush; } } } \ No newline at end of file