/
wav.go
159 lines (128 loc) · 2.9 KB
/
wav.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package wav
import (
"encoding/binary"
"fmt"
"github.com/vova616/GarageEngine/engine/audio"
"io"
"os"
)
var test = audio.AudioClip(&Wav{})
type Wav struct {
Format
Data []int16
index int
audioFormat audio.Format
}
func (this *Wav) BufferLength() int {
return int(this.Format.SampleRate) / 2
}
func (this *Wav) NextBuffer(buff []int16, mono bool) int {
if mono && this.audioFormat != audio.Mono16 {
panic("does not support stereo to mono conversion")
}
n := copy(buff, this.Data[this.index:])
this.index += n
if this.index >= len(this.Data) {
this.index = 0
}
return n
}
func (this *Wav) Clone() (audio.AudioClip, error) {
return &Wav{this.Format, this.Data, 0, this.audioFormat}, nil
}
func (this *Wav) SetPosition(pos int) {
this.index = pos
}
func (this *Wav) Length() int {
return len(this.Data)
}
func (this *Wav) SampleRate() int {
return int(this.Format.SampleRate)
}
func (this *Wav) AudioFormat() audio.Format {
return this.audioFormat
}
type Format struct {
FormatTag int16
Channels int16
SampleRate int32
AvgBytes int32
BlockAlign int16
BitsPerSample int16
}
type Format2 struct {
Format
SizeOfExtension int16
}
type Format3 struct {
Format2
ValidBitsPerSample int16
ChannelMask int32
SubFormat [16]byte
}
func NewClip(path string) (*Wav, error) {
mr, e := os.Open(path)
if e != nil {
return nil, e
}
clip, e := ReadWavHeader(mr)
if e != nil {
return nil, e
}
return clip, nil
}
func ReadWavHeader(reader io.Reader) (*Wav, error) {
var buff [4]byte
reader.Read(buff[:4])
if string(buff[:4]) != "RIFF" {
return nil, fmt.Errorf("Not a WAV file.\n")
}
var size int32
binary.Read(reader, binary.LittleEndian, &size)
reader.Read(buff[:4])
if string(buff[:4]) != "WAVE" {
return nil, fmt.Errorf("Not a WAV file.\n")
}
reader.Read(buff[:4])
if string(buff[:4]) != "fmt " {
return nil, fmt.Errorf("Not a WAV file.\n")
}
binary.Read(reader, binary.LittleEndian, &size)
var format Format
switch size {
case 16:
binary.Read(reader, binary.LittleEndian, &format)
case 18:
var f2 Format2
binary.Read(reader, binary.LittleEndian, &f2)
format = f2.Format
case 40:
var f3 Format3
binary.Read(reader, binary.LittleEndian, &f3)
format = f3.Format
}
reader.Read(buff[:4])
if string(buff[:4]) != "data" {
return nil, fmt.Errorf("Not supported WAV file.\n")
}
binary.Read(reader, binary.LittleEndian, &size)
wavData := make([]byte, size)
n, e := reader.Read(wavData)
if e != nil {
return nil, fmt.Errorf("Cannot read WAV data.\n")
}
if int32(n) != size {
return nil, fmt.Errorf("WAV data size doesnt match.\n")
}
wavInts := make([]int16, size/2)
for i := 0; i < len(wavInts); i++ {
wavInts[i] = int16(binary.LittleEndian.Uint16(wavData[i*2:]))
}
af := audio.Mono8
if format.Channels == 1 {
af = audio.Mono16
} else {
af = audio.Stereo16
}
return &Wav{format, wavInts, 0, af}, nil
}