xref: /btstack/test/sbc/sbc_encoder_test.py (revision 5f21c38a78d3e0e2f0de80d07e8677062955ba30)
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):
83    actual_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool)
84    fetch_samples_for_next_sbc_frame(fin, actual_frame)
85    sbc_encode(actual_frame)
86    return actual_frame
87
88def get_expected_frame(fin_expected):
89    expected_frame = SBCFrame()
90    sbc_unpack_frame(fin_expected, expected_frame)
91    return expected_frame
92
93usage = '''
94Usage:      ./sbc_encoder_test.py encoder_input.wav blocks subbands bitpool encoder_expected_output.sbc
95Example:    ./sbc_encoder_test.py fanfare.wav 16 4 31 fanfare-4sb.sbc
96'''
97
98if (len(sys.argv) < 6):
99    print(usage)
100    sys.exit(1)
101try:
102    encoder_input_wav = sys.argv[1]
103    nr_blocks = int(sys.argv[2])
104    nr_subbands = int(sys.argv[3])
105    bitpool = int(sys.argv[4])
106    encoder_expected_sbc = sys.argv[5]
107    sampling_frequency = 44100
108
109    if not encoder_input_wav.endswith('.wav'):
110        print(usage)
111        sys.exit(1)
112
113    if not encoder_expected_sbc.endswith('.sbc'):
114        print(usage)
115        sys.exit(1)
116
117    fin = wave.open(encoder_input_wav, 'rb')
118    nr_channels = fin.getnchannels()
119    sampling_frequency = fin.getframerate()
120    nr_audio_frames = fin.getnframes()
121
122    fin_expected = open(encoder_expected_sbc, 'rb')
123    subband_frame_count = 0
124    audio_frame_count = 0
125    nr_samples = nr_blocks * nr_subbands
126
127    while audio_frame_count < nr_audio_frames:
128        if subband_frame_count % 200 == 0:
129            print("== Frame %d ==" % (subband_frame_count))
130
131        actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency)
132        expected_frame = get_expected_frame(fin_expected)
133
134        err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
135        if err < 0:
136            exit(1)
137
138        err = sbc_compare_audio_frames(subband_frame_count, actual_frame, expected_frame)
139        if err < 0:
140            exit(1)
141        audio_frame_count += nr_samples
142        subband_frame_count += 1
143
144    print "DONE, max MSE audio sample error %d", max_error
145    fin.close()
146    fin_expected.close()
147
148except IOError:
149    print(usage)
150    sys.exit(1)
151
152
153
154
155
156