.wav到.flac的c#转换器
本文关键字:转换器 flac wav | 更新日期: 2023-09-27 18:05:35
我的程序正在录制。wav格式的声音,并将其转换为。flac格式
我将把这个。flac文件发给google,希望我能得到声音的文本。
但是当我的程序试图将文件发送到谷歌时出现错误,"该进程无法访问文件'C:'Users'Ahmad Mustofa'Documents'Visual Studio 2010'Projects'FP'voice。因为它正在被另一个进程使用。"。我不知道哪个进程还在使用那个文件。下面是我的代码:
string inputFile = Path.Combine("wav ", input);//the converter
string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
if (!File.Exists(inputFile))
throw new ApplicationException("Input file " + inputFile + " cannot be found!");
WavReader wav = new WavReader(inputFile);
FlacWriter flac = new FlacWriter(File.Create(outputFile), wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
wav.Dispose();
wav = null;
//the sender
FileStream FS_Audiofile = new FileStream("C:''Users''Ahmad Mustofa''Documents''Visual Studio 2010''Projects''FP''voice.flac", FileMode.Open, FileAccess.Read);
BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
FS_Audiofile.Close();
BR_Audiofile.Close();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
_HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
}
您确定FlacWriter
会自动处理Stream
吗?您可以尝试这样做:
...
using (var flacStream = File.Create(outputFile))
{
FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
}
...