Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 {
20public:
21 void setup();
22
23
24public Q_SLOTS:
25 void stopRecording();
27
28private:
30 QFile destinationFile; // Class member
31 QAudioSource* audio; // Class member
33};
34
35
38{
39 destinationFile.setFileName("/tmp/test.raw");
41
43 // Set up the desired format, for example:
45 format.setChannelCount(1);
46 format.setSampleFormat(QAudioFormat::UInt8);
47
49 if (!info.isFormatSupported(format)) {
50 qWarning() << "Default format not supported, trying to use the nearest.";
51 }
52
53 audio = new QAudioSource(format, this);
55
57 audio->start(&destinationFile);
58 // Records audio for 3000ms
59}
61
64{
65 audio->stop();
66 destinationFile.close();
67 delete audio;
68}
70
73{
74 switch (newState) {
76 if (audio->error() != QAudio::NoError) {
77 // Error handling
78 } else {
79 // Finished recording
80 }
81 break;
82
84 // Started recording - read from IO device
85 break;
86
87 default:
88 // ... other cases as appropriate
89 break;
90 }
91}
93
94
97public:
98 void setup();
99
100public Q_SLOTS:
102 void stopAudioOutput();
103
104private:
106 QFile sourceFile; // class member.
107 QAudioSink* audio; // class member.
109};
110
111
114{
115 sourceFile.setFileName("/tmp/test.raw");
116 sourceFile.open(QIODevice::ReadOnly);
117
119 // Set up the format, eg.
120 format.setSampleRate(8000);
121 format.setChannelCount(1);
122 format.setSampleFormat(QAudioFormat::UInt8);
123
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);
132 audio->start(&sourceFile);
133}
135
138{
139 audio->stop();
140 sourceFile.close();
141 delete audio;
142}
144
147{
148 switch (newState) {
150 // Finished playing (no more data)
152 break;
153
155 // Stopped for other reasons
156 if (audio->error() != QAudio::NoError) {
157 // Error handling
158 }
159 break;
160
161 default:
162 // ... other cases as appropriate
163 break;
164 }
165}
167
169{
172 format.setSampleRate(44100);
173 // ... other format parameters
174 format.setSampleFormat(QAudioFormat::Int16);
176
179 for (const QAudioDevice &device : devices)
180 qDebug() << "Device: " << device.description();
182}
183
186public:
187 void decode();
188
189public Q_SLOTS:
192};
193
195{
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
207 decoder->start();
208
209 // Now wait for bufferReady() signal and call decoder->read()
211}
212
214
216void applyVolume(int volumeSliderValue)
217{
218 // volumeSliderValue is in the range [0..100]
219
220 qreal linearVolume = QtAudio::convertVolume(volumeSliderValue / qreal(100.0),
223
224 player.setVolume(qRound(linearVolume * 100));
225}
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
IOBluetoothDevice * device
void handleStateChanged(QAudio::State newState)
void handleStateChanged(QAudio::State newState)
[Audio input stop recording]
Definition audio.cpp:72
void setup()
[Audio input setup]
Definition audio.cpp:36
void stopRecording()
[Audio input setup]
Definition audio.cpp:63
[Audio input state changed]
Definition audio.cpp:95
void setup()
[Audio output setup]
Definition audio.cpp:112
void handleStateChanged(QAudio::State newState)
[Audio output stop]
Definition audio.cpp:146
void stopAudioOutput()
[Audio output setup]
Definition audio.cpp:137
The QAudioDecoder class implements decoding audio.
void start()
Starts decoding the audio resource.
void setSource(const QUrl &fileName)
Sets the current audio file name to fileName.
void setAudioFormat(const QAudioFormat &format)
Set the desired audio format for decoded samples to format.
void bufferReady()
Signals that a new decoded audio buffer is available to be read.
The QAudioDevice class provides an information about audio devices and their functionality.
The QAudioFormat class stores audio stream parameter information.
constexpr void setSampleRate(int sampleRate) noexcept
Sets the sample rate to samplerate in Hertz.
constexpr void setChannelCount(int channelCount) noexcept
Sets the channel count to channels.
The QAudioSink class provides an interface for sending audio data to an audio output device.
Definition qaudiosink.h:24
void stop()
Stops the audio output, detaching from the system resource.
void stateChanged(QAudio::State state)
This signal is emitted when the device state has changed.
void start(QIODevice *device)
Starts transferring audio data from the device to the system's audio output.
QtAudio::Error error() const
Returns the error state.
The QAudioSource class provides an interface for receiving audio data from an audio input device.
void stop()
Stops the audio input, detaching from the system resource.
QtAudio::Error error() const
Returns the error state.
void start(QIODevice *device)
Starts transferring audio data from the system's audio input to the device.
void stateChanged(QAudio::State state)
This signal is emitted when the device state has changed.
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
void setFileName(const QString &name)
Sets the name of the file.
Definition qfile.cpp:302
QList< QAudioDevice > audioOutputs
\qmlproperty list<audioDevice> QtMultimedia::MediaDevices::audioOutputs Contains a list of available ...
QAudioDevice defaultAudioOutput
\qmlproperty audioDevice QtMultimedia::MediaDevices::defaultAudioOutput Returns the default audio out...
QAudioDevice defaultAudioInput
\qmlproperty audioDevice QtMultimedia::MediaDevices::defaultAudioInput Returns the default audio inpu...
The QMediaPlayer class allows the playing of a media files.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
State
Definition qaudio.h:29
@ StoppedState
Definition qaudio.h:29
@ IdleState
Definition qaudio.h:29
@ ActiveState
Definition qaudio.h:29
Q_MULTIMEDIA_EXPORT float convertVolume(float volume, VolumeScale from, VolumeScale to)
Converts an audio volume from a volume scale to another, and returns the result.
Definition qtaudio.cpp:93
@ NoError
Definition qaudio.h:28
@ LogarithmicVolumeScale
Definition qaudio.h:34
@ LinearVolumeScale
Definition qaudio.h:32
EGLDeviceEXT * devices
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei GLenum format
#define Q_OBJECT
#define Q_SLOTS
double qreal
Definition qtypes.h:187
QHostInfo info
[0]