首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NAudio将输入字节数组转换为双精度数组

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

Stack Overflow用户
提问于 2010-10-14 09:22:52
回答 1查看 3.9K关注 0票数 3

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

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

代码语言:javascript
复制
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之间的双精度数组。如果有人可以帮助我,那就太好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-14 21:46:30

您得到的缓冲区将包含16位短值。您可以使用NAudio中的WaveBuffer类,这样可以很容易地将样本值作为短值读出。除以32768可以得到双精度/浮点样本值。

代码语言:javascript
复制
    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;                
        }
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3929389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档