xref: /btstack/test/sbc/sbc_decoder_test.py (revision 4771dcb8bbb128f1a2264324662f0c69d4e4d9eb)
1#!/usr/bin/env python
2import numpy as np
3import wave
4import struct
5import sys
6from sbc import *
7from sbc_encoder import *
8from sbc_decoder import *
9
10error = 0.99
11max_error = -1
12
13def sbc_compare_pcm(frame_count, actual_frame, expected_frame):
14    global error, max_error
15
16    M = mse(actual_frame.pcm, expected_frame.pcm)
17    if M > max_error:
18        max_error = M
19
20    if M > error:
21        print "pcm error (%d, %d ) " % (frame_count, M)
22        return -1
23    return 0
24
25
26def sbc_compare_headers(frame_count, actual_frame, expected_frame):
27    if actual_frame.sampling_frequency != expected_frame.sampling_frequency:
28        print "sampling_frequency wrong ", actual_frame.sampling_frequency
29        return -1
30
31    if actual_frame.nr_blocks != expected_frame.nr_blocks:
32        print "nr_blocks wrong ", actual_frame.nr_blocks
33        return -1
34
35    if actual_frame.channel_mode != expected_frame.channel_mode:
36        print "channel_mode wrong ", actual_frame.channel_mode
37        return -1
38
39    if actual_frame.nr_channels != expected_frame.nr_channels:
40        print "nr_channels wrong ", actual_frame.nr_channels
41        return -1
42
43    if actual_frame.allocation_method != expected_frame.allocation_method:
44        print "allocation_method wrong ", actual_frame.allocation_method
45        return -1
46
47    if actual_frame.nr_subbands != expected_frame.nr_subbands:
48        print "nr_subbands wrong ", actual_frame.nr_subbands
49        return -1
50
51    if actual_frame.bitpool != expected_frame.bitpool:
52        print "bitpool wrong (E: %d, D: %d)" % (actual_frame.bitpool, expected_frame.bitpool)
53        return -1
54
55    return 0
56
57file_size = 0
58def get_actual_frame(fin):
59    global file_size
60    actual_frame = SBCFrame()
61    sbc_unpack_frame(fin, file_size - fin.tell(), actual_frame)
62    sbc_reconstruct_subband_samples(actual_frame)
63    sbc_synthesis(actual_frame)
64    return actual_frame
65
66def get_expected_frame(fin_expected, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method):
67    expected_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method)
68    fetch_samples_for_next_sbc_frame(fin_expected, expected_frame)
69    return expected_frame
70
71usage = '''
72Usage:      ./sbc_decoder_test.py decoder_input.sbc decoder_expected_output.wav
73Example:    ./sbc_decoder_test.py fanfare-4sb.sbc fanfare-4sb-decoded.wav
74'''
75
76if (len(sys.argv) < 3):
77    print(usage)
78    sys.exit(1)
79try:
80    decoder_input_sbc = sys.argv[1]
81    decoder_expected_wav = sys.argv[2]
82
83    if not decoder_input_sbc.endswith('.sbc'):
84        print(usage)
85        sys.exit(1)
86
87    if not decoder_expected_wav.endswith('.wav'):
88        print(usage)
89        sys.exit(1)
90
91    fin_expected = wave.open(decoder_expected_wav, 'rb')
92    nr_channels, sampwidth, sampling_frequency, nr_audio_frames, comptype, compname =  fin_expected.getparams()
93
94    with open(decoder_input_sbc, 'rb') as fin:
95        try:
96            subband_frame_count = 0
97            fin.seek(0,2)
98            file_size = fin.tell()
99            fin.seek(0,0)
100
101            while True:
102                if subband_frame_count % 200 == 0:
103                    print ("== Frame %d ==" % subband_frame_count)
104
105                actual_frame = get_actual_frame(fin)
106
107
108                expected_frame = get_expected_frame(fin_expected, actual_frame.nr_blocks,
109                                                actual_frame.nr_subbands, nr_channels,
110                                                actual_frame.bitpool, sampling_frequency,
111                                                actual_frame.allocation_method)
112
113
114                err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
115                if err < 0:
116                    print ("Headers differ \n%s\n%s" % (actual_frame, expected_frame))
117                    exit(1)
118
119                err = sbc_compare_pcm(subband_frame_count, actual_frame, expected_frame)
120                if err < 0:
121                    print ("PCMs differ \n%s\n%s" % (actual_frame.pcm, expected_frame.pcm))
122                    exit(1)
123
124                if subband_frame_count == 0:
125                    print actual_frame, expected_frame
126
127                subband_frame_count += 1
128
129        except TypeError:
130            fin_expected.close()
131            fin.close()
132            print ("DONE, max MSE PCM error %d" % max_error)
133            exit(0)
134
135except IOError as e:
136    print(usage)
137    sys.exit(1)
138
139
140
141
142
143