1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "spectrum.h"
42 #include "utils.h"
43 #include <QByteArray>
44 #include <QtMultimedia/QAudioFormat>
45 #include <QtCore/qmath.h>
46 #include <QtCore/qendian.h>
47
generateTone(const SweptTone & tone,const QAudioFormat & format,QByteArray & buffer)48 void generateTone(const SweptTone &tone, const QAudioFormat &format, QByteArray &buffer)
49 {
50 Q_ASSERT(isPCMS16LE(format));
51
52 const int channelBytes = format.sampleSize() / 8;
53 const int sampleBytes = format.channels() * channelBytes;
54 int length = buffer.size();
55 const int numSamples = buffer.size() / sampleBytes;
56
57 Q_ASSERT(length % sampleBytes == 0);
58 Q_UNUSED(sampleBytes) // suppress warning in release builds
59
60 unsigned char *ptr = reinterpret_cast<unsigned char *>(buffer.data());
61
62 qreal phase = 0.0;
63
64 const qreal d = 2 * M_PI / format.frequency();
65
66 // We can't generate a zero-frequency sine wave
67 const qreal startFreq = tone.startFreq ? tone.startFreq : 1.0;
68
69 // Amount by which phase increases on each sample
70 qreal phaseStep = d * startFreq;
71
72 // Amount by which phaseStep increases on each sample
73 // If this is non-zero, the output is a frequency-swept tone
74 const qreal phaseStepStep = d * (tone.endFreq - startFreq) / numSamples;
75
76 while (length) {
77 const qreal x = tone.amplitude * qSin(phase);
78 const qint16 value = realToPcm(x);
79 for (int i=0; i<format.channels(); ++i) {
80 qToLittleEndian<qint16>(value, ptr);
81 ptr += channelBytes;
82 length -= channelBytes;
83 }
84
85 phase += phaseStep;
86 while (phase > 2 * M_PI)
87 phase -= 2 * M_PI;
88 phaseStep += phaseStepStep;
89 }
90 }
91
92