本文整理汇总了Python中PyQt5.QtCore.QByteArray.mid方法的典型用法代码示例。如果您正苦于以下问题:Python QByteArray.mid方法的具体用法?Python QByteArray.mid怎么用?Python QByteArray.mid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtCore.QByteArray
的用法示例。
在下文中一共展示了QByteArray.mid方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Generator
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import mid [as 别名]
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()