forked from g3n/engine
/
audio_file.go
287 lines (249 loc) · 6.61 KB
/
audio_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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package audio
import (
"fmt"
"github.com/g3n/engine/audio/al"
"github.com/g3n/engine/audio/ov"
"io"
"os"
"unsafe"
)
// AudioInfo represents the information associated to an audio file
type AudioInfo struct {
Format int // OpenAl Format
Channels int // Number of channels
SampleRate int // Sample rate in hz
BitsSample int // Number of bits per sample (8 or 16)
DataSize int // Total data size in bytes
BytesSec int // Bytes per second
TotalTime float64 // Total time in seconds
}
// AudioFile represents an audio file
type AudioFile struct {
wavef *os.File // Pointer to wave file opened filed (nil for vorbis)
vorbisf *ov.File // Pointer to vorbis file structure (nil for wave)
info AudioInfo // Audio information structure
looping bool // Looping flag
}
// NewAudioFile creates and returns a pointer to a new audio file object and an error
func NewAudioFile(filename string) (*AudioFile, error) {
// Checks if file exists
_, err := os.Stat(filename)
if err != nil {
return nil, err
}
af := new(AudioFile)
// Try to open as a wave file
if af.openWave(filename) == nil {
return af, nil
}
// Try to open as an ogg vorbis file
if af.openVorbis(filename) == nil {
return af, nil
}
return nil, fmt.Errorf("Unsuported file type")
}
// Close closes the audiofile
func (af *AudioFile) Close() error {
if af.wavef != nil {
return af.wavef.Close()
}
return ov.Clear(af.vorbisf)
}
// Read reads decoded data from the audio file
func (af *AudioFile) Read(pdata unsafe.Pointer, nbytes int) (int, error) {
// Slice to access buffer
bs := (*[1 << 30]byte)(pdata)[0:nbytes:nbytes]
// Reads wave file directly
if af.wavef != nil {
n, err := af.wavef.Read(bs)
if err != nil {
return 0, err
}
if !af.looping {
return n, nil
}
if n == nbytes {
return n, nil
}
// EOF reached. Position file at the beginning
_, err = af.wavef.Seek(int64(waveHeaderSize), 0)
if err != nil {
return 0, nil
}
// Reads next data into the remaining buffer space
n2, err := af.wavef.Read(bs[n:])
if err != nil {
return 0, err
}
return n + n2, err
}
// Decodes Ogg vorbis
decoded := 0
for decoded < nbytes {
n, _, err := ov.Read(af.vorbisf, unsafe.Pointer(&bs[decoded]), nbytes-decoded, false, 2, true)
// Error
if err != nil {
return 0, err
}
// EOF
if n == 0 {
if !af.looping {
break
}
// Position file at the beginning
err = ov.PcmSeek(af.vorbisf, 0)
if err != nil {
return 0, err
}
}
decoded += n
}
if nbytes > 0 && decoded == 0 {
return 0, io.EOF
}
return decoded, nil
}
// Seek sets the file reading position relative to the origin
func (af *AudioFile) Seek(pos uint) error {
if af.wavef != nil {
_, err := af.wavef.Seek(int64(waveHeaderSize+pos), 0)
return err
}
return ov.PcmSeek(af.vorbisf, int64(pos))
}
// Info returns the audio info structure for this audio file
func (af *AudioFile) Info() AudioInfo {
return af.info
}
// CurrentTime returns the current time in seconds for the current file read position
func (af *AudioFile) CurrentTime() float64 {
if af.vorbisf != nil {
pos, _ := ov.TimeTell(af.vorbisf)
return pos
}
pos, err := af.wavef.Seek(0, 1)
if err != nil {
return 0
}
return float64(pos) / float64(af.info.BytesSec)
}
// Looping returns the current looping state of this audio file
func (af *AudioFile) Looping() bool {
return af.looping
}
// SetLooping sets the looping state of this audio file
func (af *AudioFile) SetLooping(looping bool) {
af.looping = looping
}
// openWave tries to open the specified file as a wave file
// and if succesfull, sets the file pointer positioned after the header.
func (af *AudioFile) openWave(filename string) error {
// Open file
osf, err := os.Open(filename)
if err != nil {
return err
}
// Reads header
header := make([]uint8, waveHeaderSize)
n, err := osf.Read(header)
if err != nil {
osf.Close()
return err
}
if n < waveHeaderSize {
osf.Close()
return fmt.Errorf("File size less than header")
}
// Checks file marks
if string(header[0:4]) != fileMark {
osf.Close()
return fmt.Errorf("'RIFF' mark not found")
}
if string(header[8:12]) != fileHead {
osf.Close()
return fmt.Errorf("'WAVE' mark not found")
}
// Decodes header fields
af.info.Format = -1
af.info.Channels = int(header[22]) + int(header[23])<<8
af.info.SampleRate = int(header[24]) + int(header[25])<<8 + int(header[26])<<16 + int(header[27])<<24
af.info.BitsSample = int(header[34]) + int(header[35])<<8
af.info.DataSize = int(header[40]) + int(header[41])<<8 + int(header[42])<<16 + int(header[43])<<24
// Sets OpenAL format field if possible
if af.info.Channels == 1 {
if af.info.BitsSample == 8 {
af.info.Format = al.FormatMono8
} else if af.info.BitsSample == 16 {
af.info.Format = al.FormatMono16
}
} else if af.info.Channels == 2 {
if af.info.BitsSample == 8 {
af.info.Format = al.FormatStereo8
} else if af.info.BitsSample == 16 {
af.info.Format = al.FormatStereo16
}
}
if af.info.Format == -1 {
osf.Close()
return fmt.Errorf("Unsupported OpenAL format")
}
// Calculates bytes/sec and total time
var bytesChannel int
if af.info.BitsSample == 8 {
bytesChannel = 1
} else {
bytesChannel = 2
}
af.info.BytesSec = af.info.SampleRate * af.info.Channels * bytesChannel
af.info.TotalTime = float64(af.info.DataSize) / float64(af.info.BytesSec)
// Seeks after the header
_, err = osf.Seek(waveHeaderSize, 0)
if err != nil {
osf.Close()
return err
}
af.wavef = osf
return nil
}
// openVorbis tries to open the specified file as an ogg vorbis file
// and if succesfull, sets up the player for playing this file
func (af *AudioFile) openVorbis(filename string) error {
// Try to open file as ogg vorbis
vf, err := ov.Fopen(filename)
if err != nil {
return err
}
// Get info for opened vorbis file
var info ov.VorbisInfo
err = ov.Info(vf, -1, &info)
if err != nil {
return err
}
if info.Channels == 1 {
af.info.Format = al.FormatMono16
} else if info.Channels == 2 {
af.info.Format = al.FormatStereo16
} else {
return fmt.Errorf("Unsupported number of channels")
}
totalSamples, err := ov.PcmTotal(vf, -1)
if err != nil {
ov.Clear(vf)
return nil
}
timeTotal, err := ov.TimeTotal(vf, -1)
if err != nil {
ov.Clear(vf)
return nil
}
af.vorbisf = vf
af.info.SampleRate = info.Rate
af.info.BitsSample = 16
af.info.Channels = info.Channels
af.info.DataSize = int(totalSamples) * info.Channels * 2
af.info.TotalTime = timeTotal
return nil
}