summaryrefslogtreecommitdiffstats
path: root/src/mixer/samplerbank.cpp
blob: 435835e48b3671c67c0adcba3b50ae54947996e3 (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
187
188
189
190
191
192
193
194
195
196
197
#include "mixer/samplerbank.h"

#include <QFileDialog>
#include <QMessageBox>

#include "control/controlpushbutton.h"
#include "mixer/playermanager.h"
#include "mixer/sampler.h"
#include "moc_samplerbank.cpp"
#include "track/track.h"
#include "util/assert.h"

SamplerBank::SamplerBank(PlayerManager* pPlayerManager)
        : QObject(pPlayerManager),
          m_pPlayerManager(pPlayerManager) {
    DEBUG_ASSERT(m_pPlayerManager);

    m_pCOLoadBank = std::make_unique<ControlPushButton>(ConfigKey("[Sampler]", "LoadSamplerBank"), this);
    connect(m_pCOLoadBank.get(),
            &ControlObject::valueChanged,
            this,
            &SamplerBank::slotLoadSamplerBank);

    m_pCOSaveBank = std::make_unique<ControlPushButton>(ConfigKey("[Sampler]", "SaveSamplerBank"), this);
    connect(m_pCOSaveBank.get(),
            &ControlObject::valueChanged,
            this,
            &SamplerBank::slotSaveSamplerBank);

    m_pCONumSamplers = new ControlProxy(ConfigKey("[Master]", "num_samplers"), this);
}

SamplerBank::~SamplerBank() {
}

void SamplerBank::slotSaveSamplerBank(double v) {
    if (v <= 0.0) {
        return;
    }

    const QString xmlSuffix = QStringLiteral(".xml");
    QString fileFilter = tr("Mixxx Sampler Banks (*%1)").arg(xmlSuffix);
    QString samplerBankPath = QFileDialog::getSaveFileName(nullptr,
            tr("Save Sampler Bank"),
            QString(),
            fileFilter,
            &fileFilter);
    if (samplerBankPath.isNull() || samplerBankPath.isEmpty()) {
        return;
    }

    // Manually add extension due to bug in QFileDialog
    // via https://bugreports.qt-project.org/browse/QTBUG-27186
    QFileInfo fileName(samplerBankPath);
    if (fileName.suffix().isEmpty() || !fileName.suffix().endsWith(xmlSuffix)) {
        samplerBankPath.append(xmlSuffix);
    }

    if (!saveSamplerBankToPath(samplerBankPath)) {
        QMessageBox::warning(nullptr,
                tr("Error Saving Sampler Bank"),
                tr("Could not write the sampler bank to '%1'.")
                        .arg(samplerBankPath));
    }
}

bool SamplerBank::saveSamplerBankToPath(const QString& samplerBankPath) {
    // The user has picked a new directory via a file dialog. This means the
    // system sandboxer (if we are sandboxed) has granted us permission to this
    // folder. We don't need access to this file on a regular basis so we do not
    // register a security bookmark.

    VERIFY_OR_DEBUG_ASSERT(m_pPlayerManager) {
        qWarning() << "SamplerBank::saveSamplerBankToPath called with no PlayerManager";
        return false;
    }

    QFile file(samplerBankPath);
    if (!file.open(QIODevice::WriteOnly)) {
        qWarning() << "Error saving sampler bank: Could not write to file"
                   << samplerBankPath;
        return false;
    }

    QDomDocument doc("SamplerBank");

    QDomElement root = doc.createElement("samplerbank");
    doc.appendChild(root);

    for (unsigned int i = 0; i < m_pPlayerManager->numSamplers(); ++i) {
        Sampler* pSampler = m_pPlayerManager->getSampler(i + 1);
        if (!pSampler) {
            continue;
        }
        QDomElement samplerNode = doc.createElement(QString("sampler"));

        samplerNode.setAttribute("group", pSampler->getGroup());

        TrackPointer pTrack = pSampler->getLoadedTrack();
        if (pTrack) {
            QString samplerLocation = pTrack->getLocation();
            samplerNode.setAttribute("location", samplerLocation);
        }
        root.appendChild(samplerNode);
    }

    QString docStr = doc.toString();

    file.write(docStr.toUtf8().constData());
    file.close();

    return true;
}

void SamplerBank::slotLoadSamplerBank(double v) {
    if (v <= 0.0) {
        return;
    }

    QString samplerBankPath = QFileDialog::getOpenFileName(nullptr,
            tr("Load Sampler Bank"),
            QString(),
            tr("Mixxx Sampler Banks (*.xml)"));
    if (samplerBankPath.isEmpty()) {
        return;
    }

    if (!loadSamplerBankFromPath(samplerBankPath)) {
        QMessageBox::warning(nullptr,
                tr("Error Reading Sampler Bank"),
                tr("Could not open the sampler bank file '%1'.")
                        .arg(samplerBankPath));
    }
}

bool SamplerBank::loadSamplerBankFromPath(const QString& samplerBankPath) {
    // The user has picked a new directory via a file dialog. This means the
    // system sandboxer (if we are sandboxed) has granted us permission to this
    // folder. We don't need access to this file on a regular basis so we do not
    // register a security bookmark.

    VERIFY_OR_DEBUG_ASSERT(m_pPlayerManager) {
        qWarning() << "SamplerBank::loadSamplerBankFromPath called with no PlayerManager";
        return false;
    }

    QFile file(samplerBankPath);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "Could not read sampler bank file" << samplerBankPath;
        return false;
    }

    QDomDocument doc;

    if (!doc.setContent(file.readAll())) {
        qWarning() << "Could not read sampler bank file" << samplerBankPath;
        return false;
    }

    QDomElement root = doc.documentElement();
    if (root.tagName() != "samplerbank") {
        qWarning() << "Could not read sampler bank file" << samplerBankPath;
        return false;
    }

    QDomNode n = root.firstChild();

    while (!n.isNull()) {
        QDomElement e = n.toElement();

        if (!e.isNull()) {
            if (e.tagName() == "sampler") {
                QString group = e.attribute("group", "");
                QString location = e.attribute("location", "");
                int samplerNum;

                if (!group.isEmpty()
                        && m_pPlayerManager->isSamplerGroup(group, &samplerNum)) {
                    if (m_pPlayerManager->numSamplers() < (unsigned) samplerNum) {
                        m_pCONumSamplers->set(samplerNum);
                    }

                    if (location.isEmpty()) {
                        m_pPlayerManager->slotLoadTrackToPlayer(TrackPointer(), group);
                    } else {
                        m_pPlayerManager->slotLoadToPlayer(location, group);
                    }
                }

            }
        }
        n = n.nextSibling();
    }

    file.close();
    return true;
}