问题
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