| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "audioinput.h" |
| 52 | |
| 53 | #include <stdlib.h> |
| 54 | #include <math.h> |
| 55 | |
| 56 | #include <QDateTime> |
| 57 | #include <QDebug> |
| 58 | #include <QPainter> |
| 59 | #include <QVBoxLayout> |
| 60 | #include <QAudioDeviceInfo> |
| 61 | #include <QAudioInput> |
| 62 | #include <qendian.h> |
| 63 | |
| 64 | AudioInfo::AudioInfo(const QAudioFormat &format) |
| 65 | : m_format(format) |
| 66 | { |
| 67 | switch (m_format.sampleSize()) { |
| 68 | case 8: |
| 69 | switch (m_format.sampleType()) { |
| 70 | case QAudioFormat::UnSignedInt: |
| 71 | m_maxAmplitude = 255; |
| 72 | break; |
| 73 | case QAudioFormat::SignedInt: |
| 74 | m_maxAmplitude = 127; |
| 75 | break; |
| 76 | default: |
| 77 | break; |
| 78 | } |
| 79 | break; |
| 80 | case 16: |
| 81 | switch (m_format.sampleType()) { |
| 82 | case QAudioFormat::UnSignedInt: |
| 83 | m_maxAmplitude = 65535; |
| 84 | break; |
| 85 | case QAudioFormat::SignedInt: |
| 86 | m_maxAmplitude = 32767; |
| 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | break; |
| 92 | |
| 93 | case 32: |
| 94 | switch (m_format.sampleType()) { |
| 95 | case QAudioFormat::UnSignedInt: |
| 96 | m_maxAmplitude = 0xffffffff; |
| 97 | break; |
| 98 | case QAudioFormat::SignedInt: |
| 99 | m_maxAmplitude = 0x7fffffff; |
| 100 | break; |
| 101 | case QAudioFormat::Float: |
| 102 | m_maxAmplitude = 0x7fffffff; // Kind of |
| 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void AudioInfo::start() |
| 114 | { |
| 115 | open(mode: QIODevice::WriteOnly); |
| 116 | } |
| 117 | |
| 118 | void AudioInfo::stop() |
| 119 | { |
| 120 | close(); |
| 121 | } |
| 122 | |
| 123 | qint64 AudioInfo::readData(char *data, qint64 maxlen) |
| 124 | { |
| 125 | Q_UNUSED(data) |
| 126 | Q_UNUSED(maxlen) |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | qint64 AudioInfo::writeData(const char *data, qint64 len) |
| 132 | { |
| 133 | if (m_maxAmplitude) { |
| 134 | Q_ASSERT(m_format.sampleSize() % 8 == 0); |
| 135 | const int channelBytes = m_format.sampleSize() / 8; |
| 136 | const int sampleBytes = m_format.channelCount() * channelBytes; |
| 137 | Q_ASSERT(len % sampleBytes == 0); |
| 138 | const int numSamples = len / sampleBytes; |
| 139 | |
| 140 | quint32 maxValue = 0; |
| 141 | const unsigned char *ptr = reinterpret_cast<const unsigned char *>(data); |
| 142 | |
| 143 | for (int i = 0; i < numSamples; ++i) { |
| 144 | for (int j = 0; j < m_format.channelCount(); ++j) { |
| 145 | quint32 value = 0; |
| 146 | |
| 147 | if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::UnSignedInt) { |
| 148 | value = *reinterpret_cast<const quint8*>(ptr); |
| 149 | } else if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::SignedInt) { |
| 150 | value = qAbs(t: *reinterpret_cast<const qint8*>(ptr)); |
| 151 | } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::UnSignedInt) { |
| 152 | if (m_format.byteOrder() == QAudioFormat::LittleEndian) |
| 153 | value = qFromLittleEndian<quint16>(src: ptr); |
| 154 | else |
| 155 | value = qFromBigEndian<quint16>(src: ptr); |
| 156 | } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::SignedInt) { |
| 157 | if (m_format.byteOrder() == QAudioFormat::LittleEndian) |
| 158 | value = qAbs(t: qFromLittleEndian<qint16>(src: ptr)); |
| 159 | else |
| 160 | value = qAbs(t: qFromBigEndian<qint16>(src: ptr)); |
| 161 | } else if (m_format.sampleSize() == 32 && m_format.sampleType() == QAudioFormat::UnSignedInt) { |
| 162 | if (m_format.byteOrder() == QAudioFormat::LittleEndian) |
| 163 | value = qFromLittleEndian<quint32>(src: ptr); |
| 164 | else |
| 165 | value = qFromBigEndian<quint32>(src: ptr); |
| 166 | } else if (m_format.sampleSize() == 32 && m_format.sampleType() == QAudioFormat::SignedInt) { |
| 167 | if (m_format.byteOrder() == QAudioFormat::LittleEndian) |
| 168 | value = qAbs(t: qFromLittleEndian<qint32>(src: ptr)); |
| 169 | else |
| 170 | value = qAbs(t: qFromBigEndian<qint32>(src: ptr)); |
| 171 | } else if (m_format.sampleSize() == 32 && m_format.sampleType() == QAudioFormat::Float) { |
| 172 | value = qAbs(t: *reinterpret_cast<const float*>(ptr) * 0x7fffffff); // assumes 0-1.0 |
| 173 | } |
| 174 | |
| 175 | maxValue = qMax(a: value, b: maxValue); |
| 176 | ptr += channelBytes; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | maxValue = qMin(a: maxValue, b: m_maxAmplitude); |
| 181 | m_level = qreal(maxValue) / m_maxAmplitude; |
| 182 | } |
| 183 | |
| 184 | emit update(); |
| 185 | return len; |
| 186 | } |
| 187 | |
| 188 | RenderArea::RenderArea(QWidget *parent) |
| 189 | : QWidget(parent) |
| 190 | { |
| 191 | setBackgroundRole(QPalette::Base); |
| 192 | setAutoFillBackground(true); |
| 193 | |
| 194 | setMinimumHeight(30); |
| 195 | setMinimumWidth(200); |
| 196 | } |
| 197 | |
| 198 | void RenderArea::paintEvent(QPaintEvent * /* event */) |
| 199 | { |
| 200 | QPainter painter(this); |
| 201 | |
| 202 | painter.setPen(Qt::black); |
| 203 | painter.drawRect(r: QRect(painter.viewport().left()+10, |
| 204 | painter.viewport().top()+10, |
| 205 | painter.viewport().right()-20, |
| 206 | painter.viewport().bottom()-20)); |
| 207 | if (m_level == 0.0) |
| 208 | return; |
| 209 | |
| 210 | int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*m_level; |
| 211 | painter.fillRect(x: painter.viewport().left()+11, |
| 212 | y: painter.viewport().top()+10, |
| 213 | w: pos, |
| 214 | h: painter.viewport().height()-21, |
| 215 | c: Qt::red); |
| 216 | } |
| 217 | |
| 218 | void RenderArea::setLevel(qreal value) |
| 219 | { |
| 220 | m_level = value; |
| 221 | update(); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | InputTest::InputTest() |
| 226 | { |
| 227 | initializeWindow(); |
| 228 | initializeAudio(deviceInfo: QAudioDeviceInfo::defaultInputDevice()); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void InputTest::initializeWindow() |
| 233 | { |
| 234 | QWidget *window = new QWidget; |
| 235 | QVBoxLayout *layout = new QVBoxLayout; |
| 236 | |
| 237 | m_canvas = new RenderArea(this); |
| 238 | layout->addWidget(m_canvas); |
| 239 | |
| 240 | m_deviceBox = new QComboBox(this); |
| 241 | const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultInputDevice(); |
| 242 | m_deviceBox->addItem(atext: defaultDeviceInfo.deviceName(), auserData: QVariant::fromValue(value: defaultDeviceInfo)); |
| 243 | for (auto &deviceInfo: QAudioDeviceInfo::availableDevices(mode: QAudio::AudioInput)) { |
| 244 | if (deviceInfo != defaultDeviceInfo) |
| 245 | m_deviceBox->addItem(atext: deviceInfo.deviceName(), auserData: QVariant::fromValue(value: deviceInfo)); |
| 246 | } |
| 247 | |
| 248 | connect(sender: m_deviceBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), receiver: this, slot: &InputTest::deviceChanged); |
| 249 | layout->addWidget(m_deviceBox); |
| 250 | |
| 251 | m_volumeSlider = new QSlider(Qt::Horizontal, this); |
| 252 | m_volumeSlider->setRange(min: 0, max: 100); |
| 253 | m_volumeSlider->setValue(100); |
| 254 | connect(sender: m_volumeSlider, signal: &QSlider::valueChanged, receiver: this, slot: &InputTest::sliderChanged); |
| 255 | layout->addWidget(m_volumeSlider); |
| 256 | |
| 257 | m_modeButton = new QPushButton(this); |
| 258 | connect(sender: m_modeButton, signal: &QPushButton::clicked, receiver: this, slot: &InputTest::toggleMode); |
| 259 | layout->addWidget(m_modeButton); |
| 260 | |
| 261 | m_suspendResumeButton = new QPushButton(this); |
| 262 | connect(sender: m_suspendResumeButton, signal: &QPushButton::clicked, receiver: this, slot: &InputTest::toggleSuspend); |
| 263 | layout->addWidget(m_suspendResumeButton); |
| 264 | |
| 265 | window->setLayout(layout); |
| 266 | |
| 267 | setCentralWidget(window); |
| 268 | window->show(); |
| 269 | } |
| 270 | |
| 271 | void InputTest::initializeAudio(const QAudioDeviceInfo &deviceInfo) |
| 272 | { |
| 273 | QAudioFormat format; |
| 274 | format.setSampleRate(8000); |
| 275 | format.setChannelCount(1); |
| 276 | format.setSampleSize(16); |
| 277 | format.setSampleType(QAudioFormat::SignedInt); |
| 278 | format.setByteOrder(QAudioFormat::LittleEndian); |
| 279 | format.setCodec("audio/pcm" ); |
| 280 | |
| 281 | if (!deviceInfo.isFormatSupported(format)) { |
| 282 | qWarning() << "Default format not supported - trying to use nearest" ; |
| 283 | format = deviceInfo.nearestFormat(format); |
| 284 | } |
| 285 | |
| 286 | m_audioInfo.reset(other: new AudioInfo(format)); |
| 287 | connect(sender: m_audioInfo.data(), signal: &AudioInfo::update, slot: [this]() { |
| 288 | m_canvas->setLevel(m_audioInfo->level()); |
| 289 | }); |
| 290 | |
| 291 | m_audioInput.reset(other: new QAudioInput(deviceInfo, format)); |
| 292 | qreal initialVolume = QAudio::convertVolume(volume: m_audioInput->volume(), |
| 293 | from: QAudio::LinearVolumeScale, |
| 294 | to: QAudio::LogarithmicVolumeScale); |
| 295 | m_volumeSlider->setValue(qRound(d: initialVolume * 100)); |
| 296 | m_audioInfo->start(); |
| 297 | toggleMode(); |
| 298 | } |
| 299 | |
| 300 | void InputTest::toggleMode() |
| 301 | { |
| 302 | m_audioInput->stop(); |
| 303 | toggleSuspend(); |
| 304 | |
| 305 | // Change bewteen pull and push modes |
| 306 | if (m_pullMode) { |
| 307 | m_modeButton->setText(tr(s: "Enable push mode" )); |
| 308 | m_audioInput->start(device: m_audioInfo.data()); |
| 309 | } else { |
| 310 | m_modeButton->setText(tr(s: "Enable pull mode" )); |
| 311 | auto io = m_audioInput->start(); |
| 312 | connect(sender: io, signal: &QIODevice::readyRead, |
| 313 | slot: [&, io]() { |
| 314 | qint64 len = m_audioInput->bytesReady(); |
| 315 | const int BufferSize = 4096; |
| 316 | if (len > BufferSize) |
| 317 | len = BufferSize; |
| 318 | |
| 319 | QByteArray buffer(len, 0); |
| 320 | qint64 l = io->read(data: buffer.data(), maxlen: len); |
| 321 | if (l > 0) |
| 322 | m_audioInfo->write(data: buffer.constData(), len: l); |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | m_pullMode = !m_pullMode; |
| 327 | } |
| 328 | |
| 329 | void InputTest::toggleSuspend() |
| 330 | { |
| 331 | // toggle suspend/resume |
| 332 | if (m_audioInput->state() == QAudio::SuspendedState || m_audioInput->state() == QAudio::StoppedState) { |
| 333 | m_audioInput->resume(); |
| 334 | m_suspendResumeButton->setText(tr(s: "Suspend recording" )); |
| 335 | } else if (m_audioInput->state() == QAudio::ActiveState) { |
| 336 | m_audioInput->suspend(); |
| 337 | m_suspendResumeButton->setText(tr(s: "Resume recording" )); |
| 338 | } else if (m_audioInput->state() == QAudio::IdleState) { |
| 339 | // no-op |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void InputTest::deviceChanged(int index) |
| 344 | { |
| 345 | m_audioInfo->stop(); |
| 346 | m_audioInput->stop(); |
| 347 | m_audioInput->disconnect(receiver: this); |
| 348 | |
| 349 | initializeAudio(deviceInfo: m_deviceBox->itemData(index).value<QAudioDeviceInfo>()); |
| 350 | } |
| 351 | |
| 352 | void InputTest::sliderChanged(int value) |
| 353 | { |
| 354 | qreal linearVolume = QAudio::convertVolume(volume: value / qreal(100), |
| 355 | from: QAudio::LogarithmicVolumeScale, |
| 356 | to: QAudio::LinearVolumeScale); |
| 357 | |
| 358 | m_audioInput->setVolume(linearVolume); |
| 359 | } |
| 360 | |