MCPcopy
hub / github.com/g3n/engine / openWave

Method openWave

audio/audio_file.go:179–253  ·  view source on GitHub ↗

openWave tries to open the specified file as a wave file and if succesfull, sets the file pointer positioned after the header.

(filename string)

Source from the content-addressed store, hash-verified

177// openWave tries to open the specified file as a wave file
178// and if succesfull, sets the file pointer positioned after the header.
179func (af *AudioFile) openWave(filename string) error {
180
181 // Open file
182 osf, err := os.Open(filename)
183 if err != nil {
184 return err
185 }
186
187 // Reads header
188 header := make([]uint8, waveHeaderSize)
189 n, err := osf.Read(header)
190 if err != nil {
191 osf.Close()
192 return err
193 }
194 if n < waveHeaderSize {
195 osf.Close()
196 return fmt.Errorf("File size less than header")
197 }
198 // Checks file marks
199 if string(header[0:4]) != fileMark {
200 osf.Close()
201 return fmt.Errorf("'RIFF' mark not found")
202 }
203 if string(header[8:12]) != fileHead {
204 osf.Close()
205 return fmt.Errorf("'WAVE' mark not found")
206 }
207
208 // Decodes header fields
209 af.info.Format = -1
210 af.info.Channels = int(header[22]) + int(header[23])<<8
211 af.info.SampleRate = int(header[24]) + int(header[25])<<8 + int(header[26])<<16 + int(header[27])<<24
212 af.info.BitsSample = int(header[34]) + int(header[35])<<8
213 af.info.DataSize = int(header[40]) + int(header[41])<<8 + int(header[42])<<16 + int(header[43])<<24
214
215 // Sets OpenAL format field if possible
216 if af.info.Channels == 1 {
217 if af.info.BitsSample == 8 {
218 af.info.Format = al.FormatMono8
219 } else if af.info.BitsSample == 16 {
220 af.info.Format = al.FormatMono16
221 }
222 } else if af.info.Channels == 2 {
223 if af.info.BitsSample == 8 {
224 af.info.Format = al.FormatStereo8
225 } else if af.info.BitsSample == 16 {
226 af.info.Format = al.FormatStereo16
227 }
228 }
229 if af.info.Format == -1 {
230 osf.Close()
231 return fmt.Errorf("Unsupported OpenAL format")
232 }
233
234 // Calculates bytes/sec and total time
235 var bytesChannel int
236 if af.info.BitsSample == 8 {

Callers 1

NewAudioFileFunction · 0.80

Calls 3

ReadMethod · 0.80
SeekMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected