soerenkj wrote:@Rescator: I found that 32767/32768 is rounded down to 1.0 and that 1.0*32768 is rounded to 32767 so I guess the first method is also ok (?)
It's rounded yes, but you get a rounding error because of this.
Also 1.0*32768 is wrong as a 16bit wav does not have a 32768 value.
Highest positive value is 32767.
This stuff would have been much simpler if 16bit samples was unsigned,
or was artificially limited to a -32767 to 32767 range instead.
As it is now 0 is the middle, but negative values has 32768 possible values
while positive values has 32767 values.
Way back I did a audio comparison tool and keep cursing for days until I realized that 0 really isn't the center. In 16bit signed values the opposite of 0 is actually -1, and the opposite of 32767 is -32768
altough 0 is used as a silence or middle. There is no way to fix this "flaw" it's just how signed values are,
you have the same issue with 8bit and 32bit etc as well.
(8bit range is -128 to 127)
However, as long as what you input is the same as the output.
(do this by making a load and a saver and no processing at all)
So that you load a 16bit signed val, turn it into a normalized float then back to a 16bit signed val,
then compare it to the original value (like in my example)
If the values are the same before and after you are doing it correctly.
If -32768 ends up as -32767 you got a flaw in the math,
if 32767 ends up as 32768 not only do you have a flaw
but 32768 do not exist and will end up as -32768 and the saved sound will have some nasty static because of it.
I have seen a lot of routines that actually reduce the negative (or truncate it) from -32768 to -32767 so that negative and positive is "balanced"
but you do risk a sligh bias error because of this.
Whether it is audible or not is a totally different discussion.
But as I said. Test your 16bit signed to float to 16 bit signed process,
do not adjust volume or do anything else, just test the conversion itself.
The goal is to have the output match the input exactly.
Good test values are 0 and 1 and -1 and 32767 and -32768
if those match in the input and output you can be pretty confident the other values will as well.
Btw! The example did, altough not optimized in anyway is actually damn quick (thanks to its simplicity) even a wav that is 50+MB should only take a few seconds to "load" if your read routine has a good buffer routine,
and thanks to PB4 a read buffer is built in by default even
PS! Once you have converted the input to float,
make sure all following math is in the float domain,
mixing float and integer will be slower than pure float
because I'm pretty damn sure modern CPU is able to optimize it's cache
better when there is just pure float math or just pure integer math.
But don't take my word for it though, test it or wait for one or the Gurus here to chirp in on that part
