NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

Trouble decoding MP3 from a memory stream

Open Shavais opened this issue 2 years ago • 2 comments

Hello,

I hope to deploy mp3's in an encrypted zip file with my program, and stream them from the zip file at run time, so I'm trying to get streaming from a memory stream working. I have already verified that the part of my code that streams the data in from the zip file is working and the data matches the same file read from disk. I'm having trouble at the last line of this piece of code right here:


                        var frame = Mp3Frame.LoadFromStream(sourceReader.BaseStream);
                        if (frame != null)
                        {
                            if (decompressor == null)
                            {
                                // the buffered provider doesn't know what sample rate it is working at until we have a frame
                                WaveFormat waveFormat = new Mp3WaveFormat(
                                    frame.SampleRate,
                                    frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
                                    frame.FrameLength,
                                    frame.BitRate
                                );
                                
                                decompressor = new AcmMp3FrameDecompressor(waveFormat);
                            }

                            var frameBuffer = new byte[16384 * 4];
                            int decompressed = decompressor.DecompressFrame(frame, frameBuffer, 0);

In stepping through, I see that decompressed is 0, and the frameBuffer contains all zeros. It appears that the frame reading is working fine, although I notice the wafeFormat.BitsPerSample is 0. Unfortunately that field is read only. The frame says the bit rate is 414400 and the sample rate is 128000, so I guess the bits per sample are somehow less than 8? Maybe that's the problem? I wonder what's causing that..

Can you tell what I'm doing wrong?

Thanks & Regards, Shavais

Shavais avatar Dec 09 '22 10:12 Shavais

@Shavais Did you ever figure out your issue? I'm also seeing decompressed being 0 for me as well for some frames, which seems to be the reason I have hiccups when I play my audio back

qwbarch avatar Jan 03 '24 07:01 qwbarch

Nope, I gave up and switched to C# for that project. It uses NAudio and DotNetZip and it's working great.

Some years back I worked with an Intel team for a while, and one of the team members was tasked with building a multi-media viewing component for the C++ app we were working on. Several months in, he was still wrestling with bugs and problems that as I recall were not terribly dissimilar.

Shavais avatar Jan 04 '24 00:01 Shavais