Exemplo n.º 1
0
    def send_file(self):
        self._transaction.set_status(TransactionStatus.SENDING)
        file_to_byte_array = QByteArray()
        data = QByteArray()
        stream = QDataStream(data, QIODevice.WriteOnly)

        QFile.open(self._file, QIODevice.ReadOnly)
        stream.writeUInt32(0)
        stream.writeString(self._file_name)
        print("size of name ", getsizeof(self._file_name))
        file_to_byte_array = self._file.readAll()
        data.append(file_to_byte_array)
        self._file.close()

        stream.device().seek(0)
        stream.writeUInt32(data.size() - getsizeof(self._file_name))
        print("total  ", data.size() - getsizeof(self._file_name))

        x = 0
        while (x < data.size()):
            y = self._socket.write(data)
            print("poslato  ", y)
            x += y
            print("x  ", x)
        self._transaction.set_status(TransactionStatus.FINISHED)
Exemplo n.º 2
0
    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()
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
class AudioDataHandler(QIODevice):
    """Class for storing incoming audio data in a buffer for later analysis.

    _buffer_size determines how much audio data is used when trying to find
    beats per minute (BPM) value. The value set has been found experimentally
    to give accurate BPM values without too much delay when BPM changes.

    With current settings (44100Hz, 8bit uint), the buffe fills with 44100B/s.
    Therefore, buffer of 350000 holds a bit more than 7 seconds of audio data.
    """
    data_ready = Signal(np.ndarray)
    _buffer_size = 350000

    def __init__(self, format_):
        super().__init__()

        self._buffer = QByteArray()
        self._format = format_
        self.data = None

    def start(self):
        self.open(QIODevice.WriteOnly)

    def stop(self):
        self.close()

    @Slot(QByteArray, int)
    def writeData(self, new_data, len_):
        """Write new data to buffer.

        Keeps buffer at constant size.

        Emits:
            data_ready(numpy.ndarray): When new data is written, emit all data
                                       for later analysis.
        """
        self._buffer.append(new_data.data())

        data = self._buffer.data()
        self._buffer = self._buffer.right(self._buffer_size)

        data_array = np.frombuffer(data, dtype="uint8")
        data_array = data_array.astype("int")
        self.data_ready.emit(data_array)

        return len_
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
 def testAppend(self):
     b = QByteArray()
     b.append("A")
     self.assertEqual(b.size(), 1)
     b.append("AB")
     self.assertEqual(b.size(), 3)
Exemplo n.º 7
0
 def testAppend(self):
     b = QByteArray()
     b.append("A")
     self.assertEqual(b.size(), 1)
     b.append("AB")
     self.assertEqual(b.size(), 3)
Exemplo n.º 8
0
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()