/
player.go
72 lines (61 loc) · 1.83 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Package player is responsible for playback of the podcast audio
//
// NOTE: a package used in player requires libasound2-dev on Linux. If you are using a
// Debian-based distro, the following command will install it
//
// apt install libasound2-dev
package player
import (
"io"
"sync"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/pkg/errors"
)
// Player contains the controls for audio playback
type Player struct {
dataInput io.ReadCloser
decoder DecodeFunc
lock *sync.Mutex
controller *beep.Ctrl
onFinishedPlayback chan bool
}
// DecodeFunc is the definition for the function signature to decode the audio stream.
// For example, an MP3 decoder.
type DecodeFunc func(io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err error)
// New returns a new Player
func New(stream io.ReadCloser, decoder DecodeFunc) *Player {
return &Player{
dataInput: stream,
decoder: decoder,
lock: &sync.Mutex{},
}
}
// Start begins playback
func (p *Player) Start() error {
// First, decode the stream
streamer, format, err := p.decoder(p.dataInput)
if err != nil {
return errors.WithMessage(err, "Failed to decode stream")
}
// Initialize the speaker with the stream data
if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)); err != nil {
return errors.WithMessagef(err, "Failed to initialize speakers with Sample Rate [%d]", format.SampleRate)
}
// Create stream controller
p.controller = &beep.Ctrl{
Streamer: streamer,
Paused: false,
}
p.onFinishedPlayback = make(chan bool)
speaker.Play(beep.Seq(p.controller, beep.Callback(func() {
p.onFinishedPlayback <- true
close(p.onFinishedPlayback)
})))
return nil
}
// Wait blocks until the player finishes playback
func (p *Player) Wait() {
<-p.onFinishedPlayback
}