n音讯 - 转换32位WAV到16位WAV [英] Naudio - Convert 32 bit wav to 16 bit wav

查看:1801
本文介绍了n音讯 - 转换32位WAV到16位WAV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想一个32位立体声WAV转换成16位单声道的WAV。我用n音讯捕捉到的声音我并认为只用两个四个显著字节会工作。

I've been trying to convert a 32 bit stereo wav to 16 bit mono wav. I use naudio to capture the sound I and thought that using just the two of four more significant bytes will work.

下面是DataAvailable实现:

Here is the DataAvailable implementation:

void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
     byte[] newArray = new byte[e.BytesRecorded / 2];
     short two;
     for (int i = 0, j = 0; i < e.BytesRecorded; i = i + 4, j = j + 2)
     {
          two = (short)BitConverter.ToInt16(e.Buffer, i + 2);


          newArray[j] = (byte)(two & 0xFF);
          newArray[j + 1] = (byte)((two >> 8) & 0xFF);
     }
     //do something with the new array:
}

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

推荐答案

我终于找到了解决办法。我只是用32767乘以换算值,并将其转换为短:

I finally found the solution. I just had to multiply the converted value by 32767 and cast it to short:

void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    byte[] newArray16Bit = new byte[e.BytesRecorded / 2];
    short two;
    float value;
    for (int i = 0, j = 0; i < e.BytesRecorded; i += 4, j += 2)
    {
        value = (BitConverter.ToSingle(e.Buffer, i));
        two = (short)(value * short.MaxValue);

        newArray16Bit[j] = (byte)(two & 0xFF);
        newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
    }
}

这篇关于n音讯 - 转换32位WAV到16位WAV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆