# Measure actual number of bits in audio

I’m doing currently auditioning a lot of sounds, and would like a way to ensure that the bit depth is in fact correct
(as you might know, you can easily convert an 8-bit sound into a 16-bit version, and most software will tell you that the sound is 16 bit)

I know about Tobybear’s BitViewer, which is supposed to do just this kind of thing, but it displays my sound (which is definitely 16 bits “at most”) as 32 bits.
I suspected this was because of the default headroom in Renoise being set to -6dB, so I set it to 0dB - same with the track I’m auditioning from, and the sample is being played at full volume, too.

Seems I’m missing some subtle aspect of the Renoise signal path that would cause the number of bits to bump up?

Can’t get BitViewer to do much here either. But we can just directly analyze the sample data ourselves, rather than relying on what’s going through the audio engine.

I was lazy, so I stole this little snippet which returns the binary representation of an integer:

(I’m sure there’s an easier way to do this crap using the BitOp library, but I’m too tired to remember it at the moment.)

Then it’s a simple matter of stepping through each sample frame and keeping track of which bits were used:

``````
function get_bit_depth(sample)

local function reverse(t)
local nt = {}
local size = #t + 1
for k,v in ipairs(t) do
nt = v
end
return nt
end

local function tobits(num)
local t = {}
while num > 0 do
local rest = num % 2
t[#t + 1] = rest
num = (num - rest) / 2
end
t = reverse(t)
return t
end

-- Vars and crap
local bit_depth = 0
local sample_max = math.pow(2, 32) / 2
local buffer = sample.sample_buffer

-- If we got some sample data to analyze
if (buffer.has_sample_data) then

local channels = buffer.number_of_channels
local frames = buffer.number_of_frames

-- For each frame
for f = 1, frames do

-- For each channel
for c = 1, channels do

-- Convert float to 32-bit unsigned int
local s = (1 + buffer:sample_data(c, f)) * sample_max

-- Measure bits used
local bits = tobits(s)
for b = 1, #bits do
if bits[b] == 1 then
if b > bit_depth then
bit_depth = b
end
end
end
end
end
end

return bit_depth
end

local bit_depth = get_bit_depth(renoise.song().selected_sample)

print(string.format('Sample appears to be %d-bit!', bit_depth))

``````

Wow, amazing stuff. You turned my question into a scripting related topic

Going to check it out this snippet ASAP