From b374116d35b08e73d10df48e5643d357b49988b6 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 17 Dec 2022 17:28:06 +0100 Subject: [PATCH] Implemented persisting of channel/note for each pad --- DrumMachine.pro | 2 ++ drummachinesettings.cpp | 21 +++++++++++++++++++++ drummachinesettings.h | 15 +++++++++++++++ mainwindow.cpp | 9 +++++++++ mainwindow.h | 4 ++++ presetsmodel.cpp | 12 +++++++++++- sampleswidget.cpp | 24 ++++++++---------------- sampleswidget.h | 3 +++ samplewidget.cpp | 15 +++++++++++++++ samplewidget.h | 10 ++++++++++ synthisizer.cpp | 4 ++++ synthisizer.h | 4 ++++ 12 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 drummachinesettings.cpp create mode 100644 drummachinesettings.h diff --git a/DrumMachine.pro b/DrumMachine.pro index 4e9ce6e..8d3d194 100755 --- a/DrumMachine.pro +++ b/DrumMachine.pro @@ -15,6 +15,7 @@ SOURCES += \ audioformat.cpp \ audioplayer.cpp \ djwidget.cpp \ + drummachinesettings.cpp \ filesmodel.cpp \ graphrenderer.cpp \ jsonconverters.cpp \ @@ -39,6 +40,7 @@ HEADERS += \ audioformat.h \ audioplayer.h \ djwidget.h \ + drummachinesettings.h \ filesmodel.h \ graphrenderer.h \ jsonconverters.h \ diff --git a/drummachinesettings.cpp b/drummachinesettings.cpp new file mode 100644 index 0000000..ed88960 --- /dev/null +++ b/drummachinesettings.cpp @@ -0,0 +1,21 @@ +#include "drummachinesettings.h" + +quint8 DrumMachineSettings::padChannel(quint8 pad) const +{ + return value(QString{"pad%0/channel"}.arg(pad)).toUInt(); +} + +void DrumMachineSettings::setPadChannel(quint8 pad, quint8 channel) +{ + setValue(QString{"pad%0/channel"}.arg(pad), channel); +} + +quint8 DrumMachineSettings::padNote(quint8 pad) const +{ + return value(QString{"pad%0/note"}.arg(pad)).toUInt(); +} + +void DrumMachineSettings::setPadNote(quint8 pad, quint8 note) +{ + setValue(QString{"pad%0/note"}.arg(pad), note); +} diff --git a/drummachinesettings.h b/drummachinesettings.h new file mode 100644 index 0000000..5ac8ade --- /dev/null +++ b/drummachinesettings.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class DrumMachineSettings : public QSettings +{ +public: + using QSettings::QSettings; + + quint8 padChannel(quint8 pad) const; + void setPadChannel(quint8 pad, quint8 channel); + + quint8 padNote(quint8 pad) const; + void setPadNote(quint8 pad, quint8 note); +}; diff --git a/mainwindow.cpp b/mainwindow.cpp index 15355be..86b3668 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -101,6 +101,7 @@ MainWindow::MainWindow(const presets::PresetsConfig &presetsConfig, QWidget *par connect(m_ui->pushButtonAudioDevice, &QAbstractButton::pressed, this, &MainWindow::openAudioDevice); m_presetsProxyModel.setFilterCaseSensitivity(Qt::CaseInsensitive); + m_presetsProxyModel.setSortRole(Qt::EditRole); m_presetsProxyModel.setSourceModel(&m_presetsModel); m_ui->presetsView->setModel(&m_presetsProxyModel); @@ -111,6 +112,8 @@ MainWindow::MainWindow(const presets::PresetsConfig &presetsConfig, QWidget *par m_ui->filesView->setModel(&m_filesModel); connect(m_ui->presetsView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &MainWindow::currentRowChanged); + + loadSettings(); } MainWindow::~MainWindow() @@ -251,3 +254,9 @@ void MainWindow::updateAudioDevices() m_ui->comboBoxAudioDevice->addItem(info->name); } } + +void MainWindow::loadSettings() +{ + m_synthisizer.loadSettings(m_settings); + m_ui->samplesWidget->loadSettings(m_settings); +} diff --git a/mainwindow.h b/mainwindow.h index 883d6d5..e18713e 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -13,6 +13,7 @@ #include "filesmodel.h" #include "midiinwrapper.h" #include "synthisizer.h" +#include "drummachinesettings.h" namespace Ui { class MainWindow; } namespace presets { struct PresetsConfig; } @@ -38,9 +39,12 @@ private slots: private: void updateMidiDevices(); void updateAudioDevices(); + void loadSettings(); const std::unique_ptr m_ui; + DrumMachineSettings m_settings; + std::unique_ptr m_paStream; MidiInWrapper m_midiIn; diff --git a/presetsmodel.cpp b/presetsmodel.cpp index 3e89fab..0c7e5d2 100755 --- a/presetsmodel.cpp +++ b/presetsmodel.cpp @@ -140,7 +140,17 @@ QVariant PresetsModel::data(const QModelIndex &index, int role) const switch (index.column()) { - case ColumnId: return handleData(preset.id); + case ColumnId: + { + if (preset.id) + { + bool ok; + if (auto id = preset.id->toInt(&ok); ok) + return id; + } + + return handleData(preset.id); + } case ColumnName: return handleData(preset.name); case ColumnAuthor: return handleData(preset.author); case ColumnOrderBy: return handleData(preset.orderBy); diff --git a/sampleswidget.cpp b/sampleswidget.cpp index bf18b9b..f9be185 100755 --- a/sampleswidget.cpp +++ b/sampleswidget.cpp @@ -20,31 +20,23 @@ SamplesWidget::SamplesWidget(QWidget *parent) : connect(m_ui->pushButtonStopAll, &QAbstractButton::pressed, this, &SamplesWidget::stopAll); + quint8 padNr{}; for (SampleWidget &widget : getWidgets()) { + widget.setPadNr(padNr++); widget.injectNetworkAccessManager(m_networkAccessManager); connect(&widget, &SampleWidget::chokeTriggered, this, &SamplesWidget::chokeTriggered); } - - m_ui->sampleWidget_1->setNote(48); - m_ui->sampleWidget_2->setNote(50); - m_ui->sampleWidget_3->setNote(52); - m_ui->sampleWidget_4->setNote(53); - m_ui->sampleWidget_5->setNote(55); - m_ui->sampleWidget_6->setNote(57); - m_ui->sampleWidget_7->setNote(59); - m_ui->sampleWidget_8->setNote(60); - m_ui->sampleWidget_9->setNote(62); - m_ui->sampleWidget_10->setNote(64); - m_ui->sampleWidget_11->setNote(65); - m_ui->sampleWidget_12->setNote(67); - m_ui->sampleWidget_22->setNote(69); - m_ui->sampleWidget_23->setNote(71); - m_ui->sampleWidget_24->setNote(72); } SamplesWidget::~SamplesWidget() = default; +void SamplesWidget::loadSettings(DrumMachineSettings &settings) +{ + for (SampleWidget &widget : getWidgets()) + widget.loadSettings(settings); +} + void SamplesWidget::setPreset(const presets::Preset &preset) { m_preset = preset; diff --git a/sampleswidget.h b/sampleswidget.h index d5ffda8..3933f10 100755 --- a/sampleswidget.h +++ b/sampleswidget.h @@ -14,6 +14,7 @@ namespace Ui { class SamplesWidget; } namespace midi { class MidiMessage; } class SampleWidget; +class DrumMachineSettings; class SamplesWidget : public QWidget { @@ -23,6 +24,8 @@ public: explicit SamplesWidget(QWidget *parent = nullptr); ~SamplesWidget() override; + void loadSettings(DrumMachineSettings &settings); + void setPreset(const presets::Preset &preset); void messageReceived(const midi::MidiMessage &message); diff --git a/samplewidget.cpp b/samplewidget.cpp index 52d92d9..3d3b045 100755 --- a/samplewidget.cpp +++ b/samplewidget.cpp @@ -10,6 +10,7 @@ #include #include "audiodecoder.h" +#include "drummachinesettings.h" namespace { QString toString(QString value) { return value; } @@ -37,6 +38,14 @@ SampleWidget::SampleWidget(QWidget *parent) : SampleWidget::~SampleWidget() = default; +void SampleWidget::loadSettings(DrumMachineSettings &settings) +{ + m_ui->channelSpinBox->setValue(settings.padChannel(m_padNr)); + m_ui->noteSpinBox->setValue(settings.padNote(m_padNr)); + + m_settings = &settings; +} + void SampleWidget::setFile(const QString &presetId, const presets::File &file) { m_presetId = presetId; @@ -78,6 +87,9 @@ quint8 SampleWidget::channel() const void SampleWidget::setChannel(quint8 channel) { m_ui->channelSpinBox->setValue(channel); + + if (m_settings) + m_settings->setPadChannel(m_padNr, channel); } quint8 SampleWidget::note() const @@ -88,6 +100,9 @@ quint8 SampleWidget::note() const void SampleWidget::setNote(quint8 note) { m_ui->noteSpinBox->setValue(note); + + if (m_settings) + m_settings->setPadNote(m_padNr, note); } int SampleWidget::speed() const diff --git a/samplewidget.h b/samplewidget.h index 9f7470e..a738c99 100755 --- a/samplewidget.h +++ b/samplewidget.h @@ -13,6 +13,7 @@ class QNetworkAccessManager; class QNetworkReply; class QAudioBuffer; class AudioDecoder; +class DrumMachineSettings; class SampleWidget : public QFrame { @@ -22,6 +23,11 @@ public: explicit SampleWidget(QWidget *parent = nullptr); ~SampleWidget() override; + quint8 padNr() const { return m_padNr; } + void setPadNr(quint8 padNr) { m_padNr = padNr; } + + void loadSettings(DrumMachineSettings &settings); + void setFile(const QString &presetId, const presets::File &file); quint8 channel() const; @@ -66,6 +72,8 @@ private: const std::unique_ptr m_ui; + DrumMachineSettings *m_settings{}; + std::shared_ptr m_reply; std::unique_ptr m_decoder; @@ -77,6 +85,8 @@ private: QNetworkAccessManager *m_networkAccessManager{}; + quint8 m_padNr{}; + bool m_learning{}; QColor m_oldColor; QBrush m_oldBrush; diff --git a/synthisizer.cpp b/synthisizer.cpp index 1021388..f740133 100644 --- a/synthisizer.cpp +++ b/synthisizer.cpp @@ -6,6 +6,10 @@ constexpr double pi = std::acos(-1); +void Synthisizer::loadSettings(const DrumMachineSettings &settings) +{ +} + void Synthisizer::writeSamples(frame_t *begin, frame_t *end) { const auto frequency = m_frequency; diff --git a/synthisizer.h b/synthisizer.h index 948bc15..4d8a400 100644 --- a/synthisizer.h +++ b/synthisizer.h @@ -4,9 +4,13 @@ namespace midi { class MidiMessage; } +class DrumMachineSettings; + class Synthisizer { public: + void loadSettings(const DrumMachineSettings &settings); + void setFrequency(int16_t frequency) { m_frequency = frequency; } void writeSamples(frame_t *begin, frame_t *end);