/
player.go
75 lines (65 loc) · 1.3 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
73
74
75
package main
import (
"io"
"os"
"github.com/mjibson/mog/codec"
"github.com/mjibson/mog/output"
// codecs
_ "github.com/mjibson/mog/codec/flac"
_ "github.com/mjibson/mog/codec/gme"
_ "github.com/mjibson/mog/codec/mpa"
_ "github.com/mjibson/mog/codec/nsf"
_ "github.com/mjibson/mog/codec/rar"
_ "github.com/mjibson/mog/codec/vorbis"
_ "github.com/mjibson/mog/codec/wav"
)
func PlayPath(p string, vis Visualizer) error {
var song codec.Song
songs, _, err := codec.ByExtension(p, fileReader(p))
if err != nil {
return err
}
for _, song = range songs {
// Get first song
break
}
sampleRate, channels, err := song.Init()
if err != nil {
return err
}
defer song.Close()
seekRate := int(sampleRate / 10.0) // fps
vis.SetFreq(float64(seekRate))
out, err := output.Get(sampleRate, channels)
if err != nil {
return err
}
out.Start()
for {
samples, err := song.Play(seekRate)
if err != nil {
return err
}
out.Push(samples)
vis.Push(samples)
if len(samples) < seekRate {
// Done
break
}
}
return nil
}
func fileReader(path string) codec.Reader {
return func() (io.ReadCloser, int64, error) {
f, err := os.Open(path)
if err != nil {
return nil, 0, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, 0, err
}
return f, fi.Size(), nil
}
}