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

Pattern for "stopping" sound #49

Closed schollz closed 5 years ago

schollz commented 5 years ago

Hi @faiface, thank you so much for this library. It is an incredible work of code and performs really beautifully.

One thing I couldn't figure out from reading your docs (which are also well written btw!) is whether there is a pattern to stop / cancel a streamer?

In the most basic case, something like this:

done := make(chan bool)
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
    done <- true
})))

time.Sleep(1 * time.Second)
// stop streamer during its playback?

<-done

Even if there is a hacky way to do this (e.g. maybe setting the volume to zero and still letting it play out?), I'd be very grateful for any advice.

faiface commented 5 years ago

Hi! I'm glad you enjoy Beep :)

What you're asking for is entirely supported if I understand it correctly. You need to use Ctrl. Specifically, to cancel a streamer you need to set the Streamer field of Ctrl to nil.

You can learn about it in this part of the tutorial. I recommend reading the whole tutorial, it'll give a bunch of insight.

schollz commented 5 years ago

@faiface Thanks for the quick reply!

Oops, it was right there in the docs which I admit I didn't read yet! I think that will work perfect. I'm working on implementing a midi keyboard and want to trigger sounds on on key down and trigger sounds off on key up.

faiface commented 5 years ago

Oh, that's great! Feel free to share it when you have it done :)

A few tips. Make sure to read up on the Buffer type, either in the docs or in the tutorial (though I recommend the tutorial). Also make sure to use a small buffer size (the thing you set with speaker.Init at the beginning of your program) to get the best latency. Low latency is pretty important to make playing on the keyboard pleasant.

schollz commented 5 years ago

@faiface Definitely will share, and thanks for the tips. I made something similar before (https://github.com/schollz/pianoai) but I want to be able to play directly through the computer instead of through the piano. Thanks to you, I should be able to!

faiface commented 5 years ago

Wow, that's rad! So you're quite a programmer, I see :) Definitely looking forward.