def test_simple_empty(): params_send = TransmissionParameters() params_send.set_num_channels(16) params_recv = TransmissionParameters() params_recv.set_num_channels(16) params_recv.set_is_master(False) encoder = MessageEncoder(params_send) decoder = MessageDecoder(params_recv) decoder.start() org_data = b'' l = [] l.append(encoder.encode(org_data)) l.append(encoder.encode(org_data)) l.append(np.zeros(1000000, dtype='float32')) audio_data = np.hstack(l) decoder.add_frames(audio_data) time.sleep(5) recv_data0 = decoder.get_message() recv_data1 = decoder.get_message() decoder.stop() if org_data != recv_data0 or org_data != recv_data1: print('test_simple_empty failed') print(recv_data0) print(recv_data1) else: print('test_simple_empty success')
def test_simple_no_pad(): params_send = TransmissionParameters() params_send.set_num_channels(16) params_recv = TransmissionParameters() params_recv.set_num_channels(16) params_recv.set_is_master(False) encoder = MessageEncoder(params_send) decoder = MessageDecoder(params_recv) decoder.start() org_data = b'a' * params_send.get_max_payload_size() l = [] l.append(encoder.encode(org_data)) audio_data = np.hstack(l) decoder.add_frames(audio_data) time.sleep(5) recv_data0 = decoder.get_message() decoder.stop() if org_data != recv_data0: print('test_simple_no_pad failed') print(recv_data0) else: print('test_simple_no_pad success')
def test_segmented_pad(): params_send = TransmissionParameters() params_send.set_num_channels(16) params_recv = TransmissionParameters() params_recv.set_num_channels(16) params_recv.set_is_master(False) encoder = MessageEncoder(params_send) decoder = MessageDecoder(params_recv) decoder.start() org_data = b'a' * params_send.get_max_payload_size() l = [] l.append(np.zeros(12672, dtype='float32')) l.append(encoder.encode(org_data)) l.append(np.zeros(16672, dtype='float32')) audio_data = np.hstack(l) unit_test_test = np.empty((0, ), dtype='float32') for i in range(0, len(audio_data), 1000): decoder.add_frames(audio_data[i:i + 1000]) unit_test_test = np.hstack([unit_test_test, audio_data[i:i + 1000]]) if not np.array_equal(unit_test_test, audio_data): print('arrays are not equal, this should not happen') print(len(unit_test_test)) print(len(audio_data)) time.sleep(5) recv_data0 = decoder.get_message() decoder.stop() if org_data != recv_data0: print('test_segmented_pad failed') print(recv_data0) else: print('test_segmented_pad success')
def test_message_legnth_calc(): print('test_message_legnth_calc is slow') for ch in range(1, 17): params_send = TransmissionParameters() params_send.set_num_channels(ch) params_recv = TransmissionParameters() params_recv.set_num_channels(ch) params_recv.set_is_master(False) encoder = MessageEncoder(params_send) decoder = MessageDecoder(params_recv) for length in range(0, 63): orig = bytes([random.randint(0, 255) for _ in range(length)]) true_length = len(encoder.encode(orig)) calc_length = decoder._MessageDecoder__calc_message_size( len(orig), FrequencySet.RECV) if calc_length != true_length: print('test_message_legnth_calc failed') print(f'{ch=} {length=} {true_length=} {calc_length=}') return print('test_message_legnth_calc success')
class MainWindow: def __init__(self, ui_file): builder = Gtk.Builder() builder.add_from_file(ui_file) builder.connect_signals(self) self.__builder = builder self.__activate_switch = builder.get_object('activate_switch') self.__base_frequency = builder.get_object('base_frequency') self.__max_payload_size = builder.get_object('max_payload_size') self.__max_windows = builder.get_object('max_windows') self.__message_box = builder.get_object('message_box') self.__mode_selector = builder.get_object('mode_selector') self.__num_channels = builder.get_object('num_channels') self.__sample_rate = builder.get_object('sample_rate') self.__send = builder.get_object('send') self.__window = builder.get_object('window') self.__window_length = builder.get_object('window_length') self.__timeout = builder.get_object('timeout') self.__headerbar = builder.get_object('headerbar') self.__message_history = builder.get_object('message_history') self.__window.show_all() self.__params = None self.__resetting = False self.__set_default() self.__sliding_window = None GLib.timeout_add(100, self.on_stream_tick) self.__partial_message = '' def __enable_settings(self, state): self.__base_frequency.set_sensitive(state) self.__max_payload_size.set_sensitive(state) self.__max_windows.set_sensitive(state) self.__mode_selector.set_sensitive(state) self.__num_channels.set_sensitive(state) self.__sample_rate.set_sensitive(state) self.__window_length.set_sensitive(state) self.__message_box.set_sensitive(not state) if state: self.__message_box.set_text('') self.__message_box.set_placeholder_text(MESSAGE_INPUT_DISABLED) else: self.__message_box.set_placeholder_text(MESSAGE_INPUT_ACTIVE) #send is always disabled, if we leave setup mode because the text entry is empty self.__send.set_sensitive(False) def __update_subtitle(self): self.__headerbar.set_subtitle('Biterate: ~{:.1f} bps'.format(self.__params.get_max_bps())) def __set_default(self): self.__activate_switch.set_active(False) self.__enable_settings(True) self.__params = TransmissionParameters() self.__resetting = True self.__base_frequency.set_value(self.__params.get_base_freq()) self.__max_windows.set_value(self.__params.get_seq_max()) self.__max_payload_size.set_value(self.__params.get_max_payload_size()) self.__num_channels.set_value(self.__params.get_num_channels()) self.__sample_rate.set_value(self.__params.get_sample_rate() / 1000) self.__window_length.set_value(self.__params.get_window_length()) self.__mode_selector.set_active(not self.__params.get_is_master()) self.__timeout.set_text('{:.1f}'.format(self.__params.get_timeout())) self.__update_subtitle() self.__resetting = False def __add_message(self, msg): it = self.__message_history.get_end_iter() self.__message_history.insert(it, msg) def on_send_complete(self): self.__message_box.set_sensitive(True) self.__message_box.set_placeholder_text(MESSAGE_INPUT_ACTIVE) def on_data_available(self): data = self.__sliding_window.recv() self.__partial_message += data.decode('ascii') if self.__partial_message.endswith('\n'): self.__add_message('< ' + self.__partial_message) self.__partial_message = '' def on_stream_tick(self): if self.__sliding_window: self.__sliding_window.tick() return True def on_destroy(self, widget): if self.__sliding_window: self.__sliding_window.stop() self.__sliding_window = None Gtk.main_quit() def on_update_parameters(self, widget): if self.__resetting: return self.__params.set_base_freq(self.__base_frequency.get_value()) self.__params.set_seq_max(self.__max_windows.get_value_as_int()) self.__params.set_max_payload_size(self.__max_payload_size.get_value_as_int()) self.__params.set_num_channels(self.__num_channels.get_value_as_int()) self.__params.set_sample_rate(round(self.__sample_rate.get_value() * 1000)) self.__params.set_window_length(self.__window_length.get_value()) self.__params.set_is_master(not self.__mode_selector.get_active()) self.__update_subtitle() self.__timeout.set_text('{:.1f}'.format(self.__params.get_timeout())) def on_activate(self, widget, state): if state: self.__enable_settings(False) self.__sliding_window = SlidingWindow(self.__params) self.__sliding_window.attach_on_send_complete(self.on_send_complete) self.__sliding_window.attach_on_data_availbale(self.on_data_available) else: self.__enable_settings(True) self.__sliding_window.stop() self.__sliding_window = None def on_reset(self, widget): self.__set_default() def on_mode_switch(self, widget): if self.__mode_selector.get_active(): self.__mode_selector.set_label('Slave') else: self.__mode_selector.set_label('Master') self.on_update_parameters(None) def on_text_entry_change(self, widget): if self.__message_box.get_text() == '': self.__send.set_sensitive(False) else: self.__send.set_sensitive(True) def on_send(self, widget): message = self.__message_box.get_text() message += '\n' self.__add_message('> ' + message) self.__message_box.set_text('') self.__message_box.set_sensitive(False) self.__message_box.set_placeholder_text(MESSAGE_INPUT_SENDING) self.__sliding_window.send(message.encode('ascii'))