aboutsummaryrefslogtreecommitdiffstats
path: root/examples/multimedia
diff options
context:
space:
mode:
authorCristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io>2021-05-12 17:10:35 +0200
committerCristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io>2021-05-13 13:14:55 +0200
commit3c1a6f732a3a2c69e7133d07b89db0bdd788316b (patch)
tree44114d011f4e0a3522896df9c8113bee1bba3b78 /examples/multimedia
parenta6dfbb2a72235ecabc7b1d61c085a7d7de3df8d0 (diff)
examples: clean and improve code
- removing '\' from long lines, - use f-strings instead of concatenating strings - Use f-strings instead of the old '%' formatting Task-number: PYSIDE-841 Change-Id: I4983c25a6272e10119d5d1a74c180828ca6f64e6 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/multimedia')
-rw-r--r--examples/multimedia/audiooutput/audiooutput.py43
-rw-r--r--examples/multimedia/camera/camera.py6
2 files changed, 26 insertions, 23 deletions
diff --git a/examples/multimedia/audiooutput/audiooutput.py b/examples/multimedia/audiooutput/audiooutput.py
index 322603c2b..0e9a2c10a 100644
--- a/examples/multimedia/audiooutput/audiooutput.py
+++ b/examples/multimedia/audiooutput/audiooutput.py
@@ -69,40 +69,40 @@ class Generator(QIODevice):
self.m_pos = 0
self.close()
- def generateData(self, format, durationUs, sampleRate):
+ def generateData(self, fmt, durationUs, sampleRate):
pack_format = ''
- if format.sampleSize() == 8:
- if format.sampleType() == QAudioFormat.UnSignedInt:
+ if fmt.sampleSize() == 8:
+ if fmt.sampleType() == QAudioFormat.UnSignedInt:
scaler = lambda x: ((1.0 + x) / 2 * 255)
pack_format = 'B'
- elif format.sampleType() == QAudioFormat.SignedInt:
+ elif fmt.sampleType() == QAudioFormat.SignedInt:
scaler = lambda x: x * 127
pack_format = 'b'
- elif format.sampleSize() == 16:
- if format.sampleType() == QAudioFormat.UnSignedInt:
+ elif fmt.sampleSize() == 16:
+ if fmt.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:
+ pack_format = '<H' if fmt.byteOrder() == QAudioFormat.LittleEndian else '>H'
+ elif fmt.sampleType() == QAudioFormat.SignedInt:
scaler = lambda x: x * 32767
- pack_format = '<h' if format.byteOrder() == QAudioFormat.LittleEndian else '>h'
+ pack_format = '<h' if fmt.byteOrder() == QAudioFormat.LittleEndian else '>h'
assert(pack_format != '')
- channelBytes = format.sampleSize() // 8
- sampleBytes = format.channelCount() * channelBytes
+ channelBytes = fmt.sampleSize() // 8
+ sampleBytes = fmt.channelCount() * channelBytes
- length = (format.sampleRate() * format.channelCount() * (format.sampleSize() // 8)) * durationUs // 100000
+ length = (fmt.sampleRate() * fmt.channelCount() * (fmt.sampleSize() // 8)) * durationUs // 100000
self.m_buffer.clear()
sampleIndex = 0
- factor = 2 * pi * sampleRate / format.sampleRate()
+ factor = 2 * pi * sampleRate / fmt.sampleRate()
while length != 0:
- x = sin((sampleIndex % format.sampleRate()) * factor)
+ x = sin((sampleIndex % fmt.sampleRate()) * factor)
packed = pack(pack_format, int(scaler(x)))
- for _ in range(format.channelCount()):
+ for _ in range(fmt.channelCount()):
self.m_buffer.append(packed)
length -= channelBytes
@@ -230,10 +230,12 @@ class AudioTest(QMainWindow):
self.m_audioOutput.setVolume(value / 100.0)
def notified(self):
- qWarning("bytesFree = %d, elapsedUSecs = %d, processedUSecs = %d" % (
- self.m_audioOutput.bytesFree(),
- self.m_audioOutput.elapsedUSecs(),
- self.m_audioOutput.processedUSecs()))
+ bytes_free = self.m_audioOutput.bytesFree()
+ elapsed = self.m_audioOutput.elapsedUSecs()
+ processed = self.m_audioOutput.processedUSecs()
+ qWarning(f"bytesFree = {bytes_free}, "
+ f"elapsedUSecs = {elapsed}, "
+ f"processedUSecs = {processed}")
def pullTimerExpired(self):
if self.m_audioOutput is not None and self.m_audioOutput.state() != QAudio.StoppedState:
@@ -284,7 +286,8 @@ class AudioTest(QMainWindow):
QAudio.IdleState: "IdleState"}
def handleStateChanged(self, state):
- qWarning("state = " + self.stateMap.get(state, "Unknown"))
+ state = self.stateMap.get(state, 'Unknown')
+ qWarning(f"state = {state}")
if __name__ == '__main__':
diff --git a/examples/multimedia/camera/camera.py b/examples/multimedia/camera/camera.py
index e7e12cbe7..8ec3c869d 100644
--- a/examples/multimedia/camera/camera.py
+++ b/examples/multimedia/camera/camera.py
@@ -127,8 +127,8 @@ class MainWindow(QMainWindow):
if self.camera.status() != QCamera.UnavailableStatus:
name = self.cameraInfo.description()
- self.setWindowTitle("PySide6 Camera Example (" + name + ")")
- self.statusBar().showMessage("Starting: '" + name + "'", 5000)
+ self.setWindowTitle(f"PySide6 Camera Example ({name})")
+ self.statusBar().showMessage(f"Starting: '{name}'", 5000)
self.camera.start()
else:
self.setWindowTitle("PySide6 Camera Example")
@@ -138,7 +138,7 @@ class MainWindow(QMainWindow):
def nextImageFileName(self):
picturesLocation = QStandardPaths.writableLocation(QStandardPaths.PicturesLocation)
dateString = QDate.currentDate().toString("yyyyMMdd")
- pattern = picturesLocation + "/pyside6_camera_" + dateString + "_{:03d}.jpg"
+ pattern = f"{picturesLocation}/pyside6_camera_{dateString}_{:03d}.jpg"
n = 1
while True:
result = pattern.format(n)