NAudio 将输入字节数组转换为双精度数组

发布于 2024-09-27 16:56:29 字数 738 浏览 0 评论 0原文

我对 NAudio 非常陌生,我需要将输入样本的缓冲区从输入设备转换为范围从 -1 到 1 的双精度数组。

我按如下方式创建输入设备:

WaveIn inputDevice = new WaveIn();

//change the input device to the one i want to receive audio from  
inputDevice.DeviceNumber = 1;

//change the wave format to what i want it to be.  
inputDevice.WaveFormat = new WaveFormat(24000, 16, 2);

//set up the event handlers  
inputDevice.DataAvailable += 
   new EventHandler<WaveInEventArgs>(inputDevice_DataAvailable);
inputDevice.RecordingStopped += 
   new EventHandler(inputDevice_RecordingStopped);  

//start the device recording  
inputDevice.StartRecording();

现在,当调用“inputDevice_DataAvailable”回调时我得到了音频数据的缓冲区。我需要将这些数据转换为代表 -1 和 1 之间音量级别的双精度数组。如果有人可以帮助我,那就太好了。

I am very new to NAudio and I need to convert the buffer of input samples from an input device to an array of doubles that range from -1 to 1.

I create the input device as follows:

WaveIn inputDevice = new WaveIn();

//change the input device to the one i want to receive audio from  
inputDevice.DeviceNumber = 1;

//change the wave format to what i want it to be.  
inputDevice.WaveFormat = new WaveFormat(24000, 16, 2);

//set up the event handlers  
inputDevice.DataAvailable += 
   new EventHandler<WaveInEventArgs>(inputDevice_DataAvailable);
inputDevice.RecordingStopped += 
   new EventHandler(inputDevice_RecordingStopped);  

//start the device recording  
inputDevice.StartRecording();

Now when the 'inputDevice_DataAvailable' callback gets called I get a buffer of audio data. I need to convert this data to and array of doubles that represent volume levels between -1 and 1. If anyone can help me out that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-04 16:56:29

您返回的缓冲区将包含 16 位短值。您可以使用 NAudio 中的 WaveBuffer 类,这将可以轻松地将示例值读取为 Shorts。除以 32768 即可得到双精度/浮点样本值。

    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        byte[] buffer = e.Buffer;

        for (int index = 0; index < e.BytesRecorded; index += 2)
        {
            short sample = (short)((buffer[index + 1] << 8) |
                                    buffer[index]);
            float sample32 = sample / 32768f;                
        }
    }

the buffer you get back will contain 16 bit short values. You can use the WaveBuffer class from NAudio which will make it easy to read the sample values out as shorts. Divide by 32768 to get your double/float sample value.

    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        byte[] buffer = e.Buffer;

        for (int index = 0; index < e.BytesRecorded; index += 2)
        {
            short sample = (short)((buffer[index + 1] << 8) |
                                    buffer[index]);
            float sample32 = sample / 32768f;                
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文