2015-03-02 82 views
0

我在我的代碼中使用此WaveReader class。我得到這個錯誤:無法將Wav轉換爲FLAC,C#,1錯誤

ERROR: Ensure that samples are integers (e.g. not floating-point numbers)

if (format.wFormatTag != 1) // 1 = PCM 2 = Float 
     throw new ApplicationException("Format tag " + format.wFormatTag + " is not supported!"); 

所有我想要的WAV文件轉換成FLAC,所以我可以將其提供給GoogleSpeechAPI。我可以做第一步,記錄WAV文件。我被困在第二步:將WAV文件轉換爲FLAC。我可以做第三步:使用GoogleSpeech API將FLAC轉換爲文本。

對於第二步,在那裏我被卡住,這裏是我的代碼:

public void WAV_to_FLAC_converter() 
    { 
     string inputFile = "inputFile.wav"; 
     //string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac")); 
     string outputFile = "outputFile.flac"; 

     if (!File.Exists(inputFile)) 
      throw new ApplicationException("Input file " + inputFile + " cannot be found!"); 
     var stream = File.OpenRead(@"C:\inputFile.wav"); 
     WavReader wav = new WavReader(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;//**I GET THE ABOVE ERROR HERE.** 
      do 
      { 
       bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length); 
       flac.Write(buffer, 0, bytesRead); 
      } while (bytesRead > 0); 
      flac.Dispose(); 
      flac = null; 
     } 
    } 

顯然有一些錯誤輸入wav文件我給的功能。我認爲它說我創建的流變量是浮點而不是整數。但是我應該怎麼做?我沒有弄亂WAV文件。這只是一個WAV文件。我怎樣才能改變一個WAV文件從浮點到整數?我不知道如何解決這個問題。

回答

2

我已經用隨機波形文件測試了你的代碼,它工作完美。 後來我從here下載了一個立體聲32位浮點數據波形樣本,我得到了同樣的錯誤,你:

ERROR: Ensure that samples are integers (e.g. not floating-point numbers)

然後我調試的代碼和以下異常被拋出

// Ensure that samples are 16 or 24-bit 
if (format.wBitsPerSample != 16 && format.wBitsPerSample != 24) 
    throw new ApplicationException(format.wBitsPerSample + " bits per sample is not supported by FLAC!"); 

我害怕WavReader類不支持32位浮點波形採樣,FlacWriter也不支持。

更新:我現在得到您的項目工作。您必須在調試文件夾中將libFlac.dll重命名爲LibFlac.dll。加載庫不應該有更多的問題。我得到的是一個PInvokeStackImabalance異常。如果你也瞭解,你可以按照here之後的說明進行操作,或者在Debug-> Exceptions-> Managed Debugging Assistans-> PInvokeStackImalance下關閉這種類型的異常。

+0

我將bin/Debug下的DLL文件重命名爲LibFlac.dll。我仍然得到DLLNotFoundException unhandeled。我確定'PInvokeStackImbalance'被設置爲'拋出',但我沒有在你的主要方法中實現異常 – 2015-03-05 15:12:00

+0

實現'Console.WriteLine(Environment.CurrentDirectory);'...在這個目錄中複製dll(和重命名它) – stefankmitph 2015-03-05 15:18:55

+0

yupp這是我添加LibFlac.dll相同的目錄。 我從網上下載了另一個LibFlac_dynamic.dll。我在依賴walker中打開了這個新的libFlac_dynamic.dll,它沒有錯誤。但我以前使用的LibFlac.dll在dependancyWalker中有錯誤。所以我將LibFlac_dynamic.dll重命名爲LibFlac.dll並將其放入當前目錄並運行。我得到一個新的錯誤:BadImageFormatException是unhandeled。試圖加載格式不正確的程序。 (來自HRESULT的異常:0x8007000B) – 2015-03-05 15:33:46

相關問題