aboutsummaryrefslogtreecommitdiffstats
path: root/examples/graphs/2d/graphsaudio/main.py
blob: 239aee036eb01e5cce984265b5d0bcb879b9afd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations

import sys
from pathlib import Path
from PySide6.QtCore import QObject, QPointF, Slot, Signal
from PySide6.QtMultimedia import QAudioFormat, QAudioSource, QMediaDevices
from PySide6.QtWidgets import QMessageBox
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtGui import QGuiApplication


SAMPLE_COUNT = 2000
RESOLUTION = 4


class Audio(QObject):
    dataUpdated = Signal(list)

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

        format_audio = QAudioFormat()
        format_audio.setSampleRate(8000)
        format_audio.setChannelCount(1)
        format_audio.setSampleFormat(QAudioFormat.UInt8)

        self.device_name = device.description()

        self._audio_input = QAudioSource(device, format_audio, self)
        self._io_device = self._audio_input.start()
        self._io_device.readyRead.connect(self._readyRead)

        self._buffer = [QPointF(x, 0) for x in range(SAMPLE_COUNT)]

    def closeEvent(self, event):
        if self._audio_input is not None:
            self._audio_input.stop()
        event.accept()

    @Slot()
    def _readyRead(self):
        data = self._io_device.readAll()
        available_samples = data.size() // RESOLUTION
        start = 0
        if (available_samples < SAMPLE_COUNT):
            start = SAMPLE_COUNT - available_samples
            for s in range(start):
                self._buffer[s].setY(self._buffer[s + available_samples].y())

        data_index = 0
        for s in range(start, SAMPLE_COUNT):
            value = (ord(data[data_index]) - 128) / 128
            self._buffer[s].setY(value)
            data_index = data_index + RESOLUTION

        self.dataUpdated.emit(self._buffer)


if __name__ == '__main__':
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    input_devices = QMediaDevices.audioInputs()
    if not input_devices:
        QMessageBox.warning(None, "audio", "There is no audio input device available.")
        sys.exit(-1)

    audio_bridge = Audio(input_devices[0])
    engine.rootContext().setContextProperty("audio_bridge", audio_bridge)

    device = input_devices[0]
    device_name = device.description()
    engine.rootContext().setContextProperty("device_name", device_name)

    engine.addImportPath(Path(__file__).parent)
    engine.loadFromModule("GraphsAudio", "Main")

    sys.exit(app.exec())