| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtMultimedia/QAudioFormat>
|
|---|
| 42 | #include "utils.h"
|
|---|
| 43 |
|
|---|
| 44 | qint64 audioDuration(const QAudioFormat &format, qint64 bytes)
|
|---|
| 45 | {
|
|---|
| 46 | return (bytes * 1000000) /
|
|---|
| 47 | (format.frequency() * format.channels() * (format.sampleSize() / 8));
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | qint64 audioLength(const QAudioFormat &format, qint64 microSeconds)
|
|---|
| 51 | {
|
|---|
| 52 | return (format.frequency() * format.channels() * (format.sampleSize() / 8))
|
|---|
| 53 | * microSeconds / 1000000;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | qreal nyquistFrequency(const QAudioFormat &format)
|
|---|
| 57 | {
|
|---|
| 58 | return format.frequency() / 2;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | QString formatToString(const QAudioFormat &format)
|
|---|
| 62 | {
|
|---|
| 63 | QString result;
|
|---|
| 64 |
|
|---|
| 65 | if (QAudioFormat() != format) {
|
|---|
| 66 | if (format.codec() == "audio/pcm") {
|
|---|
| 67 | Q_ASSERT(format.sampleType() == QAudioFormat::SignedInt);
|
|---|
| 68 |
|
|---|
| 69 | const QString formatEndian = (format.byteOrder() == QAudioFormat::LittleEndian)
|
|---|
| 70 | ? QString("LE") : QString("BE");
|
|---|
| 71 |
|
|---|
| 72 | QString formatType;
|
|---|
| 73 | switch(format.sampleType()) {
|
|---|
| 74 | case QAudioFormat::SignedInt:
|
|---|
| 75 | formatType = "signed";
|
|---|
| 76 | break;
|
|---|
| 77 | case QAudioFormat::UnSignedInt:
|
|---|
| 78 | formatType = "unsigned";
|
|---|
| 79 | break;
|
|---|
| 80 | case QAudioFormat::Float:
|
|---|
| 81 | formatType = "float";
|
|---|
| 82 | break;
|
|---|
| 83 | case QAudioFormat::Unknown:
|
|---|
| 84 | formatType = "unknown";
|
|---|
| 85 | break;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | QString formatChannels = QString("%1 channels").arg(format.channels());
|
|---|
| 89 | switch (format.channels()) {
|
|---|
| 90 | case 1:
|
|---|
| 91 | formatChannels = "mono";
|
|---|
| 92 | break;
|
|---|
| 93 | case 2:
|
|---|
| 94 | formatChannels = "stereo";
|
|---|
| 95 | break;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | result = QString("%1 Hz %2 bit %3 %4 %5")
|
|---|
| 99 | .arg(format.frequency())
|
|---|
| 100 | .arg(format.sampleSize())
|
|---|
| 101 | .arg(formatType)
|
|---|
| 102 | .arg(formatEndian)
|
|---|
| 103 | .arg(formatChannels);
|
|---|
| 104 | } else {
|
|---|
| 105 | result = format.codec();
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | return result;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | bool isPCM(const QAudioFormat &format)
|
|---|
| 113 | {
|
|---|
| 114 | return (format.codec() == "audio/pcm");
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | bool isPCMS16LE(const QAudioFormat &format)
|
|---|
| 119 | {
|
|---|
| 120 | return (isPCM(format) &&
|
|---|
| 121 | format.sampleType() == QAudioFormat::SignedInt &&
|
|---|
| 122 | format.sampleSize() == 16 &&
|
|---|
| 123 | format.byteOrder() == QAudioFormat::LittleEndian);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | const qint16 PCMS16MaxValue = 32767;
|
|---|
| 127 | const quint16 PCMS16MaxAmplitude = 32768; // because minimum is -32768
|
|---|
| 128 |
|
|---|
| 129 | qreal pcmToReal(qint16 pcm)
|
|---|
| 130 | {
|
|---|
| 131 | return qreal(pcm) / PCMS16MaxAmplitude;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | qint16 realToPcm(qreal real)
|
|---|
| 135 | {
|
|---|
| 136 | return real * PCMS16MaxValue;
|
|---|
| 137 | }
|
|---|