def populateTable(self): row = 0 format = QAudioFormat() for codec in self.deviceInfo.supportedCodecs(): format.setCodec(codec) for frequency in self.deviceInfo.supportedFrequencies(): format.setFrequency(frequency) for channels in self.deviceInfo.supportedChannels(): format.setChannels(channels) for sampleType in self.deviceInfo.supportedSampleTypes(): format.setSampleType(sampleType) for sampleSize in self.deviceInfo.supportedSampleSizes( ): format.setSampleSize(sampleSize) for endian in self.deviceInfo.supportedByteOrders( ): format.setByteOrder(endian) if self.deviceInfo.isFormatSupported(format): self.allFormatsTable.setRowCount(row + 1) codecItem = QTableWidgetItem( format.codec()) self.allFormatsTable.setItem( row, 0, codecItem) frequencyItem = QTableWidgetItem( str(format.frequency())) self.allFormatsTable.setItem( row, 1, frequencyItem) channelsItem = QTableWidgetItem( str(format.channels())) self.allFormatsTable.setItem( row, 2, channelsItem) sampleTypeItem = QTableWidgetItem( sampletoString(format.sampleType())) self.allFormatsTable.setItem( row, 3, sampleTypeItem) sampleSizeItem = QTableWidgetItem( str(format.sampleSize())) self.allFormatsTable.setItem( row, 4, sampleSizeItem) byteOrderItem = QTableWidgetItem( byteOrdertoString(format.byteOrder())) self.allFormatsTable.setItem( row, 5, byteOrderItem) row += 1
def populateTable(self): row = 0 format = QAudioFormat() for codec in self.deviceInfo.supportedCodecs(): format.setCodec(codec) for frequency in self.deviceInfo.supportedFrequencies(): format.setFrequency(frequency) for channels in self.deviceInfo.supportedChannels(): format.setChannels(channels) for sampleType in self.deviceInfo.supportedSampleTypes(): format.setSampleType(sampleType) for sampleSize in self.deviceInfo.supportedSampleSizes(): format.setSampleSize(sampleSize) for endian in self.deviceInfo.supportedByteOrders(): format.setByteOrder(endian) if self.deviceInfo.isFormatSupported(format): self.allFormatsTable.setRowCount(row + 1); codecItem = QTableWidgetItem(format.codec()) self.allFormatsTable.setItem(row, 0, codecItem) frequencyItem = QTableWidgetItem(str(format.frequency())) self.allFormatsTable.setItem(row, 1, frequencyItem) channelsItem = QTableWidgetItem(str(format.channels())) self.allFormatsTable.setItem(row, 2, channelsItem) sampleTypeItem = QTableWidgetItem(sampletoString(format.sampleType())) self.allFormatsTable.setItem(row, 3, sampleTypeItem) sampleSizeItem = QTableWidgetItem(str(format.sampleSize())) self.allFormatsTable.setItem(row, 4, sampleSizeItem) byteOrderItem = QTableWidgetItem(byteOrdertoString(format.byteOrder())) self.allFormatsTable.setItem(row, 5, byteOrderItem) row += 1
class AudioTest(QObject): modeHasChanged = Signal("QVariantList") newResult = Signal(str) newNearest = Signal("QVariantList") def __init__(self, parent=None): QObject.__init__(self, parent) self.deviceInfo = QAudioDeviceInfo() self.settings = QAudioFormat() self.mode = QAudio.Mode() self._availableDevices = {} self._availableCodecs = {} self._availableFreqs = {} self._availableChannels = {} self._availableTypes = {} self._availableSizes = {} self._availableEnds = {} self.mode = QAudio.AudioOutput @Slot() def test(self): # tries to set all the settings picked. if not self.deviceInfo.isNull(): if self.deviceInfo.isFormatSupported(self.settings): self.newResult.emit("Success") ''' self.testResult.setText(self.tr("Success")) self.nearestFreq.setText("") self.nearestChannel.setText("") self.nearestCodec.setText("") self.nearestSampleSize.setText("") self.nearestSampleType.setText("") self.nearestEndian.setText("") ''' else: nearest = self.deviceInfo.nearestFormat(self.settings) self.newResult.emit("Failed") newValues = [] newValues.append("") # keep the same positions as newLabels list newValues.append(nearest.codec()) # codec - 1 newValues.append(str(nearest.frequency())) # freq - 2 newValues.append(str(nearest.channels())) # channel - 3 newValues.append(sampletoString(nearest.sampleType())) # type - 4 newValues.append(str(nearest.sampleSize())) # size - 5 newValues.append(byteOrdertoString(nearest.byteOrder())) # end - 6 self.newNearest.emit(newValues) else: self.newResult.emit("No Device") @Slot(str) def modeChanged(self, idx): # mode has changed if idx == "Input": mode = QAudio.AudioInput else: mode = QAudio.AudioOutput self._availableDevices = {} for deviceInfo in QAudioDeviceInfo.availableDevices(mode): self._availableDevices[deviceInfo.deviceName()] = deviceInfo firstDevice = QAudioDeviceInfo.availableDevices(mode)[0].deviceName() self.deviceChanged(firstDevice) @Slot(str) def deviceChanged(self, idx): if len(self._availableDevices) == 0: return newLabels = [] newLabels.append(idx) # device - 0 # device has changed self.deviceInfo = self._availableDevices[idx] codecz = self.deviceInfo.supportedCodecs() self._availableCodecs = {} for codec in codecz: self._availableCodecs[codec] = codec if len(codecz): self.settings.setCodec(codecz[0]) newLabels.append(codecz[0]) # codec - 1 freqz = self.deviceInfo.supportedFrequencies() self._availableFreqs = {} for freq in freqz: self._availableFreqs[str(freq)] = freq if len(freqz): self.settings.setFrequency(freqz[0]) newLabels.append(str(freqz[0])) # freq - 2 chz = self.deviceInfo.supportedChannels() self._availableChannels = {} for ch in chz: self._availableChannels[str(ch)] = ch # Add false to create failed condition! self._availableChannels["FAIL"] = -1 if len(chz): self.settings.setChannels(chz[0]) newLabels.append(str(chz[0])) # channel - 3 sampleTypes = self.deviceInfo.supportedSampleTypes() self._availableTypes = {} for sample in sampleTypes: self._availableTypes[sampletoString(sample)] = sample if len(sampleTypes): self.settings.setSampleType(sampleTypes[0]) newLabels.append(sampletoString(sampleTypes[0])) # type - 4 sampleSizes = self.deviceInfo.supportedSampleSizes() self._availableSizes = {} for sample in sampleSizes: self._availableSizes[str(sample)] = sample if len(sampleSizes): self.settings.setSampleSize(sampleSizes[0]) newLabels.append(str(sampleSizes[0])) # size - 5 endianz = self.deviceInfo.supportedByteOrders() self._availableEnds = {} for endian in endianz: self._availableEnds[byteOrdertoString(endian)] = endian if len(endianz): self.settings.setByteOrder(endianz[0]) newLabels.append(byteOrdertoString(endianz[0])) # end - 6 # Update all the buttons self.modeHasChanged.emit(newLabels) @Slot(str) def codecChanged(self, idx): self.settings.setCodec(self._availableCodecs[idx]) @Slot(str) def freqChanged(self, idx): # freq has changed self.settings.setFrequency(int(self._availableFreqs[idx])) @Slot(str) def channelChanged(self, idx): self.settings.setChannels(int(self._availableChannels[idx])) @Slot(str) def sizeChanged(self, idx): self.settings.setSampleSize(int(self._availableSizes[idx])) @Slot(str) def typeChanged(self, idx): sampleType = stringToSample(self._availableTypes[idx]) if sampleType == QAudioFormat.SignedInt: self.settings.setSampleType(QAudioFormat.SignedInt) elif sampleType == QAudioFormat.UnSignedInt: self.settings.setSampleType(QAudioFormat.UnSignedInt) elif sampleType == QAudioFormat.Float: self.settings.setSampleType(QAudioFormat.Float) @Slot(str) def endChanged(self, idx): endian = stringToByteOrder(self._availableEnds[idx]) if endian == QAudioFormat.LittleEndian: self.settings.setByteOrder(QAudioFormat.LittleEndian) elif endian == QAudioFormat.BigEndian: self.settings.setByteOrder(QAudioFormat.BigEndian) @Property("QStringList", constant=True) def availableDevices(self): return self._availableDevices.keys() @Property("QStringList", constant=True) def availableCodecs(self): return self._availableCodecs.keys() @Property("QStringList", constant=True) def availableFreqs(self): return self._availableFreqs.keys() @Property("QStringList", constant=True) def availableChannels(self): return self._availableChannels.keys() @Property("QStringList", constant=True) def availableTypes(self): return self._availableTypes.keys() @Property("QStringList", constant=True) def availableSizes(self): return self._availableSizes.keys() @Property("QStringList", constant=True) def availableEnds(self): return self._availableEnds.keys()
class AudioTest(AudioDevicesBase, Ui_AudioDevicesBase): def __init__(self, parent=None): AudioDevicesBase.__init__(self, parent) self.deviceInfo = QAudioDeviceInfo() self.settings = QAudioFormat() self.mode = QAudio.Mode() self.mode = QAudio.AudioOutput self.testButton.clicked.connect(self.test) self.modeBox.activated[int].connect(self.modeChanged) self.deviceBox.activated[int].connect(self.deviceChanged) self.frequencyBox.activated[int].connect(self.freqChanged) self.channelsBox.activated[int].connect(self.channelChanged) self.codecsBox.activated[int].connect(self.codecChanged) self.sampleSizesBox.activated[int].connect(self.sampleSizeChanged) self.sampleTypesBox.activated[int].connect(self.sampleTypeChanged) self.endianBox.activated[int].connect(self.endianChanged) self.populateTableButton.clicked.connect(self.populateTable) self.modeBox.setCurrentIndex(0) self.modeChanged(0) self.deviceBox.setCurrentIndex(0) self.deviceChanged(0) def test(self): # tries to set all the settings picked. self.testResult.clear() if not self.deviceInfo.isNull(): if self.deviceInfo.isFormatSupported(self.settings): self.testResult.setText(self.tr("Success")) self.nearestFreq.setText("") self.nearestChannel.setText("") self.nearestCodec.setText("") self.nearestSampleSize.setText("") self.nearestSampleType.setText("") self.nearestEndian.setText("") else: nearest = self.deviceInfo.nearestFormat(self.settings) self.testResult.setText(self.tr("Failed")) self.nearestFreq.setText(str(nearest.frequency())) self.nearestChannel.setText(str(nearest.channels())) self.nearestCodec.setText(nearest.codec()) self.nearestSampleSize.setText(str(nearest.sampleSize())) self.nearestSampleType.setText( sampletoString(nearest.sampleType())) self.nearestEndian.setText( byteOrdertoString(nearest.byteOrder())) else: self.testResult.setText(self.tr("No Device")) def modeChanged(self, idx): self.testResult.clear() # mode has changed if idx == 0: mode = QAudio.AudioInput else: mode = QAudio.AudioOutput self.deviceBox.clear() for deviceInfo in QAudioDeviceInfo.availableDevices(mode): self.deviceBox.addItem(deviceInfo.deviceName(), deviceInfo) self.deviceBox.setCurrentIndex(0) self.deviceChanged(0) def deviceChanged(self, idx): self.testResult.clear() if self.deviceBox.count() == 0: return # device has changed self.deviceInfo = self.deviceBox.itemData(idx) self.frequencyBox.clear() freqz = self.deviceInfo.supportedFrequencies() for freq in freqz: self.frequencyBox.addItem(str(freq)) if len(freqz): self.settings.setFrequency(freqz[0]) self.channelsBox.clear() chz = self.deviceInfo.supportedChannels() for ch in chz: self.channelsBox.addItem(str(ch)) if len(chz): self.settings.setChannels(chz[0]) self.codecsBox.clear() codecz = self.deviceInfo.supportedCodecs() for codec in codecz: self.codecsBox.addItem(codec) if len(codecz): self.settings.setCodec(codecz[0]) # Add false to create failed condition! self.codecsBox.addItem("audio/test") self.sampleSizesBox.clear() sampleSizez = self.deviceInfo.supportedSampleSizes() for sample in sampleSizez: self.sampleSizesBox.addItem(str(sample)) if len(sampleSizez): self.settings.setSampleSize(sampleSizez[0]) self.sampleTypesBox.clear() sampleTypez = self.deviceInfo.supportedSampleTypes() for sample in sampleTypez: self.sampleTypesBox.addItem(sampletoString(sample)) if len(sampleTypez): self.settings.setSampleType(sampleTypez[0]) self.endianBox.clear() endianz = self.deviceInfo.supportedByteOrders() for endian in endianz: self.endianBox.addItem(byteOrdertoString(endian)) if len(endianz): self.settings.setByteOrder(endianz[0]) self.allFormatsTable.clearContents() def populateTable(self): row = 0 format = QAudioFormat() for codec in self.deviceInfo.supportedCodecs(): format.setCodec(codec) for frequency in self.deviceInfo.supportedFrequencies(): format.setFrequency(frequency) for channels in self.deviceInfo.supportedChannels(): format.setChannels(channels) for sampleType in self.deviceInfo.supportedSampleTypes(): format.setSampleType(sampleType) for sampleSize in self.deviceInfo.supportedSampleSizes( ): format.setSampleSize(sampleSize) for endian in self.deviceInfo.supportedByteOrders( ): format.setByteOrder(endian) if self.deviceInfo.isFormatSupported(format): self.allFormatsTable.setRowCount(row + 1) codecItem = QTableWidgetItem( format.codec()) self.allFormatsTable.setItem( row, 0, codecItem) frequencyItem = QTableWidgetItem( str(format.frequency())) self.allFormatsTable.setItem( row, 1, frequencyItem) channelsItem = QTableWidgetItem( str(format.channels())) self.allFormatsTable.setItem( row, 2, channelsItem) sampleTypeItem = QTableWidgetItem( sampletoString(format.sampleType())) self.allFormatsTable.setItem( row, 3, sampleTypeItem) sampleSizeItem = QTableWidgetItem( str(format.sampleSize())) self.allFormatsTable.setItem( row, 4, sampleSizeItem) byteOrderItem = QTableWidgetItem( byteOrdertoString(format.byteOrder())) self.allFormatsTable.setItem( row, 5, byteOrderItem) row += 1 def freqChanged(self, idx): # freq has changed self.settings.setFrequency(int(self.frequencyBox.itemText(idx))) def channelChanged(self, idx): self.settings.setChannels(int(self.channelsBox.itemText(idx))) def codecChanged(self, idx): self.settings.setCodec(self.codecsBox.itemText(idx)) def sampleSizeChanged(self, idx): self.settings.setSampleSize(int(self.sampleSizesBox.itemText(idx))) def sampleTypeChanged(self, idx): sampleType = stringToSample(self.sampleTypesBox.itemText(idx)) if sampleType == QAudioFormat.SignedInt: self.settings.setSampleType(QAudioFormat.SignedInt) elif sampleType == QAudioFormat.UnSignedInt: self.settings.setSampleType(QAudioFormat.UnSignedInt) elif sampleType == QAudioFormat.Float: self.settings.setSampleType(QAudioFormat.Float) def endianChanged(self, idx): endian = stringToByteOrder(self.endianBox.itemText(idx)) if endian == QAudioFormat.LittleEndian: self.settings.setByteOrder(QAudioFormat.LittleEndian) elif endian == QAudioFormat.BigEndian: self.settings.setByteOrder(QAudioFormat.BigEndian)
class AudioTest(AudioDevicesBase, Ui_AudioDevicesBase): def __init__(self, parent=None): AudioDevicesBase.__init__(self, parent) self.deviceInfo = QAudioDeviceInfo() self.settings = QAudioFormat() self.mode = QAudio.Mode() self.mode = QAudio.AudioOutput self.testButton.clicked.connect(self.test) self.modeBox.activated[int].connect(self.modeChanged) self.deviceBox.activated[int].connect(self.deviceChanged) self.frequencyBox.activated[int].connect(self.freqChanged) self.channelsBox.activated[int].connect(self.channelChanged) self.codecsBox.activated[int].connect(self.codecChanged) self.sampleSizesBox.activated[int].connect(self.sampleSizeChanged) self.sampleTypesBox.activated[int].connect(self.sampleTypeChanged) self.endianBox.activated[int].connect(self.endianChanged) self.populateTableButton.clicked.connect(self.populateTable) self.modeBox.setCurrentIndex(0) self.modeChanged(0) self.deviceBox.setCurrentIndex(0) self.deviceChanged(0) def test(self): # tries to set all the settings picked. self.testResult.clear() if not self.deviceInfo.isNull(): if self.deviceInfo.isFormatSupported(self.settings): self.testResult.setText(self.tr("Success")) self.nearestFreq.setText("") self.nearestChannel.setText("") self.nearestCodec.setText("") self.nearestSampleSize.setText("") self.nearestSampleType.setText("") self.nearestEndian.setText("") else: nearest = self.deviceInfo.nearestFormat(self.settings) self.testResult.setText(self.tr("Failed")) self.nearestFreq.setText(str(nearest.frequency())) self.nearestChannel.setText(str(nearest.channels())) self.nearestCodec.setText(nearest.codec()) self.nearestSampleSize.setText(str(nearest.sampleSize())) self.nearestSampleType.setText(sampletoString(nearest.sampleType())) self. nearestEndian.setText(byteOrdertoString(nearest.byteOrder())) else: self.testResult.setText(self.tr("No Device")) def modeChanged(self, idx): self.testResult.clear(); # mode has changed if idx == 0: mode = QAudio.AudioInput else: mode = QAudio.AudioOutput self.deviceBox.clear() for deviceInfo in QAudioDeviceInfo.availableDevices(mode): self.deviceBox.addItem(deviceInfo.deviceName(), deviceInfo) self.deviceBox.setCurrentIndex(0) self.deviceChanged(0) def deviceChanged(self, idx): self.testResult.clear() if self.deviceBox.count() == 0: return # device has changed self.deviceInfo = self.deviceBox.itemData(idx) self.frequencyBox.clear() freqz = self.deviceInfo.supportedFrequencies() for freq in freqz: self.frequencyBox.addItem(str(freq)) if len(freqz): self.settings.setFrequency(freqz[0]) self.channelsBox.clear(); chz = self.deviceInfo.supportedChannels() for ch in chz: self.channelsBox.addItem(str(ch)) if len(chz): self.settings.setChannels(chz[0]) self.codecsBox.clear() codecz = self.deviceInfo.supportedCodecs() for codec in codecz: self.codecsBox.addItem(codec) if len(codecz): self.settings.setCodec(codecz[0]) # Add false to create failed condition! self.codecsBox.addItem("audio/test"); self.sampleSizesBox.clear() sampleSizez = self.deviceInfo.supportedSampleSizes() for sample in sampleSizez: self.sampleSizesBox.addItem(str(sample)) if len(sampleSizez): self.settings.setSampleSize(sampleSizez[0]) self.sampleTypesBox.clear() sampleTypez = self.deviceInfo.supportedSampleTypes() for sample in sampleTypez: self.sampleTypesBox.addItem(sampletoString(sample)) if len(sampleTypez): self.settings.setSampleType(sampleTypez[0]) self.endianBox.clear(); endianz = self.deviceInfo.supportedByteOrders() for endian in endianz: self.endianBox.addItem(byteOrdertoString(endian)) if len(endianz): self.settings.setByteOrder(endianz[0]) self.allFormatsTable.clearContents() def populateTable(self): row = 0 format = QAudioFormat() for codec in self.deviceInfo.supportedCodecs(): format.setCodec(codec) for frequency in self.deviceInfo.supportedFrequencies(): format.setFrequency(frequency) for channels in self.deviceInfo.supportedChannels(): format.setChannels(channels) for sampleType in self.deviceInfo.supportedSampleTypes(): format.setSampleType(sampleType) for sampleSize in self.deviceInfo.supportedSampleSizes(): format.setSampleSize(sampleSize) for endian in self.deviceInfo.supportedByteOrders(): format.setByteOrder(endian) if self.deviceInfo.isFormatSupported(format): self.allFormatsTable.setRowCount(row + 1); codecItem = QTableWidgetItem(format.codec()) self.allFormatsTable.setItem(row, 0, codecItem) frequencyItem = QTableWidgetItem(str(format.frequency())) self.allFormatsTable.setItem(row, 1, frequencyItem) channelsItem = QTableWidgetItem(str(format.channels())) self.allFormatsTable.setItem(row, 2, channelsItem) sampleTypeItem = QTableWidgetItem(sampletoString(format.sampleType())) self.allFormatsTable.setItem(row, 3, sampleTypeItem) sampleSizeItem = QTableWidgetItem(str(format.sampleSize())) self.allFormatsTable.setItem(row, 4, sampleSizeItem) byteOrderItem = QTableWidgetItem(byteOrdertoString(format.byteOrder())) self.allFormatsTable.setItem(row, 5, byteOrderItem) row += 1 def freqChanged(self, idx): # freq has changed self.settings.setFrequency(int(self.frequencyBox.itemText(idx))) def channelChanged(self, idx): self.settings.setChannels(int(self.channelsBox.itemText(idx))) def codecChanged(self, idx): self.settings.setCodec(self.codecsBox.itemText(idx)) def sampleSizeChanged(self, idx): self.settings.setSampleSize(int(self.sampleSizesBox.itemText(idx))) def sampleTypeChanged(self, idx): sampleType = stringToSample(self.sampleTypesBox.itemText(idx)) if sampleType == QAudioFormat.SignedInt: self.settings.setSampleType(QAudioFormat.SignedInt) elif sampleType == QAudioFormat.UnSignedInt: self.settings.setSampleType(QAudioFormat.UnSignedInt) elif sampleType == QAudioFormat.Float: self.settings.setSampleType(QAudioFormat.Float) def endianChanged(self, idx): endian = stringToByteOrder(self.endianBox.itemText(idx)) if endian == QAudioFormat.LittleEndian: self.settings.setByteOrder(QAudioFormat.LittleEndian) elif endian == QAudioFormat.BigEndian: self.settings.setByteOrder(QAudioFormat.BigEndian)
class TonePlayer(QObject): changed = Signal() def getStateLabel(self): print "Getting" return self._label def setStateLabel(self, value): print "Setting", value self._label = value self.changed.emit() stateLabel = Property(str, getStateLabel, setStateLabel, notify=changed) def __init__(self, devices=None, filename=None, parent=None): QObject.__init__(self, parent) self.pullTimer = QTimer(self) self.buf = QByteArray() self.devices = devices self.device = QAudioDeviceInfo.defaultOutputDevice() self.generator = None self.audioOutput = None self.output = None self.fmt = QAudioFormat() self.pullMode = False self.dump = filename self._label = SUSPEND_LABEL self.initializeAudio() def initializeAudio(self): self.pullTimer.timeout.connect(self.pullTimerExpired) self.pullMode = True self.fmt.setFrequency(DATA_FREQUENCY_HZ) self.fmt.setChannels(1) self.fmt.setSampleSize(16) self.fmt.setCodec('audio/pcm') self.fmt.setByteOrder(QAudioFormat.LittleEndian) self.fmt.setSampleType(QAudioFormat.SignedInt) info = QAudioDeviceInfo(QAudioDeviceInfo.defaultOutputDevice()) if not info.isFormatSupported(self.fmt): print 'Default format not supported - trying to use nearest' self.fmt = info.nearestFormat(self.fmt) self.generator = Generator(self.fmt, DURATION_SECONDS * 1000000, TONE_FREQUENCY_HZ, self, self.dump) self.createAudioOutput() def createAudioOutput(self): self.audioOutput = QAudioOutput(self.device, self.fmt, self) self.audioOutput.notify.connect(self.notified) self.audioOutput.stateChanged.connect(self.stateChanged) self.generator.start() self.audioOutput.start(self.generator) @Slot() def toggleSuspendResume(self): if self.audioOutput.state() == QAudio.SuspendedState: print 'Status: Suspended, resuming' self.audioOutput.resume() self.stateLabel = SUSPEND_LABEL elif self.audioOutput.state() == QAudio.ActiveState: print 'Status: Active, suspending' self.audioOutput.suspend() self.stateLabel = RESUME_LABEL elif self.audioOutput.state() == QAudio.StoppedState: print 'Status: Stopped, resuming' self.audioOutput.resume() self.stateLabel = SUSPEND_LABEL elif self.audioOutput.state() == QAudio.IdleState: print 'Status: Idle' playbackResumed = Signal() @Slot() def toggleMode(self): self.pullTimer.stop() self.audioOutput.stop() if self.pullMode: print "Enabling push mode" self.output = self.audioOutput.start() self.pullMode = False self.pullTimer.start(5) else: print "Enabling pull mode" self.pullMode = True self.audioOutput.start(self.generator) self.playbackResumed.emit() @Slot(int) def deviceChanged(self, index): print "Device changed: index:", index print "Selected device name: ", self.devices[index].deviceName() self.pullTimer.stop() self.generator.stop() self.audioOutput.stop() self.audioOutput.disconnect(self) self.device = self.devices[index] self.createAudioOutput() def stateChanged(self, state): print 'State changed: ', state def notified(self): print 'Bytes free %d, elapsed usecs %d, processed usecs %d' % ( self.audioOutput.bytesFree(), self.audioOutput.elapsedUSecs(), self.audioOutput.processedUSecs()) def pullTimerExpired(self): if self.audioOutput.state() != QAudio.StoppedState: chunks = self.audioOutput.bytesFree( ) / self.audioOutput.periodSize() while chunks: data = self.generator.read(self.audioOutput.periodSize()) self.output.write(data) if len(data) != self.audioOutput.periodSize(): break chunks -= 1
class AudioTest(QMainWindow): def __init__(self, filename=None): QMainWindow.__init__(self) self.pullTimer = QTimer(self) # Owned by layout self.modeButton = None self.suspendResumeButton = None self.deviceBox = None self.device = QAudioDeviceInfo.defaultOutputDevice() self.generator = None self.audioOutput = None self.output = None self.fmt = QAudioFormat() self.pullMode = False self.buf = QByteArray(BUFFER_SIZE, 0) self.dump = filename self.initializeWindow() self.initializeAudio() def initializeWindow(self): window = QWidget() layout = QVBoxLayout() self.deviceBox = QComboBox(self) for info in QAudioDeviceInfo.availableDevices(QAudio.AudioOutput): self.deviceBox.addItem(info.deviceName(), info) self.deviceBox.activated[int].connect(self.deviceChanged) layout.addWidget(self.deviceBox) self.modeButton = QPushButton(self) self.modeButton.setText(PUSH_MODE_LABEL) self.modeButton.clicked.connect(self.toggleMode) layout.addWidget(self.modeButton) self.suspendResumeButton = QPushButton(self) self.suspendResumeButton.setText(SUSPEND_LABEL) self.suspendResumeButton.clicked.connect(self.toggleSuspendResume) layout.addWidget(self.suspendResumeButton) window.setLayout(layout) self.setCentralWidget(window) window.show() def initializeAudio(self): self.pullTimer.timeout.connect(self.pullTimerExpired) self.pullMode = True self.fmt.setFrequency(DATA_FREQUENCY_HZ) self.fmt.setChannels(1) self.fmt.setSampleSize(16) self.fmt.setCodec('audio/pcm') self.fmt.setByteOrder(QAudioFormat.LittleEndian) self.fmt.setSampleType(QAudioFormat.SignedInt) info = QAudioDeviceInfo(QAudioDeviceInfo.defaultOutputDevice()) if not info.isFormatSupported(self.fmt): print 'Default format not supported - trying to use nearest' self.fmt = info.nearestFormat(self.fmt) self.generator = Generator(self.fmt, DURATION_SECONDS * 1000000, TONE_FREQUENCY_HZ, self, self.dump) self.createAudioOutput() def createAudioOutput(self): self.audioOutput = QAudioOutput(self.device, self.fmt, self) self.audioOutput.notify.connect(self.notified) self.audioOutput.stateChanged.connect(self.stateChanged) self.generator.start() self.audioOutput.start(self.generator) # Slots def notified(self): print 'Bytes free %d, elapsed usecs %d, processed usecs %d' % ( self.audioOutput.bytesFree(), self.audioOutput.elapsedUSecs(), self.audioOutput.processedUSecs()) def pullTimerExpired(self): if self.audioOutput.state() != QAudio.StoppedState: chunks = self.audioOutput.bytesFree() / self.audioOutput.periodSize() while chunks: data = self.generator.read(self.audioOutput.periodSize()) self.output.write(data) if len(data) != self.audioOutput.periodSize(): break chunks -= 1 def toggleMode(self): self.pullTimer.stop() self.audioOutput.stop() if self.pullMode: self.modeButton.setText(PULL_MODE_LABEL) self.output = self.audioOutput.start() self.pullMode = False self.pullTimer.start(5) else: self.modeButton.setText(PUSH_MODE_LABEL) self.pullMode = True self.audioOutput.start(self.generator) self.suspendResumeButton.setText(SUSPEND_LABEL) def toggleSuspendResume(self): if self.audioOutput.state() == QAudio.SuspendedState: print 'Status: Suspended, resuming' self.audioOutput.resume() self.suspendResumeButton.setText(SUSPEND_LABEL) elif self.audioOutput.state() == QAudio.ActiveState: print 'Status: Active, suspending' self.audioOutput.suspend() self.suspendResumeButton.setText(RESUME_LABEL) elif self.audioOutput.state() == QAudio.StoppedState: print 'Status: Stopped, resuming' self.audioOutput.resume() self.suspendResumeButton.setText(SUSPEND_LABEL) elif self.audioOutput.state() == QAudio.IdleState: print 'Status: Idle' def stateChanged(self, state): print 'State changed: ', state def deviceChanged(self, index): self.pullTimer.stop() self.generator.stop() self.audioOutput.stop() self.audioOutput.disconnect(self) self.device = self.deviceBox.itemData(index) self.createAudioOutput()
class TonePlayer(QObject): changed = Signal() def getStateLabel(self): print "Getting" return self._label def setStateLabel(self, value): print "Setting", value self._label = value self.changed.emit() stateLabel = Property(str, getStateLabel, setStateLabel, notify=changed) def __init__(self, devices=None, filename=None, parent=None): QObject.__init__(self, parent) self.pullTimer = QTimer(self) self.buf = QByteArray() self.devices = devices self.device = QAudioDeviceInfo.defaultOutputDevice() self.generator = None self.audioOutput = None self.output = None self.fmt = QAudioFormat() self.pullMode = False self.dump = filename self._label = SUSPEND_LABEL self.initializeAudio() def initializeAudio(self): self.pullTimer.timeout.connect(self.pullTimerExpired) self.pullMode = True self.fmt.setFrequency(DATA_FREQUENCY_HZ) self.fmt.setChannels(1) self.fmt.setSampleSize(16) self.fmt.setCodec("audio/pcm") self.fmt.setByteOrder(QAudioFormat.LittleEndian) self.fmt.setSampleType(QAudioFormat.SignedInt) info = QAudioDeviceInfo(QAudioDeviceInfo.defaultOutputDevice()) if not info.isFormatSupported(self.fmt): print "Default format not supported - trying to use nearest" self.fmt = info.nearestFormat(self.fmt) self.generator = Generator(self.fmt, DURATION_SECONDS * 1000000, TONE_FREQUENCY_HZ, self, self.dump) self.createAudioOutput() def createAudioOutput(self): self.audioOutput = QAudioOutput(self.device, self.fmt, self) self.audioOutput.notify.connect(self.notified) self.audioOutput.stateChanged.connect(self.stateChanged) self.generator.start() self.audioOutput.start(self.generator) @Slot() def toggleSuspendResume(self): if self.audioOutput.state() == QAudio.SuspendedState: print "Status: Suspended, resuming" self.audioOutput.resume() self.stateLabel = SUSPEND_LABEL elif self.audioOutput.state() == QAudio.ActiveState: print "Status: Active, suspending" self.audioOutput.suspend() self.stateLabel = RESUME_LABEL elif self.audioOutput.state() == QAudio.StoppedState: print "Status: Stopped, resuming" self.audioOutput.resume() self.stateLabel = SUSPEND_LABEL elif self.audioOutput.state() == QAudio.IdleState: print "Status: Idle" playbackResumed = Signal() @Slot() def toggleMode(self): self.pullTimer.stop() self.audioOutput.stop() if self.pullMode: print "Enabling push mode" self.output = self.audioOutput.start() self.pullMode = False self.pullTimer.start(5) else: print "Enabling pull mode" self.pullMode = True self.audioOutput.start(self.generator) self.playbackResumed.emit() @Slot(int) def deviceChanged(self, index): print "Device changed: index:", index print "Selected device name: ", self.devices[index].deviceName() self.pullTimer.stop() self.generator.stop() self.audioOutput.stop() self.audioOutput.disconnect(self) self.device = self.devices[index] self.createAudioOutput() def stateChanged(self, state): print "State changed: ", state def notified(self): print "Bytes free %d, elapsed usecs %d, processed usecs %d" % ( self.audioOutput.bytesFree(), self.audioOutput.elapsedUSecs(), self.audioOutput.processedUSecs(), ) def pullTimerExpired(self): if self.audioOutput.state() != QAudio.StoppedState: chunks = self.audioOutput.bytesFree() / self.audioOutput.periodSize() while chunks: data = self.generator.read(self.audioOutput.periodSize()) self.output.write(data) if len(data) != self.audioOutput.periodSize(): break chunks -= 1
class AudioTest(QObject): modeHasChanged = Signal("QVariantList") newResult = Signal(str) newNearest = Signal("QVariantList") def __init__(self, parent=None): QObject.__init__(self, parent) self.deviceInfo = QAudioDeviceInfo() self.settings = QAudioFormat() self.mode = QAudio.Mode() self._availableDevices = {} self._availableCodecs = {} self._availableFreqs = {} self._availableChannels = {} self._availableTypes = {} self._availableSizes = {} self._availableEnds = {} self.mode = QAudio.AudioOutput @Slot() def test(self): # tries to set all the settings picked. if not self.deviceInfo.isNull(): if self.deviceInfo.isFormatSupported(self.settings): self.newResult.emit("Success") ''' self.testResult.setText(self.tr("Success")) self.nearestFreq.setText("") self.nearestChannel.setText("") self.nearestCodec.setText("") self.nearestSampleSize.setText("") self.nearestSampleType.setText("") self.nearestEndian.setText("") ''' else: nearest = self.deviceInfo.nearestFormat(self.settings) self.newResult.emit("Failed") newValues = [] newValues.append( "") # keep the same positions as newLabels list newValues.append(nearest.codec()) # codec - 1 newValues.append(str(nearest.frequency())) # freq - 2 newValues.append(str(nearest.channels())) # channel - 3 newValues.append(sampletoString( nearest.sampleType())) # type - 4 newValues.append(str(nearest.sampleSize())) # size - 5 newValues.append(byteOrdertoString( nearest.byteOrder())) # end - 6 self.newNearest.emit(newValues) else: self.newResult.emit("No Device") @Slot(str) def modeChanged(self, idx): # mode has changed if idx == "Input": mode = QAudio.AudioInput else: mode = QAudio.AudioOutput self._availableDevices = {} for deviceInfo in QAudioDeviceInfo.availableDevices(mode): self._availableDevices[deviceInfo.deviceName()] = deviceInfo firstDevice = QAudioDeviceInfo.availableDevices(mode)[0].deviceName() self.deviceChanged(firstDevice) @Slot(str) def deviceChanged(self, idx): if len(self._availableDevices) == 0: return newLabels = [] newLabels.append(idx) # device - 0 # device has changed self.deviceInfo = self._availableDevices[idx] codecz = self.deviceInfo.supportedCodecs() self._availableCodecs = {} for codec in codecz: self._availableCodecs[codec] = codec if len(codecz): self.settings.setCodec(codecz[0]) newLabels.append(codecz[0]) # codec - 1 freqz = self.deviceInfo.supportedFrequencies() self._availableFreqs = {} for freq in freqz: self._availableFreqs[str(freq)] = freq if len(freqz): self.settings.setFrequency(freqz[0]) newLabels.append(str(freqz[0])) # freq - 2 chz = self.deviceInfo.supportedChannels() self._availableChannels = {} for ch in chz: self._availableChannels[str(ch)] = ch # Add false to create failed condition! self._availableChannels["FAIL"] = -1 if len(chz): self.settings.setChannels(chz[0]) newLabels.append(str(chz[0])) # channel - 3 sampleTypes = self.deviceInfo.supportedSampleTypes() self._availableTypes = {} for sample in sampleTypes: self._availableTypes[sampletoString(sample)] = sample if len(sampleTypes): self.settings.setSampleType(sampleTypes[0]) newLabels.append(sampletoString(sampleTypes[0])) # type - 4 sampleSizes = self.deviceInfo.supportedSampleSizes() self._availableSizes = {} for sample in sampleSizes: self._availableSizes[str(sample)] = sample if len(sampleSizes): self.settings.setSampleSize(sampleSizes[0]) newLabels.append(str(sampleSizes[0])) # size - 5 endianz = self.deviceInfo.supportedByteOrders() self._availableEnds = {} for endian in endianz: self._availableEnds[byteOrdertoString(endian)] = endian if len(endianz): self.settings.setByteOrder(endianz[0]) newLabels.append(byteOrdertoString(endianz[0])) # end - 6 # Update all the buttons self.modeHasChanged.emit(newLabels) @Slot(str) def codecChanged(self, idx): self.settings.setCodec(self._availableCodecs[idx]) @Slot(str) def freqChanged(self, idx): # freq has changed self.settings.setFrequency(int(self._availableFreqs[idx])) @Slot(str) def channelChanged(self, idx): self.settings.setChannels(int(self._availableChannels[idx])) @Slot(str) def sizeChanged(self, idx): self.settings.setSampleSize(int(self._availableSizes[idx])) @Slot(str) def typeChanged(self, idx): sampleType = stringToSample(self._availableTypes[idx]) if sampleType == QAudioFormat.SignedInt: self.settings.setSampleType(QAudioFormat.SignedInt) elif sampleType == QAudioFormat.UnSignedInt: self.settings.setSampleType(QAudioFormat.UnSignedInt) elif sampleType == QAudioFormat.Float: self.settings.setSampleType(QAudioFormat.Float) @Slot(str) def endChanged(self, idx): endian = stringToByteOrder(self._availableEnds[idx]) if endian == QAudioFormat.LittleEndian: self.settings.setByteOrder(QAudioFormat.LittleEndian) elif endian == QAudioFormat.BigEndian: self.settings.setByteOrder(QAudioFormat.BigEndian) @Property("QStringList", constant=True) def availableDevices(self): return self._availableDevices.keys() @Property("QStringList", constant=True) def availableCodecs(self): return self._availableCodecs.keys() @Property("QStringList", constant=True) def availableFreqs(self): return self._availableFreqs.keys() @Property("QStringList", constant=True) def availableChannels(self): return self._availableChannels.keys() @Property("QStringList", constant=True) def availableTypes(self): return self._availableTypes.keys() @Property("QStringList", constant=True) def availableSizes(self): return self._availableSizes.keys() @Property("QStringList", constant=True) def availableEnds(self): return self._availableEnds.keys()