CopperSpice API  1.9.1
Audio Overview

CsMultimedia offers a range of audio classes, covering both low and high level approaches to audio input, output and processing.

Playing Compressed Audio

To play audio or video audio files that are compressed use the QMediaPlayer class. The supported compression formats depend on the operating system and any media plugins the user installed.

The following example shows how to play a local mp3 file.

player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/home/cindy/Music/mySong.mp3"));
player->setVolume(50);
player->play();

You can also put files or remote URLs into a playlist.

player = new QMediaPlayer;
playlist = new QMediaPlaylist(player);
playlist->addMedia(QUrl("http://example.com/myfile1.mp3"));
playlist->addMedia(QUrl("http://example.com/myfile2.mp3"));
playlist->setCurrentIndex(1);
player->play();

Recording Audio to a File

The QAudioRecorder class allows you to compress audio data from an input device and record it.

audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr"));
audioRecorder->record();

Low Latency Sound Effects

In addition to the raw access to sound devices described above, the QSoundEffect class offers a slightly higher level way to play sounds. These classes allow you to specify a WAV format file which can then be played with low latency when necessary. Both QSoundEffect and SoundEffect have essentially the same API.

You can adjust the number of loops a sound effect is played, as well as the volume (or muting) of the effect.

To support older applications QSound is also available. QSoundEffect is preferred where possible.

Monitoring Audio Data During Playback or Recording

The QAudioProbe class allows you to monitor audio data being played or recorded using classes like QMediaPlayer, QCamera and QAudioRecorder.

After creating an instance of one of these classed set the source of the probe to the instance. The probe will receive audio buffers as they are processed. This is useful for audio processing tasks like visualization or adjusting gain. The buffers are read only and they may arrive at a slightly different time than the media pipeline processes them.

The following example shows how to install a probe before recording. The probe will emit a signal each time a new audio buffer is recorded.

audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr"));
audioProbe = new QAudioProbe(this);
if (audioProbe->setSource(audioRecorder)) {
// Probing succeeded, audioProbe->isValid() should be true
connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(calculateLevel(QAudioBuffer)));
}
audioRecorder->record();

Decoding Compressed Audio to Memory

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 supports decoding local files or data streams from QIODevice instances.

The following is an example of decoding a local file.

QAudioFormat desiredFormat;
desiredFormat.setChannelCount(2);
desiredFormat.setCodec("audio/x-raw");
desiredFormat.setSampleType(QAudioFormat::UnSignedInt);
desiredFormat.setSampleRate(48000);
desiredFormat.setSampleSize(16);
QAudioDecoder *decoder = new QAudioDecoder(this);
decoder->setAudioFormat(desiredFormat);
decoder->setSourceFilename("level1.mp3");
connect(decoder, SIGNAL(bufferReady()), this, SLOT(readBuffer()));
decoder->start();
// now wait for bufferReady() signal and call decoder->read()