distinguish whether a sound is 8-bit or 16-bit
# This function tells whether a sound is 8-bit (1 byte) or 16-bit (2 bytes). Effectively this means,
# can it hold values from -127...+128 or from -32k..+32k+1?
# We determine which of these two it is by overwriting the very first sample (it could be any
# sample, but we choose the first) with a value of 32k and then we read the value back out.
# If we get the value that we put in, the sound must be 2-byte (16-bits); if the value is only
# 128, the sound is 1-byte (8-bits), and the sample value was clipped when we put it in.
# Finally, we have to clear up our mess after ourselves by puttting the original sample value
# back before returning either the value 1 or the value 2.
def bytesPerSample(sound):
probeValue = 32767
# Get original value of first sample and save it
realValue = getSampleValueAt(sound, 1)
# Change first value to value that is valid for 16-bit sounds then read it back.
# If it is what we set it as, the sound is 16-bit. Otherwise, it is clipped and sound is therefore 8-bit.
# Remember to change the sample back to its original value before returning.
setSampleValueAt(sound, 1, probeValue)
if getSampleValueAt(sound, 1) == probeValue:
nBytes = 2
else:
nBytes = 1
setSampleValueAt(sound, 1, realValue)
return nBytes
Link to this Page