# Path: acquisition.py
# class UDPThread(QtCore.QThread):
#   newPacket = QtCore.pyqtSignal(object)
#   
#   def __init__(self, port=8888):
#     super(UDPThread, self).__init__()
# 
#     self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
#     self.socket.bind(('127.0.0.1', port))
# 
#     self.buffer = []
# 
#   def run(self):
#     while True:
#       data, addr = self.socket.recvfrom(1024)
#       data = eval(data)
#       
#       self.newPacket.emit(eval(str(data)))
#       #QtGui.QApplication.instance().processEvents()


from acquisition import UDPThread
from pyqtgraph import QtCore
import sys
import signal
import wave
import struct


app = QtCore.QCoreApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

source = UDPThread(port=9999)


f = wave.open(sys.argv[1], 'w')
f.setnchannels(8)
f.setsampwidth(3)
f.setframerate(250)


def handle_packet(packet):
	for ch in packet:
		data = struct.pack('<i', ch)
		f.writeframes(data[1:])
		#print (len(data[:1]))


	#print (data)



# Path: acquisition.py
# class UDPThread(QtCore.QThread):
#   newPacket = QtCore.pyqtSignal(object)
#   
#   def __init__(self, port=8888):
#     super(UDPThread, self).__init__()
# 
#     self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
#     self.socket.bind(('127.0.0.1', port))
# 
#     self.buffer = []
# 
#   def run(self):
#     while True:
#       data, addr = self.socket.recvfrom(1024)
#       data = eval(data)
#       
#       self.newPacket.emit(eval(str(data)))
#       #QtGui.QApplication.instance().processEvents()


# Path: record_wav.py
from acquisition import UDPThread
from pyqtgraph import QtCore
import sys
import signal
import wave
import struct



app = QtCore.QCoreApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

source = UDPThread(port=9999)


f = wave.open(sys.argv[1], 'w')
f.setnchannels(8)
f.setsampwidth(3)
f.setframerate(250)


def handle_packet(packet):
	for ch in packet:
		data = struct.pack('<i', ch)
		f.writeframes(data[1:])
		#print (len(data[:1]))


	#print (data)



source.newPacket.connect(handle_packet)