summaryrefslogtreecommitdiffstats
path: root/examples/multimedia/spectrum/utils.cpp
blob: 3b4146a51b2e88c894ffe0a5fd5931e5531bd07b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "utils.h"
#include <QAudioFormat>

qreal nyquistFrequency(const QAudioFormat &format)
{
    return format.sampleRate() / 2;
}

QString formatToString(const QAudioFormat &format)
{
    QString result;

    if (QAudioFormat() != format) {

        QString formatType;
        switch (format.sampleFormat()) {
        case QAudioFormat::UInt8:
            formatType = "Unsigned8";
            break;
        case QAudioFormat::Int16:
            formatType = "Signed16";
            break;
        case QAudioFormat::Int32:
            formatType = "Signed32";
            break;
        case QAudioFormat::Float:
            formatType = "Float";
            break;
        default:
            formatType = "Unknown";
            break;
        }

        QString formatChannels = QStringLiteral("%1 channels").arg(format.channelCount());
        switch (format.channelCount()) {
        case 1:
            formatChannels = "mono";
            break;
        case 2:
            formatChannels = "stereo";
            break;
        }

        result = QStringLiteral("%1 Hz %2 bit %3 %4")
                         .arg(format.sampleRate())
                         .arg(format.bytesPerSample() * 8)
                         .arg(formatType)
                         .arg(formatChannels);
    }

    return result;
}

const qint16 PCMS16MaxValue = 32767;
const quint16 PCMS16MaxAmplitude = 32768; // because minimum is -32768

qreal pcmToReal(qint16 pcm)
{
    return qreal(pcm) / PCMS16MaxAmplitude;
}

qint16 realToPcm(qreal real)
{
    return real * PCMS16MaxValue;
}