Skip to content

Commit

Permalink
Add widget for wifi debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgarcia committed Jan 19, 2018
1 parent 6cd5b1c commit 00435cb
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 13 deletions.
130 changes: 130 additions & 0 deletions hearinglosssimulator/gui/wifidevice/gui_debug_wifidevice.py
@@ -0,0 +1,130 @@

from hearinglosssimulator.gui.myqt import QT
import pyqtgraph as pg

import numpy as np
from hearinglosssimulator.gui.wifidevice.wifidevicewidget import WifiDeviceWidget
from hearinglosssimulator.gui.wifidevice.qwificlient import QWifiClient, ThreadAudioStream
from hearinglosssimulator.gui.wifidevice.wifideviceparameters import WifiDeviceParameter





class WindowDebugWifiDevice(QT.QWidget):
def __init__(self, client=None, parent = None):
QT.QWidget.__init__(self, parent)

self.client = client

self.resize(600,600)

mainlayout =QT.QVBoxLayout()
self.setLayout(mainlayout)

self.devicewidget = WifiDeviceWidget(client=client)
mainlayout.addWidget(self.devicewidget)

##
mainlayout.addWidget(QT.QLabel(u'<h1><b>Audio params</b>'))
h = QT.QHBoxLayout()
mainlayout.addLayout(h)
but = QT.QPushButton('Get params from device')
h.addWidget(but)
but.clicked.connect(self.get_params_from_device)
but = QT.QPushButton('Set params to device')
h.addWidget(but)
but.clicked.connect(self.set_params_to_device)
self.wifiDeviceParameter = WifiDeviceParameter()
mainlayout.addWidget(self.wifiDeviceParameter)

##
mainlayout.addWidget(QT.QLabel(u'<h1><b>ssid</b>'))
but = QT.QPushButton('Apply ssid')
mainlayout.addWidget(but)
but.clicked.connect(self.set_ssid)
self.edit_ssid = QT.QLineEdit()
mainlayout.addWidget(self.edit_ssid)

##
mainlayout.addWidget(QT.QLabel(u'<h1><b>Audio loop</b>'))
h = QT.QHBoxLayout()
mainlayout.addLayout(h)
but = QT.QPushButton('Start')
h.addWidget(but)
but.clicked.connect(self.start_audio)
but = QT.QPushButton('Stop')
h.addWidget(but)
but.clicked.connect(self.stop_audio)

self.thread_audio_loop = ThreadAudioStream(client.client_protocol, parent=self)
self.thread_audio_loop.connection_broken.connect(self.client.on_connection_broken)


def get_params_from_device(self):
sr = self.client.secure_call('get_sample_rate')
lat = self.client.secure_call('get_audio_latency')
speaker_gain = self.client.secure_call('get_speaker_gain')
microphone_gain = self.client.secure_call('get_microphone_gain')

#~ print(sr, lat, speaker_gain, microphone_gain)
self.wifiDeviceParameter.set_configuration(
nb_buffer_latency=lat,
sample_rate=sr,
speaker_gain=speaker_gain,
microphone_gain=microphone_gain,
)


def set_params_to_device(self):
p = self.wifiDeviceParameter.get_configuration()
#~ self.client.secure_call('set_sample_rate', p['sample_rate'])
self.client.secure_call('set_audio_latency', p['nb_buffer_latency'])
self.client.secure_call('set_speaker_gain', p['speaker_gain'])
self.client.secure_call('set_microphone_gain', p['microphone_gain'])

print('send', p)

self.devicewidget.refresh_label_param()

def set_ssid(self):
new_ssid = self.edit_ssid.text()
if len(new_ssid)>0:
print('apply_new_ssid', new_ssid)
self.client.secure_call('set_ssid', new_ssid)

def start_audio(self):
if self.client.state == 'disconnected':
print('oups disconnected')
elif self.client.state.endswith('loop'):
print('oups loop')
elif self.client.state == 'connected':
self.client.start_loop(self.thread_audio_loop, 'audio')
self.devicewidget.timer_missing.start()

def stop_audio(self):
if self.client.state == 'disconnected':
print('oups disconnected')
elif self.client.state == 'audio-loop':
self.client.stop_loop('audio')
elif self.client.state == 'connected':
print('oups not running')


def test_WindowDebugWifiDevice():
udp_ip = "192.168.1.1"
udp_port = 6666


import pyqtgraph as pg

client = QWifiClient(udp_ip=udp_ip, udp_port=udp_port)

app = pg.mkQApp()
win = WindowDebugWifiDevice(client=client)
win.show()
app.exec_()


if __name__ =='__main__':
test_WindowDebugWifiDevice()
3 changes: 2 additions & 1 deletion hearinglosssimulator/gui/wifidevice/packet_types.py
Expand Up @@ -88,7 +88,8 @@


# TIMEOUT
TIMEOUT_AUDIO = 0.2 #s
#~ TIMEOUT_AUDIO = 0.2 #s
TIMEOUT_AUDIO = 0.5 #s
TIMEOUT_TEST = 0.5 #s
TIMEOUT_PING_PONG = 0.3 #s
TIMEOUT_ACK_START_STREAM = 1.#s
Expand Down
18 changes: 12 additions & 6 deletions hearinglosssimulator/gui/wifidevice/protocol.py
Expand Up @@ -71,11 +71,17 @@ def wait_for_ack(self, timeout_per_packet=0.5, nb_try=3, reason=''):
#~ print('i', i)
header, data = self.receiv_one_packet(timeout=timeout_per_packet)
#~ print(header)
if header['type'] == pt.ACK and header['option'] == self.packet_num:
done = True
break
if header['type'] == pt.ACK and header['option'] == 0:
raise(NoAckError('NO-ACK'))
if header['type'] == pt.ACK:
if header['option'] == self.packet_num:
done = True
break

elif header['option'] == 0:
raise(NoAckError('NO-ACK'))

else:
print('!!!!! ACK with bad num_paquet', self.packet_num, header['option'] )


#~ assert done, 'No ACK for packet {} {}'.format(self.packet_num, reason)
if not done:
Expand Down Expand Up @@ -107,7 +113,7 @@ def send_stop_stream(self, stream_type='audio', insist=False):
self.send_one_packet(type=pt.STOP_STREAM, option=pt.stream_types[stream_type])
if insist:
try:
self.wait_for_ack(reason='STOP_STREAM '+ stream_type, nb_try=10)
self.wait_for_ack(reason='STOP_STREAM '+ stream_type, nb_try=15)
except:
for i in range(5):
print('NEW TRY stop stream', i)
Expand Down
10 changes: 9 additions & 1 deletion hearinglosssimulator/gui/wifidevice/qwificlient.py
Expand Up @@ -78,14 +78,22 @@ def run(self):

broken = False


first_loop = True
while True:
if not self.is_running():
break

try:
#~ if True:
#recv
header, data = self.client_protocol.receiv_one_packet(variable_size=1024, timeout=pt.TIMEOUT_AUDIO)
if first_loop:
first_loop = False
timeout = 1.
else:
timeout=pt.TIMEOUT_AUDIO

header, data = self.client_protocol.receiv_one_packet(variable_size=1024, timeout=timeout)
if not self.is_running():
break

Expand Down
25 changes: 25 additions & 0 deletions hearinglosssimulator/gui/wifidevice/tests/test_qwificlient.py
@@ -0,0 +1,25 @@


from hearinglosssimulator.gui.wifidevice.wifidevicewidget import WifiDeviceWidget
from hearinglosssimulator.gui.wifidevice.qwificlient import QWifiClient, ThreadAudioStream
from hearinglosssimulator.gui.wifidevice.gui_debug_wifidevice import WindowDebugWifiDevice


udp_ip = "192.168.1.1"
udp_port = 6666

def test_qwificlient():

import pyqtgraph as pg

client = QWifiClient(udp_ip=udp_ip, udp_port=udp_port)

app = pg.mkQApp()
win = WindowDebugWifiDevice(client=client)
win.show()
app.exec_()


if __name__ =='__main__':
test_qwificlient()

3 changes: 2 additions & 1 deletion hearinglosssimulator/gui/wifidevice/wifidevicewidget.py
Expand Up @@ -84,8 +84,9 @@ def refresh_label_state(self, new_state):
def refresh_label_param(self):
sr = self.client.secure_call('get_sample_rate')
nb_lat = self.client.secure_call('get_audio_latency')
ssid = self.client.secure_call('get_ssid')
latency = nb_lat * 256 /sr*1000
text = 'sample_rate: {} \nnb_buffer_latency: {} \nlatency: {:.1f}ms'.format(sr, nb_lat,latency)
text = 'ssid: {}\n sample_rate: {} \n nb_buffer_latency: {} \n latency: {:.1f}ms'.format(ssid, sr, nb_lat,latency)
self.label_param.setText(text)

def refresh_missing_label(self):
Expand Down
10 changes: 9 additions & 1 deletion hearinglosssimulator/gui/wifidevice_mainwindow.py
Expand Up @@ -93,9 +93,12 @@ def process_one_packet(self, header, data_buffer_in):
sound_in_float /= 2**15
self.index += 256
#~ print('self.index', self.index, sound_in_float.shape)
#~ t2 = time.perf_counter()
returns = self.processing.proccesing_func(self.index, sound_in_float)
index2, sound_out_float = returns['main_output']

#~ t3 = time.perf_counter()

#~ print('index2', index2)
if index2 is not None:
sound_out_int = apply_gain_and_cast(sound_out_float)
Expand All @@ -104,7 +107,12 @@ def process_one_packet(self, header, data_buffer_in):


#~ t1 = time.perf_counter()
#~ print(int(t1-t0)*1000/1000., 'ms')
#~ print(int(t2*10000-t0*10000)/10, 'ms')

#~ print(int(t3*10000-t2*10000)/10, 'ms')
#~ print(int(t1*10000-t3*10000)/10, 'ms')
#~ print('tot', int(t1*10000-t0*10000)/10, 'ms')
#~ print()
#~ print(t1-t0, 's')

variable = sound_out_int.tobytes()
Expand Down
18 changes: 18 additions & 0 deletions hearinglosssimulator/scripts.py
Expand Up @@ -38,6 +38,20 @@ def open_wifidevice_mainwindow():
win = WifiDeviceMainWindow()
win.show()
app.exec_()

def open_wifidebug():
from hearinglosssimulator.gui.wifidevice.gui_debug_wifidevice import WindowDebugWifiDevice
from hearinglosssimulator.gui.wifidevice.qwificlient import QWifiClient
import pyqtgraph as pg

udp_ip = "192.168.1.1"
udp_port = 6666
client = QWifiClient(udp_ip=udp_ip, udp_port=udp_port)

app = pg.mkQApp()
win = WindowDebugWifiDevice(client=client)
win.show()
app.exec_()

def hls():
argv = sys.argv[1:]
Expand Down Expand Up @@ -65,12 +79,16 @@ def hls():

elif command=='wifidevice':
open_hls_wifi()

elif command=='wifidebug':
open_wifidebug()



if __name__=='__main__':
#~ open_audiodevice_mainwindow()
open_wifidevice_mainwindow()
#~ open_wifidebug()



Expand Down
6 changes: 3 additions & 3 deletions hearinglosssimulator/tests/benchmark_processing.py
Expand Up @@ -13,9 +13,9 @@
#~ sample_rate = 20000.
#~ sample_rate = 15000.

#~ chunksize = 256
chunksize = 256
#~ chunksize = 512
chunksize = 1024
#~ chunksize = 1024
#~ chunksize = 2048
#~ backward_chunksize = chunksize*5
#~ backward_chunksize = chunksize*4
Expand All @@ -24,7 +24,7 @@
lost_chunksize = 1024
backward_chunksize = lost_chunksize + chunksize

nloop = 100
nloop = 1000

length = int(chunksize*nloop)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -43,6 +43,7 @@
entry_points={
'console_scripts': ['hls=hearinglosssimulator.scripts:hls',
'hlswifi=hearinglosssimulator.scripts:open_wifidevice_mainwindow',
'wifidebug=hearinglosssimulator.scripts:open_wifidebug',
],
},

Expand Down

0 comments on commit 00435cb

Please sign in to comment.