summaryrefslogtreecommitdiffstats
path: root/src/analyzer/analyzerkey.cpp
blob: 4079a87e3bf5aecdc38fb487d2a0d3427eb9eb7d (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "analyzerkey.h"

#include <QtDebug>
#include <QVector>

#include "proto/keys.pb.h"
#include "track/key_preferences.h"
#include "track/keyfactory.h"

using mixxx::track::io::key::ChromaticKey;
using mixxx::track::io::key::ChromaticKey_IsValid;

AnalyzerKey::AnalyzerKey(UserSettingsPointer pConfig)
        : m_pConfig(pConfig),
          m_pVamp(NULL),
          m_iSampleRate(0),
          m_iTotalSamples(0),
          m_bPreferencesKeyDetectionEnabled(true),
          m_bPreferencesFastAnalysisEnabled(false),
          m_bPreferencesReanalyzeEnabled(false) {
}

AnalyzerKey::~AnalyzerKey() {
    delete m_pVamp;
}

bool AnalyzerKey::initialize(TrackPointer tio, int sampleRate, int totalSamples) {
    if (totalSamples == 0) {
        return false;
    }

    m_bPreferencesKeyDetectionEnabled = static_cast<bool>(
        m_pConfig->getValueString(
            ConfigKey(KEY_CONFIG_KEY, KEY_DETECTION_ENABLED)).toInt());
    if (!m_bPreferencesKeyDetectionEnabled) {
        qDebug() << "Key detection is deactivated";
        return false;
    }

    m_bPreferencesFastAnalysisEnabled = static_cast<bool>(
        m_pConfig->getValueString(
            ConfigKey(KEY_CONFIG_KEY, KEY_FAST_ANALYSIS)).toInt());
    QString library = m_pConfig->getValueString(
        ConfigKey(VAMP_CONFIG_KEY, VAMP_ANALYZER_KEY_LIBRARY),
        // TODO(rryan) this default really doesn't belong here.
        "libmixxxminimal");
    QString pluginID = m_pConfig->getValueString(
        ConfigKey(VAMP_CONFIG_KEY, VAMP_ANALYZER_KEY_PLUGIN_ID),
        // TODO(rryan) this default really doesn't belong here.
        VAMP_ANALYZER_KEY_DEFAULT_PLUGIN_ID);

    m_pluginId = pluginID;
    m_iSampleRate = sampleRate;
    m_iTotalSamples = totalSamples;

    // if we can't load a stored track reanalyze it
    bool bShouldAnalyze = !loadStored(tio);

    if (bShouldAnalyze) {
        m_pVamp = new VampAnalyzer();
        bShouldAnalyze = m_pVamp->Init(
            library, m_pluginId, sampleRate, totalSamples,
            m_bPreferencesFastAnalysisEnabled);
        if (!bShouldAnalyze) {
            delete m_pVamp;
            m_pVamp = NULL;
        }
    }

    if (bShouldAnalyze) {
        qDebug() << "Key calculation started with plugin" << m_pluginId;
    } else {
        qDebug() << "Key calculation will not start.";
    }

    return bShouldAnalyze;
}

bool AnalyzerKey::loadStored(TrackPointer tio) const {
    bool bPreferencesFastAnalysisEnabled = static_cast<bool>(
        m_pConfig->getValueString(
            ConfigKey(KEY_CONFIG_KEY, KEY_FAST_ANALYSIS)).toInt());

    QString library = m_pConfig->getValueString(
        ConfigKey(VAMP_CONFIG_KEY, VAMP_ANALYZER_KEY_LIBRARY));
    QString pluginID = m_pConfig->getValueString(
        ConfigKey(VAMP_CONFIG_KEY, VAMP_ANALYZER_KEY_PLUGIN_ID));

    // TODO(rryan): This belongs elsewhere.
    if (library.isEmpty() || library.isNull())
        library = "libmixxxminimal";

    // TODO(rryan): This belongs elsewhere.
    if (pluginID.isEmpty() || pluginID.isNull())
        pluginID = VAMP_ANALYZER_KEY_DEFAULT_PLUGIN_ID;

    const Keys& keys = tio->getKeys();
    if (keys.isValid()) {
        QString version = keys.getVersion();
        QString subVersion = keys.getSubVersion();

        QHash<QString, QString> extraVersionInfo = getExtraVersionInfo(
            pluginID, bPreferencesFastAnalysisEnabled);
        QString newVersion = KeyFactory::getPreferredVersion();
        QString newSubVersion = KeyFactory::getPreferredSubVersion(extraVersionInfo);

        if (version == newVersion && subVersion == newSubVersion) {
            // If the version and settings have not changed then if the world is
            // sane, re-analyzing will do nothing.
            qDebug() << "Keys version/sub-version unchanged since previous analysis. Not analyzing.";
            return true;
        } else if (m_bPreferencesReanalyzeEnabled) {
            return false;
        } else {
            qDebug() << "Track has previous key detection result that is not up"
                     << "to date with latest settings but user preferences"
                     << "indicate we should not re-analyze it.";
            return true;
        }
    } else {
        // If we got here, we want to analyze this track.
        return false;
    }
}

void AnalyzerKey::process(const CSAMPLE *pIn, const int iLen) {
    if (m_pVamp == NULL)
        return;
    bool success = m_pVamp->Process(pIn, iLen);
    if (!success) {
        delete m_pVamp;
        m_pVamp = NULL;
    }
}

void AnalyzerKey::cleanup(TrackPointer tio) {
    Q_UNUSED(tio);
    delete m_pVamp;
    m_pVamp = NULL;
}

void AnalyzerKey::finalize(TrackPointer tio) {
    if (m_pVamp == NULL) {
        return;
    }

    bool success = m_pVamp->End();
    qDebug() << "Key Detection" << (success ? "complete" : "failed");

    QVector<double> frames = m_pVamp->GetInitFramesVector();
    QVector<double> keys = m_pVamp->GetLastValuesVector();
    delete m_pVamp;
    m_pVamp = NULL;

    if (frames.size() == 0 || frames.size() != keys.size()) {
        qWarning() << "AnalyzerKey: Key sequence and list of times do not match.";
        return;
    }

    KeyChangeList key_changes;
    for (int i = 0; i < keys.size(); ++i) {
        if (ChromaticKey_IsValid(keys[i])) {
            key_changes.push_back(qMakePair(
                // int() intermediate cast required by MSVC.
                static_cast<ChromaticKey>(int(keys[i])), frames[i]));
        }
    }

    QHash<QString, QString> extraVersionInfo = getExtraVersionInfo(
        m_pluginId, m_bPreferencesFastAnalysisEnabled);
    Keys track_keys = KeyFactory::makePreferredKeys(
        key_changes, extraVersionInfo,
        m_iSampleRate, m_iTotalSamples);
    tio->setKeys(track_keys);
}

// static
QHash<QString, QString> AnalyzerKey::getExtraVersionInfo(
    QString pluginId, bool bPreferencesFastAnalysis) {
    QHash<QString, QString> extraVersionInfo;
    extraVersionInfo["vamp_plugin_id"] = pluginId;
    if (bPreferencesFastAnalysis) {
        extraVersionInfo["fast_analysis"] = "1";
    }
    return extraVersionInfo;
}