faiface / beep

A little package that brings sound to any Go application. Suitable for playback and audio-processing.
MIT License
2.08k stars 152 forks source link

Audio blocking #10

Closed WesleiRamos closed 6 years ago

WesleiRamos commented 6 years ago

First of all, I dont speak english (i'm brazilian) Well, I'm using this code

package main

import "os"
import "time"
import "github.com/faiface/beep/mp3"
import "github.com/faiface/beep/speaker"

func main() {
    f, _ := os.Open("test.mp3")
    s, format, _ := mp3.Decode(f)
    speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
    speaker.Play(s)
    select {}
}

And when the program is in the background the audio stops and returns successively.

faiface commented 6 years ago

So, your program never returns, right? You want it to return, is that correct?

If so, you can follow the little tutorial in README, that discusses this issue and shows a solution.

WesleiRamos commented 6 years ago

I don't think I explained it right

I recorded the sound output of windows with audacity https://soundcloud.com/weslei-ramos-2/problem

There are many interruptions when playing

faiface commented 6 years ago

Interesting. Does this only happen when the program is in the background?

Beep uses https://godoc.org/github.com/hajimehoshi/oto for low-level audio playback. Currently, the latency is best on Linux. On other platforms, if you set too small buffer size (the second argument to beep.Init), it might cause something like you're experiencing. However, buffer size of 1/10s should be perfectly fine for all platforms.

WesleiRamos commented 6 years ago

Does this only happen when the program is in the background?

Sometimes on program window too

I changed time.Second/10 to 48000 and works more fine, but audio stops before length

faiface commented 6 years ago

Try chaning time/Second/10 to time.Second and tell what happens.

WesleiRamos commented 6 years ago

I tried it before and the audio stops before length (few seconds)

faiface commented 6 years ago

Did you change your program to exit immediately after playing the audio? If so, stopping some time before the end is expected, because you exit when everything is pushed to the buffer, not when the buffer is finished playing. You know, let's say the buffer has size time.Second and you push the last chunk to the buffer (which will be at most one second long) and then you exit, so the last chunk won't play. This can't be fixed in any exact way, you just have to wait for a little while before exiting the program. However, this behaviour will occur only when exiting the program, so only solve it there.

faiface commented 6 years ago

Look at it like this. Bigger the buffer size, more stable the playback (but at some point it's unreasonable to increase the buffer size). Lower the buffer size, lower the latency.

WesleiRamos commented 6 years ago

I understand, that was really it. Now is working perfectly, thanks.

faiface commented 6 years ago

Awesome! So, I guess the issue can be closed?

WesleiRamos commented 6 years ago

Yeah!