aboutsummaryrefslogtreecommitdiffstats
path: root/doc/codesnippets/doc/src/snippets/audio/main.cpp
blob: 5b3e9eb5a202bc04ee3774c81a755ec502bcabc4 (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

class Window2 (QWidget):
//![0]
    @Slot(QAudio.State)
    def stateChanged(self, newState):
        if newState == QAudio.StopState:
            if self.input.error() != QAudio.NoError:
                # Error handling
//![0]

class Window (QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.output = QAudioOutput()
        self.output.stateChanged[QAudio.State].connect(self.stateChanged)

    def setupFormat(self):
//![1]
        format = QAudioFormat()
        format.setFrequency(44100)
//![1]
        format.setChannels(2)
        format.setSampleSize(16)
        format.setCodec("audio/pcm")
        format.setByteOrder(QAudioFormat.LittleEndian)
//![2]
        format.setSampleType(QAudioFormat.SignedInt)

        info = QAudioDeviceInfo(QAudioDeviceInfo.defaultOutputDevice())

        if not info.isFormatSupported(format):
            format = info.nearestFormat(format)
//![2]

//![3]
    @Slot(QAudio.State)
    def stateChanged(self, newState):
        if newState == QAudio.StopState:
            if self.output.error() != QAudio.NoError:
                # Perform error handling
            else:
                # Normal stop
//![3]

        # Handle
        elif newState == QAudio.ActiveState:
            # Handle active state...

app = QApplication(sys.argv)

window = Window()
window.show()
sys.exit(app.exec_())