xref: /btstack/test/sbc/sbc_encoder_test.py (revision 5665ea35c6aeb92912f2adba24f1fcfce0aa3616)
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_audio_frames(frame_count, actual_frame, expected_frame):
14    global error, max_error
15
16    M = mse(actual_frame.audio_sample, expected_frame.audio_sample)
17    if M > max_error:
18        max_error = M
19
20    if M > error:
21        print "audio_sample 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.syncword != expected_frame.syncword:
28        print "syncword wrong ", actual_frame.syncword
29        return -1
30
31    if actual_frame.sampling_frequency != expected_frame.sampling_frequency:
32        print "sampling_frequency wrong ", actual_frame.sampling_frequency
33        return -1
34
35    if actual_frame.nr_blocks != expected_frame.nr_blocks:
36        print "nr_blocks wrong ", actual_frame.nr_blocks
37        return -1
38
39    if actual_frame.channel_mode != expected_frame.channel_mode:
40        print "channel_mode wrong ", actual_frame.channel_mode
41        return -1
42
43    if actual_frame.nr_channels != expected_frame.nr_channels:
44        print "nr_channels wrong ", actual_frame.nr_channels
45        return -1
46
47    if actual_frame.allocation_method != expected_frame.allocation_method:
48        print "allocation_method wrong ", actual_frame.allocation_method
49        return -1
50
51    if actual_frame.nr_subbands != expected_frame.nr_subbands:
52        print "nr_subbands wrong ", actual_frame.nr_subbands
53        return -1
54
55    if actual_frame.bitpool != expected_frame.bitpool:
56        print "bitpool wrong (E: %d, D: %d)" % (actual_frame.bitpool, expected_frame.bitpool)
57        return -1
58
59    if  mse(actual_frame.join, expected_frame.join) > 0:
60        print "join error \nE:\n %s \nD:\n %s" % (actual_frame.join, expected_frame.join)
61        return -1
62
63    if  mse(actual_frame.scale_factor, expected_frame.scale_factor) > 0:
64        print "scale_factor error \nE:\n %s \nD:\n %s" % (actual_frame.scale_factor, expected_frame.scale_factor)
65        return -1
66
67    if  mse(actual_frame.scalefactor, expected_frame.scalefactor) > 0:
68        print "scalefactor error \nE:\n %s \nD:\n %s" % (actual_frame.scalefactor, expected_frame.scalefactor)
69        return -1
70
71    if  mse(actual_frame.bits, expected_frame.bits) > 0:
72        print "bits error \nE:\n %s \nD:\n %s" % (actual_frame.bits, expected_frame.bits)
73        return -1
74
75    if actual_frame.crc_check != expected_frame.crc_check:
76        print "crc_check wrong (E: %d, D: %d)" % (actual_frame.crc_check, expected_frame.crc_check)
77        return -1
78
79    return 0
80
81
82def get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method):
83    actual_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool, allocation_method)
84    fetch_samples_for_next_sbc_frame(fin, actual_frame)
85    sbc_encode(actual_frame)
86    return actual_frame
87
88file_size = 0
89def get_expected_frame(fin_expected):
90    global file_size
91    expected_frame = SBCFrame()
92
93    sbc_unpack_frame(fin_expected, file_size - fin_expected.tell(), expected_frame)
94    return expected_frame
95
96usage = '''
97Usage:      ./sbc_encoder_test.py encoder_input.wav blocks subbands bitpool allocation_method encoder_expected_output.sbc
98Example:    ./sbc_encoder_test.py fanfare.wav 16 4 31 0 fanfare-4sb.sbc
99'''
100
101if (len(sys.argv) < 7):
102    print(usage)
103    sys.exit(1)
104try:
105    encoder_input_wav = sys.argv[1]
106    nr_blocks = int(sys.argv[2])
107    nr_subbands = int(sys.argv[3])
108    bitpool = int(sys.argv[4])
109    allocation_method = int(sys.argv[5])
110    encoder_expected_sbc = sys.argv[6]
111    sampling_frequency = 44100
112
113    if not encoder_input_wav.endswith('.wav'):
114        print(usage)
115        sys.exit(1)
116
117    if not encoder_expected_sbc.endswith('.sbc'):
118        print(usage)
119        sys.exit(1)
120
121    fin = wave.open(encoder_input_wav, 'rb')
122    nr_channels = fin.getnchannels()
123    sampling_frequency = fin.getframerate()
124    nr_audio_frames = fin.getnframes()
125
126    fin_expected = open(encoder_expected_sbc, 'rb')
127    fin_expected.seek(0,2)
128    file_size = fin_expected.tell()
129    fin_expected.seek(0,0)
130
131    subband_frame_count = 0
132    audio_frame_count = 0
133    nr_samples = nr_blocks * nr_subbands
134
135    while audio_frame_count < nr_audio_frames:
136        if subband_frame_count % 200 == 0:
137            print("== Frame %d ==" % (subband_frame_count))
138
139        actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method)
140        expected_frame = get_expected_frame(fin_expected)
141
142        err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
143        if err < 0:
144            exit(1)
145
146        err = sbc_compare_audio_frames(subband_frame_count, actual_frame, expected_frame)
147        if err < 0:
148            exit(1)
149
150        if subband_frame_count == 0:
151            print actual_frame
152
153        audio_frame_count += nr_samples
154        subband_frame_count += 1
155
156    print "DONE, max MSE audio sample error %d", max_error
157    fin.close()
158    fin_expected.close()
159
160except TypeError:
161    print "DONE, max MSE audio sample error %d", max_error
162    fin.close()
163    fin_expected.close()
164
165except IOError:
166    print(usage)
167    sys.exit(1)
168
169
170
171
172
173