Comments (8)
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.
@dadleyy did you ever fixed the problem? I have the exact same issue right now, using the same packages...
from portaudio.
@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.
@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.
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.
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.
@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.
@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)
- Opening a stream with a framesPerBuffer of 0 causes a buffer underrun and a stutter HOT 1
- "Unknow Error" in MacOS Big Sur HOT 1
- Get nothing using OpenDefaultStream in the latest mac os Big Sur. HOT 1
- unable to open on a device HOT 6
- Does not appear to work with PipeWire HOT 3
- Running mp3 example with hajimehoshi/go-mp3 library HOT 5
- Playing Opus stream causes playback half speed HOT 3
- Issues installing on Windows HOT 5
- how can I use go get ? HOT 2
- GEt live audio from microphone HOT 2
- Resulting windows apps built with golang 1.17.5 crash on startup. HOT 1
- windows dll HOT 4
- MacOS compile Windows error HOT 2
- Example `play.go` not working correctly HOT 1
- "go install <your portaudio module>" doesn't work HOT 3
- play.go example aiff file sounds "strange" HOT 2
- How do I get microphone level? [HELP] HOT 2
- fatal error: checkptr: pointer arithmetic result points to invalid allocation HOT 7
- examples/record.go not working keep getting panice error
- The built binary file cannot run another Windows HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from portaudio.