Debian Codegraph @

BambooTracker/gui/instrument_editor/adpcm_sample_editor.cpp

raw
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_sample_editor.hpp"
27 #include "ui_adpcm_sample_editor.h"
28 #include <cmath>
29 #include <limits>
30 #include <vector>
31 #include <set>
32 #include <algorithm>
33 #include <QMimeData>
34 #include <QFile>
35 #include <QIODevice>
36 #include <QFileInfo>
37 #include <QFileDialog>
38 #include <QStringList>
39 #include <QPainter>
40 #include <QRect>
41 #include <QRectF>
42 #include <QToolBar>
43 #include <QMenu>
44 #include <QToolButton>
45 #include <QWheelEvent>
46 #include <QHoverEvent>
47 #include "chip/codec/ymb_codec.hpp"
48 #include "instrument/sample_adpcm.hpp"
49 #include "gui/event_guard.hpp"
50 #include "gui/instrument_editor/sample_length_dialog.hpp"
51 #include "gui/instrument_editor/grid_settings_dialog.hpp"
52 #include "gui/file_io_error_message_box.hpp"
53 #include "gui/instrument_editor/instrument_editor_utils.hpp"
54 #include "gui/note_name_manager.hpp"
55 #include "utils.hpp"
56
57 ADPCMSampleEditor::ADPCMSampleEditor(QWidget *parent) :
58 QWidget(parent),
59 ui(new Ui::ADPCMSampleEditor),
60 isIgnoreEvent_(false),
61 zoom_(0),
62 viewedSampLen_(2),
63 gridIntr_(1),
64 cursorSamp_(0, 0),
65 prevPressedSamp_(-1, -1),
66 addrStart_(0),
67 addrStop_(0),
68 sample_(2),
69 drawMode_(DrawMode::Disabled)
70 {
71 ui->setupUi(this);
72
73 for (int i = 0; i < 12; ++i) {
74 ui->rootKeyComboBox->addItem(NoteNameManager::getManager().getNoteName(i));
75 }
76
77 auto rkfunc = [&](int) {
78 if (!isIgnoreEvent_) {
79 int rk = ui->rootKeySpinBox->value() * 12 + ui->rootKeyComboBox->currentIndex();
80 bt_.lock()->setSampleADPCMRootKeyNumber(ui->sampleNumSpinBox->value(), rk);
81 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
82 emit modified();
83 }
84 };
85 // Leave Before Qt5.7.0 style due to windows xp
86 QObject::connect(ui->rootKeyComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, rkfunc);
87 QObject::connect(ui->rootKeySpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, rkfunc);
88
89 ui->memoryWidget->installEventFilter(this);
90 ui->sampleViewWidget->setAttribute(Qt::WA_Hover);
91 ui->sampleViewWidget->installEventFilter(this);
92
93 auto tb = new QToolBar();
94 tb->setIconSize(QSize(16, 16));
95 tb->setMinimumHeight(28);
96 tb->setMaximumHeight(28);
97 tb->addActions({ ui->action_Clear, ui->action_Import, ui->action_Resize });
98 tb->addSeparator();
99 tb->addActions({ ui->actionZoom_In, ui->actionZoom_Out });
100 auto gridMenu = new QMenu();
101 gridMenu->addActions({ ui->action_Grid_View, ui->actionG_rid_Settings });
102 auto gridButton = new QToolButton();
103 gridButton->setPopupMode(QToolButton::MenuButtonPopup);
104 gridButton->setMenu(gridMenu);
105 gridButton->setDefaultAction(ui->action_Grid_View);
106 tb->addWidget(gridButton);
107 tb->addSeparator();
108 auto editMenu = new QMenu();
109 editMenu->addActions({ ui->action_Draw_Sample, ui->actionDirec_t_Draw });
110 auto editButton = new QToolButton();
111 editButton->setPopupMode(QToolButton::MenuButtonPopup);
112 editButton->setMenu(editMenu);
113 editButton->setDefaultAction(ui->action_Draw_Sample);
114 QObject::connect(editButton, &QToolButton::triggered, editButton, &QToolButton::setDefaultAction);
115 tb->addWidget(editButton);
116 tb->addAction(ui->actionRe_verse);
117 ui->verticalLayout_2->insertWidget(0, tb);
118 }
119
120 ADPCMSampleEditor::~ADPCMSampleEditor()
121 {
122 delete ui;
123 }
124
125 void ADPCMSampleEditor::setCore(std::weak_ptr<BambooTracker> core)
126 {
127 bt_ = core;
128 }
129
130 void ADPCMSampleEditor::setConfiguration(std::weak_ptr<Configuration> config)
131 {
132 config_ = config;
133 }
134
135 void ADPCMSampleEditor::setColorPalette(std::shared_ptr<ColorPalette> palette)
136 {
137 palette_ = palette;
138 }
139
140 int ADPCMSampleEditor::getSampleNumber() const
141 {
142 return ui->sampleNumSpinBox->value();
143 }
144
145 void ADPCMSampleEditor::dragEnterEvent(QDragEnterEvent* event)
146 {
147 const QMimeData* mime = event->mimeData();
148 if (mime->hasUrls() && mime->urls().length() == 1
149 && QFileInfo(mime->urls().at(0).toLocalFile()).suffix().toLower() == "wav") {
150 event->acceptProposedAction();
151 }
152 }
153
154 bool ADPCMSampleEditor::eventFilter(QObject* obj, QEvent* ev)
155 {
156 if (obj == ui->memoryWidget) {
157 switch (ev->type()) {
158 case QEvent::Paint:
159 {
160 QPainter painter(ui->memoryWidget);
161 painter.drawPixmap(ui->memoryWidget->rect(), memPixmap_);
162 break;
163 }
164 case QEvent::Resize:
165 memPixmap_ = QPixmap(ui->memoryWidget->size());
166 updateSampleMemoryBar();
167 break;
168 default:
169 break;
170 }
171 return false;
172 }
173
174 if (obj == ui->sampleViewWidget) {
175 switch (ev->type()) {
176 case QEvent::Paint:
177 {
178 QPainter painter(ui->sampleViewWidget);
179 painter.drawPixmap(ui->sampleViewWidget->rect(), sampViewPixmap_);
180 break;
181 }
182 case QEvent::Resize:
183 sampViewPixmap_ = QPixmap(ui->sampleViewWidget->size());
184 updateSampleView();
185 break;
186 case QEvent::Wheel:
187 {
188 auto we = dynamic_cast<QWheelEvent*>(ev);
189 int cnt = we->angleDelta().y() / 120;
190 bool ctrl = we->modifiers().testFlag(Qt::ControlModifier);
191 if (cnt > 0) {
192 for (int i = 0; i < cnt; ++i) {
193 if (ctrl) on_actionZoom_In_triggered();
194 else ui->horizontalScrollBar->setValue(ui->horizontalScrollBar->value() - 1);
195 }
196 }
197 else {
198 for (int i = 0; cnt < i; --i) {
199 if (ctrl) on_actionZoom_Out_triggered();
200 else ui->horizontalScrollBar->setValue(ui->horizontalScrollBar->value() + 1);
201 }
202 }
203 break;
204 }
205 case QEvent::HoverMove:
206 {
207 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
208 auto pos = dynamic_cast<QHoverEvent*>(ev)->position();
209 #else
210 auto pos = dynamic_cast<QHoverEvent*>(ev)->pos();
211 #endif
212 detectCursorSamplePosition(pos.x(), pos.y());
213
214 if (prevPressedSamp_.x() != -1) { // Change sample
215 const int px = prevPressedSamp_.x();
216 const int py = prevPressedSamp_.y();
217 const int cx = cursorSamp_.x();
218 const int cy = cursorSamp_.y();
219 if (px < cx) {
220 const int dx = cx - px;
221 const int dy = cy - py;
222 for (int x = px + 1; x <= cx; ++x)
223 sample_.at(x) = (x - px) * dy / dx + py;
224 }
225 else if (px == cx) {
226 sample_.at(cx) = cy;
227 }
228 else {
229 const int dx = px - cx;
230 const int dy = py - cy;
231 for (int x = cx; x < px; ++x)
232 sample_.at(x) = (x - cx) * dy / dx + cy;
233 }
234 prevPressedSamp_ = cursorSamp_;
235
236 if (drawMode_ == DrawMode::Direct) {
237 sendEditedSample();
238 }
239 else {
240 updateSampleView();
241 ui->sampleViewWidget->update();
242 }
243 }
244 break;
245 }
246 case QEvent::MouseButtonPress:
247 {
248 if (drawMode_ == DrawMode::Disabled) break;
249 sample_.at(cursorSamp_.x()) = cursorSamp_.y();
250 prevPressedSamp_ = cursorSamp_;
251
252 if (drawMode_ == DrawMode::Direct) {
253 sendEditedSample();
254 }
255 else {
256 updateSampleView();
257 ui->sampleViewWidget->update();
258 }
259 break;
260 }
261 case QEvent::MouseButtonRelease:
262 {
263 prevPressedSamp_.setX(-1); // Clear
264 break;
265 }
266 default:
267 break;
268 }
269 return false;
270 }
271
272 return false;
273 }
274
275 void ADPCMSampleEditor::dropEvent(QDropEvent* event)
276 {
277 importSampleFrom(reinterpret_cast<QDropEvent*>(event)->mimeData()->urls().first().toLocalFile());
278 }
279
280 void ADPCMSampleEditor::setInstrumentSampleParameters(int sampNum, bool repeatable, const SampleRepeatRange& repeatRange,
281 int rKeyNum, int rDeltaN, size_t start, size_t stop,
282 std::vector<uint8_t> sample)
283 {
284 Ui::EventGuard ev(isIgnoreEvent_);
285
286 ui->sampleNumSpinBox->setValue(sampNum);
287
288 updateUsersView();
289
290 ui->repeatCheckBox->setChecked(repeatable);
291 ui->repeatEndSpinBox->setMaximumByBytes(sample.size() - 1);
292 ui->repeatBeginSpinBox->setMaximumByBytes(sample.size() - 1);
293 ui->repeatEndSpinBox->setValueByAddress(repeatRange.last());
294 ui->repeatBeginSpinBox->setValueByAddress(repeatRange.first());
295 ui->rootKeyComboBox->setCurrentIndex(rKeyNum % 12);
296 ui->rootKeySpinBox->setValue(rKeyNum / 12);
297 ui->rootRateSpinBox->setValue(rDeltaN);
298
299 addrStart_ = start;
300 addrStop_ = stop;
301 updateSampleMemoryBar();
302 ui->memoryWidget->update();
303
304 if (!ui->action_Draw_Sample->isChecked()) {
305 size_t sampSize = sample.size() << 1;
306 sample_.resize(sampSize);
307 codec::ymb_decode(sample.data(), sample_.data(), static_cast<long>(sampSize));
308
309 // Slider settings
310 for (int z = 0, len = sampSize; ; ++z) {
311 int tmp = (len + 1) >> 1;
312 if (tmp < 2) {
313 zoom_ = z;
314 viewedSampLen_ = len;
315 ui->horizontalScrollBar->setMaximum(sampSize - len);
316 break;
317 }
318 else if (z == zoom_) {
319 viewedSampLen_ = len;
320 ui->horizontalScrollBar->setMaximum(sampSize - len);
321 break;
322 }
323 else {
324 len = tmp;
325 }
326 }
327 updateSampleView();
328 ui->sampleViewWidget->update();
329 }
330
331 ui->detailLabel->setText(updateDetailView());
332 }
333
334 void ADPCMSampleEditor::importSampleFrom(const QString file)
335 {
336 std::unique_ptr<io::WavContainer> wav;
337 try {
338 io::BinaryContainer container;
339 {
340 QFile fp(file);
341 if (!fp.open(QIODevice::ReadOnly)) {
342 FileIOErrorMessageBox::openError(file, true, io::FileType::WAV, this);
343 return;
344 }
345 QByteArray array = fp.readAll();
346 fp.close();
347 std::move(array.begin(), array.end(), std::back_inserter(container));
348 }
349
350 wav = std::make_unique<io::WavContainer>(container);
351 }
352 catch (io::FileIOError& e) {
353 FileIOErrorMessageBox(file, true, e, this).exec();
354 return;
355 }
356 catch (std::exception& e) {
357 FileIOErrorMessageBox(file, true, io::FileType::WAV, QString(e.what()), this).exec();
358 return;
359 }
360
361 if (wav->getSampleRate() < 2000 || 55466 < wav->getSampleRate()) {
362 FileIOErrorMessageBox(file, true, io::FileType::WAV,
363 tr("Supported sample rate is 2kHz-55.5kHz, but the rate of selected sample is %1.")
364 .arg(wav->getSampleRate()), this).exec();
365 return;
366 }
367
368 if (wav->getChannelCount() != 1) {
369 FileIOErrorMessageBox(file, true, io::FileType::WAV,
370 tr("The selected sample is not mono channel."), this).exec();
371 return;
372 }
373
374 io::BinaryContainer bc = wav->getSample();
375 size_t rawSize = bc.size() / 2;
376 std::vector<int16_t> raw(rawSize);
377 for (size_t i = 0; i < rawSize; ++i) {
378 raw[i] = bc.readInt16(i * 2);
379 }
380 std::vector<uint8_t> adpcm((raw.size() + 1) / 2);
381 codec::ymb_encode(raw.data(), adpcm.data(), static_cast<long>(raw.size()));
382
383 bt_.lock()->storeSampleADPCMRawSample(ui->sampleNumSpinBox->value(), adpcm);
384 ui->repeatCheckBox->setChecked(false);
385 ui->repeatBeginSpinBox->setMaximumByBytes(adpcm.size() - 1);
386 ui->repeatEndSpinBox->setMaximumByBytes(adpcm.size() - 1);
387 ui->repeatBeginSpinBox->setValueByBytes(0);
388 ui->repeatEndSpinBox->setValueByBytes(adpcm.size() - 1);
389 ui->rootKeyComboBox->setCurrentIndex(SampleADPCM::DEF_ROOT_KEY % 12);
390 ui->rootKeySpinBox->setValue(SampleADPCM::DEF_ROOT_KEY / 12);
391 ui->rootRateSpinBox->setValue(SampleADPCM::calculateADPCMDeltaN(wav->getSampleRate()));
392
393 emit modified();
394 emit sampleAssignRequested();
395 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
396 }
397
398 void ADPCMSampleEditor::updateSampleMemoryBar()
399 {
400 QRect bar = memPixmap_.rect();
401 if (!bar.isValid()) return;
402
403 QPainter painter(&memPixmap_);
404 painter.fillRect(bar, palette_->instADPCMMemBackColor);
405
406 double maxSize = bt_.lock()->getADPCMStoredSize() >> 5;
407 double limit = bt_.lock()->getADPCMLimit() >> 5; // By 32 bytes
408 QRectF allSamp(0, 0, std::max(1., bar.width() * (maxSize / limit)), rect().height());
409 painter.fillRect(allSamp, palette_->instADPCMMemAllColor);
410
411 if (addrStart_ || addrStart_ != addrStop_) {
412 QRectF curSamp(bar.width() * (addrStart_ / limit),
413 0, std::max(1., bar.width() * ((addrStop_ - addrStart_) / limit)), rect().height());
414 painter.fillRect(curSamp, palette_->instADPCMMemCurColor);
415 }
416 }
417
418 void ADPCMSampleEditor::updateSampleView()
419 {
420 QRect rect = sampViewPixmap_.rect();
421 if (!rect.isValid()) return;
422
423 QPainter painter(&sampViewPixmap_);
424 painter.fillRect(rect, palette_->instADPCMSampViewBackColor);
425
426 painter.setPen(palette_->instADPCMSampViewCenterColor);
427 const int maxX = rect.width();
428 const int centerY = rect.height() >> 1;
429 painter.drawLine(0, centerY, maxX, centerY);
430
431 QColor foreColor;
432 switch (drawMode_) {
433 case DrawMode::Disabled: foreColor = palette_->instADPCMSampViewForeColor; break;
434 case DrawMode::Normal: foreColor = palette_->instADPCMSampViewDrawColor; break;
435 case DrawMode::Direct: foreColor = palette_->instADPCMSampViewDirectDrawColor; break;
436 }
437 painter.setPen(foreColor);
438 const int16_t maxY = std::numeric_limits<int16_t>::max();
439 const size_t first = ui->horizontalScrollBar->value();
440 const bool showGrid = ui->action_Grid_View->isChecked();
441 const bool showRepeat = ui->repeatCheckBox->isChecked();
442 size_t repeatBegin = static_cast<size_t>(ui->repeatBeginSpinBox->value());
443 size_t repeatEnd = static_cast<size_t>(ui->repeatEndSpinBox->value());
444 if (maxX < viewedSampLen_) {
445 int prevY = centerY;
446 size_t g = first;
447 size_t prevp = -(viewedSampLen_ - 1) / (maxX - 1);
448 for (int x = 0; x < maxX; ++x) {
449 size_t i = (viewedSampLen_ - 1) * x / (maxX - 1);
450 size_t p = i + first;
451 int16_t sample = sample_.at(p);
452 int y = centerY - (centerY * sample / maxY);
453 if (showGrid && g <= p) {
454 painter.setPen(palette_->instADPCMSampViewGridColor);
455 painter.drawLine(x, 0, x, rect.height());
456 g = (g / gridIntr_ + 1) * gridIntr_;
457 painter.setPen(foreColor);
458 }
459 if (showRepeat) {
460 if (prevp < repeatBegin && repeatBegin <= p) {
461 painter.setPen(palette_->instADPCMSampViewRepeatBeginColor);
462 painter.drawLine(x, 0, x, rect.height());
463 painter.setPen(foreColor);
464 }
465 if (prevp < repeatEnd && repeatEnd <= p) {
466 painter.setPen(palette_->instADPCMSampViewRepeatEndColor);
467 painter.drawLine(x, 0, x, rect.height());
468 painter.setPen(foreColor);
469 }
470 }
471 if (x) painter.drawLine(x - 1, prevY, x, y);
472 prevY = y;
473 prevp = p;
474 }
475 }
476 else {
477 QPoint prev, p;
478 for (int i = 0; i < viewedSampLen_; ++i) {
479 p.setX((maxX - 1) * i / (viewedSampLen_ - 1));
480 size_t sampP = i + first;
481 int16_t sample = sample_[sampP];
482 p.setY(centerY - (centerY * sample / maxY));
483 if (showGrid && !(i % gridIntr_)) {
484 painter.setPen(palette_->instADPCMSampViewGridColor);
485 painter.drawLine(p.x(), 0, p.x(), rect.height());
486 painter.setPen(foreColor);
487 }
488 if (showRepeat) {
489 if (sampP == repeatBegin) {
490 painter.setPen(palette_->instADPCMSampViewRepeatBeginColor);
491 painter.drawLine(p.x(), 0, p.x(), rect.height());
492 painter.setPen(foreColor);
493 }
494 if (sampP == repeatEnd) {
495 painter.setPen(palette_->instADPCMSampViewRepeatEndColor);
496 painter.drawLine(p.x(), 0, p.x(), rect.height());
497 painter.setPen(foreColor);
498 }
499 }
500 if (p.x()) painter.drawLine(prev, p);
501 prev = p;
502 }
503 }
504 }
505
506 void ADPCMSampleEditor::updateUsersView()
507 {
508 std::multiset<int> users = bt_.lock()->getSampleADPCMUsers(ui->sampleNumSpinBox->value());
509 ui->usersLineEdit->setText(inst_edit_utils::generateUsersString(users));
510 }
511
512 void ADPCMSampleEditor::detectCursorSamplePosition(int cx, int cy)
513 {
514 const QRect& rect = ui->sampleViewWidget->rect();
515
516 // Detect x
517 const int w = rect.width();
518 if (viewedSampLen_ < w) {
519 const double segW = rect.width() / (viewedSampLen_ - 1.);
520 double th = segW / 2.;
521 for (int i = 0; i < viewedSampLen_; ++i, th += segW) {
522 if (cx < th) {
523 cursorSamp_.setX(ui->horizontalScrollBar->value() + i);
524 break;
525 }
526 }
527 }
528 else {
529 cursorSamp_.setX(
530 ui->horizontalScrollBar->value() + (viewedSampLen_ - 1) * utils::clamp(cx, 0, w - 1) / (w - 1));
531 }
532
533 // Detect y
534 const double centerY = rect.height() >> 1;
535 int y = std::numeric_limits<int16_t>::max() * (centerY - cy) / centerY;
536 cursorSamp_.setY(utils::clamp(y, static_cast<int>(std::numeric_limits<int16_t>::min()),
537 static_cast<int>(std::numeric_limits<int16_t>::max())));
538
539 // Update position view
540 ui->detailLabel->setText(updateDetailView());
541 }
542
543 void ADPCMSampleEditor::sendEditedSample()
544 {
545 std::vector<uint8_t> adpcm(sample_.size() >> 1);
546 codec::ymb_encode(sample_.data(), adpcm.data(), static_cast<long>(sample_.size()));
547 bt_.lock()->storeSampleADPCMRawSample(ui->sampleNumSpinBox->value(), std::move(adpcm));
548 emit modified();
549 emit sampleAssignRequested();
550 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
551 }
552
553 void ADPCMSampleEditor::onSampleNumberChanged()
554 {
555 updateUsersView();
556 }
557
558 void ADPCMSampleEditor::onSampleMemoryUpdated(size_t start, size_t stop)
559 {
560 addrStart_ = start;
561 addrStop_ = stop;
562 updateSampleMemoryBar();
563 ui->memoryWidget->update();
564 }
565
566 void ADPCMSampleEditor::onNoteNamesUpdated()
567 {
568 for (int i = 0; i < 12; ++i) {
569 ui->rootKeyComboBox->setItemText(i, NoteNameManager::getManager().getNoteName(i));
570 }
571 }
572
573 void ADPCMSampleEditor::on_sampleNumSpinBox_valueChanged(int arg1)
574 {
575 if (!isIgnoreEvent_) emit sampleNumberChanged(arg1); // Direct connection
576
577 onSampleNumberChanged();
578
579 updateSampleMemoryBar();
580 ui->memoryWidget->update();
581
582 updateSampleView();
583 ui->sampleViewWidget->update();
584 }
585
586 void ADPCMSampleEditor::on_repeatCheckBox_toggled(bool checked)
587 {
588 ui->repeatBeginSpinBox->setEnabled(checked);
589 ui->repeatEndSpinBox->setEnabled(checked);
590
591 if (!isIgnoreEvent_) {
592 bt_.lock()->setSampleADPCMRepeatEnabled(ui->sampleNumSpinBox->value(), checked);
593 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
594 emit modified();
595 }
596 }
597
598 void ADPCMSampleEditor::on_repeatBeginSpinBox_valueChanged(int)
599 {
600 if (isIgnoreEvent_) return;
601
602 int begin = ui->repeatBeginSpinBox->valueByAddress();
603
604 if (ui->repeatEndSpinBox->valueByAddress() < begin) {
605 ui->repeatEndSpinBox->setValueByAddress(begin);
606 }
607
608 bt_.lock()->setSampleADPCMRepeatRange(ui->sampleNumSpinBox->value(), SampleRepeatRange(begin, ui->repeatEndSpinBox->valueByAddress()));
609
610 updateSampleView();
611 ui->sampleViewWidget->update();
612 }
613
614
615 void ADPCMSampleEditor::on_repeatEndSpinBox_valueChanged(int)
616 {
617 if (isIgnoreEvent_) return;
618
619 int end = ui->repeatEndSpinBox->valueByAddress();
620
621 if (end < ui->repeatBeginSpinBox->valueByAddress()) {
622 ui->repeatBeginSpinBox->setValueByAddress(end);
623 }
624
625 bt_.lock()->setSampleADPCMRepeatRange(ui->sampleNumSpinBox->value(), SampleRepeatRange(ui->repeatBeginSpinBox->valueByAddress(), end));
626
627 updateSampleView();
628 ui->sampleViewWidget->update();
629 }
630
631 void ADPCMSampleEditor::on_rootRateSpinBox_valueChanged(int arg1)
632 {
633 ui->rootRateSpinBox->setSuffix(
634 QString(" (0x") + QString("%1 | ").arg(arg1, 3, 16, QChar('0')).toUpper()
635 + QString("%1Hz)").arg(QString::number(arg1 * 55500. * std::pow(2., -16.), 'f', 3)));
636
637 if (!isIgnoreEvent_) {
638 bt_.lock()->setSampleADPCMRootDeltaN(ui->sampleNumSpinBox->value(), arg1);
639 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
640 emit modified();
641 }
642 }
643
644 void ADPCMSampleEditor::on_action_Resize_triggered()
645 {
646 SampleLengthDialog dialog(sample_.size(), this);
647 if (dialog.exec() == QDialog::Accepted) {
648 ui->repeatBeginSpinBox->setMaximumByBytes((dialog.getLength() - 1) >> 1);
649 ui->repeatEndSpinBox->setMaximumByBytes((dialog.getLength() - 1) >> 1);
650
651 sample_.resize(dialog.getLength());
652 sendEditedSample();
653
654 updateSampleView();
655 ui->sampleViewWidget->update();
656 }
657 }
658
659 void ADPCMSampleEditor::on_actionRe_verse_triggered()
660 {
661 std::reverse(sample_.begin(), sample_.end());
662 sendEditedSample();
663
664 updateSampleView();
665 ui->sampleViewWidget->update();
666 }
667
668 void ADPCMSampleEditor::on_actionZoom_In_triggered()
669 {
670 int len = (viewedSampLen_ + 1) >> 1;
671 if (len > 1) {
672 ++zoom_;
673 viewedSampLen_ = len;
674 ui->horizontalScrollBar->setMaximum(sample_.size() - len);
675 updateSampleView();
676 ui->sampleViewWidget->update();
677
678 ui->detailLabel->setText(updateDetailView());
679 }
680 }
681
682 void ADPCMSampleEditor::on_actionZoom_Out_triggered()
683 {
684 if (zoom_) {
685 --zoom_;
686 viewedSampLen_ = sample_.size() >> zoom_;
687 ui->horizontalScrollBar->setMaximum(sample_.size() - viewedSampLen_);
688 updateSampleView();
689 ui->sampleViewWidget->update();
690
691 ui->detailLabel->setText(updateDetailView());
692 }
693 }
694
695 void ADPCMSampleEditor::on_horizontalScrollBar_valueChanged(int)
696 {
697 updateSampleView();
698 ui->sampleViewWidget->update();
699 }
700
701 void ADPCMSampleEditor::on_action_Import_triggered()
702 {
703 QString dir = QString::fromStdString(config_.lock()->getWorkingDirectory());
704 QString file = QFileDialog::getOpenFileName(this, tr("Import sample"),
705 (dir.isEmpty() ? "./" : dir),
706 tr("WAV signed 16-bit PCM (*.wav)") + ";;" + tr("All files (*)"), nullptr
707 #if defined(Q_OS_LINUX) || (defined(Q_OS_BSD4) && !defined(Q_OS_DARWIN))
708 , QFileDialog::DontUseNativeDialog
709 #endif
710 );
711 if (file.isNull()) return;
712
713 importSampleFrom(file);
714 }
715
716 void ADPCMSampleEditor::on_action_Clear_triggered()
717 {
718 bt_.lock()->clearSampleADPCMRawSample(ui->sampleNumSpinBox->value());
719
720 updateSampleMemoryBar();
721 ui->memoryWidget->update();
722
723 emit modified();
724
725 if (config_.lock()->getWriteOnlyUsedSamples()) {
726 emit sampleAssignRequested();
727 }
728 else {
729 emit sampleMemoryChanged();
730 }
731
732 emit sampleParameterChanged(ui->sampleNumSpinBox->value());
733 }
734
735 void ADPCMSampleEditor::on_action_Grid_View_triggered()
736 {
737 updateSampleView();
738 ui->sampleViewWidget->update();
739 }
740
741 void ADPCMSampleEditor::on_actionG_rid_Settings_triggered()
742 {
743 GridSettingsDialog dialog(gridIntr_, this);
744 if (dialog.exec() == QDialog::Accepted) {
745 gridIntr_ = dialog.getInterval();
746 updateSampleView();
747 ui->sampleViewWidget->update();
748 }
749 }
750
751 void ADPCMSampleEditor::on_action_Draw_Sample_triggered(bool checked)
752 {
753 if (checked) {
754 ui->actionDirec_t_Draw->setChecked(false);
755 drawMode_ = DrawMode::Normal;
756 }
757 else {
758 if (drawMode_ == DrawMode::Normal) drawMode_ = DrawMode::Disabled;
759 sendEditedSample(); // Convert sample
760 }
761 updateSampleView();
762 }
763
764 void ADPCMSampleEditor::on_actionDirec_t_Draw_triggered(bool checked)
765 {
766 if (checked) {
767 ui->action_Draw_Sample->setChecked(false);
768 if (drawMode_ == DrawMode::Normal) sendEditedSample(); // Convert sample
769 drawMode_ = DrawMode::Direct;
770 }
771 else {
772 if (drawMode_ == DrawMode::Direct) drawMode_ = DrawMode::Disabled;
773 }
774 updateSampleView();
775 }