aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmppresetselect.cpp
blob: 8ad05a3e3350b7145c25a7d9376c54565540ca7d (plain) (blame)
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
#include <cstdio>
#include "qmppresetselect.hpp"
#include "ui_qmppresetselect.h"
#include "qmpmidioutfluid.hpp"
#include "qmpmainwindow.hpp"

qmpPresetSelector::qmpPresetSelector(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::qmpPresetSelector)
{
    ui->setupUi(this);
}

qmpPresetSelector::~qmpPresetSelector()
{
    delete ui;
}

void qmpPresetSelector::showEvent(QShowEvent *e)
{
    memset(presets, 0, sizeof(presets));
    if (!qmpMainWindow::getInstance()->getFluid()->getSFCount())
        return e->ignore();
    ui->lwBankSelect->clear();
    ui->lwPresetSelect->clear();
    e->accept();
}
void qmpPresetSelector::setupWindow(int chid)
{
    CMidiPlayer *plyr = qmpMainWindow::getInstance()->getPlayer();
    ch = chid;
    int r;
    char name[256];
    uint16_t b;
    uint8_t p;
    std::string pstname;
    sprintf(name, "Preset Selection - Channel #%d", ch + 1);
    setWindowTitle(name);
    r = plyr->getChannelOutputDevice(ch)->getChannelPreset(ch, &b, &p, pstname);
    if (!r)
    {
        b = plyr->getCC(ch, 0) << 7 | plyr->getCC(ch, 32);
        p = plyr->getCC(ch, 128);
    }
    ui->lwBankSelect->blockSignals(true);
    ui->lwBankSelect->clear();
    ui->lwPresetSelect->clear();
    ui->lwBankSelect->blockSignals(false);
    if (plyr->getChannelOutputDevice(ch)->getBankList().empty())
    {
        ui->lwPresetSelect->setEnabled(false);
        ui->lwBankSelect->setEnabled(false);
        ui->spCustomLSB->setEnabled(true);
        ui->spCustomMSB->setEnabled(true);
        ui->spCustomPC->setEnabled(true);
        ui->spCustomMSB->setValue(plyr->getCC(chid, 0));
        ui->spCustomLSB->setValue(plyr->getCC(chid, 32));
        ui->spCustomPC->setValue(p);
    }
    else
    {
        ui->lwPresetSelect->setEnabled(true);
        ui->lwBankSelect->setEnabled(true);
        ui->spCustomLSB->setEnabled(false);
        ui->spCustomMSB->setEnabled(false);
        ui->spCustomPC->setEnabled(false);
        ui->lwBankSelect->blockSignals(true);
        for (auto &i : plyr->getChannelOutputDevice(ch)->getBankList())
        {
            snprintf(name, 256, "%03d %s", i.first, i.second.c_str());
            ui->lwBankSelect->addItem(name);
            if (i.first == b)
                ui->lwBankSelect->setCurrentRow(ui->lwBankSelect->count() - 1);
        }
        ui->lwBankSelect->blockSignals(false);
        for (auto &i : plyr->getChannelOutputDevice(ch)->getPresets(b))
        {
            snprintf(name, 256, "%03d %s", i.first, i.second.c_str());
            ui->lwPresetSelect->addItem(name);
            if (i.first == p)
                ui->lwPresetSelect->setCurrentRow(ui->lwPresetSelect->count() - 1);
        }
    }
}

void qmpPresetSelector::on_pbCancel_clicked()
{
    close();
}

void qmpPresetSelector::on_pbOk_clicked()
{
    CMidiPlayer *plyr = qmpMainWindow::getInstance()->getPlayer();
    if (plyr->getChannelOutput(ch))
    {
        if (ui->spCustomMSB->isEnabled())
            plyr->setChannelPreset(ch, (ui->spCustomMSB->value() << 7) | ui->spCustomLSB->value(), ui->spCustomPC->value());
        else
        {
            if (!ui->lwBankSelect->currentItem() || !ui->lwPresetSelect->currentItem())
                return (void)close();
            int b = ui->lwBankSelect->currentItem()->text().split(' ').first().toInt();
            int p = ui->lwPresetSelect->currentItem()->text().split(' ').first().toInt();
            plyr->setChannelPreset(ch, b, p);
        }
    }
    else
    {
        if (!ui->lwBankSelect->currentItem() || !ui->lwPresetSelect->currentItem())
            return (void)close();
        int b, p;
        b = ui->lwBankSelect->currentItem()->text().toInt();
        p = ui->lwPresetSelect->currentItem()->text().split(' ').first().toInt();
        std::string s = qmpMainWindow::getInstance()->getSettings()->getOptionEnumIntOptName("FluidSynth/BankSelect");
        if (s == "XG")
        {
            if (b == 128)
                b = 127 << 7;
        }
        else if (s == "GS")
            b <<= 7;
        plyr->setChannelPreset(ch, b, p);
    }
    qmpMainWindow::getInstance()->invokeCallback("preset.set", nullptr);
    close();
}

void qmpPresetSelector::on_lwPresetSelect_itemDoubleClicked()
{
    on_pbOk_clicked();
}

void qmpPresetSelector::on_lwBankSelect_currentRowChanged()
{
    ui->lwPresetSelect->clear();
    if (!ui->lwBankSelect->currentItem())
        return;
    char name[256];
    int b;
    sscanf(ui->lwBankSelect->currentItem()->text().toStdString().c_str(), "%d", &b);
    CMidiPlayer *plyr = qmpMainWindow::getInstance()->getPlayer();
    for (auto &i : plyr->getChannelOutputDevice(ch)->getPresets(b))
    {
        snprintf(name, 256, "%03d %s", i.first, i.second.c_str());
        ui->lwPresetSelect->addItem(name);
    }
}

void qmpPresetSelector::on_buttonBox_accepted()
{
    on_pbOk_clicked();
}

void qmpPresetSelector::on_buttonBox_rejected()
{
    on_pbCancel_clicked();
}