go-audio / wav

Battle tested Wav decoder/encoder
Apache License 2.0
282 stars 43 forks source link

Get raw byte buffer for OpenAL #23

Open bergwerf opened 4 years ago

bergwerf commented 4 years ago

I am trying gomobile with the OpenAL module (https://godoc.org/golang.org/x/mobile/exp/audio/al). I need to pass raw byte data, but it seems this library only provides ints and floats. Am I missing something?

mattetti commented 4 years ago

I'm not quite sure what you mean by raw byte data, what data are we talking about? I've never used OpenAL but you can take a look at the wav encoder that write PCM data to a wav container/file. https://github.com/go-audio/wav/blob/master/encoder.go#L63

I doubt you are trying to write a wav file to an AL file descriptor but if that's the case, you can use the encoder on the file: https://github.com/go-audio/wav/blob/master/encoder.go#L41

I hope it helps,

bergwerf commented 4 years ago

The gomobile OpenAL binding expects bytes where 16bit data is divided over two bytes. See: https://pkg.go.dev/golang.org/x/mobile@v0.0.0-20200329125638-4c31acba0007/exp/audio/al?tab=doc#Buffer.BufferData

For my 16bit data I used this ad-hoc solution:

byteBuffer := make([]byte, len(intBuffer.Data)*2)
for i := 0; i < len(intBuffer.Data); i++ {
        i32 := intBuffer.Data[i]
        byteBuffer[2*i] = byte(i32)
        byteBuffer[2*i+1] = byte(i32 >> 8)
}

This particular OpenAL bindings appears to only support 8 and 16bit data.