class Generator(QIODevice): def __init__(self, format, durationUs, sampleRate, parent): super(Generator, self).__init__(parent) self.m_pos = 0 self.m_buffer = QByteArray() self.generateData(format, durationUs, sampleRate) def start(self): self.open(QIODevice.ReadOnly) def stop(self): self.m_pos = 0 self.close() def generateData(self, format, durationUs, sampleRate): pack_format = '' if format.sampleSize() == 8: if format.sampleType() == QAudioFormat.UnSignedInt: scaler = lambda x: ((1.0 + x) / 2 * 255) pack_format = 'B' elif format.sampleType() == QAudioFormat.SignedInt: scaler = lambda x: x * 127 pack_format = 'b' elif format.sampleSize() == 16: if format.sampleType() == QAudioFormat.UnSignedInt: scaler = lambda x: (1.0 + x) / 2 * 65535 pack_format = '<H' if format.byteOrder() == QAudioFormat.LittleEndian else '>H' elif format.sampleType() == QAudioFormat.SignedInt: scaler = lambda x: x * 32767 pack_format = '<h' if format.byteOrder() == QAudioFormat.LittleEndian else '>h' assert(pack_format != '') channelBytes = format.sampleSize() // 8 sampleBytes = format.channelCount() * channelBytes length = (format.sampleRate() * format.channelCount() * (format.sampleSize() // 8)) * durationUs // 100000 self.m_buffer.clear() sampleIndex = 0 factor = 2 * pi * sampleRate / format.sampleRate() while length != 0: x = sin((sampleIndex % format.sampleRate()) * factor) packed = pack(pack_format, int(scaler(x))) for _ in range(format.channelCount()): self.m_buffer.append(packed) length -= channelBytes sampleIndex += 1 def readData(self, maxlen): data = QByteArray() total = 0 while maxlen > total: chunk = min(self.m_buffer.size() - self.m_pos, maxlen - total) data.append(self.m_buffer.mid(self.m_pos, chunk)) self.m_pos = (self.m_pos + chunk) % self.m_buffer.size() total += chunk return data.data() def writeData(self, data): return 0 def bytesAvailable(self): return self.m_buffer.size() + super(Generator, self).bytesAvailable()
class Generator(QIODevice): def __init__(self, format, durationUs, sampleRate, parent): super(Generator, self).__init__(parent) self.m_pos = 0 self.m_buffer = QByteArray() self.generateData(format, durationUs, sampleRate) def start(self): self.open(QIODevice.ReadOnly) def stop(self): self.m_pos = 0 self.close() def generateData(self, format, durationUs, sampleRate): pack_format = '' if format.sampleSize() == 8: if format.sampleType() == QAudioFormat.UnSignedInt: scaler = lambda x: ((1.0 + x) / 2 * 255) pack_format = 'B' elif format.sampleType() == QAudioFormat.SignedInt: scaler = lambda x: x * 127 pack_format = 'b' elif format.sampleSize() == 16: if format.sampleType() == QAudioFormat.UnSignedInt: scaler = lambda x: (1.0 + x) / 2 * 65535 pack_format = '<H' if format.byteOrder( ) == QAudioFormat.LittleEndian else '>H' elif format.sampleType() == QAudioFormat.SignedInt: scaler = lambda x: x * 32767 pack_format = '<h' if format.byteOrder( ) == QAudioFormat.LittleEndian else '>h' assert (pack_format != '') channelBytes = format.sampleSize() // 8 sampleBytes = format.channelCount() * channelBytes length = (format.sampleRate() * format.channelCount() * (format.sampleSize() // 8)) * durationUs // 100000 self.m_buffer.clear() sampleIndex = 0 factor = 2 * pi * sampleRate / format.sampleRate() while length != 0: x = sin((sampleIndex % format.sampleRate()) * factor) packed = pack(pack_format, int(scaler(x))) for _ in range(format.channelCount()): self.m_buffer.append(packed) length -= channelBytes sampleIndex += 1 def readData(self, maxlen): data = QByteArray() total = 0 while maxlen > total: chunk = min(self.m_buffer.size() - self.m_pos, maxlen - total) data.append(self.m_buffer.mid(self.m_pos, chunk)) self.m_pos = (self.m_pos + chunk) % self.m_buffer.size() total += chunk return data.data() def writeData(self, data): return 0 def bytesAvailable(self): return self.m_buffer.size() + super(Generator, self).bytesAvailable()
class NetworkConnectionClass(QtCore.QObject): SignalDataRec = Signal(str, int) SignalFrameDataAvailable = Signal() def __init__(self, parent=None): super(NetworkConnectionClass, self).__init__(parent=parent) #self.CommandSocket = QTcpSocket() self.CommandSocket = QUdpSocket() self.CommandSocket.bind(QHostAddress("192.168.100.2"), 2323) self.CommandSocket.connected.connect(self.EventConnectedHandle) self.CommandSocket.readyRead.connect(self.RecieveData) #self.CommandSocket.connectToHost("192.168.100.5",2323,QIODevice.ReadWrite) #self.CommandSocket.connectToHost("127.0.0.1",2323,QIODevice.ReadWrite) #self.CommandSocket.connectToHost("192.168.20.196",2323,QIODevice.ReadWrite) #print("NETWORK - ",self.CommandSocket.localAddress(),self.CommandSocket.peerAddress()) self.Display = WindowNetworkConnection() self.NetworkDataArray = QByteArray() self.NetworkDataArray.resize(2000) self.SignalDataRec.connect(self.Display.PrintData) self.BufferWrite = QBuffer(self.NetworkDataArray) self.BufferWrite.open(QBuffer.WriteOnly) self.BufferRead = QBuffer(self.NetworkDataArray) self.BufferRead.open(QBuffer.ReadOnly) self.ReadDataStream = QDataStream(self.BufferRead) self.ReadDataStream.setByteOrder(QDataStream.LittleEndian) self.WriteDataStream = QDataStream(self.BufferWrite) self.WriteDataStream.setByteOrder(QDataStream.LittleEndian) self.SocketDataStream = QDataStream(self.CommandSocket) self.SocketDataStream.setByteOrder(QDataStream.LittleEndian) self.Timer = QTime() self.Timer.start() self.BufferRead.seek(0) self.BufferWrite.seek(0) self.LimitBufferSize = 2000 self.MinTransferUnit = 7 self.MaxTransferUnit = 18 self.bytesAvailable = 0 self.Display.ui.pushButton.clicked.connect(self.SendData) #===================================================== WRITE BUFFER self.SendBuffer = QByteArray() #===================================================== # TestMessage = QByteArray(bytearray.fromhex('A1 A2 A3')); TestMessage2 = QByteArray(bytearray.fromhex('A4 A5 A6')) # self.BufferWriteQueue.write(TestMessage); self.BufferWriteQueue.write(TestMessage2) # print("BUFFER - ",self.NetworkDataArray) # self.Message = MessageData() # self.Message << self.NetworkDataStream << self.NetworkDataStream def __rshift__(self, Reciever: DataTransferHeader): Reciever << self.ReadDataStream self.bytesAvailable -= Reciever.UNIT_SIZE print("BYTES AVAILABLE - ", self.bytesAvailable) #print(" REABUFFER POS - ",self.BufferRead.pos(),'BYTES AVAILABLE - ',self.bytesAvailable,"UNIT SIZE -",Reciever.UNIT_SIZE) #print(" READ UNIT -",Reciever.UNIT_SIZE) #print(" READ BUFFER POS -",self.BufferRead.pos()) #print(" READ BUFFER -",self.BufferRead.data().toHex()) if not self.BufferRead.pos( ) < self.LimitBufferSize - self.MaxTransferUnit: self.BufferRead.seek(0) print("READ BUFFER SEEK TO 0 - ", self.BufferRead.pos()) return self def EventConnectedHandle(self): self.SignalDataRec.emit("CONNECTED TO HOST", 0) #self.SignalDataRec.emit("192.168.100.5 PORT 2323",0) self.SignalDataRec.emit("192.168.20.197 PORT 2323", 0) #self.SignalDataRec.emit("BYTES IN REC BUFFER - " + str(self.bytesAvailable)) def RecieveData(self): #self.CommandSocket.receiveDatagram(20) HEADER = QByteArray() HEADER.resize(2) if self.bytesAvailable < self.LimitBufferSize - self.MaxTransferUnit: #check that memory for packet available if self.BufferWrite.pos( ) < self.LimitBufferSize - self.MaxTransferUnit: (newData, sender, senderPort) = self.CommandSocket.readDatagram(20) #newData = self.CommandSocket.read(self.LimitBufferSize - self.bytesAvailable) #print(" NEW DATA - ",newData.toHex(),"to POS - ",self.BufferWrite.pos()) self.BufferWrite.write(newData) self.bytesAvailable += newData.size() print(self.Timer.restart()) #print("REC ",data) #print(" BUFFER - ",self.BufferWrite.data().toHex()) else: H1 = 0 H2 = 0 while self.CommandSocket.bytesAvailable() > 0: H1 = H2 H2 = self.SocketDataStream.readInt16() if H1 == 0xF1 and (H2 == 0xD1 or H2 == 0xD2 or H2 == 0xD3 or H2 == 0xC1): self.BufferWrite.seek(0) newData = self.CommandSocket.read( self.LimitBufferSize - self.bytesAvailable) self.WriteDataStream.writeInt16(H1) self.WriteDataStream.writeInt16(H2) print( "==============================================================" ) print("SEEK TO 0") print("NEW DATA - ", newData, "to POS - ", self.BufferWrite.pos() - 4) self.BufferWrite.write(newData) self.bytesAvailable += (4 + newData.size()) #print("BUFFER - ",self.NetworkDataArray.toHex()) break else: self.WriteDataStream.writeInt16(H2) self.bytesAvailable += 1 #self.SignalDataRec.emit("BYTES IN REC BUFFER - " + str(self.bytesAvailable)) if (self.bytesAvailable >= self.MinTransferUnit): self.SignalFrameDataAvailable.emit() def SendData(self): Req = ConnectCheckRequest() self.SendBuffer.clear() self.SendBuffer.resize(Req.MESSAGE_SIZE) WriteDataStream = QDataStream(self.SendBuffer, QIODevice.ReadWrite) WriteDataStream.setByteOrder(QDataStream.LittleEndian) Req >> WriteDataStream self.CommandSocket.write(self.SendBuffer) self.CommandSocket.waitForBytesWritten(20) self.SignalDataRec.emit("CHECK CONNECT - " + str(Req.Connect), 1) #self.CommandSocket.write(bytearray(b'TestMessage\r\n')) #print("WRITE BUFFER - ",self.SendBuffer.toHex()) #print("===============================================") def HandleErrorSocket(self): print("ErrorSocket")