音頻概述

音頻特徵

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