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 57 58def get_actual_frame(fin): 59 actual_frame = SBCFrame() 60 sbc_unpack_frame(fin, actual_frame) 61 sbc_reconstruct_subband_samples(actual_frame) 62 sbc_synthesis(actual_frame) 63 return actual_frame 64 65def get_expected_frame(fin_expected, nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool): 66 expected_frame = SBCFrame(nr_blocks, nr_subbands, nr_channels, sampling_frequency, bitpool) 67 fetch_samples_for_next_sbc_frame(fin_expected, expected_frame) 68 return expected_frame 69 70usage = ''' 71Usage: ./sbc_decoder_test.py decoder_input.sbc decoder_expected_output.wav 72Example: ./sbc_decoder_test.py fanfare-4sb.sbc fanfare-4sb-decoded.wav 73''' 74 75if (len(sys.argv) < 3): 76 print(usage) 77 sys.exit(1) 78try: 79 decoder_input_sbc = sys.argv[1] 80 decoder_expected_wav = sys.argv[2] 81 82 if not decoder_input_sbc.endswith('.sbc'): 83 print(usage) 84 sys.exit(1) 85 86 if not decoder_expected_wav.endswith('.wav'): 87 print(usage) 88 sys.exit(1) 89 90 fin_expected = wave.open(decoder_expected_wav, 'rb') 91 nr_channels, sampwidth, sampling_frequency, nr_audio_frames, comptype, compname = fin_expected.getparams() 92 93 # print nr_channels, sampwidth, sampling_frequency, nr_audio_frames, comptype, compname 94 95 with open(decoder_input_sbc, 'rb') as fin: 96 try: 97 subband_frame_count = 0 98 while True: 99 if subband_frame_count % 200 == 0: 100 print "== Frame %d ==" % (subband_frame_count) 101 102 actual_frame = get_actual_frame(fin) 103 104 expected_frame = get_expected_frame(fin_expected, actual_frame.nr_blocks, 105 actual_frame.nr_subbands, nr_channels, 106 actual_frame.bitpool, sampling_frequency) 107 108 109 err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame) 110 if err < 0: 111 exit(1) 112 113 err = sbc_compare_pcm(subband_frame_count, actual_frame, expected_frame) 114 if err < 0: 115 exit(1) 116 117 subband_frame_count += 1 118 119 except TypeError: 120 fin_expected.close() 121 fin.close() 122 print "DONE, max MSE PCM error %d", max_error 123 exit(0) 124 125except IOError as e: 126 print(usage) 127 sys.exit(1) 128 129 130 131 132 133