1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
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 | qint64 result = (format.frequency() * format.channels() * (format.sampleSize() / 8))
|
---|
53 | * microSeconds / 1000000;
|
---|
54 | result -= result % (format.channelCount() * format.sampleSize());
|
---|
55 | return result;
|
---|
56 | }
|
---|
57 |
|
---|
58 | qreal nyquistFrequency(const QAudioFormat &format)
|
---|
59 | {
|
---|
60 | return format.frequency() / 2;
|
---|
61 | }
|
---|
62 |
|
---|
63 | QString formatToString(const QAudioFormat &format)
|
---|
64 | {
|
---|
65 | QString result;
|
---|
66 |
|
---|
67 | if (QAudioFormat() != format) {
|
---|
68 | if (format.codec() == "audio/pcm") {
|
---|
69 | Q_ASSERT(format.sampleType() == QAudioFormat::SignedInt);
|
---|
70 |
|
---|
71 | const QString formatEndian = (format.byteOrder() == QAudioFormat::LittleEndian)
|
---|
72 | ? QString("LE") : QString("BE");
|
---|
73 |
|
---|
74 | QString formatType;
|
---|
75 | switch(format.sampleType()) {
|
---|
76 | case QAudioFormat::SignedInt:
|
---|
77 | formatType = "signed";
|
---|
78 | break;
|
---|
79 | case QAudioFormat::UnSignedInt:
|
---|
80 | formatType = "unsigned";
|
---|
81 | break;
|
---|
82 | case QAudioFormat::Float:
|
---|
83 | formatType = "float";
|
---|
84 | break;
|
---|
85 | case QAudioFormat::Unknown:
|
---|
86 | formatType = "unknown";
|
---|
87 | break;
|
---|
88 | }
|
---|
89 |
|
---|
90 | QString formatChannels = QString("%1 channels").arg(format.channels());
|
---|
91 | switch (format.channels()) {
|
---|
92 | case 1:
|
---|
93 | formatChannels = "mono";
|
---|
94 | break;
|
---|
95 | case 2:
|
---|
96 | formatChannels = "stereo";
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | result = QString("%1 Hz %2 bit %3 %4 %5")
|
---|
101 | .arg(format.frequency())
|
---|
102 | .arg(format.sampleSize())
|
---|
103 | .arg(formatType)
|
---|
104 | .arg(formatEndian)
|
---|
105 | .arg(formatChannels);
|
---|
106 | } else {
|
---|
107 | result = format.codec();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 | bool isPCM(const QAudioFormat &format)
|
---|
115 | {
|
---|
116 | return (format.codec() == "audio/pcm");
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | bool isPCMS16LE(const QAudioFormat &format)
|
---|
121 | {
|
---|
122 | return (isPCM(format) &&
|
---|
123 | format.sampleType() == QAudioFormat::SignedInt &&
|
---|
124 | format.sampleSize() == 16 &&
|
---|
125 | format.byteOrder() == QAudioFormat::LittleEndian);
|
---|
126 | }
|
---|
127 |
|
---|
128 | const qint16 PCMS16MaxValue = 32767;
|
---|
129 | const quint16 PCMS16MaxAmplitude = 32768; // because minimum is -32768
|
---|
130 |
|
---|
131 | qreal pcmToReal(qint16 pcm)
|
---|
132 | {
|
---|
133 | return qreal(pcm) / PCMS16MaxAmplitude;
|
---|
134 | }
|
---|
135 |
|
---|
136 | qint16 realToPcm(qreal real)
|
---|
137 | {
|
---|
138 | return real * PCMS16MaxValue;
|
---|
139 | }
|
---|