Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
audio.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/* Audio related snippets */
5#include <QFile>
6#include <QTimer>
7#include <QDebug>
8#include <qobject.h>
9#include <qfile.h>
10
11#include "qaudiodevice.h"
12#include "qaudiosource.h"
13#include "qaudiooutput.h"
14#include "qaudiodecoder.h"
15#include "qmediaplayer.h"
16#include "qmediadevices.h"
17
18class AudioInputExample : public QObject {
19 Q_OBJECT
20public:
21 void setup();
22
23
24public Q_SLOTS:
25 void stopRecording();
26 void handleStateChanged(QtAudio::State newState);
27
28private:
29 //! [Audio input class members]
30 QFile destinationFile; // Class member
31 QAudioSource* audio; // Class member
32 //! [Audio input class members]
33};
34
35
36void AudioInputExample::setup()
37//! [Audio input setup]
38{
39 destinationFile.setFileName("/tmp/test.raw");
40 destinationFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
41
42 QAudioFormat format;
43 // Set up the desired format, for example:
44 format.setSampleRate(44100);
45 format.setChannelCount(1);
46 format.setSampleFormat(QAudioFormat::Int16);
47
48 QAudioDevice info = QMediaDevices::defaultAudioInput();
49 if (!info.isFormatSupported(format)) {
50 qWarning() << "Default format not supported, trying to use the nearest.";
51 }
52
53 audio = new QAudioSource(format, this);
54 connect(audio, &QAudioSource::stateChanged, this, &AudioInputExample::handleStateChanged);
55
56 QTimer::singleShot(3000, this, &AudioInputExample::stopRecording);
57 audio->start(&destinationFile);
58 // Records audio for 3000ms
59}
60//! [Audio input setup]
61
62//! [Audio input stop recording]
63void AudioInputExample::stopRecording()
64{
65 audio->stop();
66 destinationFile.close();
67 delete audio;
68}
69//! [Audio input stop recording]
70
71//! [Audio input state changed]
72void AudioInputExample::handleStateChanged(QtAudio::State newState)
73{
74 switch (newState) {
75 case QtAudio::StoppedState:
76 if (audio->error() != QtAudio::NoError) {
77 // Error handling
78 } else {
79 // Finished recording
80 }
81 break;
82
83 case QtAudio::ActiveState:
84 // Started recording - read from IO device
85 break;
86
87 default:
88 // ... other cases as appropriate
89 break;
90 }
91}
92//! [Audio input state changed]
93
94
97public:
98 void setup();
99
100public Q_SLOTS:
103
104private:
105 //! [Audio output class members]
106 QFile sourceFile; // class member.
107 QAudioSink* audio; // class member.
108 //! [Audio output class members]
109};
110
111
113//! [Audio output setup]
114{
115 sourceFile.setFileName("/tmp/test.raw");
116 sourceFile.open(QIODevice::ReadOnly);
117
118 QAudioFormat format;
119 // Set up the format, eg.
120 format.setSampleRate(44100);
121 format.setChannelCount(1);
122 format.setSampleFormat(QAudioFormat::Int16);
123
124 QAudioDevice info(QMediaDevices::defaultAudioOutput());
125 if (!info.isFormatSupported(format)) {
126 qWarning() << "Raw audio format not supported by backend, cannot play audio.";
127 return;
128 }
129
130 audio = new QAudioSink(format, this);
131 connect(audio, QAudioSink::stateChanged, this, &AudioInputExample::handleStateChanged);
132 audio->start(&sourceFile);
133}
134//! [Audio output setup]
135
136//! [Audio output stop]
138{
139 audio->stop();
140 sourceFile.close();
141 delete audio;
142}
143//! [Audio output stop]
144
145//! [Audio output state changed]
146void AudioOutputExample::handleStateChanged(QtAudio::State newState)
147{
148 switch (newState) {
149 case QtAudio::IdleState:
150 // Finished playing (no more data)
152 break;
153
154 case QtAudio::StoppedState:
155 // Stopped for other reasons
156 if (audio->error() != QtAudio::NoError) {
157 // Error handling
158 }
159 break;
160
161 default:
162 // ... other cases as appropriate
163 break;
164 }
165}
166//! [Audio output state changed]
167
169{
170 //! [Setting audio format]
171 QAudioFormat format;
172 format.setSampleRate(44100);
173 // ... other format parameters
174 format.setSampleFormat(QAudioFormat::Int16);
175 //! [Setting audio format]
176
177 //! [Dumping audio formats]
178 const auto devices = QMediaDevices::audioOutputs();
179 for (const QAudioDevice &device : devices)
180 qDebug() << "Device: " << device.description();
181 //! [Dumping audio formats]
182}
183
186public:
187 void decode();
188
189public Q_SLOTS:
192};
193
195{
196 //! [Local audio decoding]
197 QAudioFormat desiredFormat;
198 desiredFormat.setChannelCount(2);
199 desiredFormat.setSampleFormat(QAudioFormat::Int16);
200 desiredFormat.setSampleRate(48000);
201
202 QAudioDecoder *decoder = new QAudioDecoder(this);
203 decoder->setAudioFormat(desiredFormat);
204 decoder->setSource("level1.mp3");
205
206 connect(decoder, &QAudioDecoder::bufferReady, this, &AudioDecodingExample::readBuffer);
207 decoder->start();
208
209 // Now wait for bufferReady() signal and call decoder->read()
210 //! [Local audio decoding]
211}
212
214
215//! [Volume conversion]
216void applyVolume(int volumeSliderValue)
217{
218 // volumeSliderValue is in the range [0..100]
219
220 qreal linearVolume = QtAudio::convertVolume(volumeSliderValue / qreal(100.0),
221 QtAudio::LogarithmicVolumeScale,
222 QtAudio::LinearVolumeScale);
223
224 player.setVolume(qRound(linearVolume * 100));
225}
226//! [Volume conversion]
void applyVolume(int volumeSliderValue)
[Volume conversion]
Definition audio.cpp:216
void AudioDeviceInfo()
[Audio output state changed]
Definition audio.cpp:168
QMediaPlayer player
Definition audio.cpp:213
[Audio input state changed]
Definition audio.cpp:95
void stopAudioOutput()
[Audio output setup]
Definition audio.cpp:137