2019-02-04 02:17:56 +00:00
|
|
|
package asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/go-mp3"
|
|
|
|
"github.com/hajimehoshi/oto"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
type AudioPlayer struct {
|
|
|
|
player *oto.Player
|
|
|
|
beep []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAudioPlayer() *AudioPlayer {
|
2019-02-04 02:17:56 +00:00
|
|
|
|
2019-02-24 06:14:12 +00:00
|
|
|
bytes, err := Asset("quindar-tone.mp3")
|
2019-02-04 02:17:56 +00:00
|
|
|
if err != nil {
|
2019-05-20 04:14:13 +00:00
|
|
|
log.Fatal("Failed to find audio file")
|
2019-02-04 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
player, err := oto.NewPlayer(44100, 2, 2, 8192)
|
2019-02-04 02:17:56 +00:00
|
|
|
if err != nil {
|
2019-08-03 21:54:41 +00:00
|
|
|
// it is expected to fail when some of the system
|
|
|
|
// libraries are not available (e.g. libasound2)
|
|
|
|
// it is not the main functionality of the application,
|
|
|
|
// so we allow startup in no-sound mode
|
|
|
|
return nil
|
2019-02-04 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
return &AudioPlayer{
|
|
|
|
player: player,
|
|
|
|
beep: bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AudioPlayer) Beep() {
|
|
|
|
|
|
|
|
decoder, err := mp3.NewDecoder(NewAssetFile(a.beep))
|
2019-02-04 02:17:56 +00:00
|
|
|
if err != nil {
|
2019-03-10 04:41:23 +00:00
|
|
|
panic(err)
|
2019-02-04 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
if _, err := io.Copy(a.player, decoder); err != nil {
|
|
|
|
panic(err)
|
2019-02-04 02:17:56 +00:00
|
|
|
}
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
2019-02-04 02:17:56 +00:00
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
func (a *AudioPlayer) Close() {
|
|
|
|
_ = a.player.Close()
|
2019-02-04 02:17:56 +00:00
|
|
|
}
|