mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-26 12:35:25 +00:00
sbc: joint stereo in progress
This commit is contained in:
parent
9462b3da58
commit
57f2bc22c3
@ -127,7 +127,7 @@ offset8 = np.array([[ -2, 0, 0, 0, 0, 0, 0, 1 ],
|
|||||||
def calculate_scalefactor(max_subbandsample):
|
def calculate_scalefactor(max_subbandsample):
|
||||||
x = 0
|
x = 0
|
||||||
while True:
|
while True:
|
||||||
y = 1 << x + 1
|
y = (1 << x) + 1
|
||||||
if y > max_subbandsample:
|
if y > max_subbandsample:
|
||||||
break
|
break
|
||||||
x += 1
|
x += 1
|
||||||
@ -157,7 +157,7 @@ def calculate_scalefactors(nr_blocks, nr_channels, nr_subbands, sb_sample):
|
|||||||
|
|
||||||
def calculate_channel_mode_and_scale_factors(frame):
|
def calculate_channel_mode_and_scale_factors(frame):
|
||||||
frame.scale_factor, frame.scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, frame.sb_sample)
|
frame.scale_factor, frame.scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, frame.sb_sample)
|
||||||
|
|
||||||
if frame.nr_channels == 1:
|
if frame.nr_channels == 1:
|
||||||
frame.channel_mode = MONO
|
frame.channel_mode = MONO
|
||||||
return
|
return
|
||||||
@ -165,20 +165,19 @@ def calculate_channel_mode_and_scale_factors(frame):
|
|||||||
frame.channel_mode = STEREO
|
frame.channel_mode = STEREO
|
||||||
frame.join = np.zeros(frame.nr_subbands, dtype = np.uint8)
|
frame.join = np.zeros(frame.nr_subbands, dtype = np.uint8)
|
||||||
return
|
return
|
||||||
|
sb_sample = np.zeros(shape = (frame.nr_blocks,2,frame.nr_subbands), dtype = np.int32)
|
||||||
sb_sample = np.zeros(shape = (frame.nr_blocks,2,frame.nr_subbands), dtype = np.uint16)
|
|
||||||
for blk in range(frame.nr_blocks):
|
for blk in range(frame.nr_blocks):
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
sb_sample[blk][0][sb] = np.uint16(frame.sb_sample[blk][0][sb] + frame.sb_sample[blk][1][sb])/2
|
sb_sample[blk][0][sb] = (frame.sb_sample[blk][0][sb] + frame.sb_sample[blk][1][sb])/2
|
||||||
sb_sample[blk][1][sb] = np.uint16(frame.sb_sample[blk][0][sb] - frame.sb_sample[blk][1][sb])/2
|
sb_sample[blk][1][sb] = (frame.sb_sample[blk][0][sb] - frame.sb_sample[blk][1][sb])/2
|
||||||
|
|
||||||
scale_factor, scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, sb_sample)
|
scale_factor, scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, sb_sample)
|
||||||
|
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands-1):
|
||||||
suma = frame.scale_factor[0][sb] + frame.scale_factor[1][sb]
|
suma = frame.scale_factor[0][sb] + frame.scale_factor[1][sb]
|
||||||
sumb = scale_factor[0][sb] + scale_factor[1][sb]
|
sumb = scale_factor[0][sb] + scale_factor[1][sb]
|
||||||
|
|
||||||
if suma > sumb:
|
if suma >= sumb:
|
||||||
frame.channel_mode = JOINT_STEREO
|
frame.channel_mode = JOINT_STEREO
|
||||||
frame.join[sb] = 1
|
frame.join[sb] = 1
|
||||||
|
|
||||||
@ -190,7 +189,7 @@ def calculate_channel_mode_and_scale_factors(frame):
|
|||||||
for blk in range(frame.nr_blocks):
|
for blk in range(frame.nr_blocks):
|
||||||
frame.sb_sample[blk][0][sb] = sb_sample[blk][0][sb]
|
frame.sb_sample[blk][0][sb] = sb_sample[blk][0][sb]
|
||||||
frame.sb_sample[blk][1][sb] = sb_sample[blk][1][sb]
|
frame.sb_sample[blk][1][sb] = sb_sample[blk][1][sb]
|
||||||
|
|
||||||
|
|
||||||
class SBCFrame:
|
class SBCFrame:
|
||||||
syncword = 0
|
syncword = 0
|
||||||
@ -399,7 +398,6 @@ def sbc_bit_allocation_mono_dual(frame):
|
|||||||
if bitneed[ch][sb] > max_bitneed:
|
if bitneed[ch][sb] > max_bitneed:
|
||||||
max_bitneed = bitneed[ch][sb]
|
max_bitneed = bitneed[ch][sb]
|
||||||
|
|
||||||
print "mono: bitneed", bitneed, max_bitneed
|
|
||||||
# calculate how many bitslices fit into the bitpool
|
# calculate how many bitslices fit into the bitpool
|
||||||
bitcount = 0
|
bitcount = 0
|
||||||
slicecount = 0
|
slicecount = 0
|
||||||
@ -421,7 +419,6 @@ def sbc_bit_allocation_mono_dual(frame):
|
|||||||
bitcount = bitcount + slicecount
|
bitcount = bitcount + slicecount
|
||||||
bitslice = bitslice - 1
|
bitslice = bitslice - 1
|
||||||
|
|
||||||
print "mono: bitslice", bitslice
|
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
if bitneed[ch][sb] < bitslice+2 :
|
if bitneed[ch][sb] < bitslice+2 :
|
||||||
bits[ch][sb]=0;
|
bits[ch][sb]=0;
|
||||||
@ -440,7 +437,6 @@ def sbc_bit_allocation_mono_dual(frame):
|
|||||||
|
|
||||||
sb = sb + 1
|
sb = sb + 1
|
||||||
|
|
||||||
|
|
||||||
sb = 0
|
sb = 0
|
||||||
while bitcount < frame.bitpool and sb < frame.nr_subbands:
|
while bitcount < frame.bitpool and sb < frame.nr_subbands:
|
||||||
if bits[ch][sb] < 16:
|
if bits[ch][sb] < 16:
|
||||||
|
@ -78,15 +78,16 @@ def sbc_reconstruct_subband_samples(frame):
|
|||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
frame.levels[ch][sb] = pow(2.0, frame.bits[ch][sb]) - 1
|
frame.levels[ch][sb] = pow(2.0, frame.bits[ch][sb]) - 1
|
||||||
|
|
||||||
|
joint_stereo_const = 1
|
||||||
|
|
||||||
for blk in range(frame.nr_blocks):
|
for blk in range(frame.nr_blocks):
|
||||||
for ch in range(frame.nr_channels):
|
for ch in range(frame.nr_channels):
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
if frame.levels[ch][sb] > 0:
|
if frame.levels[ch][sb] > 0:
|
||||||
AS = frame.audio_sample[blk][ch][sb]
|
AS = frame.audio_sample[blk][ch][sb]
|
||||||
SF = frame.scalefactor[ch][sb]
|
|
||||||
L = frame.levels[ch][sb]
|
L = frame.levels[ch][sb]
|
||||||
|
SF = frame.scalefactor[ch][sb]
|
||||||
frame.sb_sample[blk][ch][sb] = SF * ((AS*2.0+1.0) / L -1.0 )
|
frame.sb_sample[blk][ch][sb] = SF * ((AS*2.0+1.0) / (L) -1.0 )
|
||||||
else:
|
else:
|
||||||
frame.sb_sample[blk][ch][sb] = 0
|
frame.sb_sample[blk][ch][sb] = 0
|
||||||
|
|
||||||
|
@ -89,17 +89,22 @@ def sbc_quantization(frame):
|
|||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
frame.levels[ch][sb] = (1 << frame.bits[ch][sb]) - 1 #pow(2.0, frame.bits[ch][sb]) - 1
|
frame.levels[ch][sb] = (1 << frame.bits[ch][sb]) - 1 #pow(2.0, frame.bits[ch][sb]) - 1
|
||||||
|
|
||||||
frame.syncword = 156
|
frame.syncword = 0x9c
|
||||||
frame.crc_check = calculate_crc(frame)
|
frame.crc_check = calculate_crc(frame)
|
||||||
|
|
||||||
|
|
||||||
for blk in range(frame.nr_blocks):
|
for blk in range(frame.nr_blocks):
|
||||||
for ch in range(frame.nr_channels):
|
for ch in range(frame.nr_channels):
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
if frame.levels[ch][sb] > 0:
|
if frame.levels[ch][sb] > 0:
|
||||||
SB = frame.sb_sample[blk][ch][sb]
|
SB = frame.sb_sample[blk][ch][sb]
|
||||||
L = frame.levels[ch][sb]
|
L = frame.levels[ch][sb]
|
||||||
SF = frame.scalefactor[ch][sb]
|
SF = frame.scalefactor[ch][sb]
|
||||||
frame.audio_sample[blk][ch][sb] = np.uint16(((SB * L / SF + L) - 1.0)/2.0)
|
|
||||||
|
# if frame.channel_mode == JOINT_STEREO and frame.join[sb]:
|
||||||
|
# SB = SB * 2
|
||||||
|
|
||||||
|
frame.audio_sample[blk][ch][sb] = np.uint16(((SB * L / SF + L) - 1.0)/2.0)
|
||||||
else:
|
else:
|
||||||
frame.audio_sample[blk][ch][sb] = 0
|
frame.audio_sample[blk][ch][sb] = 0
|
||||||
|
|
||||||
@ -153,8 +158,8 @@ if __name__ == "__main__":
|
|||||||
sbc_encode(sbc_encoder_frame)
|
sbc_encode(sbc_encoder_frame)
|
||||||
sbc_write_frame(fout, sbc_encoder_frame)
|
sbc_write_frame(fout, sbc_encoder_frame)
|
||||||
|
|
||||||
if subband_frame_count == 1:
|
# if subband_frame_count == 1:
|
||||||
exit(0)
|
# exit(0)
|
||||||
audio_frame_count += nr_samples
|
audio_frame_count += nr_samples
|
||||||
subband_frame_count += 1
|
subband_frame_count += 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user