c# - C#中的.wav到.flac转换器

标签 c# audio

我的程序正在以.wav录制语音并将其转换为.flac
我会将这个.flac文件发送给Google,希望我能收到语音提示。
但是当我的程序尝试将文件发送给google时出现错误,“该进程无法访问文件'C:\ Users \ Ahmad Mustofa \ Documents \ Visual Studio 2010 \ Projects \ FP \ voice.flac',因为该文件正在使用另一个过程。” 。我不知道哪个进程仍在使用该文件。
这是我的代码:

        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;
}

...

关于c# - C#中的.wav到.flac转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925497/

相关文章:

c# - 引用库二进制 - 调试或发布版本?

c# - 当我使用 PingAsync 同时 ping 到多个 IP 时,如何获取 ping 结果(例如往返时间和 ping 状态)?

javascript - 音频文件仅播放一次,简单示例 Tabrisjs 2.4

ios - 如何在iOS应用程序中随着背景音乐一起播放声音?

c# - 从 POJO 到 POCO 的转换

c# - ASP.NET Core 应用程序不从输出目录中读取 appsettings.json,而是从项目一中读取

c# - 这是可解决的 ‘-4’ 不是属性 ‘Width’ 的有效值 ..?

javascript - jplayer 多个实例一页 - 但是数量是可变的

grails - 在Groovy中生成音频波形

javascript - 在浏览器中以低延迟捕获声音输入