How to relate audio data to time

断了今生、忘了曾经 提交于 2019-12-24 22:24:15

问题


Lets say I would like to read 1 millisecond of sound data from an Mp3 file and present it. Something like this:

var stream = NAudio.WaveStream.Mp3FileReader(filename);
int intervalInMilliseconds = someInterval;
int bytesPerInterval = ???;
for(int i = 0; i < bytesPerInterval;i++)
{
     Console.WriteLine(stream.GetByte());
}

What do I need to do to solve for bytesPerInterval? Is this even possible to do accurately? (Note:NAudio is not a requirement here, just what I am currently using.)

Or if I have a byte index, is it possible to determine what point in time that byte would be played (relative to time 00:00:0000)?


回答1:


You can't read 1 millisecond of audio data from MP3 directly. MP3 works in the frequency domain with fixed size time slices. A typical slice is around 13 milliseconds.

To get accurate timing, you need to convert your audio back to samples in the time domain. CD-quality audio is sampled 44,100 times per second with 16-bit PCM samples. Calculating the raw size in bytes for this data is simple:

sampleCount * bytesPerSample * channels

Also, one millisecond of audio doesn't evenly divide into 44.1kHz, so you will need to pick timing that does.



来源:https://stackoverflow.com/questions/28888838/how-to-relate-audio-data-to-time

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