mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-25 16:43:28 +00:00
sbc: handle empty frame during unpacking
This commit is contained in:
parent
c0d620effb
commit
5665ea35c6
@ -147,12 +147,13 @@ class SBCFrame:
|
||||
levels = np.zeros(shape=(2, 8), dtype = np.int32)
|
||||
|
||||
|
||||
def __init__(self, nr_blocks=16, nr_subbands=4, nr_channels=1, bitpool=31, sampling_frequency=44100):
|
||||
def __init__(self, nr_blocks=16, nr_subbands=4, nr_channels=1, bitpool=31, sampling_frequency=44100, allocation_method = 0):
|
||||
self.nr_blocks = nr_blocks
|
||||
self.nr_subbands = nr_subbands
|
||||
self.nr_channels = nr_channels
|
||||
self.sampling_frequency = sampling_frequency_index(sampling_frequency)
|
||||
self.bitpool = bitpool
|
||||
self.allocation_method = allocation_method
|
||||
self.scale_factor = np.zeros(shape=(nr_channels, nr_subbands), dtype = np.int32)
|
||||
self.scalefactor = np.zeros(shape=(nr_channels, nr_subbands), dtype = np.int32)
|
||||
self.audio_sample = np.zeros(shape=(nr_blocks, nr_channels, nr_subbands), dtype = np.uint16)
|
||||
@ -524,4 +525,3 @@ def mse(a,b):
|
||||
res = sqr.sum()*1.0/count
|
||||
# res = ((a - b) ** 2).mean()
|
||||
return res
|
||||
|
@ -7,7 +7,10 @@ from sbc import *
|
||||
|
||||
V = np.zeros(shape = (2, 10*2*8))
|
||||
|
||||
def sbc_unpack_frame(fin, frame):
|
||||
def sbc_unpack_frame(fin, available_bytes, frame):
|
||||
if available_bytes == 0:
|
||||
raise TypeError
|
||||
|
||||
frame.syncword = get_bits(fin,8)
|
||||
if frame.syncword != 156:
|
||||
print "incorrect syncword ", frame.syncword
|
||||
@ -195,13 +198,17 @@ if __name__ == "__main__":
|
||||
|
||||
with open (infile, 'rb') as fin:
|
||||
try:
|
||||
fin.seek(0, 2)
|
||||
file_size = fin.tell()
|
||||
fin.seek(0, 0)
|
||||
|
||||
frame_count = 0
|
||||
while True:
|
||||
sbc_decoder_frame = SBCFrame(0,0,0,0,0)
|
||||
sbc_decoder_frame = SBCFrame()
|
||||
if frame_count % 200 == 0:
|
||||
print "== Frame %d ==" % (frame_count)
|
||||
|
||||
err = sbc_unpack_frame(fin, sbc_decoder_frame)
|
||||
err = sbc_unpack_frame(fin, file_size - fin.tell(), sbc_decoder_frame)
|
||||
|
||||
if err:
|
||||
print "error, frame_count: ", frame_count
|
||||
|
@ -62,8 +62,8 @@ def get_actual_frame(fin):
|
||||
sbc_synthesis(actual_frame)
|
||||
return actual_frame
|
||||
|
||||
def get_expected_frame(fin_expected, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool):
|
||||
expected_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool)
|
||||
def get_expected_frame(fin_expected, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method):
|
||||
expected_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method)
|
||||
fetch_samples_for_next_sbc_frame(fin_expected, expected_frame)
|
||||
return expected_frame
|
||||
|
||||
@ -103,7 +103,8 @@ try:
|
||||
|
||||
expected_frame = get_expected_frame(fin_expected, actual_frame.nr_blocks,
|
||||
actual_frame.nr_subbands, nr_channels,
|
||||
actual_frame.bitpool, sampling_frequency)
|
||||
actual_frame.bitpool, sampling_frequency,
|
||||
actual_frame.allocation_method)
|
||||
|
||||
|
||||
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
||||
@ -114,6 +115,9 @@ try:
|
||||
if err < 0:
|
||||
exit(1)
|
||||
|
||||
if subband_frame_count == 0:
|
||||
print actual_frame
|
||||
|
||||
subband_frame_count += 1
|
||||
|
||||
except TypeError:
|
||||
|
@ -177,14 +177,14 @@ def sbc_write_frame(fout, sbc_encoder_frame):
|
||||
|
||||
if __name__ == "__main__":
|
||||
usage = '''
|
||||
Usage: ./sbc_encoder.py input.wav blocks subbands bitpool
|
||||
Example: ./sbc_encoder.py fanfare.wav 16 4 31
|
||||
Usage: ./sbc_encoder.py input.wav blocks subbands bitpool allocation_method[0-LOUDNESS,1-SNR]
|
||||
Example: ./sbc_encoder.py fanfare.wav 16 4 31 0
|
||||
'''
|
||||
nr_blocks = 0
|
||||
nr_subbands = 0
|
||||
|
||||
|
||||
if (len(sys.argv) < 5):
|
||||
if (len(sys.argv) < 6):
|
||||
print(usage)
|
||||
sys.exit(1)
|
||||
try:
|
||||
@ -196,7 +196,8 @@ if __name__ == "__main__":
|
||||
|
||||
nr_blocks = int(sys.argv[2])
|
||||
nr_subbands = int(sys.argv[3])
|
||||
bitpool = int(sys.argv[4])
|
||||
bitpool = int(sys.argv[4])
|
||||
allocation_method = int(sys.argv[5])
|
||||
|
||||
fin = wave.open(infile, 'rb')
|
||||
nr_channels = fin.getnchannels()
|
||||
@ -211,7 +212,7 @@ if __name__ == "__main__":
|
||||
if subband_frame_count % 200 == 0:
|
||||
print("== Frame %d ==" % (subband_frame_count))
|
||||
|
||||
sbc_encoder_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency)
|
||||
sbc_encoder_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method)
|
||||
fetch_samples_for_next_sbc_frame(fin, sbc_encoder_frame)
|
||||
|
||||
sbc_encode(sbc_encoder_frame)
|
||||
|
@ -79,23 +79,26 @@ def sbc_compare_headers(frame_count, actual_frame, expected_frame):
|
||||
return 0
|
||||
|
||||
|
||||
def get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool):
|
||||
actual_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool)
|
||||
def get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method):
|
||||
actual_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method)
|
||||
fetch_samples_for_next_sbc_frame(fin, actual_frame)
|
||||
sbc_encode(actual_frame)
|
||||
return actual_frame
|
||||
|
||||
file_size = 0
|
||||
def get_expected_frame(fin_expected):
|
||||
global file_size
|
||||
expected_frame = SBCFrame()
|
||||
sbc_unpack_frame(fin_expected, expected_frame)
|
||||
|
||||
sbc_unpack_frame(fin_expected, file_size - fin_expected.tell(), expected_frame)
|
||||
return expected_frame
|
||||
|
||||
usage = '''
|
||||
Usage: ./sbc_encoder_test.py encoder_input.wav blocks subbands bitpool encoder_expected_output.sbc
|
||||
Example: ./sbc_encoder_test.py fanfare.wav 16 4 31 fanfare-4sb.sbc
|
||||
Usage: ./sbc_encoder_test.py encoder_input.wav blocks subbands bitpool allocation_method encoder_expected_output.sbc
|
||||
Example: ./sbc_encoder_test.py fanfare.wav 16 4 31 0 fanfare-4sb.sbc
|
||||
'''
|
||||
|
||||
if (len(sys.argv) < 6):
|
||||
if (len(sys.argv) < 7):
|
||||
print(usage)
|
||||
sys.exit(1)
|
||||
try:
|
||||
@ -103,7 +106,8 @@ try:
|
||||
nr_blocks = int(sys.argv[2])
|
||||
nr_subbands = int(sys.argv[3])
|
||||
bitpool = int(sys.argv[4])
|
||||
encoder_expected_sbc = sys.argv[5]
|
||||
allocation_method = int(sys.argv[5])
|
||||
encoder_expected_sbc = sys.argv[6]
|
||||
sampling_frequency = 44100
|
||||
|
||||
if not encoder_input_wav.endswith('.wav'):
|
||||
@ -120,6 +124,10 @@ try:
|
||||
nr_audio_frames = fin.getnframes()
|
||||
|
||||
fin_expected = open(encoder_expected_sbc, 'rb')
|
||||
fin_expected.seek(0,2)
|
||||
file_size = fin_expected.tell()
|
||||
fin_expected.seek(0,0)
|
||||
|
||||
subband_frame_count = 0
|
||||
audio_frame_count = 0
|
||||
nr_samples = nr_blocks * nr_subbands
|
||||
@ -128,7 +136,7 @@ try:
|
||||
if subband_frame_count % 200 == 0:
|
||||
print("== Frame %d ==" % (subband_frame_count))
|
||||
|
||||
actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency)
|
||||
actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method)
|
||||
expected_frame = get_expected_frame(fin_expected)
|
||||
|
||||
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
||||
@ -138,6 +146,10 @@ try:
|
||||
err = sbc_compare_audio_frames(subband_frame_count, actual_frame, expected_frame)
|
||||
if err < 0:
|
||||
exit(1)
|
||||
|
||||
if subband_frame_count == 0:
|
||||
print actual_frame
|
||||
|
||||
audio_frame_count += nr_samples
|
||||
subband_frame_count += 1
|
||||
|
||||
@ -145,6 +157,11 @@ try:
|
||||
fin.close()
|
||||
fin_expected.close()
|
||||
|
||||
except TypeError:
|
||||
print "DONE, max MSE audio sample error %d", max_error
|
||||
fin.close()
|
||||
fin_expected.close()
|
||||
|
||||
except IOError:
|
||||
print(usage)
|
||||
sys.exit(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user