|
|
@@ -8,8 +8,10 @@ import ( |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type WAVSource struct { |
|
|
type WAVSource struct { |
|
|
frames []Frame |
|
|
|
|
|
index int |
|
|
|
|
|
|
|
|
frames []Frame |
|
|
|
|
|
index int |
|
|
|
|
|
SampleRate int |
|
|
|
|
|
Channels int |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func LoadWAVSource(path string) (*WAVSource, error) { |
|
|
func LoadWAVSource(path string) (*WAVSource, error) { |
|
|
@@ -27,16 +29,24 @@ func LoadWAVSource(path string) (*WAVSource, error) { |
|
|
return nil, fmt.Errorf("unsupported wav header") |
|
|
return nil, fmt.Errorf("unsupported wav header") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
audioFormat := binary.LittleEndian.Uint16(header[20:22]) |
|
|
channels := binary.LittleEndian.Uint16(header[22:24]) |
|
|
channels := binary.LittleEndian.Uint16(header[22:24]) |
|
|
|
|
|
sampleRate := binary.LittleEndian.Uint32(header[24:28]) |
|
|
bitsPerSample := binary.LittleEndian.Uint16(header[34:36]) |
|
|
bitsPerSample := binary.LittleEndian.Uint16(header[34:36]) |
|
|
dataSize := binary.LittleEndian.Uint32(header[40:44]) |
|
|
dataSize := binary.LittleEndian.Uint32(header[40:44]) |
|
|
|
|
|
|
|
|
|
|
|
if audioFormat != 1 { |
|
|
|
|
|
return nil, fmt.Errorf("only PCM wav supported") |
|
|
|
|
|
} |
|
|
if bitsPerSample != 16 { |
|
|
if bitsPerSample != 16 { |
|
|
return nil, fmt.Errorf("only 16-bit PCM wav supported") |
|
|
return nil, fmt.Errorf("only 16-bit PCM wav supported") |
|
|
} |
|
|
} |
|
|
if channels != 1 && channels != 2 { |
|
|
if channels != 1 && channels != 2 { |
|
|
return nil, fmt.Errorf("only mono/stereo wav supported") |
|
|
return nil, fmt.Errorf("only mono/stereo wav supported") |
|
|
} |
|
|
} |
|
|
|
|
|
if sampleRate == 0 { |
|
|
|
|
|
return nil, fmt.Errorf("invalid wav sample rate") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
raw := make([]byte, dataSize) |
|
|
raw := make([]byte, dataSize) |
|
|
if _, err := io.ReadFull(f, raw); err != nil { |
|
|
if _, err := io.ReadFull(f, raw); err != nil { |
|
|
@@ -54,7 +64,11 @@ func LoadWAVSource(path string) (*WAVSource, error) { |
|
|
frames = append(frames, NewFrame(l, r)) |
|
|
frames = append(frames, NewFrame(l, r)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return &WAVSource{frames: frames}, nil |
|
|
|
|
|
|
|
|
return &WAVSource{ |
|
|
|
|
|
frames: frames, |
|
|
|
|
|
SampleRate: int(sampleRate), |
|
|
|
|
|
Channels: int(channels), |
|
|
|
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *WAVSource) NextFrame() Frame { |
|
|
func (s *WAVSource) NextFrame() Frame { |
|
|
|