gordonklaus / portaudio

Go bindings for the PortAudio audio I/O library
MIT License
704 stars 95 forks source link

Running mp3 example with hajimehoshi/go-mp3 library #45

Closed ygabuev closed 3 years ago

ygabuev commented 3 years ago

Hi!

I am trying to reproduce the mp3 example but using a different mp3 library. My current attempt is available in this gist.

The code compiles, the sound is playing, but it is heavily distorted - there is a glitch per each read-write cycle. One can see that by tweaking the bufsize variable - the smaller the buffer, the higher the glitching frequency.

I took a look at this issue, but I cannot quite figure out what is the problem in my case :(

Thanks! Yuriy

gordonklaus commented 3 years ago

Hi!

My initial thoughts are that the mp3 library you are using is either too slow or is introducing artifacts. You can test the first hypothesis by decoding your mp3 ahead of time. If the problem persists then the second hypothesis must be true.

Also, check the int return from decoder.Read to be sure it is filling the whole buffer.

I hope this helps.

ygabuev commented 3 years ago

Thanks for the advice! I'll try out your suggestions in the upcoming days and post an update here.

ygabuev commented 3 years ago

I played around with the code and discovered the following. It turns out that decoder.Read always reads a specific number of bytes (4608 in my case) regardless of the provided buffer's size, which is why I get the glitches unless I set the buffer length to that specific value.

Do you know, is that the usual case for mp3 decoders? Or have I not found some hidden switch that lets me set or at least see that buffer length in advance?

UPD: the hidden switch is to use io.ReadFull (as suggested here) :)

gordonklaus commented 3 years ago

I see. That must be an optimization in the mp3 library to return fewer than the requested number of bytes. To avoid unnecessary buffering, I guess.

Glad you found the solution. I assume your code works as expected now?

ygabuev commented 3 years ago

Yes, everything works now. @gordonklaus thank you for your help and for your work on the library!