-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong.go
More file actions
53 lines (42 loc) · 1.09 KB
/
Copy pathsong.go
File metadata and controls
53 lines (42 loc) · 1.09 KB
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
package main
import (
"os"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/mp3"
)
type Song struct {
name string
length time.Duration
streamer beep.StreamSeekCloser
resampled *beep.Resampler
ctrl *beep.Ctrl
volume *effects.Volume
format *beep.Format
}
func NewSong(songName string, sampleRate beep.SampleRate) (*Song, error) {
f, err := os.Open(songName)
if err != nil {
return nil, err
}
streamer, format, err := mp3.Decode(f)
if err != nil {
return nil, err
}
length := format.SampleRate.D(streamer.Len()).Round(time.Second)
ctrl := &beep.Ctrl{Streamer: streamer, Paused: false}
resampled := beep.Resample(4, format.SampleRate, sampleRate, ctrl)
volume := &effects.Volume{Streamer: resampled, Base: 2}
s := &Song{songName, length, streamer, resampled, ctrl, volume, &format}
return s, nil
}
func (s *Song) GetPosition() time.Duration {
return s.format.SampleRate.D(s.streamer.Position())
}
func (s *Song) Pause(value bool) {
s.ctrl.Paused = value
}
func (s *Song) Volume(value float64) {
s.volume.Volume = value
}