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