1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qaudiohelpers_p.h"
5
6#include <QDebug>
7
8QT_BEGIN_NAMESPACE
9
10namespace QAudioHelperInternal
11{
12
13template<class T> void adjustSamples(qreal factor, const void *src, void *dst, int samples)
14{
15 const T *pSrc = (const T *)src;
16 T *pDst = (T*)dst;
17 for ( int i = 0; i < samples; i++ )
18 pDst[i] = pSrc[i] * factor;
19}
20
21// Unsigned samples are biased around 0x80/0x8000 :/
22// This makes a pure template solution a bit unwieldy but possible
23template<class T> struct signedVersion {};
24template<> struct signedVersion<quint8>
25{
26 using TS = qint8;
27 static constexpr int offset = 0x80;
28};
29
30template<class T> void adjustUnsignedSamples(qreal factor, const void *src, void *dst, int samples)
31{
32 const T *pSrc = (const T *)src;
33 T *pDst = (T*)dst;
34 for ( int i = 0; i < samples; i++ ) {
35 pDst[i] = signedVersion<T>::offset + ((typename signedVersion<T>::TS)(pSrc[i] - signedVersion<T>::offset) * factor);
36 }
37}
38
39void qMultiplySamples(qreal factor, const QAudioFormat &format, const void* src, void* dest, int len)
40{
41 const int samplesCount = len / qMax(a: 1, b: format.bytesPerSample());
42
43 switch (format.sampleFormat()) {
44 case QAudioFormat::Unknown:
45 case QAudioFormat::NSampleFormats:
46 return;
47 case QAudioFormat::UInt8:
48 QAudioHelperInternal::adjustUnsignedSamples<quint8>(factor,src,dst: dest,samples: samplesCount);
49 break;
50 case QAudioFormat::Int16:
51 QAudioHelperInternal::adjustSamples<qint16>(factor,src,dst: dest,samples: samplesCount);
52 break;
53 case QAudioFormat::Int32:
54 QAudioHelperInternal::adjustSamples<qint32>(factor,src,dst: dest,samples: samplesCount);
55 break;
56 case QAudioFormat::Float:
57 QAudioHelperInternal::adjustSamples<float>(factor,src,dst: dest,samples: samplesCount);
58 break;
59 }
60}
61}
62
63QT_END_NAMESPACE
64

source code of qtmultimedia/src/multimedia/audio/qaudiohelpers.cpp