-
Notifications
You must be signed in to change notification settings - Fork 44
/
example_test.go
121 lines (113 loc) · 2.53 KB
/
example_test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package flac_test
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"log"
"github.com/mewkiz/flac"
)
func ExampleParseFile() {
// Parse metadata of love.flac
stream, err := flac.ParseFile("testdata/love.flac")
if err != nil {
log.Fatal(err)
}
defer stream.Close()
fmt.Printf("unencoded audio md5sum: %032x\n", stream.Info.MD5sum[:])
for i, block := range stream.Blocks {
fmt.Printf("block %d: %v\n", i, block.Type)
}
// Output:
// unencoded audio md5sum: bdf6f7d31f77cb696a02b2192d192a89
// block 0: seek table
// block 1: vorbis comment
// block 2: padding
}
func ExampleOpen() {
// Open love.flac for audio streaming without parsing metadata.
stream, err := flac.Open("testdata/love.flac")
if err != nil {
log.Fatal(err)
}
defer stream.Close()
// Parse audio samples and verify the MD5 signature of the decoded audio
// samples.
md5sum := md5.New()
for {
// Parse one frame of audio samples at the time, each frame containing one
// subframe per audio channel.
frame, err := stream.ParseNext()
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
frame.Hash(md5sum)
// Print first three samples from each channel of the first five frames.
if frame.Num < 5 {
fmt.Printf("frame %d\n", frame.Num)
for i, subframe := range frame.Subframes {
fmt.Printf(" subframe %d\n", i)
for j, sample := range subframe.Samples {
if j >= 3 {
break
}
fmt.Printf(" sample %d: %v\n", j, sample)
}
}
}
}
fmt.Println()
got, want := md5sum.Sum(nil), stream.Info.MD5sum[:]
fmt.Println("decoded audio md5sum valid:", bytes.Equal(got, want))
// Output:
// frame 0
// subframe 0
// sample 0: 126
// sample 1: 126
// sample 2: 126
// subframe 1
// sample 0: 126
// sample 1: 126
// sample 2: 126
// frame 1
// subframe 0
// sample 0: 126
// sample 1: 126
// sample 2: 126
// subframe 1
// sample 0: 126
// sample 1: 126
// sample 2: 126
// frame 2
// subframe 0
// sample 0: 121
// sample 1: 130
// sample 2: 137
// subframe 1
// sample 0: 121
// sample 1: 130
// sample 2: 137
// frame 3
// subframe 0
// sample 0: -9501
// sample 1: -6912
// sample 2: -3916
// subframe 1
// sample 0: -9501
// sample 1: -6912
// sample 2: -3916
// frame 4
// subframe 0
// sample 0: 513
// sample 1: 206
// sample 2: 152
// subframe 1
// sample 0: 513
// sample 1: 206
// sample 2: 152
//
// decoded audio md5sum valid: true
}