音频概述

音频特征

Qt Multimedia offers a range of audio classes that cover both low and high level approaches to: audio input, output and processing.

音频实现细节

播放压缩音频

对于播放不简单媒体或音频文件、未压缩音频,可以使用 QMediaPlayer C++ 类,或 MediaPlayer QML type. The QMediaPlayer 类和关联 QML 类型还能播放 视频 , if required.

Supported Media Formats for more detail.

The media player needs to be connected to a QAudioOutput object (or the QML AudioOutput element) to play back audio.

这里是如何使用 C++ 播放本地文件:

player = new QMediaPlayer;
audioOutput = new QAudioOutput;
player->setAudioOutput(audioOutput);
// ...
player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
audioOutput->setVolume(50);
player->play();
					

The same functionality in QML:

MediaPlayer {
    audioOutput: AudioOutput {}
    source: "file:///path/to/my/music.mp3"
    Component.onCompleted: { play() }
}
					

把音频录制到文件

To record audio to a file, you need to create a capture session and connect to it an audio input and a recorder. These elements are implemented with the QMediaCaptureSession , QAudioInput ,和 QMediaRecorder classes. The default constructed QAudioInput selects the system default audio input. The recorder controls the recording process with a simple record() and stop() functions. Additionally, you can use it to select the output location, audio encoder, or file container format.

A session recording audio from the default microphone would look as follows in C++:

QMediaCaptureSession session;
QAudioInput audioInput;
session.setAudioInput(&input);
QMediaRecorder recorder;
session.setRecorder(&recorder);
recorder.setQuality(QMediaRecorder::HighQuality);
recorder.setOutputLocation(QUrl::fromLocalFile("test.mp3"));
recorder.record();
					

In QML, the same can be achieved by:

CaptureSession {
    audioInput: AudioInput {}
    mediaRecorder: MediaRecorder {
        id: recorder
        outputLocation: "file:///path/to/test.mp3"
    }
    Component.onCompleted: { recorder.record() }
}
					

QMediaCaptureSession also provides support for more complex use cases such as image capturing or video recording.

低延迟音效

除了 raw access to sound devices, the QSoundEffect 类 (和 SoundEffect QML type) offers a more abstract way to play sounds. This class allows you to specify a WAV format file, which can then be played with low latency when necessary.

You can adjust the:

低电平音频回放和录制

The C++ API of Qt Multimedia offers classes for raw access to audio input and output facilities, allowing applications to receive raw data from devices like microphones, and to write raw data to speakers or other devices. Generally these classes do not do any audio decoding, or other processing, but they can support different types of raw audio data.

The QAudioSink 类提供原生音频数据输出,而 QAudioSource offers raw audio data input. The available hardware determines what audio outputs and inputs are available.

压入和拉出

低级音频类可以运转于 2 种模式下 push and pull 。在 pull 模式,音频设备的启动是通过将它赋予 QIODevice 。对于输出设备, QAudioSink 类将 pull (拉出) 数据从 QIODevice (使用 QIODevice::read ()) 当要求更多音频数据时。相反,对于 pull 模式采用 QAudioSource ,当音频数据可用时,数据将被直接写入 QIODevice .

push 模式,音频设备提供 QIODevice instance that can be written or read to as needed. Typically, this results in simpler code but more buffering, which may affect latency.

把压缩音频解码到内存

In some cases you may want to decode a compressed audio file and do further processing yourself. For example, mixing multiple samples or using custom digital signal processing algorithms. QAudioDecoder 支持解码本地文件或数据流从 QIODevice 实例。

这里是本地文件解码范例:

QAudioFormat desiredFormat;
desiredFormat.setChannelCount(2);
desiredFormat.setSampleFormat(QAudioFormat::Int16);
desiredFormat.setSampleRate(48000);
QAudioDecoder *decoder = new QAudioDecoder(this);
decoder->setAudioFormat(desiredFormat);
decoder->setSource("level1.mp3");
connect(decoder, SIGNAL(bufferReady()), this, SLOT(readBuffer()));
decoder->start();
// Now wait for bufferReady() signal and call decoder->read()
					

Spatial Audio

The Qt Spatial Audio module provides an API for implementation sound fields in 3D space.

参考文档编制

C++ 类

QAmbientSound A stereo overlay sound
QAudio 包含用于音频类的枚举
QAudioBuffer 表示具有特定格式和采样率的一批音频样本
QAudioDecoder Implements decoding audio
QAudioDevice Information about audio devices and their functionality
QAudioEngine Manages a three dimensional sound field
QAudioFormat 存储音频流参数信息
QAudioInput Represents an input channel for audio
QAudioListener Defines the position and orientation of the person listening to a sound field defined by QAudioEngine
QAudioOutput Represents an output channel for audio
QAudioRoom
QAudioSink 把音频数据发送到音频输出设备的接口
QAudioSource 从音频输入设备接收音频数据的接口
QMediaCaptureSession Allows capturing of audio and video content
QMediaRecorder Used for encoding and recording a capture session
QSoundEffect 播放低延迟音效的办法
QSpatialSound A sound object in 3D space

QML 类型

AmbientSound A stereo overlay sound
AudioEngine Manages sound objects inside a 3D scene
AudioInput An audio input to be used for capturing audio in a capture session
AudioListener Defines the position and orientation of the person listening to a sound field defined by a AudioEngine
AudioOutput An audio output to be used for playback or monitoring of a capture session
AudioRoom
CaptureSession Allows capturing of audio and video content
MediaPlayer Adds media playback to a scene
MediaRecorder For encoding and recording media generated in a CaptureSession
SoundEffect 提供在 QML 中播放音效方式的类型
SpatialSound A sound object in 3D space
audioDevice Describes an audio device
mediaMetaData Provides meta-data for media files

范例

音频设备范例 Testing the available audio devices and their configuration.
音频输出范例 Enabling audio playback using the QAudioSink class.
音频录制器范例 Discovering the available devices and supported codecs.
Audio Source Example Recording audio using the QAudioSource class.
Spatial Audio Panning Example Shows some of the capabilities of the spatial audio engine in Qt