使用C#进行Flac音频转换

时间:2017-02-20 09:11:29

标签: c# audio wav flac google-speech-api

我正在尝试将谷歌语音用于文本API。我很难将wav音频文件转换为flac音频格式。这是我的代码:

这是wav录制代码:

 private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
         if (waveWriter == null) return;

         waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
         waveWriter.Flush();
    }
    fileName = "D:\\voice.wav";
    int deviceNumber = hardware.SelectedItems[0].Index;
    try
    {
        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.DeviceNumber = deviceNumber;
        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

        sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
        waveWriter = new NAudio.Wave.WaveFileWriter(fileName, sourceStream.WaveFormat);

        sourceStream.StartRecording();
    }
    catch (Exception ex)
    {
        state.Text = "disini" + ex.Message;
    }

flac转换器代码

 string inputFile = Path.Combine("wav ", input);
    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);

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

我已从以下linklink2获取此代码的参考 我的问题是在导入dll时出现的。我已经导入了Alvas.dll,NAudio.dll但仍然是我的FlacWriter类,并且有一些WaveReader类参数无法识别。

0 个答案:

没有答案