2014-06-11 19 views
0

我有一大堆的.wav文件需要根據提供給它的文件名列表由程序連接起來。 該程序保持將清晰的音頻文件轉換爲垃圾..經過大膽調查後,我意識到這些文件已被全部錄製在一些單聲道格式的不同設備上,一些立體聲,採樣率不同。將這些文件轉換爲所需的通用採樣率 - 全部採用立體聲等格式,我需要在大膽模式下運行一些批量轉換。 爲此,我需要將文件分成具有相同採樣率和相同軌道數量的組。 我在c#中編寫了一個實用程序,它可以掃描具有所有這些文件的文件夾,並給我一個用逗號分隔的txt文件文件名和格式信息。 我正在將文件流讀入字節數組,但解釋格式化信息時 - 我認爲程序以某種方式在每個格式信息的末尾添加了額外的0,根據格式信息對波形文件列表進行排序

出了什麼問題?任何幫助表示讚賞。下面 相關的代碼 -

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@pathString)) 
{ 
    fileLine += "[Resource Name ], [File Size], [Format Chunk Size], [isPCM ? ], [Num of Channels], [Sample Rate], [(Sample Rate * BitsPerSample * Channels)/8],[BitsPerSample * Channels)/8], [Bits PerSample], [Data Size ]" + Environment.NewLine; 
    file.WriteLine(fileLine); 
    fileLine = ""; 
    for (i = 0; i < FileNames.Length; i++) 
    { 
     string fname = ""; 
     fname = FileNames[i]; 
     fileLine += "'" + fname + "',"; 
     Byte[] resBytesArr = ByteArrFromFileStream(FileNames[i]); 
     if (resBytesArr.Length > 44) 
     { 
     // file size 
     fileLine += "," + resBytesArr[4] + resBytesArr[5] + resBytesArr[6] + resBytesArr[7] ; 
     // fmt size 
     fileLine += "," + resBytesArr[16] + resBytesArr[17] + resBytesArr[18] + resBytesArr[19] ; 
     // format ? 1 for PCM 
     fileLine += "," + resBytesArr[20] + resBytesArr[21]; 
     // num of channels 
     fileLine += "," + resBytesArr[22] + resBytesArr[23] ; 
     // sample rate 
     fileLine += "," + resBytesArr[24] + resBytesArr[25] + resBytesArr[26] + resBytesArr[27] ; 
     //(Sample Rate * BitsPerSample * Channels)/8 
     fileLine += "," + resBytesArr[28] + resBytesArr[29] + resBytesArr[30] + resBytesArr[31] ; 
     //(BitsPerSample * Channels)/8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo 
     fileLine += "," + resBytesArr[32] + resBytesArr[33] ; 
     //Bits per sample 
     fileLine += "," + resBytesArr[34] + resBytesArr[35] ; 
     // data size 
     fileLine += "," + resBytesArr[40] + resBytesArr[41] + resBytesArr[42] + resBytesArr[43] ; 
     // end of info for one resource, move to next 
     fileLine += Environment.NewLine; 
     file.WriteLine(fileLine); 
     fileLine = ""; 
     file.WriteLine(".....done." + Environment.NewLine); 
     } 
     else 
     { 
     file.WriteLine("Byte Array seems invalid for " + fname + Environment.NewLine); 
     } 
    } 
} 

回答

0

記住陣列成員(包括字節[])被自動初始化爲數組類型的默認初始值。字節[]數組被初始化爲0x0。如果沒有真正看到發生了什麼事在:

ByteArrFromFileStream(...) 

我不得不猜測,我的猜測是,在某些文件,最初編碼的二進制流的實際應用程序創建一個標準的塊大小的malloc( )內存分配緩衝區。當流的實際編碼完成時,流的長度比最後的內存分配少了幾個字節。所以當文件寫入磁盤時,一堆0x0就會被寫出來。

作爲一個方面說明,當使用任何的「流」類型的一定要小心,誰和什麼關停流,進一步閱讀我推薦這篇文章的精讀:http://msdn.microsoft.com/en-us/library/bb985010.aspx

相關問題