drumstick  2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
sonivoxsettingsdialog.cpp
Go to the documentation of this file.
1 /*
2  Virtual Piano test using the MIDI Sequencer C++ library
3  Copyright (C) 2006-2023, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <QDialogButtonBox>
20 #include <QPushButton>
21 #include <QMessageBox>
22 #include <QDir>
23 #include <QStandardPaths>
24 #include <QFileDialog>
25 
26 #include "sonivoxsettingsdialog.h"
27 #include "ui_sonivoxsettingsdialog.h"
30 
36 namespace drumstick { namespace widgets {
37 
38 const QString SonivoxSettingsDialog::QSTR_PREFERENCES = QStringLiteral("SonivoxEAS");
39 const QString SonivoxSettingsDialog::QSTR_BUFFERTIME = QStringLiteral("BufferTime");
40 const QString SonivoxSettingsDialog::QSTR_REVERBTYPE = QStringLiteral("ReverbType");
41 const QString SonivoxSettingsDialog::QSTR_REVERBAMT = QStringLiteral("ReverbAmt");
42 const QString SonivoxSettingsDialog::QSTR_CHORUSTYPE = QStringLiteral("ChorusType");
43 const QString SonivoxSettingsDialog::QSTR_CHORUSAMT = QStringLiteral("ChorusAmt");
44 const QString SonivoxSettingsDialog::QSTR_SOUNDFONT = QStringLiteral("InstrumentsDefinition");
45 const QString SonivoxSettingsDialog::QSTR_DATADIR = QStringLiteral("soundfonts");
46 const QString SonivoxSettingsDialog::QSTR_DATADIR2 = QStringLiteral("sounds/sf2");
47 
48 SonivoxSettingsDialog::SonivoxSettingsDialog(QWidget *parent) :
49  QDialog(parent),
50  ui(new Ui::SonivoxSettingsDialog)
51 {
52  ui->setupUi(this);
53  ui->combo_Reverb->addItem(QStringLiteral("Large Hall"), 0);
54  ui->combo_Reverb->addItem(QStringLiteral("Hall"), 1);
55  ui->combo_Reverb->addItem(QStringLiteral("Chamber"), 2);
56  ui->combo_Reverb->addItem(QStringLiteral("Room"), 3);
57  ui->combo_Reverb->addItem(QStringLiteral("None"), -1);
58  ui->combo_Reverb->setCurrentIndex(4);
59 
60  ui->combo_Chorus->addItem(QStringLiteral("Preset 1"), 0);
61  ui->combo_Chorus->addItem(QStringLiteral("Preset 2"), 1);
62  ui->combo_Chorus->addItem(QStringLiteral("Preset 3"), 2);
63  ui->combo_Chorus->addItem(QStringLiteral("Preset 4"), 3);
64  ui->combo_Chorus->addItem(QStringLiteral("None"), -1);
65  ui->combo_Chorus->setCurrentIndex(4);
66  connect(ui->btn_soundfont, &QToolButton::clicked, this, &SonivoxSettingsDialog::showFileDialog);
67  connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::pressed,
68  this, &SonivoxSettingsDialog::restoreDefaults);
69 
71  m_driver = man.outputBackendByName("SonivoxEAS");
72  //qDebug() << Q_FUNC_INFO;
73 }
74 
75 SonivoxSettingsDialog::~SonivoxSettingsDialog()
76 {
77  //qDebug() << Q_FUNC_INFO;
78  if (m_driver != nullptr) {
79  m_driver->close();
80  }
81  delete ui;
82 }
83 
84 void SonivoxSettingsDialog::accept()
85 {
86  //qDebug() << Q_FUNC_INFO;
87  writeSettings();
88  if (m_driver != nullptr) {
89  QString title;
90  QVariant varStatus = m_driver->property("status");
91  if (varStatus.isValid()) {
92  title = varStatus.toBool() ? tr("Sonivox Initialized") : tr("Sonivox Initialization Failed");
93  QVariant varDiag = m_driver->property("diagnostics");
94  if (varDiag.isValid()) {
95  QString text = varDiag.toStringList().join(QChar::LineFeed).trimmed();
96  if (varStatus.toBool()) {
97  if (!text.isEmpty()) {
98  QMessageBox::information(this, title, text);
99  }
100  } else {
101  QMessageBox::critical(this, title, text);
102  return;
103  }
104  }
105  }
106  }
107  QDialog::accept();
108 }
109 
110 void SonivoxSettingsDialog::showEvent(QShowEvent *event)
111 {
112  //qDebug() << Q_FUNC_INFO;
113  readSettings();
114  event->accept();
115 }
116 
117 void SonivoxSettingsDialog::readSettings()
118 {
119  //qDebug() << Q_FUNC_INFO;
120  SettingsFactory settings;
121  settings->beginGroup(QSTR_PREFERENCES);
122  int bufferTime = settings->value(QSTR_BUFFERTIME, 30).toInt();
123  int reverbType = settings->value(QSTR_REVERBTYPE, 1).toInt();
124  int reverbAmt = settings->value(QSTR_REVERBAMT, 25800).toInt();
125  int chorusType = settings->value(QSTR_CHORUSTYPE, -1).toInt();
126  int chorusAmt = settings->value(QSTR_CHORUSAMT, 0).toInt();
127  QString soundfont = settings->value(QSTR_SOUNDFONT, QString()).toString();
128  settings->endGroup();
129 
130  if (qEnvironmentVariableIsSet("PULSE_LATENCY_MSEC")) {
131  bufferTime = qEnvironmentVariableIntValue("PULSE_LATENCY_MSEC");
132  }
133 
134  ui->spnTime->setValue(bufferTime);
135  ui->soundfont_dls->setText(soundfont);
136  ui->dial_Reverb->setValue(reverbAmt);
137  ui->dial_Chorus->setValue(chorusAmt);
138  int reverbIndex = ui->combo_Reverb->findData(reverbType);
139  int chorusIndex = ui->combo_Chorus->findData(chorusType);
140  ui->combo_Reverb->setCurrentIndex(reverbIndex);
141  ui->combo_Chorus->setCurrentIndex(chorusIndex);
142 
143  chkDriverProperties(settings.getQSettings());
144 }
145 
146 void SonivoxSettingsDialog::writeSettings()
147 {
148  //qDebug() << Q_FUNC_INFO;
149  SettingsFactory settings;
150  settings->beginGroup(QSTR_PREFERENCES);
151  settings->setValue(QSTR_BUFFERTIME, ui->spnTime->value());
152  settings->setValue(QSTR_REVERBTYPE, ui->combo_Reverb->currentData());
153  settings->setValue(QSTR_CHORUSTYPE, ui->combo_Chorus->currentData());
154  settings->setValue(QSTR_REVERBAMT, ui->dial_Reverb->value());
155  settings->setValue(QSTR_CHORUSAMT, ui->dial_Chorus->value());
156  settings->setValue(QSTR_SOUNDFONT, ui->soundfont_dls->text());
157  settings->endGroup();
158  settings->sync();
159  qputenv("PULSE_LATENCY_MSEC", QByteArray::number( ui->spnTime->value() ));
160  chkDriverProperties(settings.getQSettings());
161 }
162 
163 void SonivoxSettingsDialog::chkDriverProperties(QSettings *settings)
164 {
165  //qDebug() << Q_FUNC_INFO;
166  if (m_driver != nullptr) {
167  //drumstick::rt::MIDIConnection conn;
168  m_driver->close();
169  m_driver->initialize(settings);
170  QVariant varVersion = m_driver->property("libversion");
171  if (varVersion.isValid()) {
172  ui->lblLibraryText->clear();
173  ui->lblLibraryText->setText(varVersion.toString());
174  }
175  QVariant varStatus = m_driver->property("status");
176  if (varStatus.isValid()) {
177  ui->lblStatusText->clear();
178  ui->lblStatusText->setText(varStatus.toBool() ? tr("Ready") : tr("Failed") );
179  ui->lblStatusIcon->setPixmap(varStatus.toBool() ? QPixmap(":/checked.png") : QPixmap(":/error.png") );
180  }
181  }
182 }
183 
184 void SonivoxSettingsDialog::restoreDefaults()
185 {
186  ui->spnTime->setValue(30);
187  ui->soundfont_dls->clear();
188  ui->combo_Reverb->setCurrentIndex(1);
189  ui->dial_Reverb->setValue(25800);
190  ui->combo_Chorus->setCurrentIndex(4);
191  ui->dial_Chorus->setValue(0);
192 }
193 
194 void SonivoxSettingsDialog::showFileDialog()
195 {
196  QDir dir(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSTR_DATADIR, QStandardPaths::LocateDirectory));
197  if (!dir.exists()) {
198  dir = QDir(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSTR_DATADIR2, QStandardPaths::LocateDirectory));
199  }
200  QString fileName = QFileDialog::getOpenFileName(this, tr("Select SoundFont"), dir.absolutePath(), tr("SoundFont Files (*.dls)"));
201  if (!fileName.isEmpty()) {
202  ui->soundfont_dls->setText(fileName);
203  }
204 }
205 
206 void SonivoxSettingsDialog::changeSoundFont(const QString& fileName)
207 {
208  readSettings();
209  ui->soundfont_dls->setText(fileName);
210  writeSettings();
211 }
212 
213 } // namespace widgets
214 } // namespace drumstick
215 
BackendManager class declaration.
The QSettings class provides persistent platform-independent application settings.
The BackendManager class manages lists of dynamic and static backends for applications based on drums...
MIDIOutput * outputBackendByName(const QString name)
outputBackendByName
virtual void close()=0
close the MIDI port
void DRUMSTICK_WIDGETS_EXPORT changeSoundFont(const QString driver, const QString fileName, QWidget *parent=nullptr)
Changes the sound font configuration Some RT output drivers accept soundfonts.
Drumstick common.
Definition: alsaclient.cpp:68
SettingsFactory class declaration.
Definition of the Sonivox Synth configuration dialog.