brentnd/go-snowboy

How to close properly the "ReadAndDetect"

lorico opened this issue · 5 comments

Hello,
First of all, thanks a lot for this really cool lib ! :)
I am trying to use the listen example, but I can't figure out, once you start the detection with "d.ReadAndDetect", how do you stop it properly (closing the relevant mic listening etc...) ?
Maybe I missed something... ?
Thanks in advance for your tip!

As is typical with idiomatic go code, the stream opened by portaudio and the detector by snowbody are closed with defer statements. The last call you mentioned d.ReadAndDetect(...) is designed to block as longs as the io.Reader passed to it does not return EOF and will call whatever handlers it has setup while it's running.

It was designed with the same pattern as the net.http server ListenAndServe() function. Given that, if you do want to have the process stop you could run d.ReadAndDetect(...) in a go-routine and then use a select statement or another method to block until a certain time or event.

Thanks Brentnd for your quick answer ! Actually, adding a chan to the input arguments of function "ReadAndDetect" + a "select" statement did actually made the trick ! Thanks again !

In fact, the goroutine was actually a bad plan....
So I revert back to your lib as is, and just send a "stream.Close()" when a word is detected in listen mode (in the HandleFunc function)....
That looks much cleaner & actually words better... as always..kiss method...

Yeah, that's a better way to do it. The goroutine suggestion would be more appropriate if you want it to keep detecting after the first keyword is detected.

yeah, actually, stream.Close() + portaudio.Terminate() is even cleaner...