Double array to wav

北城以北 提交于 2019-12-25 10:01:24

问题


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!


回答1:


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);

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




回答2:


You are going to have to write all of the necessary bits of the WAV file according to standards. You will have issues with mono vs. stereo and sampling rate issues too. Strongly recommended you look form some source code that already exists that deals with this issues (I did not find a any C# version in a quick search, though there were several C/C++ version). Trying to write the code just based on a header spec will be hit or miss



来源:https://stackoverflow.com/questions/19715553/double-array-to-wav

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!