aboutsummaryrefslogtreecommitdiffstats
path: root/examples/multimedia/audiooutput/audiooutput.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/multimedia/audiooutput/audiooutput.py')
-rw-r--r--examples/multimedia/audiooutput/audiooutput.py81
1 files changed, 24 insertions, 57 deletions
diff --git a/examples/multimedia/audiooutput/audiooutput.py b/examples/multimedia/audiooutput/audiooutput.py
index 2950a66a0..c2cbd730f 100644
--- a/examples/multimedia/audiooutput/audiooutput.py
+++ b/examples/multimedia/audiooutput/audiooutput.py
@@ -1,44 +1,7 @@
-
-#############################################################################
-##
-## Copyright (C) 2013 Riverbank Computing Limited.
-## Copyright (C) 2016 The Qt Company Ltd.
-## Contact: https://www.qt.io/licensing/
-##
-## This file is part of the Qt for Python examples of the Qt Toolkit.
-##
-## $QT_BEGIN_LICENSE:BSD$
-## You may use this file under the terms of the BSD license as follows:
-##
-## "Redistribution and use in source and binary forms, with or without
-## modification, are permitted provided that the following conditions are
-## met:
-## * Redistributions of source code must retain the above copyright
-## notice, this list of conditions and the following disclaimer.
-## * Redistributions in binary form must reproduce the above copyright
-## notice, this list of conditions and the following disclaimer in
-## the documentation and/or other materials provided with the
-## distribution.
-## * Neither the name of The Qt Company Ltd nor the names of its
-## contributors may be used to endorse or promote products derived
-## from this software without specific prior written permission.
-##
-##
-## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-##
-## $QT_END_LICENSE$
-##
-#############################################################################
+# Copyright (C) 2013 Riverbank Computing Limited.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+from __future__ import annotations
"""PySide6 port of the multimedia/audiooutput example from Qt v5.x, originating from PyQt"""
@@ -48,7 +11,7 @@ from struct import pack
from PySide6.QtCore import (QByteArray, QIODevice, Qt, QSysInfo, QTimer,
qWarning, Slot)
-from PySide6.QtMultimedia import (QAudio, QAudioDevice, QAudioFormat,
+from PySide6.QtMultimedia import (QAudio, QAudioFormat,
QAudioSink, QMediaDevices)
from PySide6.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QLabel,
QMainWindow, QPushButton, QSlider,
@@ -78,24 +41,23 @@ class Generator(QIODevice):
sample_size = fmt.bytesPerSample() * 8
if sample_size == 8:
if fmt.sampleFormat() == QAudioFormat.UInt8:
- scaler = lambda x: ((1.0 + x) / 2 * 255)
+ scaler = lambda x: ((1.0 + x) / 2 * 255) # noqa: E731
pack_format = 'B'
elif fmt.sampleFormat() == QAudioFormat.Int16:
- scaler = lambda x: x * 127
+ scaler = lambda x: x * 127 # noqa: E731
pack_format = 'b'
elif sample_size == 16:
little_endian = QSysInfo.ByteOrder == QSysInfo.LittleEndian
if fmt.sampleFormat() == QAudioFormat.UInt8:
- scaler = lambda x: (1.0 + x) / 2 * 65535
+ scaler = lambda x: (1.0 + x) / 2 * 65535 # noqa: E731
pack_format = '<H' if little_endian else '>H'
elif fmt.sampleFormat() == QAudioFormat.Int16:
- scaler = lambda x: x * 32767
+ scaler = lambda x: x * 32767 # noqa: E731
pack_format = '<h' if little_endian else '>h'
- assert(pack_format != '')
+ assert pack_format != ''
channel_bytes = fmt.bytesPerSample()
- sample_bytes = fmt.channelCount() * channel_bytes
length = (fmt.sampleRate() * fmt.channelCount() * channel_bytes) * durationUs // 100000
@@ -171,16 +133,14 @@ class AudioTest(QMainWindow):
layout.addWidget(self.m_modeButton)
- self.m_suspendResumeButton = QPushButton(
- clicked=self.toggle_suspend_resume)
+ self.m_suspendResumeButton = QPushButton(clicked=self.toggle_suspend_resume)
self.m_suspendResumeButton.setText(self.SUSPEND_LABEL)
layout.addWidget(self.m_suspendResumeButton)
volume_box = QHBoxLayout()
volume_label = QLabel("Volume:")
- self.m_volumeSlider = QSlider(Qt.Horizontal, minimum=0, maximum=100,
- singleStep=10)
+ self.m_volumeSlider = QSlider(Qt.Horizontal, minimum=0, maximum=100, singleStep=10)
self.m_volumeSlider.valueChanged.connect(self.volume_changed)
volume_box.addWidget(volume_label)
@@ -205,8 +165,8 @@ class AudioTest(QMainWindow):
qWarning("Default format not supported - trying to use nearest")
self.m_format = info.nearestFormat(self.m_format)
- self.m_generator = Generator(self.m_format,
- self.DURATION_SECONDS * 1000000, self.TONE_SAMPLE_RATE_HZ, self)
+ self.m_generator = Generator(self.m_format, self.DURATION_SECONDS * 1000000,
+ self.TONE_SAMPLE_RATE_HZ, self)
self.create_audio_output()
@@ -218,11 +178,18 @@ class AudioTest(QMainWindow):
self.m_audioSink.start(self.m_generator)
self.m_volumeSlider.setValue(self.m_audioSink.volume() * 100)
- @Slot(int)
- def device_changed(self, index):
+ def closeEvent(self, e):
+ self.stop()
+ e.accept()
+
+ def stop(self):
self.m_pullTimer.stop()
self.m_generator.stop()
self.m_audioSink.stop()
+
+ @Slot(int)
+ def device_changed(self, index):
+ self.stop()
self.m_device = self.m_deviceBox.itemData(index)
self.create_audio_output()
@@ -289,7 +256,7 @@ class AudioTest(QMainWindow):
QAudio.StoppedState: "StoppedState",
QAudio.IdleState: "IdleState"}
- @Slot(QAudio.State)
+ @Slot("QAudio::State")
def handle_state_changed(self, state):
state = self.state_map.get(state, 'Unknown')
qWarning(f"state = {state}")