Coder Social home page Coder Social logo

Comments (8)

gordonklaus avatar gordonklaus commented on July 24, 2024

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).

from portaudio.

svanharmelen avatar svanharmelen commented on July 24, 2024

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

from portaudio.

svanharmelen avatar svanharmelen commented on July 24, 2024

@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...

from portaudio.

gordonklaus avatar gordonklaus commented on July 24, 2024

@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.

from portaudio.

svanharmelen avatar svanharmelen commented on July 24, 2024

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...

from portaudio.

gordonklaus avatar gordonklaus commented on July 24, 2024

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)

from portaudio.

svanharmelen avatar svanharmelen commented on July 24, 2024

@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!!

from portaudio.

gordonklaus avatar gordonklaus commented on July 24, 2024

@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.

from portaudio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.