208 lines
7.9 KiB
C#
208 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Diagnostics;
|
|
using SafeMobileLib;
|
|
using System.IO;
|
|
|
|
|
|
namespace AppServer
|
|
{
|
|
class USB3000
|
|
{
|
|
// Thread for handling recordings decoding
|
|
private Thread decodingThread;
|
|
// flag that the recordings decoding should be running or not
|
|
private bool isRunning = false;
|
|
|
|
// queue which will keep a track
|
|
private InterthreadMessageQueue<string> toDecodeFiles = new InterthreadMessageQueue<string>();
|
|
// class which will gestion the usb3000 status
|
|
private USBMonitoring usbMonitoring;
|
|
private string usb3000VID = "0403";
|
|
private string usb3000PID = "6001";
|
|
private int sampleRate = 8000;
|
|
private int bitdepth = 16;
|
|
|
|
public USB3000(bool startThread, int sr, int bd)
|
|
{
|
|
this.sampleRate = sr;
|
|
this.bitdepth = bd;
|
|
usbMonitoring = new USBMonitoring(new DeviceInfo(usb3000VID, usb3000PID), 2);
|
|
usbMonitoring.OnDeviceStatusChanged += delegate(bool isConnected, DeviceInfo device)
|
|
{
|
|
Console.ForegroundColor = (isConnected ? ConsoleColor.Cyan : ConsoleColor.DarkRed);
|
|
Console.WriteLine("USB 3000 is now " + (isConnected ? "connected" : "disconnected"));
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
};
|
|
usbMonitoring.StartUSBMonitoring();
|
|
|
|
// start the Thread which
|
|
if(startThread)
|
|
StartRecordingsDecoding();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the USB3000 parameters in order for the detection and communication to be established
|
|
/// The detection algorithm will receive the COM Port at which USB3000 is connected
|
|
/// </summary>
|
|
/// <param name="vid">Vendor ID for the watched device (without VID_)</param>
|
|
/// <param name="pid">Product ID for the watched device (without PID_)</param>
|
|
public void SetUSB3000VIDandPID(string vid, string pid)
|
|
{
|
|
usbMonitoring.SetWatchedDevice(new DeviceInfo(vid, pid));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Add a recording file path to the decoding queue in order to be decoded
|
|
/// </summary>
|
|
/// <param name="filePath">File path of the recording which will be decoded</param>
|
|
public void AddRecordingInDecodingQueue(string filePath)
|
|
{
|
|
toDecodeFiles.PostItem(filePath);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Start the thread which will manage the monitoring of the usb device plug & unplug
|
|
/// </summary>
|
|
public void StartRecordingsDecoding()
|
|
{
|
|
if (this.isRunning)
|
|
return;
|
|
|
|
this.isRunning = true;
|
|
decodingThread = new Thread(new ThreadStart(RecordingsDecodingThreadHandler));
|
|
decodingThread.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop the thread which is monitoring the detection of plug & unplug of the usb device
|
|
/// </summary>
|
|
public void StopRecordingsDecoding()
|
|
{
|
|
this.isRunning = false;
|
|
usbMonitoring.StopUSBMonitoring();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Thread function which will handle eveything
|
|
/// </summary>
|
|
private void RecordingsDecodingThreadHandler()
|
|
{
|
|
string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\recordings\";
|
|
|
|
if (!Directory.Exists(folder))
|
|
{
|
|
// create folder
|
|
Directory.CreateDirectory(folder);
|
|
}
|
|
// check for old amb files that are not decoded
|
|
|
|
string[] filePaths = Directory.GetFiles(folder, "*.amb");
|
|
foreach (string filepath in filePaths)
|
|
{
|
|
// add file to the queue if an amb file
|
|
if (filepath.Contains(".amb"))
|
|
{
|
|
toDecodeFiles.PostItem(@"recordings\" + Path.GetFileName(filepath));
|
|
}
|
|
}
|
|
|
|
while (isRunning && MainForm.isRunning)
|
|
{
|
|
// check if the com port for the usb3000 was found or not
|
|
if (usbMonitoring.GetComPortForWatchedDevice().Length < 1)
|
|
{
|
|
Thread.Sleep(300);
|
|
continue;
|
|
}
|
|
|
|
|
|
// wait until the usbMonitor is connected
|
|
if (!usbMonitoring.IsDeviceConnected())
|
|
{
|
|
Thread.Sleep(300);
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
// get a recordings file path
|
|
string filePath = toDecodeFiles.GetItem(100);
|
|
|
|
if (filePath == null)
|
|
continue;
|
|
|
|
Process p = new Process();
|
|
p = new Process();
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
|
|
//String arguments = String.Format("/c usb3kcom -port {0} {1} -version", usbMonitoring.GetComPortForWatchedDevice(), 460800);
|
|
String arguments = String.Format("/c usb3kcom -port {0} {1} -r 34 -dec {2} {3}",
|
|
usbMonitoring.GetComPortForWatchedDevice(), 460800, filePath, filePath.Replace(".amb",".tmp"));
|
|
|
|
p.StartInfo.Arguments = arguments;
|
|
p.StartInfo.UseShellExecute = false;
|
|
p.StartInfo.RedirectStandardOutput = true;
|
|
p.Start();
|
|
String output = p.StandardOutput.ReadToEnd();
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.WriteLine("OUTPUT : " + output);
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
|
|
// add the file again to the queue if it failes to convert
|
|
if (output.ToUpper().Contains("CAN") || output.ToUpper().Contains("error"))
|
|
toDecodeFiles.PostItem(filePath);
|
|
else
|
|
{
|
|
// open file that was decoded but has no WAV Header
|
|
FileStream tmpDecodedFile = new FileStream(filePath.Replace(".amb", ".tmp"), System.IO.FileMode.Open);
|
|
// read all bytes from the temp file
|
|
byte[] tempDecodedArray = new byte[tmpDecodedFile.Length];
|
|
tmpDecodedFile.Read(tempDecodedArray, 0, (int)tmpDecodedFile.Length);
|
|
|
|
// create the wav file
|
|
FileStream wavDecodedFile = new FileStream(filePath.Replace(".amb",".wav"), System.IO.FileMode.Create);
|
|
|
|
// write WAV header to the final WAV file
|
|
VoiceRecording.WriteHeader(wavDecodedFile, (int)tmpDecodedFile.Length, 1, sampleRate, bitdepth);
|
|
|
|
// write other decoded file to the wav header file
|
|
System.IO.BinaryWriter bw = new BinaryWriter(wavDecodedFile);
|
|
bw.Write(tempDecodedArray);
|
|
bw.Close();
|
|
tmpDecodedFile.Close();
|
|
wavDecodedFile.Close();
|
|
|
|
|
|
// delete the encoded file .amb
|
|
if (File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
|
|
// delete the temporary decoded file .tmp
|
|
if (File.Exists(filePath.Replace(".amb", ".tmp")))
|
|
{
|
|
File.Delete(filePath.Replace(".amb", ".tmp"));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("USB 3000 Thread ended ♥♥♥");
|
|
}
|
|
}
|
|
}
|