diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index dcfcc35..afb3b85 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -591,8 +591,6 @@ "CommandSender".WriteLog("下发听音结束指令:" + BitConverter.ToString(afCrc)); sp.Write(afCrc, 0, afCrc.Length); - - PlayWav.Get.Stop(); } catch (Exception e) { diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index dcfcc35..afb3b85 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -591,8 +591,6 @@ "CommandSender".WriteLog("下发听音结束指令:" + BitConverter.ToString(afCrc)); sp.Write(afCrc, 0, afCrc.Length); - - PlayWav.Get.Stop(); } catch (Exception e) { diff --git a/Correlator/Util/FlowStatus.cs b/Correlator/Util/FlowStatus.cs index 2f989ef..b29a5fb 100644 --- a/Correlator/Util/FlowStatus.cs +++ b/Correlator/Util/FlowStatus.cs @@ -44,5 +44,12 @@ //水听器蓝色信号接收器解析之后的Tags public static readonly Dictionary> BlueSensorResponseTags = new Dictionary>(); + + //正在听音的设备编号 + public static string CurrentListenSoundDevCode = DevCode.Dev1; + + //听音数据 + public static readonly List RedSoundCaches = new List(); + public static readonly List BlueSoundCaches = new List(); } } \ No newline at end of file diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index dcfcc35..afb3b85 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -591,8 +591,6 @@ "CommandSender".WriteLog("下发听音结束指令:" + BitConverter.ToString(afCrc)); sp.Write(afCrc, 0, afCrc.Length); - - PlayWav.Get.Stop(); } catch (Exception e) { diff --git a/Correlator/Util/FlowStatus.cs b/Correlator/Util/FlowStatus.cs index 2f989ef..b29a5fb 100644 --- a/Correlator/Util/FlowStatus.cs +++ b/Correlator/Util/FlowStatus.cs @@ -44,5 +44,12 @@ //水听器蓝色信号接收器解析之后的Tags public static readonly Dictionary> BlueSensorResponseTags = new Dictionary>(); + + //正在听音的设备编号 + public static string CurrentListenSoundDevCode = DevCode.Dev1; + + //听音数据 + public static readonly List RedSoundCaches = new List(); + public static readonly List BlueSoundCaches = new List(); } } \ No newline at end of file diff --git a/Correlator/Util/LogHelper.cs b/Correlator/Util/LogHelper.cs index 29b6425..833cbf5 100644 --- a/Correlator/Util/LogHelper.cs +++ b/Correlator/Util/LogHelper.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using log4net; -using log4net.Config; namespace Correlator.Util { diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index dcfcc35..afb3b85 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -591,8 +591,6 @@ "CommandSender".WriteLog("下发听音结束指令:" + BitConverter.ToString(afCrc)); sp.Write(afCrc, 0, afCrc.Length); - - PlayWav.Get.Stop(); } catch (Exception e) { diff --git a/Correlator/Util/FlowStatus.cs b/Correlator/Util/FlowStatus.cs index 2f989ef..b29a5fb 100644 --- a/Correlator/Util/FlowStatus.cs +++ b/Correlator/Util/FlowStatus.cs @@ -44,5 +44,12 @@ //水听器蓝色信号接收器解析之后的Tags public static readonly Dictionary> BlueSensorResponseTags = new Dictionary>(); + + //正在听音的设备编号 + public static string CurrentListenSoundDevCode = DevCode.Dev1; + + //听音数据 + public static readonly List RedSoundCaches = new List(); + public static readonly List BlueSoundCaches = new List(); } } \ No newline at end of file diff --git a/Correlator/Util/LogHelper.cs b/Correlator/Util/LogHelper.cs index 29b6425..833cbf5 100644 --- a/Correlator/Util/LogHelper.cs +++ b/Correlator/Util/LogHelper.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using log4net; -using log4net.Config; namespace Correlator.Util { diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index a6d9ac4..b349baf 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -548,6 +548,57 @@ } /// + /// 保存听音上传的原始数据 + /// + /// + public static void SaveSoundData(this List dataArray) + { + using (var manager = new DataBaseManager()) + { + var today = DateTime.Now.ToString("yyyyMMdd"); + var configModel = manager + .Table() + .Where(x => x.Date == today) + .OrderBy(x => x.Date) + .LastOrDefault() ?? new CorrelatorConfigModel + { + Date = today, + Pipe = 1, + LocateTimes = 1, + ListenTimes = 1 + }; + + //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 + var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; + var listenDataDir = DirectoryManager.GetListenDir(); + + var listenTimes = configModel.LocateTimes; + var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; + + var builder = new StringBuilder(); + for (var i = 0; i < dataArray.Count; i++) + { + var per = BitConverter.ToString(dataArray[i]).Replace("-", ""); + if (i == dataArray.Count - 1) + { + builder.Append(per); + } + else + { + builder.Append(per).Append("\r\n"); + } + } + + File.AppendAllText(fileName, builder.ToString()); + + //插入新纪录 + configModel.Pipe++; + configModel.LocateTimes++; + manager.Insert(configModel); + } + } + + /// /// 保存定位数据 /// /// 完整路径 diff --git a/Correlator/App.config b/Correlator/App.config index 480eed3..a673178 100644 --- a/Correlator/App.config +++ b/Correlator/App.config @@ -38,7 +38,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -99,7 +99,7 @@ - + diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index dc572f5..f348a94 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -45,6 +45,7 @@ //Data containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //Dialog or Window containerRegistry.RegisterDialog(); diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e2fd826..5d2635c 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -200,6 +200,8 @@ + + @@ -227,7 +229,6 @@ - @@ -246,6 +247,7 @@ + diff --git a/Correlator/DataService/AudioServiceImpl.cs b/Correlator/DataService/AudioServiceImpl.cs new file mode 100644 index 0000000..d72a74e --- /dev/null +++ b/Correlator/DataService/AudioServiceImpl.cs @@ -0,0 +1,48 @@ +using System; +using Correlator.Util; +using NAudio.Wave; +using WaveFormat = NAudio.Wave.WaveFormat; +using WaveOut = NAudio.Wave.WaveOut; + +namespace Correlator.DataService +{ + public class AudioServiceImpl : IAudioService + { + private WaveFileWriter _waveFileWriter; + + private readonly WaveOut _waveOut = new WaveOut(); + private readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(7500, 24, 1)); + + public void Start() + { + _waveOut.Init(_waveProvider); + _waveOut.Play(); + + var fileName = $"{DirectoryManager.GetAudioDir()}/听音_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.wav"; + _waveFileWriter = new WaveFileWriter(fileName, new WaveFormat(7500, 24, 1)); + //初始化wav文件头 + var fileHeader = AudioHub.CreateWaveFileHeader(int.MaxValue, 1, 7500, 24); + _waveFileWriter.Write(fileHeader, 0, fileHeader.Length); + } + + public void Write(byte[] pcm) + { + _waveProvider.AddSamples(pcm, 0, pcm.Length); + + //将pcm数据保存为wav格式文件 + if (_waveFileWriter == null) + { + return; + } + _waveFileWriter.Write(pcm, 0, pcm.Length); + } + + public void Stop() + { + _waveOut.Stop(); + + _waveFileWriter.Dispose(); + _waveFileWriter = null; + } + } +} \ No newline at end of file diff --git a/Correlator/DataService/IAudioService.cs b/Correlator/DataService/IAudioService.cs new file mode 100644 index 0000000..c010987 --- /dev/null +++ b/Correlator/DataService/IAudioService.cs @@ -0,0 +1,11 @@ +namespace Correlator.DataService +{ + public interface IAudioService + { + void Start(); + + void Write(byte[] pcm); + + void Stop(); + } +} \ No newline at end of file diff --git a/Correlator/DataService/SerialPortServiceImpl.cs b/Correlator/DataService/SerialPortServiceImpl.cs index fd421fd..6276999 100644 --- a/Correlator/DataService/SerialPortServiceImpl.cs +++ b/Correlator/DataService/SerialPortServiceImpl.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.IO.Ports; -using System.Linq; using System.Text; using System.Threading; using System.Windows; +using System.Windows.Threading; using Correlator.Events; using Correlator.Model; using Correlator.SensorHubTag; @@ -18,6 +17,7 @@ public class SerialPortServiceImpl : ISerialPortService { private readonly IEventAggregator _eventAggregator; + private readonly IAudioService _audioService; private readonly DevStatus _devStatus = new DevStatus(); @@ -26,10 +26,23 @@ //左右传感器设备数据 private CorrelatorDataModel _dataModel; - public SerialPortServiceImpl(IEventAggregator eventAggregator) + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + public SerialPortServiceImpl(IEventAggregator eventAggregator, IAudioService audioService) { _eventAggregator = eventAggregator; + _audioService = audioService; Sp.DataReceived += DataReceivedFromSerialPort; + //波形图数据处理线程 + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _timer.Start(); } private void DataReceivedFromSerialPort(object sender, SerialDataReceivedEventArgs args) @@ -260,59 +273,67 @@ var noiseTag = tags.GetUploadNoiseTag(); if (noiseTag != null) { + //原始噪音数据 var dataValue = noiseTag.DataValue; - var bytesData = new byte[7500 * 3]; //每个采样点2字节 + + //PCM数据 + var pcm = new byte[7500 * 3]; //每个采样点2字节 for (var i = 0; i < 7500; i++) { - bytesData[i * 3 + 2] = dataValue[i * 2 + 0]; - bytesData[i * 3 + 1] = dataValue[i * 2 + 1]; - bytesData[i * 3 + 0] = 0x00; + pcm[i * 3 + 2] = dataValue[i * 2 + 0]; + pcm[i * 3 + 1] = dataValue[i * 2 + 1]; + pcm[i * 3 + 0] = 0x00; } - //写入缓存区 - PlayWav.Get.Listen(bytesData); + _audioService.Write(pcm); + + //波形图数据栈 + _arrayStack.Push(pcm); + // _timer.Tick += delegate + // { + // AudioWaveModel waveModel; + // if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = true + // }; + // } + // else + // { + // waveModel = new AudioWaveModel + // { + // IsRedSensor = false + // }; + // } + // + // var pop = _arrayStack.Pop(); + // var floatArray = new float[pop.Length / 4]; + // Buffer.BlockCopy(pop, 0, floatArray, 0, pop.Length); + // // for (var i = 0; i < floatArray.Length; i++) + // // { + // // floatArray[i] = pcm[i * 4] | pcm[i * 4 + 1] << 8 | pcm[i * 4 + 2] << 16 | pcm[i * 4 + 3] << 24; + // // } + // + // waveModel.WavePoints = floatArray; + // _eventAggregator.GetEvent().Publish(waveModel); + // }; //保存听音数据 - using (var manager = new DataBaseManager()) + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) { - var today = DateTime.Now.ToString("yyyyMMdd"); - var configModel = manager - .Table() - .Where(x => x.Date == today) - .OrderBy(x => x.Date) - .LastOrDefault() ?? new CorrelatorConfigModel - { - Date = today, - Pipe = 1, - LocateTimes = 1, - ListenTimes = 1 - }; - - //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 - var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; - var listenDataDir = DirectoryManager.GetListenDir(); - - var listenTimes = configModel.LocateTimes; - var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; - - var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - - var s = BitConverter.ToString(dataValue).Replace("-", ""); - var data = Encoding.Default.GetBytes(s); - fs.Position = fs.Length; - fs.Write(data, 0, data.Length); - fs.Flush(); - fs.Close(); - - //插入新纪录 - configModel.Pipe++; - configModel.LocateTimes++; - manager.Insert(configModel); + FlowStatus.RedSoundCaches.Add(pcm); + } + else + { + FlowStatus.BlueSoundCaches.Add(pcm); } } } + private readonly Stack _arrayStack = new Stack(); + public void HandleHydrophoneData(string devCode, List tags) { if (_dataModel == null) diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index dcfcc35..afb3b85 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -591,8 +591,6 @@ "CommandSender".WriteLog("下发听音结束指令:" + BitConverter.ToString(afCrc)); sp.Write(afCrc, 0, afCrc.Length); - - PlayWav.Get.Stop(); } catch (Exception e) { diff --git a/Correlator/Util/FlowStatus.cs b/Correlator/Util/FlowStatus.cs index 2f989ef..b29a5fb 100644 --- a/Correlator/Util/FlowStatus.cs +++ b/Correlator/Util/FlowStatus.cs @@ -44,5 +44,12 @@ //水听器蓝色信号接收器解析之后的Tags public static readonly Dictionary> BlueSensorResponseTags = new Dictionary>(); + + //正在听音的设备编号 + public static string CurrentListenSoundDevCode = DevCode.Dev1; + + //听音数据 + public static readonly List RedSoundCaches = new List(); + public static readonly List BlueSoundCaches = new List(); } } \ No newline at end of file diff --git a/Correlator/Util/LogHelper.cs b/Correlator/Util/LogHelper.cs index 29b6425..833cbf5 100644 --- a/Correlator/Util/LogHelper.cs +++ b/Correlator/Util/LogHelper.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using log4net; -using log4net.Config; namespace Correlator.Util { diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index a6d9ac4..b349baf 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -548,6 +548,57 @@ } /// + /// 保存听音上传的原始数据 + /// + /// + public static void SaveSoundData(this List dataArray) + { + using (var manager = new DataBaseManager()) + { + var today = DateTime.Now.ToString("yyyyMMdd"); + var configModel = manager + .Table() + .Where(x => x.Date == today) + .OrderBy(x => x.Date) + .LastOrDefault() ?? new CorrelatorConfigModel + { + Date = today, + Pipe = 1, + LocateTimes = 1, + ListenTimes = 1 + }; + + //如果配置里面的日期相同,则取配置里面的Pipe作为index,如果配置里面的日期不相同,则默认index=1 + var pipeIndex = configModel.Date.Equals(today) ? configModel.Pipe : 1; + var listenDataDir = DirectoryManager.GetListenDir(); + + var listenTimes = configModel.LocateTimes; + var fileName = $"{listenDataDir}\\听音数据{today}.{pipeIndex}.{listenTimes}.txt"; + + var builder = new StringBuilder(); + for (var i = 0; i < dataArray.Count; i++) + { + var per = BitConverter.ToString(dataArray[i]).Replace("-", ""); + if (i == dataArray.Count - 1) + { + builder.Append(per); + } + else + { + builder.Append(per).Append("\r\n"); + } + } + + File.AppendAllText(fileName, builder.ToString()); + + //插入新纪录 + configModel.Pipe++; + configModel.LocateTimes++; + manager.Insert(configModel); + } + } + + /// /// 保存定位数据 /// /// 完整路径 diff --git a/Correlator/ViewModels/AuditionViewModel.cs b/Correlator/ViewModels/AuditionViewModel.cs index 2dd1791..ba51489 100644 --- a/Correlator/ViewModels/AuditionViewModel.cs +++ b/Correlator/ViewModels/AuditionViewModel.cs @@ -43,10 +43,12 @@ private bool _isStartRecording; private bool _isRedSensor; private readonly ISerialPortService _serialPortService; + private readonly IAudioService _audioService; - public AuditionViewModel(ISerialPortService serialPortService) + public AuditionViewModel(ISerialPortService serialPortService, IAudioService audioService) { _serialPortService = serialPortService; + _audioService = audioService; GoBackCommand = new DelegateCommand(delegate { @@ -58,7 +60,7 @@ ListenRedSensorCommand = new DelegateCommand(delegate { _isRedSensor = true; - RecordAudio(); + ListenAudio(); }); RedSensorMuteCommand = new DelegateCommand(SetCurrentMicVolume); @@ -66,7 +68,7 @@ ListenBlueSensorCommand = new DelegateCommand(delegate { _isRedSensor = false; - RecordAudio(); + ListenAudio(); }); BlueSensorMuteCommand = new DelegateCommand(SetCurrentMicVolume); } @@ -74,18 +76,39 @@ /// /// 听音指令 /// - private void RecordAudio() + private void ListenAudio() { if (_isStartRecording) { _isStartRecording = false; CommandSender.SendSoundStopCmd(_serialPortService.Sp, _isRedSensor ? DevCode.Dev1 : DevCode.Dev2); + _audioService.Stop(); + if (FlowStatus.CurrentListenSoundDevCode == DevCode.Dev1) + { + //TODO Test + // using (var fs = new FileStream("C:\\Users\\Administrator\\Desktop\\input.pcm", + // FileMode.Append, FileAccess.Write)) + // { + // foreach (var cache in FlowStatus.RedSoundCaches) + // { + // fs.Write(cache, 0, cache.Length); + // } + // } + + FlowStatus.RedSoundCaches.SaveSoundData(); + FlowStatus.RedSoundCaches.Clear(); + } + else + { + FlowStatus.BlueSoundCaches.SaveSoundData(); + FlowStatus.BlueSoundCaches.Clear(); + } } else { - //初始化wav文件 - PlayWav.Get.InitWaveOut(); _isStartRecording = true; + _audioService.Start(); + FlowStatus.CurrentListenSoundDevCode = _isRedSensor ? DevCode.Dev1 : DevCode.Dev2; CommandSender.SendSoundCollectCmd(_serialPortService.Sp, _isRedSensor ? DevCode.Dev1 : DevCode.Dev2); } }