一些音频文件转Wav
常见的 音频文件就不用多说了,今天就说一下比较冷门的音频文件。
DSS、DS2文件转换的代码:
AudioFunctions ds2 = new AudioFunctions();
ds2.DSSToWav(income_fn, wav_fn);
需要的环境:
DPMControl.msi
OlyAudCom.dll
PIA.DpmCtrlLib.dll
代码:
下面是AudioFileSourceType2.OLYMPUS、 AudioFileSourceType2.PHILIPS的处理
public void DSSToWav(string dssFile, string outputFile, bool processUnknown = true){if (File.Exists(outputFile)){File.Delete(outputFile);}OlympusConverter converter = new OlympusConverter();AudioFileDetails2 fileDetails = converter.GetAudioFileDetails(dssFile);if (fileDetails == null){throw new Exception("Unable to determine file source type.");}else if (fileDetails.AudioFileSourceType == AudioFileSourceType2.OLYMPUS){Console.WriteLine("\tConverting with Olympus Converter");converter.Convert(dssFile, outputFile);converter = null;fileDetails = null;GC.Collect(2, GCCollectionMode.Forced);}else if (fileDetails.AudioFileSourceType == AudioFileSourceType2.PHILIPS){Console.WriteLine("\tConverting with Philips DPMCtrl Converter");PhilipsConverter DPMCtrl = new PhilipsConverter();DPMCtrl.convert(dssFile, outputFile, 0, false);DPMCtrl = null;converter = null;fileDetails = null;GC.Collect(2, GCCollectionMode.Forced);}else if (fileDetails.AudioFileSourceType == AudioFileSourceType2.UNKNOWN && processUnknown == true){Console.WriteLine("\tTrying DSS as DS2");string ds2File = dssFile + ".ds2";WavFileUtils.ChangeDSS2DS2(dssFile, ds2File);DSSToWav(ds2File, outputFile, false);File.Delete(ds2File);}}
public static void ChangeDSS2DS2(string inPath, string outPath){using (BinaryReader br = new BinaryReader(new FileStream(inPath, FileMode.Open))){using (BinaryWriter wr = new BinaryWriter(new FileStream(outPath, FileMode.Create))){int index = 0;int count = 1024;byte[] buffer = new byte[count];int bufferSize = 0;while ((bufferSize = br.Read(buffer, 0, count)) > 0){if (index == 0){for (int i = 0; i < 10; i++){if (buffer[i] == 'd' && buffer[i + 1] == 's' && buffer[i + 2] == 's'){buffer[i] = (byte)'d';buffer[i + 1] = (byte)'s';buffer[i + 2] = (byte)'2';break;}}}wr.Write(buffer, 0, bufferSize);index += bufferSize;}}}}
对于 AudioFileSourceType2.PHILIPS使用下面的代码进行处理:
public class PhilipsConverter{public void convert(string sourceFile, string destFile, int AudioFormat, bool delayed){try{IDPMControl iDPM = new DPMControl();iDPM.Initialize(false);//After initializing the DPMControl it is possible to extract job information from a file. //var fileInfo = iDPM.get_FileInfo(sourceFile);//The last arguement is the files password, as we do not support password protected DS2 it is always blank string. //If we get a password protected file then the task will fail. iDPM.Decrypt(sourceFile, destFile, (dpmAudioFormat)AudioFormat, delayed, "");iDPM.Deinitialize();}catch (COMException e){//From my testing, when passing a file with a password the Error message is DPMCtrlFileNotFound. var err = e.ErrorCode;dpmCtrlError dpmErr = (dpmCtrlError)err;Console.WriteLine(string.Format("****\nErrCode: {0}\nError: {1}\n****", err, dpmErr));throw new Exception("Failed to convert DS2 to Wav with error message " + dpmErr, innerException: e);}}}