audio icon indicating copy to clipboard operation
audio copied to clipboard

Incorrect conversion of Float to Int

Open dh1tw opened this issue 7 years ago • 1 comments

Hi,

I tried to convert a Float32Buffer to an IntBuffer, resulting in silence. Problem is, that the current implementation has a flaw:

https://github.com/go-audio/audio/blob/9867513453de590a60792e5c5a033f608e7f1404/float_buffer.go#L36

https://github.com/go-audio/audio/blob/9867513453de590a60792e5c5a033f608e7f1404/float_buffer.go#L107

Float audio samples use the value range of -1....0....1 casting them just to int won't do the job. For a proper conversion, the bit depth of the integer (8/16/32/64) must be known. Here is a good stack overflow article how to do it right.

Problem is, that within a Float32Buffer, the bit depth is not available :-(

dh1tw avatar Jan 21 '18 00:01 dh1tw

@dh1tw there is a transform for that https://github.com/go-audio/transforms/blob/master/pcm_scale.go#L27 It does expect that you kept around the original bit depth. you can see that the PCMBuffer uses SourceBitDepth to track that. The Float32Buffer also does have that field: https://github.com/go-audio/audio/blob/master/float_buffer.go#L85

I still do need to go and clean up the other types but I'm thinking about entirely dropping Floats and only keep float32s.

mattetti avatar Feb 06 '18 23:02 mattetti