问题
I'm working on streaming mp3 player. Initialy when creating AcmMp3FrameDecompressor I used the first frame to determine wave format as the demo shows. Then I realized that some files play at the wrong speed (usually slower). I looked at the implementation of Mp3FileReader and found that it decides between the first and the second frames.
// workaround for a longstanding issue with some files failing to load
// because they report a spurious sample rate change
var secondFrame = Mp3Frame.LoadFromStream(mp3Stream);
if (secondFrame != null &&
(secondFrame.SampleRate != firstFrame.SampleRate ||
secondFrame.ChannelMode != firstFrame.ChannelMode))
{
...
firstFrame = secondFrame;
}
I've started using the same approach and it resolved the issue with playback speed. Later I found a bunch of files which still not playing correctly. I extended the logic to consider the third frame. That resolved some more issues but finally I got the problem when the decompressor throws NAudio.MmException: AcmNotPossible calling acmStreamConvert.
I've decided to analyze all the starting file frames. I've created decompressor with wave format from each of first 10 mp3 frames and tried to decompress the whole file. Here are the results. The first column is frame number used for decompressor initialization, the second is the number of frames successfully decompressed, then are the properties of the frame.
0: decompressed: 6215 sample rate: 44100 Stereo bitrate: 96000
1: decompressed: 6215 sample rate: 44100 JointStereo bitrate: 128000
2: decompressed: 0 sample rate: 12000 DualChannel bitrate: 96000
3: decompressed: 0 sample rate: 44100 Mono bitrate: 224000
4: decompressed: 0 sample rate: 16000 Mono bitrate: 24000
5: decompressed: 6215 sample rate: 48000 DualChannel bitrate: 64000
6: decompressed: 6215 sample rate: 44100 DualChannel bitrate: 96000
7: decompressed: 0 sample rate: 48000 Mono bitrate: 96000
8: decompressed: 0 sample rate: 11025 JointStereo bitrate: 112000
9: decompressed: 6215 sample rate: 32000 JointStereo bitrate: 160000
10:decompressed: 6215 sample rate: 48000 JointStereo bitrate: 384000
Is there any way of determining the best frame for that? I play audio from a stream so I can't read the whole file and calculate average bit rate as the Mp3FileReader does. Or maybe it's possible to create separate decompressor for each frame or subset of frames? I tried doing that but new decompressor always returns empty data for first decompressed frame.
回答1:
I think these are album art being misinterpreted as valid MP3 frames. Ideally the NAudio code should be updated to look for say 10 consecutive frames in a row with the same sample rate and channel count before deciding it has found the start of the real audio.
来源:https://stackoverflow.com/questions/36754560/naudio-select-the-proper-frame-for-acmmp3framedecompressor