summaryrefslogtreecommitdiffstats
path: root/src/track/bpm.cpp
blob: 647a2a924be97333eab936431b3c2cc764835e32 (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
#include "track/bpm.h"

namespace mixxx {

/*static*/ constexpr double Bpm::kValueUndefined;
/*static*/ constexpr double Bpm::kValueMin;

double Bpm::valueFromString(const QString& str, bool* pValid) {
    if (pValid) {
        *pValid = false;
    }
    if (str.trimmed().isEmpty()) {
        return kValueUndefined;
    }
    bool valueValid = false;
    double value = str.toDouble(&valueValid);
    if (valueValid) {
        if (kValueUndefined == value) {
            // special case
            if (pValid) {
                *pValid = true;
            }
            return value;
        }
        if (isValidValue(value)) {
            if (pValid) {
                *pValid = true;
            }
            return value;
        } else {
            qDebug() << "Invalid BPM value:" << str << "->" << value;
        }
    } else {
        qDebug() << "Failed to parse BPM:" << str;
    }
    return kValueUndefined;
}

QString Bpm::valueToString(double value) {
    if (isValidValue(value)) {
        //TODO: Shouldn't this be formatted in some way, instead of letting it output in scientific notation?
        return QString::number(value);
    } else {
        return QString();
    }
}

double Bpm::normalizeValue(double value) {
    if (isValidValue(value)) {
        const double normalizedValue = valueFromString(valueToString(value));
        // NOTE(uklotzde): Subsequently formatting and parsing the
        // normalized value should not alter it anymore!
        DEBUG_ASSERT(normalizedValue == valueFromString(valueToString(normalizedValue)));
        return normalizedValue;
    } else {
        return value;
    }
}

} // namespace mixxx