func writeHeader(w io.Writer, f *audio.File) { writeString(w, "RIFF") writeUint32(w, uint32(36+f.Length()*int(f.ByteDepth())*f.NumChannels())) writeString(w, "WAVE") writeString(w, "fmt ") writeUint32(w, 16) writeUint16(w, 1) writeUint16(w, uint16(f.NumChannels())) writeUint32(w, uint32(f.SampleRate)) writeUint32(w, uint32(int(f.SampleRate)*f.NumChannels()*int(f.ByteDepth()))) writeUint16(w, uint16(int(f.ByteDepth())*f.NumChannels())) writeUint16(w, uint16(f.Depth)) }
func Encode(w io.Writer, f *audio.File) error { writeHeader(w, f) writeString(w, "data") writeUint32(w, uint32(f.Length()*int(f.ByteDepth())*f.NumChannels())) for sample := range f.Data[0] { for channel := range f.Data { value := f.Data[channel][sample] switch f.ByteDepth() { case 1: writeUint8(w, uint8(value)) case 2: writeUint16(w, uint16(value)) default: return ErrUnsupportedDepth } } } return nil }
func EstimateFileSize(f *audio.File) uint64 { return 44 + uint64(f.Length()*int(f.ByteDepth())*f.NumChannels()) }