Easiest way to read 2-channel samples into array from WaveStream

假如想象 提交于 2019-12-04 03:11:35

BlockAlignReductionStream is unnecessary. Here's one simple way to read out of your buffer and into separate 16 bit left and right sample buffers.

using (WaveFileReader pcm = new WaveFileReader(@"file.wav"))
{
    int samplesDesired = 5000;
    byte[] buffer = new byte[samplesDesired * 4];
    short[] left = new short[samplesDesired];
    short[] right = new short[samplesDesired];
    int bytesRead = pcm.Read(buffer, 0, 10000);
    int index = 0;
    for(int sample = 0; sample < bytesRead/4; sample++)
    { 
        left[sample] = BitConverter.ToInt16(buffer, index);
        index += 2;
        right[sample] = BitConverter.ToInt16(buffer, index);
        index += 2;
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!