双数组到WAV [英] Double array to wav

查看:78
本文介绍了双数组到WAV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将double []数组导出到wav文件.我在处理标头(RIFF,fmt等)时遇到了一些麻烦.有什么想法我应该如何处理吗?也许使用BitConverter将double []数组转换为byte []数组,然后使用NAudio的waveFileWriter?任何示例代码都非常感谢!

I want to export a double[] array to a wav file. I'm having some trouble manipulating the headers (RIFF, fmt, etc) . Any ideas how I should approach this? Maybe use BitConverter to turn the double[] array into a byte[] array and then use NAudio's waveFileWriter? Any sample code much appreciated!

推荐答案

只需使用WaveFileWriter的WriteSample方法逐个编写每个样本,或者使用WriteSamples一次完成.由于您有一个双精度数组,因此可以先使用LINQ转换为浮点数组.

Just use WaveFileWriter's WriteSample method to write each sample one by one, or WriteSamples to do it in one hit. Since you have an array of doubles, you can use a bit of LINQ to convert to a float array first.

float[] floatArray = doubleArray.Select(s => (float)s).ToArray();
writer.WriteSamples(floatArray, 0, floatArray.Length);

这会将每个样本写入WaveFileWriter,转换为正确的位深度(例如16位或32位IEEE浮点数),具体取决于打开WaveFileWriter

This will write each sample to the WaveFileWriter, converting to the correct bit depth (e.g. 16 bit or 32 bit IEEE float), which depends on the WaveFormat you selected when you opened the WaveFileWriter

这篇关于双数组到WAV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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