-
Notifications
You must be signed in to change notification settings - Fork 1
/
wave.go
84 lines (71 loc) · 1.9 KB
/
wave.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
// Package gowave provides support for reading WAV files.
package gowave
import (
"errors"
"io"
"github.com/yut-kt/gowave/internal/chunk"
)
// Wave is a structure that handles wav files.
type Wave struct {
riffChunk *chunk.RiffChunk
fmtChunk *chunk.FmtChunk
dataChunk *chunk.DataChunk
}
// New is a function to construct Wave struct.
func New(file io.ReadSeeker) (*Wave, error) {
wave := new(Wave)
if err := wave.chunkRead(file); err != nil {
return nil, err
}
return wave, nil
}
func (wave *Wave) chunkRead(file io.ReadSeeker) error {
riffChunk, err := chunk.NewRiffChunk(file)
if err != nil {
return err
}
wave.riffChunk = riffChunk
fmtChunk, err := chunk.NewFmtChunk(file)
if err != nil {
return err
}
wave.fmtChunk = fmtChunk
dataChunk, err := chunk.NewDataChunk(file)
if err != nil {
return err
}
wave.dataChunk = dataChunk
return nil
}
// ReadSamples is a function to read all samples wave data.
func (wave *Wave) ReadSamples() (interface{}, error) {
const fullRead = -1
data, err := wave.dataChunk.ReadData(wave.fmtChunk.BitsPerSample, fullRead)
if err != nil {
return nil, err
}
return data, nil
}
// ReadNSamples is a function to read N samples wave data.
func (wave *Wave) ReadNSamples(samplingNum int64) (interface{}, error) {
if samplingNum < 1 {
return nil, errors.New("samplingNum is only natural number")
}
data, err := wave.dataChunk.ReadData(wave.fmtChunk.BitsPerSample, samplingNum)
if err != nil {
return nil, err
}
return data, nil
}
// GetSamplesAlreadyRead is a function to get already read samples wave data.
func (wave *Wave) GetSamplesAlreadyRead() interface{} {
return wave.dataChunk.Data
}
// GetNumChannels is a function to get num channels.
func (wave *Wave) GetNumChannels() uint16 {
return wave.fmtChunk.NumChannels
}
// GetSampleRate is a function to get sample rate.
func (wave *Wave) GetSampleRate() uint32 {
return wave.fmtChunk.SampleRate
}