| 1 |
/* |
| 2 |
* Copyright (C) 2020-2023 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "adpcm_drumkit_editor.hpp" |
| 27 |
#include "ui_adpcm_drumkit_editor.h" |
| 28 |
#include <QString> |
| 29 |
#include <QCoreApplication> |
| 30 |
#include "instrument.hpp" |
| 31 |
#include "note.hpp" |
| 32 |
#include "gui/event_guard.hpp" |
| 33 |
#include "gui/jam_layout.hpp" |
| 34 |
#include "gui/note_name_manager.hpp" |
| 35 |
#include "gui/slider_style.hpp" |
| 36 |
|
| 37 |
namespace |
| 38 |
{ |
| 39 |
const char* PAN_TEXT[] = { |
| 40 |
QT_TRANSLATE_NOOP("Panning", "Left"), |
| 41 |
QT_TRANSLATE_NOOP("Panning", "Center"), |
| 42 |
QT_TRANSLATE_NOOP("Panning", "Right") |
| 43 |
}; |
| 44 |
constexpr int PAN_UI2INTRNL[] = { PanType::LEFT, PanType::CENTER, PanType::RIGHT }; |
| 45 |
|
| 46 |
inline int convertPanInternalToUi(int intrPan) |
| 47 |
{ |
| 48 |
return std::distance(std::begin(PAN_UI2INTRNL), |
| 49 |
std::find(std::begin(PAN_UI2INTRNL), std::end(PAN_UI2INTRNL), intrPan)); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
AdpcmDrumkitEditor::AdpcmDrumkitEditor(int num, QWidget* parent) |
| 54 |
: InstrumentEditor(num, parent), |
| 55 |
ui(new Ui::AdpcmDrumkitEditor), |
| 56 |
isIgnoreEvent_(false), |
| 57 |
hasShown_(false) |
| 58 |
{ |
| 59 |
ui->setupUi(this); |
| 60 |
|
| 61 |
ui->keyTreeWidget->header()->setDefaultSectionSize(40); // Rearrange header widths |
| 62 |
NoteNameManager& nnm = NoteNameManager::getManager(); |
| 63 |
for (int i = 0; i < Note::NOTE_NUMBER_RANGE; ++i) { |
| 64 |
ui->keyTreeWidget->addTopLevelItem( |
| 65 |
new QTreeWidgetItem({ nnm.getNoteString(i), "-", "-" })); |
| 66 |
} |
| 67 |
|
| 68 |
//========== Sample ==========// |
| 69 |
QObject::connect(ui->sampleEditor, &ADPCMSampleEditor::modified, this, [&] { emit modified(); }); |
| 70 |
QObject::connect(ui->sampleEditor, &ADPCMSampleEditor::sampleNumberChanged, |
| 71 |
this, [&](int n) { |
| 72 |
bt_.lock()->setInstrumentDrumkitSample(instNum_, ui->keyTreeWidget->currentIndex().row(), n); |
| 73 |
setInstrumentSampleParameters(ui->keyTreeWidget->currentIndex().row()); |
| 74 |
emit sampleNumberChanged(); |
| 75 |
emit modified(); |
| 76 |
|
| 77 |
if (config_.lock()->getWriteOnlyUsedSamples()) { |
| 78 |
emit sampleAssignRequested(); |
| 79 |
} |
| 80 |
}, Qt::DirectConnection); |
| 81 |
QObject::connect(ui->sampleEditor, &ADPCMSampleEditor::sampleParameterChanged, |
| 82 |
this, [&](int sampNum) { |
| 83 |
emit sampleParameterChanged(sampNum, instNum_); |
| 84 |
}); |
| 85 |
QObject::connect(ui->sampleEditor, &ADPCMSampleEditor::sampleAssignRequested, |
| 86 |
this, [&] { emit sampleAssignRequested(); }); |
| 87 |
QObject::connect(ui->sampleEditor, &ADPCMSampleEditor::sampleMemoryChanged, |
| 88 |
this, [&] { emit sampleMemoryChanged(); }); |
| 89 |
|
| 90 |
//========== Pan ==========// |
| 91 |
ui->panHorizontalSlider->setStyle(SliderStyle::instance()); |
| 92 |
ui->panHorizontalSlider->installEventFilter(this); |
| 93 |
ui->panPosLabel->setText(QCoreApplication::translate("Panning", PAN_TEXT[ui->panHorizontalSlider->value()])); |
| 94 |
} |
| 95 |
|
| 96 |
AdpcmDrumkitEditor::~AdpcmDrumkitEditor() |
| 97 |
{ |
| 98 |
delete ui; |
| 99 |
} |
| 100 |
|
| 101 |
SoundSource AdpcmDrumkitEditor::getSoundSource() const |
| 102 |
{ |
| 103 |
return SoundSource::ADPCM; |
| 104 |
} |
| 105 |
|
| 106 |
InstrumentType AdpcmDrumkitEditor::getInstrumentType() const |
| 107 |
{ |
| 108 |
return InstrumentType::Drumkit; |
| 109 |
} |
| 110 |
|
| 111 |
void AdpcmDrumkitEditor::updateBySettingCore() |
| 112 |
{ |
| 113 |
ui->sampleEditor->setCore(bt_); |
| 114 |
updateInstrumentParameters(); |
| 115 |
} |
| 116 |
|
| 117 |
void AdpcmDrumkitEditor::updateBySettingConfiguration() |
| 118 |
{ |
| 119 |
ui->sampleEditor->setConfiguration(config_); |
| 120 |
} |
| 121 |
|
| 122 |
void AdpcmDrumkitEditor::updateBySettingColorPalette() |
| 123 |
{ |
| 124 |
ui->sampleEditor->setColorPalette(palette_); |
| 125 |
} |
| 126 |
|
| 127 |
void AdpcmDrumkitEditor::updateInstrumentParameters() |
| 128 |
{ |
| 129 |
Ui::EventGuard eg(isIgnoreEvent_); |
| 130 |
|
| 131 |
updateWindowTitle(); |
| 132 |
|
| 133 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 134 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 135 |
|
| 136 |
for (const auto& key : instKit->getAssignedKeys()) { |
| 137 |
setInstrumentSampleParameters(key); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/********** Events **********/ |
| 142 |
bool AdpcmDrumkitEditor::eventFilter(QObject* watched, QEvent* event) |
| 143 |
{ |
| 144 |
if (watched == ui->panHorizontalSlider) { |
| 145 |
if (event->type() == QEvent::Wheel) { |
| 146 |
auto e = dynamic_cast<QWheelEvent*>(event); |
| 147 |
if (e->angleDelta().y() > 0) ui->panHorizontalSlider->setValue(ui->panHorizontalSlider->value() + 1); |
| 148 |
else if (e->angleDelta().y() < 0) ui->panHorizontalSlider->setValue(ui->panHorizontalSlider->value() - 1); |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return QWidget::eventFilter(watched, event); |
| 154 |
} |
| 155 |
|
| 156 |
void AdpcmDrumkitEditor::showEvent(QShowEvent*) |
| 157 |
{ |
| 158 |
if (!hasShown_) { |
| 159 |
ui->keyTreeWidget->setCurrentItem(ui->keyTreeWidget->topLevelItem(Note::DEFAULT_NOTE_NUM)); |
| 160 |
ui->keyTreeWidget->scrollTo(ui->keyTreeWidget->model()->index(Note::DEFAULT_NOTE_NUM, 0), |
| 161 |
QAbstractItemView::PositionAtTop); |
| 162 |
int x = config_.lock()->getInstrumentDrumkitWindowHorizontalSplit(); |
| 163 |
if (x == -1) { |
| 164 |
config_.lock()->setInstrumentDrumkitWindowHorizontalSplit(ui->splitter->sizes().at(0)); |
| 165 |
} |
| 166 |
else { |
| 167 |
ui->splitter->setSizes({ x, ui->splitter->width() - ui->splitter->handleWidth() - x }); |
| 168 |
} |
| 169 |
} |
| 170 |
hasShown_ = true; |
| 171 |
} |
| 172 |
|
| 173 |
// MUST DIRECT CONNECTION |
| 174 |
void AdpcmDrumkitEditor::keyPressEvent(QKeyEvent *event) |
| 175 |
{ |
| 176 |
// General keys |
| 177 |
switch (event->key()) { |
| 178 |
case Qt::Key_Escape: |
| 179 |
close(); |
| 180 |
break; |
| 181 |
default: |
| 182 |
// For jam key on |
| 183 |
if (!event->isAutoRepeat()) { |
| 184 |
// Musical keyboard |
| 185 |
Qt::Key qtKey = static_cast<Qt::Key>(event->key()); |
| 186 |
try { |
| 187 |
JamKey jk = getJamKeyFromLayoutMapping(qtKey, config_); |
| 188 |
emit jamKeyOnEvent(jk); |
| 189 |
} catch (std::invalid_argument&) {} |
| 190 |
} |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// MUST DIRECT CONNECTION |
| 196 |
void AdpcmDrumkitEditor::keyReleaseEvent(QKeyEvent *event) |
| 197 |
{ |
| 198 |
// For jam key off |
| 199 |
if (!event->isAutoRepeat()) { |
| 200 |
Qt::Key qtKey = static_cast<Qt::Key>(event->key()); |
| 201 |
try { |
| 202 |
JamKey jk = getJamKeyFromLayoutMapping(qtKey, config_); |
| 203 |
emit jamKeyOffEvent(jk); |
| 204 |
} catch (std::invalid_argument&) {} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/********** Slots **********/ |
| 209 |
void AdpcmDrumkitEditor::on_keyTreeWidget_currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*) |
| 210 |
{ |
| 211 |
Ui::EventGuard eg(isIgnoreEvent_); |
| 212 |
|
| 213 |
int key = ui->keyTreeWidget->currentIndex().row(); |
| 214 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 215 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 216 |
|
| 217 |
bool enabled = instKit->getSampleEnabled(key); |
| 218 |
ui->sampleGroupBox->setChecked(enabled); |
| 219 |
if (enabled) { |
| 220 |
setInstrumentSampleParameters(key); |
| 221 |
ui->pitchSpinBox->setValue(instKit->getPitch(key)); |
| 222 |
ui->panHorizontalSlider->setValue(convertPanInternalToUi(instKit->getPan(key))); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void AdpcmDrumkitEditor::on_splitter_splitterMoved(int pos, int) |
| 227 |
{ |
| 228 |
config_.lock()->setInstrumentDrumkitWindowHorizontalSplit(pos); |
| 229 |
} |
| 230 |
|
| 231 |
//--- Pitch |
| 232 |
void AdpcmDrumkitEditor::on_pitchSpinBox_valueChanged(int arg1) |
| 233 |
{ |
| 234 |
int key = ui->keyTreeWidget->currentIndex().row(); |
| 235 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 236 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 237 |
|
| 238 |
if (instKit->getSampleEnabled(key)) { |
| 239 |
bt_.lock()->setInstrumentDrumkitPitch(instNum_, key, arg1); |
| 240 |
ui->keyTreeWidget->currentItem()->setText(2, QString::number(arg1)); |
| 241 |
emit modified(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
//--- Pan |
| 246 |
void AdpcmDrumkitEditor::on_panHorizontalSlider_valueChanged(int value) |
| 247 |
{ |
| 248 |
const QString text = QCoreApplication::translate("Panning", PAN_TEXT[value]); |
| 249 |
ui->panPosLabel->setText(text); |
| 250 |
|
| 251 |
int key = ui->keyTreeWidget->currentIndex().row(); |
| 252 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 253 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 254 |
|
| 255 |
if (instKit->getSampleEnabled(key)) { |
| 256 |
bt_.lock()->setInstrumentDrumkitPan(instNum_, key, PAN_UI2INTRNL[value]); |
| 257 |
ui->keyTreeWidget->currentItem()->setText(3, text); |
| 258 |
emit modified(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
//--- Sample |
| 263 |
void AdpcmDrumkitEditor::setInstrumentSampleParameters(int key) |
| 264 |
{ |
| 265 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 266 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 267 |
QTreeWidgetItem* item = ui->keyTreeWidget->topLevelItem(key); |
| 268 |
|
| 269 |
if (instKit->getSampleEnabled(key)) { |
| 270 |
int sampNum = instKit->getSampleNumber(key); |
| 271 |
ui->sampleEditor->setInstrumentSampleParameters( |
| 272 |
sampNum, |
| 273 |
instKit->isSampleRepeatable(key), |
| 274 |
instKit->getSampleRepeatRange(key), |
| 275 |
instKit->getSampleRootKeyNumber(key), |
| 276 |
instKit->getSampleRootDeltaN(key), |
| 277 |
instKit->getSampleStartAddress(key), |
| 278 |
instKit->getSampleStopAddress(key), |
| 279 |
instKit->getRawSample(key)); |
| 280 |
item->setText(1, QString::number(sampNum)); |
| 281 |
item->setText(2, QString::number(instKit->getPitch(key))); |
| 282 |
item->setText(3, QCoreApplication::translate("Panning", PAN_TEXT[convertPanInternalToUi(instKit->getPan(key))])); |
| 283 |
} |
| 284 |
else { |
| 285 |
ui->sampleEditor->setInstrumentSampleParameters( |
| 286 |
0, |
| 287 |
bt_.lock()->getSampleADPCMRepeatEnabled(0), |
| 288 |
bt_.lock()->getSampleADPCMRepeatRange(0), |
| 289 |
bt_.lock()->getSampleADPCMRootKeyNumber(0), |
| 290 |
bt_.lock()->getSampleADPCMRootDeltaN(0), |
| 291 |
bt_.lock()->getSampleADPCMStartAddress(0), |
| 292 |
bt_.lock()->getSampleADPCMStopAddress(0), |
| 293 |
bt_.lock()->getSampleADPCMRawSample(0)); |
| 294 |
item->setText(1, "-"); |
| 295 |
item->setText(2, "-"); |
| 296 |
item->setText(3, "-"); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/********** Slots **********/ |
| 301 |
void AdpcmDrumkitEditor::onSampleNumberChanged() |
| 302 |
{ |
| 303 |
ui->sampleEditor->onSampleNumberChanged(); |
| 304 |
} |
| 305 |
|
| 306 |
void AdpcmDrumkitEditor::onSampleParameterChanged(int sampNum) |
| 307 |
{ |
| 308 |
if (ui->sampleEditor->getSampleNumber() == sampNum) { |
| 309 |
setInstrumentSampleParameters(ui->keyTreeWidget->currentIndex().row()); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
void AdpcmDrumkitEditor::onSampleMemoryUpdated() |
| 314 |
{ |
| 315 |
std::unique_ptr<AbstractInstrument> inst = bt_.lock()->getInstrument(instNum_); |
| 316 |
auto instKit = dynamic_cast<InstrumentDrumkit*>(inst.get()); |
| 317 |
int key = ui->keyTreeWidget->currentIndex().row(); |
| 318 |
|
| 319 |
if (instKit->getSampleEnabled(key)) { |
| 320 |
ui->sampleEditor->onSampleMemoryUpdated(instKit->getSampleStartAddress(key), |
| 321 |
instKit->getSampleStopAddress(key)); |
| 322 |
} |
| 323 |
else { |
| 324 |
// Clear addresses |
| 325 |
ui->sampleEditor->onSampleMemoryUpdated(0, 0); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
void AdpcmDrumkitEditor::on_sampleGroupBox_clicked(bool checked) |
| 330 |
{ |
| 331 |
Ui::EventGuard eg(isIgnoreEvent_); |
| 332 |
|
| 333 |
int key = ui->keyTreeWidget->currentIndex().row(); |
| 334 |
bt_.lock()->setInstrumentDrumkitSampleEnabled(instNum_, key, checked); |
| 335 |
if (checked) { |
| 336 |
ui->pitchSpinBox->setValue(0); |
| 337 |
ui->panHorizontalSlider->setValue(convertPanInternalToUi(PanType::CENTER)); |
| 338 |
setInstrumentSampleParameters(key); |
| 339 |
} |
| 340 |
else { |
| 341 |
// Clear parameters |
| 342 |
ui->sampleEditor->setInstrumentSampleParameters( |
| 343 |
0, |
| 344 |
bt_.lock()->getSampleADPCMRepeatEnabled(0), |
| 345 |
bt_.lock()->getSampleADPCMRepeatRange(0), |
| 346 |
bt_.lock()->getSampleADPCMRootKeyNumber(0), |
| 347 |
bt_.lock()->getSampleADPCMRootDeltaN(0), |
| 348 |
bt_.lock()->getSampleADPCMStartAddress(0), |
| 349 |
bt_.lock()->getSampleADPCMStopAddress(0), |
| 350 |
bt_.lock()->getSampleADPCMRawSample(0)); |
| 351 |
|
| 352 |
auto item = ui->keyTreeWidget->currentItem(); |
| 353 |
item->setText(1, "-"); |
| 354 |
item->setText(2, "-"); |
| 355 |
item->setText(3, "-"); |
| 356 |
} |
| 357 |
|
| 358 |
emit sampleNumberChanged(); |
| 359 |
emit modified(); |
| 360 |
} |