Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working example with go-mpg123 #16

Closed
dadleyy opened this issue Jun 13, 2017 · 8 comments
Closed

Working example with go-mpg123 #16

dadleyy opened this issue Jun 13, 2017 · 8 comments

Comments

@dadleyy
Copy link

dadleyy commented Jun 13, 2017

I'm working on a project where I will be using go-mpg123 (a port of the libmpg123 bindings in golang) but I'm having a hard time getting that library to play nicely with port audio.

I've gotten reasonably far in this gist but the audio playback is really distorted for some reason and I cant figure out what would be the issue.

Has anyone been able to get these two libraries successfully working together, and if so is there anything blatantly wrong with my code example? If I'm able to get something working, would this be a good candidate for the examples directory?

@gordonklaus
Copy link
Owner

This sounds like an OK candidate for the examples directory.

Regarding making it work: I recommend checking two things: the currently ignored first return value from mp3.Read, to check whether the whole buffer is being filled; and the time between iterations of the read/write loop, to see if you are underrunning the audio buffers. You may need to add buffering (e.g., by reading the mp3 in one goroutine and sending the read audio over a channel to the playout goroutine).

@svanharmelen
Copy link
Contributor

@dadleyy did you ever fixed the problem? I have the exact same issue right now, using the same packages...

@svanharmelen
Copy link
Contributor

@gordonklaus been reading your reply a couple of times, but fail to fully understand what you are suggesting. Of course the comment about mp3.Read is clear, it's the other part I'm talking about.

Would love to get a working setup that gives me clear sound instead of the heavily distorted sound I have now.

And just FYI: next to the output being distorted is also seems to be much slower making a woman's voice sound low like a man's voice...

@gordonklaus
Copy link
Owner

@svanharmelen On second thought, timing the loop and adding buffering should be unnecessary at this point. The more likely culprit for your distorted audio is the format of the buffer.

The values of channels, encoding, and format are ignored. These values are essential in interpreting the bytes in the audio buffer. The raw []byte is not suitable for passing to portaudio; these bytes must be interpreted into a data structure that reflects the audio format.

For example, if format is Stereo16, then the data passed to portaudio could be [][]int16 (or maybe [][]uint16, the signedness seems to be lost in the mpg123 API). See portaudio.Buffer for more info.

@svanharmelen
Copy link
Contributor

Not sure how I should go about this... The mp3.Read func takes a []byte (so a []uint8) to read into... So I tried something like this:

callback := func(_, out []int16) {
    size, err := mp3.Read(audio)
    if err != nil {
        log.Fatal(err)
    }

    for i := range audio[:size] {
        out[i] = int16(audio[i])
    }
}

But this doesn't change a thing (which makes sense 😞 ) The file I am working with is mono16 (signed), so []int16 seems to be the correct data format. Just fail to understand how I can/should interpreted/convert the []byte into that []int16 correctly...

@gordonklaus
Copy link
Owner

Yep, []int16 looks right, but try []uint16 too.

Here's how to interpret bytes as 16 bit integers:

import (
    "bytes"
    "encoding/binary"
)
...
audio := make([]byte, 2*len(out))
mp3.Read(audio)
binary.Read(bytes.NewBuffer(audio), binary.LittleEndian, out)

@svanharmelen
Copy link
Contributor

@gordonklaus was just playing with the binary.Read function, but got an unexpected EOF which I didn't understand. So I missed the 2*len(out) part, which of course makes complete sense now I see it in your snippet 😏

So thank you very much for your help! It now works perfectly 😀 I'll create and post back a cleaned up gist that can be used for reference for anyone else dropping by here with a similar question.

I do have one more (hopefully) small/simple question... Is there a way to ask the stream if it is still playing, or that it is stopped? I'm not how else I can tell when my function can exit.

Thanks!!

@gordonklaus
Copy link
Owner

@svanharmelen Great! Glad to help 😄

Typically, you tell the stream to stop when you are done with it; it will not stop on its own. In your example, when mp3.Read returns EOF you would call stream.Stop and signal the main goroutine (via a channel, e.g.) to exit. Or, a simpler way in this case is to do as @dadleyy did and call the synchronous stream.Write method from the main goroutine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants