How to get sound data sample value in c#

邮差的信 提交于 2019-12-29 09:14:09

问题


I need to get the sample values of sound data of a WAV file so that by using those sample values i need to get the amplitude values of that sound data in every second.

Important: Is there any way to get audio data sample values using Naudio library or wmp library?

I am getting the sample values in this way:

byte[] data = File.ReadAllBytes(File_textBox.Text);
            var samples=new int[data.Length];
            int x = 0;
            for (int i = 44; i <data.Length; i += 2)
            {
                samples[x] = BitConverter.ToInt16(data, i);
                x++;
            }

But I am getting negative values more like (-326260). so is this right or wrong? I mean can a sample value be negative or not and if it is correct then what does it mean a sound or silence?


回答1:


NAudio can do this for you, it's in the library (I think it's the WaveStream or WaveReader class, or something similar). I can recommend it's use, if it's not too much overhead.

If you want to roll-your-own, and want to deal with arbitrary wave files, you'll have to read up on the WAV file format, and analyze the header yourself.

Although in general a WAV file contains 16bit samples, it doesn't have to, and depending on the exact format, they might be stored in little endian or big endian.

The header contains information about sample rate, number of channels, bits per sample, bytes per sample and such like, which allow you to do the actual math to get exactly one sample.



来源:https://stackoverflow.com/questions/10477697/how-to-get-sound-data-sample-value-in-c-sharp

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