faiface/beep

USB audio device unplugged and replug, make program block at speaker.Init

microidea opened this issue · 0 comments

I have an USB audio device which has poor contact, when replugged I have to call speaker.Init() again to make the speaker work again.

But it's stuck at here:

player.Write(buf)

Since it's in a for loop, the whole loop is blocked:

beep/speaker/speaker.go

Lines 48 to 57 in 98afada

go func() {
for {
select {
default:
update()
case <-done:
return
}
}
}()

the done channel cannot be consumed, when I call speaker.Init again, it calls speaker.Close first, then it's stuck at here:
done <- struct{}{}

As a result when my USB audio device replugged I cannot call speaker.Init() again, it's always stuck.
So right now I change the done channel into a buffered chan, it looks good so far, but I'm not sure whether it will cause other problem.

Is it necessary to make the done channel an unbuffered chan? How to handle the replug situation properly?