36void AudioInputExample::setup()
39 destinationFile.setFileName(
"/tmp/test.raw");
40 destinationFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
44 format.setSampleRate(44100);
45 format.setChannelCount(1);
46 format.setSampleFormat(QAudioFormat::Int16);
48 QAudioDevice info = QMediaDevices::defaultAudioInput();
49 if (!info.isFormatSupported(format)) {
50 qWarning() <<
"Default format not supported, trying to use the nearest.";
53 audio =
new QAudioSource(format,
this);
54 connect(audio, &QAudioSource::stateChanged,
this, &AudioInputExample::handleStateChanged);
56 QTimer::singleShot(3000,
this, &AudioInputExample::stopRecording);
57 audio->start(&destinationFile);
115 sourceFile.setFileName(
"/tmp/test.raw");
116 sourceFile.open(QIODevice::ReadOnly);
120 format.setSampleRate(44100);
121 format.setChannelCount(1);
122 format.setSampleFormat(QAudioFormat::Int16);
124 QAudioDevice info(QMediaDevices::defaultAudioOutput());
125 if (!info.isFormatSupported(format)) {
126 qWarning() <<
"Raw audio format not supported by backend, cannot play audio.";
130 audio =
new QAudioSink(format,
this);
131 connect(audio, QAudioSink::stateChanged,
this, &AudioInputExample::handleStateChanged);
132 audio->start(&sourceFile);
187 format.setSampleRate(44100);
188 format.setChannelCount(2);
189 format.setSampleFormat(QAudioFormat::Float);
191 QAudioDevice info(QMediaDevices::defaultAudioOutput());
192 if (!info.isFormatSupported(format)) {
193 qWarning() <<
"Raw audio format not supported by backend, cannot play audio.";
197 audio =
new QAudioSink(format,
this);
198 float phaseIncrement = 2 * M_PI * 220.0 / format.sampleRate();
199 audio->start([&phase, phaseIncrement] (QSpan<
float> interleavedAudioBuffer) {
203 const int sampleCount = interleavedAudioBuffer.size() / 2;
204 for (
int i = 0; i < sampleCount; ++i) {
205 float sample = std::sin(phase);
206 interleavedAudioBuffer[i * 2] = sample;
207 interleavedAudioBuffer[i * 2 + 1] = sample;
208 phase += phaseIncrement;
212 if (!audio->error() == QtAudio::Error::NoError) {
218 qWarning() <<
"Error starting audio output:" << audio->errorString();
242 format.setSampleRate(44100);
243 format.setChannelCount(2);
244 format.setSampleFormat(QAudioFormat::Float);
246 QAudioDevice info(QMediaDevices::defaultAudioOutput());
247 if (!info.isFormatSupported(format)) {
248 qWarning() <<
"Raw audio format not supported by backend, cannot capture audio.";
252 audio =
new QAudioSource(format,
this);
253 audio->start([&peakLevel] (QSpan<
float> interleavedAudioBuffer) {
254 float level = peakLevel.load();
256 for (
float sample : interleavedAudioBuffer) {
258 level = std::max(level, std::abs(sample));
261 peakLevel.store(level);
267 if (!audio->error() == QtAudio::Error::NoError) {
273 qWarning() <<
"Error starting audio output:" << audio->errorString();
307 QAudioFormat desiredFormat;
308 desiredFormat.setChannelCount(2);
309 desiredFormat.setSampleFormat(QAudioFormat::Int16);
310 desiredFormat.setSampleRate(48000);
312 QAudioDecoder *decoder =
new QAudioDecoder(
this);
313 decoder->setAudioFormat(desiredFormat);
314 decoder->setSource(
"level1.mp3");
330 qreal linearVolume = QtAudio::convertVolume(volumeSliderValue / qreal(100.0),
331 QtAudio::LogarithmicVolumeScale,
332 QtAudio::LinearVolumeScale);
334 player.setVolume(qRound(linearVolume * 100));