-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
115 lines (89 loc) · 2.26 KB
/
reader_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
package opus
import (
"fmt"
"github.com/pidato/audio/vad"
"io"
"os"
"reflect"
"testing"
"time"
"unsafe"
)
func TestReader(t *testing.T) {
//reader, err := OpenFile("testdata/speech_8.opus")
reader, err := OpenFile("../opus/testdata/1.opus")
if err != nil {
t.Fatal(err)
}
sampleRate := int(reader.SampleRate())
if sampleRate == 44100 {
sampleRate = 48000
}
dec, err := NewDecoder(sampleRate, int(reader.ChannelCount()))
if err != nil {
t.Fatal(err)
}
fmt.Printf("Channels: %d\n", reader.ChannelCount())
pcm := make([]int16, 20000)
pcmFile, err := os.OpenFile("../opus/testdata/1.pcm", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0755)
if err != nil {
t.Fatal(err)
}
multiple := xMAX_BITRATE / sampleRate
preSkip := int(reader.PreSkip()) / multiple
totalSamples := 0
var duration time.Duration
sampleDuration := time.Second / 16000
fmt.Println(reader.PreSkip())
fmt.Println(preSkip)
v, _ := vad.New(16000, vad.Aggressive)
granulePosition := 0
_ = granulePosition
for {
// Reads the next packet.
packet, err := reader.ReadPacket()
if len(packet) > 0 {
n, err := dec.Decode(packet, pcm)
if err != nil {
t.Fatal(err)
}
dur, err := dec.LastPacketDuration()
fmt.Println("LastPacketDuration", dur)
pcm = pcm[:n]
granulePosition += (len(pcm) * multiple)
pcmBytes := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: (uintptr)(unsafe.Pointer(&pcm[0])),
Len: len(pcm) * 2,
Cap: len(pcm) * 2,
}))
v.Process(pcm)
if preSkip > 0 {
}
totalSamples += len(pcm)
duration += sampleDuration * time.Duration(len(pcm))
fmt.Printf("Payload:\n")
fmt.Printf("\tOpus: %d\n", len(packet))
fmt.Printf("\tPage Granule Sequence: %d\n", reader.GranulePos())
fmt.Printf("\tGranule Sequence: %d\n", granulePosition)
fmt.Printf("\tPCM: %d\n", len(pcm))
_, _ = pcmFile.Write(pcmBytes)
}
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
}
fmt.Printf("Total Samples: %d\n", totalSamples)
fmt.Printf("Total Duration: %v\n", duration)
fmt.Printf("Total Duration - PreSkip: %v\n", duration-(sampleDuration*time.Duration(int(reader.PreSkip())/multiple)))
err = pcmFile.Close()
if err != nil {
t.Fatal(err)
}
err = reader.Close()
if err != nil {
t.Fatal(err)
}
}