2013-06-26 355 views
1

我正在嘗試將16位樣本轉換爲32位浮點。但是,我遇到了一個問題,我的32位浮點文件的音頻時間減半了。 (例如,我的輸入是四秒鐘的16位PCMWAV,我的輸出變成了一個兩秒IEEE浮點WAV。)下面是代碼。將16位轉換爲32位浮點

using (WaveFileReader reader = new WaveFileReader(file.wav)) 
{ 
    IWaveProvider stream32 = new Wave16ToFloatProvider(reader); 

    using (WaveFileWriter converted = new WaveFileWriter(temp.wav)) 
    { 
     // Buffer length needs to be a power of 2 for FFT to work nicely. 
     // However, make the buffer too long and pitches aren't detected fast enough. 
     // Successful buffer sizes: 8192, 4096, 2048, 1024 (some 
     // pitch detection algorithms need at least 2048). 
     byte[] buffer = new byte[8192]; 
     int bytesRead; 
     do 
     { 
      bytesRead = stream32.Read(buffer, 0, buffer.Length); 
      converted.Write(buffer, 0, bytesRead); 
     } while (bytesRead != 0 && converted.Length < reader.Length); 
    } 
} 
+0

退出時「converted.Length」和「reader.Length」的期望大小是多少?我懷疑這個檢查是否會讓你在一半時間內過早退出 –

+0

非常感謝。發現conversion.Length

回答

3

退出條件:

} while (bytesRead != 0 && converted.Length < reader.Length); 

看起來可疑的;它聽起來像你擴大的數據,所以我們應該預期converted.Lengthreader.Length末 - 所以我的猜測是,這個退出條件導致其過早退出。

+0

是的。這是問題所在。謝謝XD –