-
Notifications
You must be signed in to change notification settings - Fork 1
/
play_file.go
75 lines (63 loc) · 1.33 KB
/
play_file.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 (
"bufio"
"encoding/binary"
"io"
"log"
"net/http"
"os"
_ "os"
"strings"
"github.com/gordonklaus/portaudio"
mpg "github.com/oandrew/go-mpg123"
)
func open(path string) (io.ReadCloser, error) {
if strings.HasPrefix(path, "http") {
resp, err := http.Get(path)
if err != nil {
return nil, err
}
return resp.Body, nil
} else {
file, err := os.Open(path)
if err != nil {
return nil, err
}
return file, nil
}
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Provide a file path or an URL")
}
s, err := open(os.Args[1])
if err != nil {
log.Fatalf("Could not open the stream: %v", err)
}
defer s.Close()
portaudio.Initialize()
defer portaudio.Terminate()
r := mpg.NewReaderConfig(bufio.NewReader(s), mpg.ReaderConfig{
OutputFormat: &mpg.OutputFormat{
Channels: 2,
Rate: 44100,
Encoding: mpg.EncodingInt16,
},
})
r.Read(nil)
log.Printf("Format: %+v", r.OutputFormat())
log.Printf("FrameInfo: %+v", r.FrameInfo())
log.Printf("Offset: %+v", r.Offset())
log.Printf("Meta ID3v2: %#v", r.Meta().ID3v2)
samples := make([]int16, 320)
stream, err := portaudio.OpenDefaultStream(0, 2, 44100, len(samples), &samples)
stream.Start()
defer stream.Stop()
for {
err := binary.Read(r, binary.LittleEndian, samples)
stream.Write()
if err == io.EOF {
break
}
}
}