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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
/* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
* Website: https://bitbucket.org/kylearan/tiatracker
* Email: andre.wichmann@gmx.de
* See the file "license.txt" for information on usage and redistribution
* of this file.
*/
#include "instrumentstab.h"
#include <QComboBox>
#include <QLineEdit>
#include <cassert>
#include <QLabel>
#include <QSpinBox>
#include "envelopeshaper.h"
#include <QMessageBox>
#include <iostream>
#include <QFileDialog>
#include <QStringList>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include "mainwindow.h"
const QList<TiaSound::Distortion> InstrumentsTab::availableWaveforms{
TiaSound::Distortion::BUZZY,
TiaSound::Distortion::BUZZY_RUMBLE,
TiaSound::Distortion::FLANGY_WAVERING,
TiaSound::Distortion::PURE_HIGH,
TiaSound::Distortion::PURE_BUZZY,
TiaSound::Distortion::REEDY_RUMBLE,
TiaSound::Distortion::WHITE_NOISE,
TiaSound::Distortion::PURE_LOW,
TiaSound::Distortion::ELECTRONIC_RUMBLE,
TiaSound::Distortion::ELECTRONIC_SQUEAL,
TiaSound::Distortion::PURE_COMBINED
};
/*************************************************************************/
InstrumentsTab::InstrumentsTab(QWidget *parent) : QWidget(parent)
{
}
/*************************************************************************/
void InstrumentsTab::registerTrack(Track::Track *newTrack) {
pTrack = newTrack;
}
/*************************************************************************/
void InstrumentsTab::initInstrumentsTab() {
// Instrument names
QComboBox *cbInstruments = findChild<QComboBox *>("comboBoxInstruments");
cbInstruments->lineEdit()->setMaxLength(maxInstrumentNameLength);
foreach(Track::Instrument ins, pTrack->instruments) {
cbInstruments->addItem(ins.name);
}
// Instrument waveforms
QComboBox *cbWaveforms = findChild<QComboBox *>("comboBoxWaveforms");
foreach (TiaSound::Distortion distortion, availableWaveforms) {
cbWaveforms->addItem(TiaSound::getDistortionName(distortion));
}
cbWaveforms->setCurrentIndex(10); // Init to PURE_COMBINED
// Volume shaper
EnvelopeShaper *vs = findChild<EnvelopeShaper *>("volumeShaper");
vs->registerInstrument(&(pTrack->instruments[0]));
vs->name = "Volume";
vs->setScale(0, 15);
vs->setValues(&(pTrack->instruments[0].volumes));
// Frequency shaper
EnvelopeShaper *fs = findChild<EnvelopeShaper *>("frequencyShaper");
fs->registerInstrument(&(pTrack->instruments[0]));
fs->name = "Frequency";
fs->setScale(-8, 7);
fs->isInverted = true;
fs->setValues(&(pTrack->instruments[0].frequencies));
}
/*************************************************************************/
void InstrumentsTab::updateInstrumentsTab() {
assert(pTrack != nullptr);
/* Global values */
// Number of envelope frames used
QLabel *lWaveformsUsed = findChild<QLabel *>("labelWaveformFramesUsed");
int framesUsed = pTrack->getNumUsedEnvelopeFrames();
QString framesUsedString;
if (framesUsed < 256) {
framesUsedString = "(" + QString::number(framesUsed) + " of 256 used)";
} else {
framesUsedString = "<font color=\"#dc322f\">(" + QString::number(framesUsed) + " of 256 used)</>";
}
lWaveformsUsed->setText(framesUsedString);
// Number of instruments used
QLabel *lInstrumentsUsed = findChild<QLabel *>("labelInstrumentsUsed");
int instrumentsUsed = pTrack->getNumUsedInstruments();
QString instrumentsUsedString;
if (instrumentsUsed <= 7) {
instrumentsUsedString = "(" + QString::number(instrumentsUsed) + " of 7 used)";
} else {
instrumentsUsedString = "<font color=\"#dc322f\">(" + QString::number(instrumentsUsed) + " of 7 used)</>";
}
lInstrumentsUsed->setText(instrumentsUsedString);
// Names
QComboBox *cbInstruments = findChild<QComboBox *>("comboBoxInstruments");
for (int ins = 0; ins < pTrack->numInstruments; ++ins) {
cbInstruments->setItemText(ins, pTrack->instruments[ins].name);
}
/* Values specific to the selected intrument */
int iCurInstrument = getSelectedInstrumentIndex();
QLabel *lInstrumentNumber = findChild<QLabel *>("labelInstrumentNumber");
lInstrumentNumber->setText("Instrument " + QString::number(iCurInstrument + 1));
Track::Instrument& curInstrument = pTrack->instruments[iCurInstrument];
// Envelope length
QSpinBox *spEnvelopeLength = findChild<QSpinBox *>("spinBoxInstrumentEnvelopeLength");
int envelopeLength = curInstrument.getEnvelopeLength();
spEnvelopeLength->setValue(envelopeLength);
// Sustain and release start values
QSpinBox *spSustainStart = findChild<QSpinBox *>("spinBoxSustainStart");
int sustainStart = curInstrument.getSustainStart();
spSustainStart->setValue(sustainStart + 1);
QSpinBox *spReleaseStart = findChild<QSpinBox *>("spinBoxReleaseStart");
int releaseStart = curInstrument.getReleaseStart();
spReleaseStart->setValue(releaseStart + 1);
// Peak volume
QSpinBox *spPeakVolume = findChild<QSpinBox *>("spinBoxInstrumentVolume");
int maxVolume = curInstrument.getMaxVolume();
spPeakVolume->setValue(maxVolume);
// Base waveform
TiaSound::Distortion curDistortion = curInstrument.baseDistortion;
int iWaveform = availableWaveforms.indexOf(curDistortion);
assert(iWaveform != -1);
QComboBox *cbWaveforms = findChild<QComboBox *>("comboBoxWaveforms");
cbWaveforms->setCurrentIndex(iWaveform);
emit setWaveform(curInstrument.baseDistortion);
// EnvelopeShaper sizes and values
EnvelopeShaper *wsVolume = findChild<EnvelopeShaper *>("volumeShaper");
wsVolume->registerInstrument(&curInstrument);
wsVolume->setValues(&(curInstrument.volumes));
wsVolume->updateSize();
EnvelopeShaper *wsFrequency = findChild<EnvelopeShaper *>("frequencyShaper");
wsFrequency->registerInstrument(&curInstrument);
wsFrequency->setValues(&(curInstrument.frequencies));
wsFrequency->updateSize();
}
/*************************************************************************/
int InstrumentsTab::getSelectedInstrumentIndex() {
QComboBox *cbInstruments = findChild<QComboBox *>("comboBoxInstruments");
return cbInstruments->currentIndex();
}
/*************************************************************************/
Track::Instrument * InstrumentsTab::getSelectedInstrument() {
int iCurInstrument = getSelectedInstrumentIndex();
Track::Instrument *curInstrument = &(pTrack->instruments[iCurInstrument]);
return curInstrument;
}
/*************************************************************************/
void InstrumentsTab::on_buttonInstrumentDelete_clicked() {
Track::Instrument *curInstrument = getSelectedInstrument();
bool doDelete = true;
if (!curInstrument->isEmpty()) {
QMessageBox msgBox(QMessageBox::NoIcon,
"Delete Instrument",
"Do you really want to delete this instument?",
QMessageBox::Yes | QMessageBox::No, this,
Qt::FramelessWindowHint);
int reply = msgBox.exec();
if (reply != QMessageBox::Yes) {
doDelete = false;
}
}
if (doDelete) {
pTrack->lock();
curInstrument->deleteInstrument();
pTrack->unlock();
updateInstrumentsTab();
update();
}
}
/*************************************************************************/
void InstrumentsTab::on_buttonInstrumentExport_clicked() {
Track::Instrument *curInstrument = getSelectedInstrument();
if (curInstrument->isEmpty()) {
return;
}
// Ask for filename
QFileDialog dialog(this);
dialog.setDirectory(curInstrumentsDialogPath);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter("*.tti");
dialog.setDefaultSuffix("tti");
dialog.setViewMode(QFileDialog::Detail);
dialog.selectFile(curInstrument->name);
QStringList fileNames;
if (dialog.exec()) {
fileNames = dialog.selectedFiles();
}
if (fileNames.isEmpty()) {
return;
}
QString fileName = fileNames[0];
curInstrumentsDialogPath = dialog.directory().absolutePath();
QFile saveFile(fileName);
// Export instrument
if (!saveFile.open(QIODevice::WriteOnly)) {
MainWindow::displayMessage("Unable to open file!");
return;
}
QJsonObject insObject;
curInstrument->toJson(insObject);
QJsonDocument saveDoc(insObject);
saveFile.write(saveDoc.toJson());
saveFile.close();
}
/*************************************************************************/
void InstrumentsTab::on_buttonInstrumentImport_clicked() {
Track::Instrument *curInstrument = getSelectedInstrument();
// Ask if instrument should really be overwritten
bool doImport = true;
if (!curInstrument->isEmpty()) {
QMessageBox msgBox(QMessageBox::NoIcon,
"Import Instrument",
"Do you really want to overwrite the current instument?",
QMessageBox::Yes | QMessageBox::No, this,
Qt::FramelessWindowHint);
int reply = msgBox.exec();
if (reply != QMessageBox::Yes) {
doImport = false;
}
}
if (!doImport) {
return;
}
// Ask for filename
QFileDialog dialog(this);
dialog.setDirectory(curInstrumentsDialogPath);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter("*.tti");
dialog.setDefaultSuffix("tti");
dialog.setViewMode(QFileDialog::Detail);
QStringList fileNames;
if (dialog.exec()) {
fileNames = dialog.selectedFiles();
}
if (fileNames.isEmpty()) {
return;
}
QString fileName = fileNames[0];
curInstrumentsDialogPath = dialog.directory().absolutePath();
QFile loadFile(fileName);
if (!loadFile.open(QIODevice::ReadOnly)) {
MainWindow::displayMessage("Unable to open file!");
return;
}
QJsonDocument loadDoc(QJsonDocument::fromJson(loadFile.readAll()));
// Parse in data
if (!curInstrument->import(loadDoc.object())) {
MainWindow::displayMessage("Unable to parse instrument!");
return;
}
// Update display
updateInstrumentsTab();
update();
}
/*************************************************************************/
void InstrumentsTab::on_spinBoxInstrumentEnvelopeLength_editingFinished() {
QSpinBox *sb = findChild<QSpinBox *>("spinBoxInstrumentEnvelopeLength");
int newLength = sb->value();
Track::Instrument *curInstrument = getSelectedInstrument();
pTrack->lock();
curInstrument->setEnvelopeLength(newLength);
pTrack->unlock();
updateInstrumentsTab();
update();
}
void InstrumentsTab::on_spinBoxInstrumentEnvelopeLength_valueChanged(int newLength) {
Track::Instrument *curInstrument = getSelectedInstrument();
if (std::abs(newLength - curInstrument->getEnvelopeLength()) == 1) {
on_spinBoxInstrumentEnvelopeLength_editingFinished();
}
}
/*************************************************************************/
void InstrumentsTab::on_spinBoxSustainStart_editingFinished() {
QSpinBox *sb = findChild<QSpinBox *>("spinBoxSustainStart");
int newStart = sb->value() - 1;
Track::Instrument *curInstrument = getSelectedInstrument();
if (newStart < curInstrument->getReleaseStart()) {
// valid new value
curInstrument->setSustainAndRelease(newStart, curInstrument->getReleaseStart());
} else {
// invalid new value. Try to push release start
int newRelease = newStart + 1;
if (newRelease < curInstrument->getEnvelopeLength()) {
curInstrument->setSustainAndRelease(newStart, newRelease);
} else {
// Release start cannot be pushed ahead, so reject new sustain value
sb->setValue(curInstrument->getSustainStart() + 1);
}
}
updateInstrumentsTab();
update();
}
void InstrumentsTab::on_spinBoxSustainStart_valueChanged(int newStart) {
newStart--;
Track::Instrument *curInstrument = getSelectedInstrument();
if (std::abs(newStart - curInstrument->getSustainStart()) == 1) {
on_spinBoxSustainStart_editingFinished();
}
}
/*************************************************************************/
void InstrumentsTab::on_spinBoxReleaseStart_editingFinished() {
QSpinBox *sb = findChild<QSpinBox *>("spinBoxReleaseStart");
int newStart = sb->value() - 1;
Track::Instrument *curInstrument = getSelectedInstrument();
if (newStart < curInstrument->getEnvelopeLength()
&& newStart > curInstrument->getSustainStart()) {
// valid new value
curInstrument->setSustainAndRelease(curInstrument->getSustainStart(), newStart);
} else {
// invalid new value
sb->setValue(curInstrument->getReleaseStart() + 1);
}
updateInstrumentsTab();
update();
}
void InstrumentsTab::on_spinBoxReleaseStart_valueChanged(int newStart) {
newStart--;
Track::Instrument *curInstrument = getSelectedInstrument();
if (std::abs(newStart - curInstrument->getReleaseStart()) == 1) {
on_spinBoxReleaseStart_editingFinished();
}
}
/*************************************************************************/
void InstrumentsTab::on_spinBoxInstrumentVolume_editingFinished() {
QSpinBox *sb = findChild<QSpinBox *>("spinBoxInstrumentVolume");
int newVolume = sb->value();
Track::Instrument *curInstrument = getSelectedInstrument();
int curMin = curInstrument->getMinVolume();
int curMax = curInstrument->getMaxVolume();
int curVolumeSpan = curMax - curMin;
if (newVolume - curVolumeSpan >= 0) {
// Shift volumes
int volumeShift = newVolume - curMax;
for (int i = 0; i < curInstrument->getEnvelopeLength(); ++i) {
curInstrument->volumes[i] += volumeShift;
}
} else {
// Invalid value: Set volume to current max
sb->setValue(curInstrument->getMaxVolume());
}
updateInstrumentsTab();
update();
}
void InstrumentsTab::on_spinBoxInstrumentVolume_valueChanged(int newVolume) {
Track::Instrument *curInstrument = getSelectedInstrument();
if (std::abs(newVolume - curInstrument->getMaxVolume()) == 1) {
on_spinBoxInstrumentVolume_editingFinished();
}
}
/*************************************************************************/
void InstrumentsTab::on_comboBoxWaveforms_currentIndexChanged(int index) {
Track::Instrument *curInstrument = getSelectedInstrument();
TiaSound::Distortion newDistortion = availableWaveforms[index];
curInstrument->baseDistortion = newDistortion;
updateInstrumentsTab();
update();
}
/*************************************************************************/
void InstrumentsTab::on_comboBoxInstruments_currentIndexChanged(int) {
updateInstrumentsTab();
update();
}
void InstrumentsTab::on_comboBoxInstruments_currentTextChanged(const QString &text) {
Track::Instrument *curInstrument = getSelectedInstrument();
curInstrument->name = text;
updateInstrumentsTab();
update();
}
/*************************************************************************/
|