我正在嘗試使用XNA麥克風來捕獲音頻並將其傳遞給API我已經分析了用於顯示目的的數據。但是,API需要音頻數據爲16位整數數組。所以我的問題是相當直接的。將字節數組轉換爲短陣列的最有效方法是什麼?高效地將音頻字節 - 字節[]縮短[]
private void _microphone_BufferReady(object sender, System.EventArgs e)
{
_microphone.GetData(_buffer);
short[] shorts;
//Convert and pass the 16 bit samples
ProcessData(shorts);
}
乾杯,戴夫
編輯:這是我想出了,似乎工作,但能不能做到更快?
private short[] ConvertBytesToShorts(byte[] bytesBuffer)
{
//Shorts array should be half the size of the bytes buffer, as each short represents 2 bytes (16bits)
short[] shorts = new short[bytesBuffer.Length/2];
int currentStartIndex = 0;
for (int i = 0; i < shorts.Length - 1; i++)
{
//Convert the 2 bytes at the currentStartIndex to a short
shorts[i] = BitConverter.ToInt16(bytesBuffer, currentStartIndex);
//increment by 2, ready to combine the next 2 bytes in the buffer
currentStartIndex += 2;
}
return shorts;
}
是否需要將「bytes」組合爲「shorts」,還是更像是迭代演員? – Jodrell 2011-04-20 11:13:52
什麼類型是_buffer,byte []? – Jodrell 2011-04-20 12:13:02
@Jodrell - 是的,它是.. – 2011-04-20 12:39:41