Debian Codegraph @

src/CustomInstrumentTunerForm.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "fmit"
4
5 // "fmit" 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 2 of the License, or
8 // (at your option) any later version.
9 //
10 // "fmit" 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, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 #include "CustomInstrumentTunerForm.h"
20
21 #include <cassert>
22 #include <iostream>
23 using namespace std;
24 #include <qstring.h>
25 #include <qaction.h>
26 #include <qlabel.h>
27 // #include <qgl.h>
28 #include <qlayout.h>
29 #include <qlcdnumber.h>
30 #include <qdial.h>
31 #include <qspinbox.h>
32 #include <qcombobox.h>
33 #include <qsplitter.h>
34 #include <qprogressbar.h>
35 #include <qmessagebox.h>
36 #include <qmenubar.h>
37 #include <qpushbutton.h>
38 #include <qcheckbox.h>
39 #include <qgroupbox.h>
40 #include <qbuttongroup.h>
41 #include <qlineedit.h>
42 #include <qstatusbar.h>
43 #include <qdatetime.h>
44 #include <qradiobutton.h>
45
46 #include <qtextbrowser.h>
47 #include <qpushbutton.h>
48 #include <qwidgetaction.h>
49 #include <qevent.h>
50 #include <qabstracttextdocumentlayout.h>
51 #include <Music/Convolution.h>
52 using namespace Music;
53 #include "modules/View.h"
54 #include "qthelper.h"
55
56 #include "aboutbox.h"
57
58 CustomInstrumentTunerForm::CustomInstrumentTunerForm()
59 : m_config_form(this)
60 , m_capture_thread("fmit")
61 , m_algo_combedfft(NULL)
62 , m_settings("fmit", "fmit", "009801") // not necessarily equal to the soft version
63 , m_timer_refresh(this)
64 , m_range_filter(&m_dummy_range_filter)
65 , m_quantizer(&m_latency_quantizer)
66 {
67 setupUi(this);
68
69 /*QWidgetAction* viewsCaption = new QWidgetAction(ui_tbViews);
70 viewsCaption->setDefaultWidget(new QLabel(tr("Views"), ui_tbViews));
71 ui_tbViews->addAction(viewsCaption);*/
72
73 //saveSettingsAction->setIcon(style()->standardIcon(QStyle::SP_DialogSaveButton));
74 //pauseAction->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
75
76 View::s_settings = &m_settings;
77 m_settings.add(m_config_form.ui_chkFullScreen);
78 m_settings.add(m_config_form.ui_chkAutoSaveOnExit);
79 m_settings.add(m_config_form.ui_chkShowA4Offset);
80
81 m_settings.add(m_config_form.ui_cbTuning);
82 m_settings.add(m_config_form.ui_spinTransposition);
83 m_settings.add(m_config_form.ui_cbNotesName);
84 m_settings.add(ui_spinAFreq);
85 m_settings.add(ui_spinA3Offset);
86
87 m_settings.add(m_config_form.ui_chkAutoDetect);
88 #ifdef CAPTURE_JACK
89 m_settings.add(m_config_form.ui_chkJACKAutoConnect);
90 m_settings.add(m_config_form.ui_txtJACKSourcePort);
91 #endif
92 #ifdef CAPTURE_ALSA
93 m_settings.add(m_config_form.ui_chkALSASamplingRateMax);
94 m_settings.add(m_config_form.ui_spinALSASamplingRate);
95 m_settings.add(m_config_form.ui_chkALSAMixMultipleChannels);
96 m_settings.add(m_config_form.ui_txtALSAPCMName);
97 #endif
98 #ifdef CAPTURE_OSS
99 m_settings.add(m_config_form.ui_chkOSSSamplingRateMax);
100 m_settings.add(m_config_form.ui_spinOSSSamplingRate);
101 m_settings.add(m_config_form.ui_chkOSSMixMultipleChannels);
102 m_settings.add(m_config_form.ui_txtOSSPCMName);
103 #endif
104 #ifdef CAPTURE_PORTAUDIO
105 m_settings.add(m_config_form.ui_chkPortAudioSamplingRateMax);
106 m_settings.add(m_config_form.ui_spinPortAudioSamplingRate);
107 m_settings.add(m_config_form.ui_chkPortAudioMixMultipleChannels);
108 #endif
109 #ifdef CAPTURE_QT
110 m_settings.add(m_config_form.ui_chkQtSamplingRateMax);
111 m_settings.add(m_config_form.ui_spinQtSamplingRate);
112 m_settings.add(m_config_form.ui_chkQtMixMultipleChannels);
113 #endif
114
115 m_settings.add(m_config_form.ui_spinRefreshTime);
116 m_settings.add(m_config_form.ui_spinMinHT);
117 m_settings.add(m_config_form.ui_spinMaxHT);
118
119 m_settings.add(m_config_form.ui_grpRangeFiltering);
120 m_settings.add(m_config_form.ui_rdRangeFilteringRectangular);
121 m_settings.add(m_config_form.ui_rdRangeFilteringFIR);
122
123 m_settings.add(m_config_form.ui_spinVolumeTreshold);
124 m_settings.add(m_config_form.ui_spinWindowSizeFactor);
125 m_settings.add(m_config_form.ui_chkAlgoUseSubHarmTresh);
126 m_settings.add(m_config_form.ui_spinCombedFFTAudibilityRatio);
127
128 m_settings.add(m_config_form.up_grpFreqRefinement);
129 m_settings.add(m_config_form.ui_rdUseFreqRefinement);
130 m_settings.add(m_config_form.ui_spinFreqRefinMaxHarm);
131 m_settings.add(m_config_form.ui_rdUseTimeRefinement);
132 m_settings.add(m_config_form.ui_spinTimeRefinMaxPeriod);
133
134 m_settings.add(m_config_form.ui_grpQuantizer);
135 m_settings.add(m_config_form.ui_spinErrorLatency);
136
137 m_algo_combedfft = new CombedFT();
138
139 for(int i=0; i<int(m_capture_thread.getTransports().size()); i++)
140 m_config_form.ui_cbTransports->addItem(m_capture_thread.getTransports()[i]->getName());
141
142 if(m_capture_thread.getTransports().empty())
143 QMessageBox::critical(this, "Error", "Please compile me with a capture system ...");
144 if(m_capture_thread.getTransports().size()==1)
145 {
146 m_config_form.ui_lblSelectedCaptureSystem->hide();
147 m_config_form.ui_btnAutoDetect->hide();
148 m_config_form.ui_chkAutoDetect->hide();
149 m_config_form.ui_cbTransports->hide();
150 }
151 m_config_form.ui_grpALSA->hide();
152 m_config_form.ui_grpJACK->hide();
153 m_config_form.ui_grpPortAudio->hide();
154 m_config_form.ui_grpOSS->hide();
155 m_config_form.ui_grpQt->hide();
156
157 m_config_form.ui_grpRangeFiltering->hide();
158
159 ui_lblA3Offset->hide();
160 ui_spinA3Offset->hide();
161
162 connect(&m_capture_thread, SIGNAL(samplingRateChanged(int)), this, SLOT(samplingRateChanged(int)));
163 connect(&m_capture_thread, SIGNAL(errorRaised(const QString&)), this, SLOT(errorRaised(const QString&)));
164 connect(&m_capture_thread, SIGNAL(transportChanged(const QString&)), this, SLOT(transportChanged(const QString&)));
165
166 connect(&m_latency_quantizer, SIGNAL(noteStarted(double,double)), this, SLOT(noteStarted(double,double)));
167 connect(&m_latency_quantizer, SIGNAL(noteFinished(double,double)), this, SLOT(noteFinished(double,double)));
168 connect(&m_dummy_quantizer, SIGNAL(noteStarted(double,double)), this, SLOT(noteStarted(double,double)));
169 connect(&m_dummy_quantizer, SIGNAL(noteFinished(double,double)), this, SLOT(noteFinished(double,double)));
170
171 m_dialTune = new DialView(centralWidget());
172 ui_dialTuneLayout->addWidget(m_dialTune);
173
174 m_glGraph = new GLGraph(centralWidget());
175 connect(m_glGraph->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
176 connect(m_glGraph->setting_spinMaxHeight, SIGNAL(valueChanged(int)), this, SLOT(update_views()));
177 ui_tbViews->addAction(m_glGraph->setting_show);
178 ui_graphLayout->addWidget(m_glGraph);
179
180 m_glErrorHistory = new GLErrorHistory(centralWidget());
181 connect(m_glErrorHistory->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
182 ui_tbViews->addAction(m_glErrorHistory->setting_show);
183 ui_errorLayout->addWidget(m_glErrorHistory);
184
185 // link scales
186 connect(m_dialTune->setting_spinScale, SIGNAL(valueChanged(int)), m_glErrorHistory->setting_spinScale, SLOT(setValue(int)));
187 connect(m_glErrorHistory->setting_spinScale, SIGNAL(valueChanged(int)), m_dialTune->setting_spinScale, SLOT(setValue(int)));
188 connect(m_dialTune->setting_useCents, SIGNAL(toggled(bool)), m_glErrorHistory->setting_useCents, SLOT(setChecked(bool)));
189 connect(m_glErrorHistory->setting_useCents, SIGNAL(toggled(bool)), m_dialTune->setting_useCents, SLOT(setChecked(bool)));
190
191 m_glVolumeHistory = new GLVolumeHistory(centralWidget());
192 connect(m_config_form.ui_spinVolumeTreshold, SIGNAL(valueChanged(int)), m_glVolumeHistory, SLOT(setVolumeTreshold(int)));
193 connect(m_glVolumeHistory->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
194 ui_tbViews->addAction(m_glVolumeHistory->setting_show);
195 ui_volumeLayout->addWidget(m_glVolumeHistory);
196
197 // link keep settings
198 connect(ui_btnKeepErrorHistory, SIGNAL(toggled(bool)), m_glErrorHistory->setting_keep, SLOT(setChecked(bool)));
199 connect(m_glErrorHistory->setting_keep, SIGNAL(toggled(bool)), m_glVolumeHistory->setting_keep, SLOT(setChecked(bool)));
200 connect(m_glErrorHistory->setting_keep, SIGNAL(toggled(bool)), ui_btnKeepErrorHistory, SLOT(setChecked(bool)));
201
202 m_glSample = new GLSample(centralWidget());
203 connect(m_glSample->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
204 ui_tbViews->addAction(m_glSample->setting_show);
205 ui_sampleLayout->addWidget(m_glSample);
206
207 m_glFreqStruct = new GLFreqStruct(centralWidget());
208 connect(m_glFreqStruct->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
209 ui_tbViews->addAction(m_glFreqStruct->setting_show);
210 ui_formantsLayout->addWidget(m_glFreqStruct);
211
212 m_glFT = new GLFT(centralWidget());
213 connect(m_glFT->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
214 ui_tbViews->addAction(m_glFT->setting_show);
215 ui_FT->addWidget(m_glFT);
216
217 m_microtonalView = new MicrotonalView(centralWidget());
218 connect(m_microtonalView->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
219 connect(m_microtonalView, SIGNAL(tuningFreqChanged(float)), this, SLOT(tuningFreqChanged(float)));
220 ui_tbViews->addAction(m_microtonalView->setting_show);
221 ui_microtonalLayout->addWidget(m_microtonalView);
222
223 m_glStatistics = new GLStatistics(centralWidget());
224 connect(m_glStatistics->setting_show, SIGNAL(toggled(bool)), this, SLOT(update_views()));
225 ui_tbViews->addAction(m_glStatistics->setting_show);
226 ui_microtonalLayout->addWidget(m_glStatistics);
227
228 connect(m_dialTune->setting_spinScale, SIGNAL(valueChanged(int)), m_glStatistics->setting_spinScale, SLOT(setValue(int)));
229 connect(m_glStatistics->setting_spinScale, SIGNAL(valueChanged(int)), m_dialTune->setting_spinScale, SLOT(setValue(int)));
230 connect(m_dialTune->setting_useCents, SIGNAL(toggled(bool)), m_glStatistics->setting_useCents, SLOT(setChecked(bool)));
231 connect(m_glStatistics->setting_useCents, SIGNAL(toggled(bool)), m_dialTune->setting_useCents, SLOT(setChecked(bool)));
232
233 connect(m_dialTune->setting_showTolerance, SIGNAL(toggled(bool)), m_glStatistics->setting_showTolerance, SLOT(setChecked(bool)));
234 connect(m_glStatistics->setting_showTolerance, SIGNAL(toggled(bool)), m_dialTune->setting_showTolerance, SLOT(setChecked(bool)));
235
236 connect(&m_config_form, SIGNAL(accepted()), this, SLOT(configure_ok()));
237 connect(&m_config_form, SIGNAL(rejected()), this, SLOT(configure_cancel()));
238 connect(m_config_form.ui_btnRestoreFactorySettings, SIGNAL(clicked()), this, SLOT(restoreFactorySettings()));
239 connect(m_config_form.ui_spinMinHT, SIGNAL(valueChanged(int)), this, SLOT(noteRangeChanged()));
240 connect(m_config_form.ui_spinMaxHT, SIGNAL(valueChanged(int)), this, SLOT(noteRangeChanged()));
241 connect(m_config_form.ui_cbTuning, SIGNAL(highlighted(int)), this, SLOT(noteRangeChanged()));
242 connect(m_config_form.ui_spinTransposition, SIGNAL(valueChanged(int)), this, SLOT(transpositionChanged()));
243 connect(m_config_form.ui_cbNotesName, SIGNAL(highlighted(int)), this, SLOT(noteRangeChanged()));
244 connect(m_config_form.ui_btnAutoDetect, SIGNAL(clicked()), this, SLOT(autoDetectTransport()));
245 connect(m_config_form.ui_cbTransports, SIGNAL(textActivated(const QString&)), this, SLOT(selectTransport(const QString&)));
246 connect(m_config_form.ui_chkALSAMixMultipleChannels, SIGNAL(toggled(bool)), &m_capture_thread, SLOT(setMixMultipleChannels(bool)));
247 connect(m_config_form.ui_chkOSSMixMultipleChannels, SIGNAL(toggled(bool)), &m_capture_thread, SLOT(setMixMultipleChannels(bool)));
248 connect(m_config_form.ui_chkPortAudioMixMultipleChannels, SIGNAL(toggled(bool)), &m_capture_thread, SLOT(setMixMultipleChannels(bool)));
249
250 loadSettings();
251
252 if(m_config_form.ui_chkAutoDetect->isChecked())
253 m_capture_thread.autoDetectTransport();
254
255 configure_ok();
256
257 if(m_config_form.ui_chkFullScreen->isChecked())
258 toggleFullScreen();
259
260 m_config_form.adjustSize();
261
262 m_time_refresh_views.start();
263 m_time_refresh.start();
264 m_time.start();
265 connect((QObject*)&m_timer_refresh, SIGNAL(timeout()), this, SLOT(refresh()));
266 m_timer_refresh.start(m_config_form.ui_spinRefreshTime->value());
267
268 // cout << __FILE__ << " " << __LINE__ << endl;
269 }
270
271 void CustomInstrumentTunerForm::transportChanged(const QString& name)
272 {
273 selectTransport(name);
274
275 if(m_capture_thread.getCurrentTransportIndex()!=m_config_form.ui_cbTransports->currentIndex())
276 m_config_form.ui_cbTransports->setCurrentIndex(m_capture_thread.getCurrentTransportIndex());
277 }
278 void CustomInstrumentTunerForm::selectTransport(const QString& name)
279 {
280 m_config_form.ui_grpALSA->hide();
281 m_config_form.ui_grpJACK->hide();
282 m_config_form.ui_grpPortAudio->hide();
283 m_config_form.ui_grpOSS->hide();
284 m_config_form.ui_grpQt->hide();
285
286 if(name=="ALSA") m_config_form.ui_grpALSA->show();
287 else if(name=="JACK") m_config_form.ui_grpJACK->show();
288 else if(name=="PortAudio") m_config_form.ui_grpPortAudio->show();
289 else if(name=="OSS") m_config_form.ui_grpOSS->show();
290 else if(name=="Qt") m_config_form.ui_grpQt->show();
291 }
292 void CustomInstrumentTunerForm::autoDetectTransport()
293 {
294 m_capture_thread.autoDetectTransport();
295
296 // here transportChanged will be called
297 }
298
299 void CustomInstrumentTunerForm::toggleFullScreen()
300 {
301 static bool fs = true;
302 if(fs)
303 {
304 m_config_form.ui_chkFullScreen->setChecked(true);
305 showFullScreen();
306 }
307 else
308 {
309 m_config_form.ui_chkFullScreen->setChecked(false);
310 showNormal();
311 }
312 fs = !fs;
313 }
314
315 void CustomInstrumentTunerForm::noteRangeChanged()
316 {
317 // cout << "CustomInstrumentTunerForm::noteRangeChanged" << endl;
318
319 m_config_form.ui_txtMinHT->setText(QString::fromStdString(h2n(m_config_form.ui_spinMinHT->value())) + " = " + QString::number(h2f(m_config_form.ui_spinMinHT->value())) + " hz");
320 m_config_form.ui_txtMaxHT->setText(QString::fromStdString(h2n(m_config_form.ui_spinMaxHT->value())) + " = " + QString::number(h2f(m_config_form.ui_spinMaxHT->value())) + " hz");
321 }
322
323 void CustomInstrumentTunerForm::transpositionChanged()
324 {
325 m_config_form.ui_lblTransposition->setText(
326 QString::fromStdString(h2n(-9, GetNotesName(), 0, CHROMATIC, true))
327 + " → " +
328 QString::fromStdString(h2n(-9, GetNotesName(), m_config_form.ui_spinTransposition->value(), CHROMATIC, true)));
329 }
330
331 void CustomInstrumentTunerForm::errorRaised(const QString& error)
332 {
333 // cout << "CustomInstrumentTunerForm::errorRaised " << error << endl;
334
335 statusBar()->showMessage(QString("ERROR: ")+error);
336
337 QPalette palette;
338 palette.setColor(ui_lblSoundStability->backgroundRole(), QColor(180,74,74));
339 ui_lblSoundStability->setPalette(palette);
340 }
341
342 void CustomInstrumentTunerForm::samplingRateChanged(int sampling_rate)
343 {
344 // cout << "CustomInstrumentTunerForm::samplingRateChanged " << sampling_rate << endl;
345
346 Music::SetSamplingRate(sampling_rate);
347
348 m_rect_range_filter.reset(int(GetSamplingRate()/2000.0));
349 // m_fir_range_filter.setImpulseResponse(fir1_lowpass(400, 2/400));
350 // m_rect_range_filter.reset(int(GetSamplingRate()/h2f(GetSemitoneMin())));
351 m_glFT->spinWinLengthChanged(m_glFT->setting_winlen->value());
352 m_glFT->setSamplingRate(sampling_rate);
353
354 if(m_config_form.ui_cbTransports->currentText()=="JACK")
355 m_config_form.ui_lblJACKSamplingRate->setText(QString::number(sampling_rate));
356 }
357
358 void CustomInstrumentTunerForm::ui_spinAFreq_valueChanged(int AFreq)
359 {
360 double A = AFreq;
361 if(m_config_form.ui_chkShowA4Offset->isChecked())
362 A = h2f(ui_spinA3Offset->value()*1/100.0f, A);
363 Music::SetAFreq(A);
364 // cout << A << endl;
365 }
366 void CustomInstrumentTunerForm::ui_spinAOffset_valueChanged(int offset)
367 {
368 double A = ui_spinAFreq->value();
369 if(m_config_form.ui_chkShowA4Offset->isChecked())
370 A = h2f(offset*1/100.0f, ui_spinAFreq->value());
371 Music::SetAFreq(A);
372 // cout << A << endl;
373 }
374
375 void CustomInstrumentTunerForm::tuningFreqChanged(float freq)
376 {
377 // cout << "CustomInstrumentTunerForm::tuningFreqChanged " << freq << endl;
378
379 if(freq==0.0f)
380 {
381 if(m_compared_freq!=0.0f)
382 {
383 ui_txtNoteFreq->display(m_compared_freq);
384 ui_txtNote->setText(QString::fromStdString(h2n(f2h(m_compared_freq))));
385 }
386 }
387 else
388 {
389 m_compared_freq = freq;
390 ui_txtNoteFreq->display(int(freq*100)/100.0f);
391 ui_txtNote->setText(m_microtonalView->getTuningNoteName());
392 }
393
394 m_quantizer->reset();
395
396 // m_dialTune->setError(-10.0f);
397 }
398
399 void CustomInstrumentTunerForm::pause(bool on)
400 {
401 m_capture_thread.togglePause(on);
402
403 if(on) m_timer_refresh.stop();
404 else m_timer_refresh.start(m_config_form.ui_spinRefreshTime->value());
405 }
406
407 void CustomInstrumentTunerForm::refresh()
408 {
409 double elapsed_time = m_time_refresh.elapsed();
410 m_time_refresh.start();
411
412 QColor capture_failed_color(180,74,74);
413 QColor prb_color(208,146,0);
414 QColor empty_color(128,128,128);
415 QColor ok_color(83,165,105);
416
417 // 1/{time between refresh} = {nb refresh by seconds}
418 // limit the nb of new data by fs/{nb refresh by seconds}
419 // add 1 to {nb refresh by second} to eventualy recover small lags
420 // (a big lag is managed by m_values.size()>getPacketSizeSinceLastLock())
421 int limit = int( m_capture_thread.getSamplingRate() /
422 (1.0/(m_config_form.ui_spinRefreshTime->value()/1000.0) - 1));
423
424 // cout << "REFRESH ";
425
426 m_capture_thread.lock();
427 // cout << "CustomInstrumentTunerForm::refresh locked, values to read=" << m_capture_thread.m_values.size() << endl;
428 int nb_new_data = 0;
429 while(!m_capture_thread.m_values.empty() &&
430 (int(m_capture_thread.m_values.size())>m_capture_thread.getPacketSizeSinceLastLock() || nb_new_data<limit))
431 {
432 // cout << m_capture_thread.m_values.back() << " ";
433 double value = (*m_range_filter)(m_capture_thread.m_values.back());
434 // cout << value << " ";
435 m_capture_thread.m_values.pop_back();
436
437 m_queue.push_front(value);
438 if(m_glGraph) m_glGraph->addValue(value);
439 if(m_glFT) m_glFT->buff.push_front(value);
440
441 nb_new_data++;
442 }
443 m_capture_thread.unlock();
444 // cout << "CustomInstrumentTunerForm::refresh unlocked" << endl;
445
446 // cout << endl;
447
448 int max_size = max(m_range_filter->getLength(), max(m_glGraph->getLength(), m_algo_combedfft->getMinSize()));
449 while(!m_queue.empty() && int(m_queue.size())>max_size)
450 m_queue.pop_back();
451
452 // refresh graph data
453 m_glGraph->refreshGraph(); // TODO refresh the view each time ??
454 m_glFT->refreshGraph();
455
456 // ------- Analysis stage -------
457
458 // if something goes wrong in the capture system
459 if(nb_new_data==0 || m_algo_combedfft==NULL || elapsed_time>8*m_config_form.ui_spinRefreshTime->value())
460 {
461 QPalette palette;
462 palette.setColor(ui_lblSoundStability->backgroundRole(), capture_failed_color);
463 ui_lblSoundStability->setPalette(palette);
464 }
465 else
466 {
467 m_algo_combedfft->apply(m_queue);
468
469 // TODO hem ... use energy
470 double max_component = 20*log10(m_algo_combedfft->getComponentsMax());
471 ui_pgbVolume->setValue(100+int(max_component));
472
473 double freq = 0.0;
474 if(m_algo_combedfft->hasNoteRecognized())
475 freq = m_algo_combedfft->getFondamentalFreq();
476
477 double freq_rel = freq*m_algo_combedfft->m_plan.in.size()/double(GetSamplingRate());
478 if(freq_rel<1 || freq_rel>(m_algo_combedfft->m_plan.out.size()/2))
479 freq = 0.0;
480
481 // frequency refinement
482 if(freq>0.0 && m_config_form.up_grpFreqRefinement->isChecked())
483 {
484 if(m_config_form.ui_rdUseFreqRefinement->isChecked())
485 {
486 freq = FundFreqRefinementOfHarmonicStruct(m_algo_combedfft->m_plan.out, freq, m_config_form.ui_spinFreqRefinMaxHarm->value(), m_algo_combedfft->getZeroPaddingFactor());
487
488 }
489 else if(m_config_form.ui_rdUseTimeRefinement->isChecked())
490 {
491 double period = GetAveragePeriodFromApprox(m_queue, int(GetSamplingRate()/freq), m_config_form.ui_spinTimeRefinMaxPeriod->value());
492 if(period>0.0)
493 freq = GetSamplingRate()/period;
494 }
495 }
496
497 // cout << "2) test freq=" << m_test_freq <<endl;
498
499 m_quantizer->quantize(freq);
500
501 if(!m_quantizer->isPlaying())
502 {
503 QPalette palette;
504 palette.setColor(ui_lblSoundStability->backgroundRole(), empty_color);
505 ui_lblSoundStability->setPalette(palette);
506 }
507 else
508 {
509 // Use 100ms rect window
510 int max_amplitude_limit = min(m_queue.size(),size_t(m_capture_thread.getSamplingRate()*100.0/1000));
511 double max_amplitude = 0.0;
512 for(int i=0;i<max_amplitude_limit; i++)
513 max_amplitude = max(max_amplitude, fabs(m_queue[i]));
514
515 if(max_amplitude>=1.0)
516 {
517 QPalette palette;
518 palette.setColor(ui_lblSoundStability->backgroundRole(), prb_color);
519 ui_lblSoundStability->setPalette(palette);
520 }
521 else
522 {
523 QPalette palette;
524 palette.setColor(ui_lblSoundStability->backgroundRole(), ok_color);
525 ui_lblSoundStability->setPalette(palette);
526 }
527
528 m_freq = m_quantizer->getAverageFrequency();
529 m_error = f2hf(m_freq, m_compared_freq);
530
531 // refresh error
532 m_glErrorHistory->addError(m_error);
533 m_dialTune->setError(m_error);
534 m_dialTune->m_avg_error = m_glErrorHistory->m_notes.back().avg_err;
535 m_dialTune->m_min_error = m_glErrorHistory->m_notes.back().min_err;
536 m_dialTune->m_max_error = m_glErrorHistory->m_notes.back().max_err;
537 ui_txtFreq->display(m_freq);
538
539 // refresh intonal tuning cursor
540 m_microtonalView->setAFreq(Music::GetAFreq());
541 m_microtonalView->updateCursor(m_freq);
542
543 // volume
544 m_glVolumeHistory->addVolume(max_component);
545
546 // refresh sample data
547 refresh_data_sample();
548
549 // refresh formants data
550 refresh_data_harmonics();
551
552 m_glStatistics->addNote(f2h(m_compared_freq), m_error);
553 }
554 }
555
556 if(m_time_refresh_views.elapsed()>50) // 20 images/second max
557 refresh_views();
558 }
559
560 void CustomInstrumentTunerForm::noteStarted(double freq, double dt)
561 {
562 Q_UNUSED(freq)
563 Q_UNUSED(dt)
564 // cout << "CustomInstrumentTunerForm::noteStarted " << freq << "," << dt << endl;
565
566 // set the compared freq
567 if(m_microtonalView->setting_show->isChecked() && m_microtonalView->hasTuningFreqSelected())
568 m_compared_freq = m_microtonalView->getTuningFreq();
569 else
570 m_compared_freq = m_quantizer->getCenterFrequency(); // h2f(f2h(freq));
571
572 if(m_microtonalView->setting_show->isChecked() && m_microtonalView->hasTuningFreqSelected())
573 {
574 ui_txtNoteFreq->display(int(m_microtonalView->getTuningFreq()*100)/100.0);
575 ui_txtNote->setText(m_microtonalView->getTuningNoteName());
576 if(m_microtonalView->m_selected_jivalue->is_ratio)
577 {
578 m_glErrorHistory->addNote(GLErrorHistory::Note(m_microtonalView->setting_selectedRoot, m_microtonalView->m_selected_jivalue->num, m_microtonalView->m_selected_jivalue->den));
579 m_glVolumeHistory->addNote(GLVolumeHistory::Note(m_microtonalView->setting_selectedRoot, m_microtonalView->m_selected_jivalue->num, m_microtonalView->m_selected_jivalue->den));
580 }
581 else
582 {
583 m_glErrorHistory->addNote(GLErrorHistory::Note(m_microtonalView->setting_selectedRoot, m_microtonalView->m_selected_jivalue->cents));
584 m_glVolumeHistory->addNote(GLVolumeHistory::Note(m_microtonalView->setting_selectedRoot, m_microtonalView->m_selected_jivalue->cents));
585 }
586 }
587 else
588 {
589 ui_txtNoteFreq->display(m_compared_freq);
590 ui_txtNote->setText(QString::fromStdString(h2n(f2h(m_compared_freq))));
591 m_glErrorHistory->addNote(GLErrorHistory::Note(f2h(m_compared_freq)));
592 m_glVolumeHistory->addNote(GLVolumeHistory::Note(f2h(m_compared_freq)));
593 }
594 }
595 void CustomInstrumentTunerForm::noteFinished(double freq, double dt)
596 {
597 Q_UNUSED(freq)
598 Q_UNUSED(dt)
599 m_compared_freq = 0.0;
600 // cout << "CustomInstrumentTunerForm::noteFinished " << freq << "," << dt << endl;
601 }
602
603 void CustomInstrumentTunerForm::refresh_data_sample()
604 {
605 if(m_freq==0.0f || !m_glSample->setting_show->isChecked())
606 {
607 m_glSample->clear();
608 return;
609 }
610
611 deque<double> sample;
612 GetWaveSample(m_queue, size_t(m_capture_thread.getSamplingRate()/m_freq), sample);
613 m_glSample->add(m_time.elapsed(), sample);
614 }
615
616 void CustomInstrumentTunerForm::refresh_data_harmonics()
617 {
618 if(!(m_algo_combedfft!=NULL &&
619 m_freq>0.0f &&
620 m_glFreqStruct->setting_show->isChecked()))
621 return;
622
623 for(int i=0; i<int(m_glFreqStruct->m_components.size()); i++)
624 m_glFreqStruct->m_components[i] = -1e6;
625
626 vector<Harmonic> harms = GetHarmonicStruct(m_algo_combedfft->m_plan.out, m_freq, m_glFreqStruct->m_components.size(), m_algo_combedfft->getZeroPaddingFactor(), 0.1, true);
627
628 m_glFreqStruct->m_components_max = 0.0;
629 for(int i=0; i<int(harms.size()); i++)
630 {
631 if(harms[i].harm_number<=int(m_glFreqStruct->m_components.size()))
632 {
633 m_glFreqStruct->m_components[harms[i].harm_number-1] = 20*log10(harms[i].mod*1000.0);
634
635 m_glFreqStruct->m_components_max = max(m_glFreqStruct->m_components_max, m_glFreqStruct->m_components[i]);
636 }
637 }
638 }
639
640 void CustomInstrumentTunerForm::refresh_views()
641 {
642 // cout << "CustomInstrumentTunerForm::refresh_views " << endl;
643
644 m_dialTune->repaint();
645
646 if(m_glGraph->setting_show->isChecked())
647 m_glGraph->update();
648
649 if(m_glErrorHistory->setting_show->isChecked())
650 m_glErrorHistory->update();
651
652 if(m_glVolumeHistory->setting_show->isChecked())
653 m_glVolumeHistory->update();
654
655 if(m_microtonalView->setting_show->isChecked())
656 m_microtonalView->update();
657
658 if(m_glSample->setting_show->isChecked())
659 m_glSample->update();
660
661 if(m_glFreqStruct->setting_show->isChecked())
662 m_glFreqStruct->update();
663
664 if(m_glFT->setting_show->isChecked())
665 m_glFT->update();
666
667 if(m_glStatistics->setting_show->isChecked())
668 m_glStatistics->update();
669
670 m_time_refresh_views.start();
671 }
672
673 void CustomInstrumentTunerForm::keyPressEvent(QKeyEvent * e)
674 {
675 if(e->key()==Qt::Key_F)
676 toggleFullScreen();
677 }
678
679 void CustomInstrumentTunerForm::resizeEvent(QResizeEvent* e)
680 {
681 Q_UNUSED(e)
682 update_views();
683 }
684
685 void CustomInstrumentTunerForm::update_views()
686 {
687 if( !m_glGraph->setting_show->isChecked() &&
688 !m_glErrorHistory->setting_show->isChecked() &&
689 !m_glVolumeHistory->setting_show->isChecked() &&
690 !m_glSample->setting_show->isChecked() &&
691 !m_glFreqStruct->setting_show->isChecked() &&
692 !m_glFT->setting_show->isChecked())
693 m_dialTune->setMaximumWidth(size().width());
694 else
695 m_dialTune->setMaximumWidth(ui_rightLayout->minimumSize().width());
696
697 if(m_glGraph->setting_show->isChecked() &&
698 !m_glErrorHistory->setting_show->isChecked() &&
699 !m_glVolumeHistory->setting_show->isChecked() &&
700 !m_glSample->setting_show->isChecked() &&
701 !m_glFreqStruct->setting_show->isChecked() &&
702 !m_glFT->setting_show->isChecked())
703 m_glGraph->setMaximumHeight(size().height());
704 else
705 m_glGraph->setMaximumHeight(m_glGraph->setting_spinMaxHeight->value());
706
707 if(!m_glErrorHistory->setting_show->isChecked() && !m_glVolumeHistory->setting_show->isChecked())
708 ui_btnKeepErrorHistory->hide();
709 else
710 ui_btnKeepErrorHistory->show();
711 }
712
713 void CustomInstrumentTunerForm::configure()
714 {
715 // Checking PortAudio devices while capturing using ALSA seems to crash ALSA
716 // thus, stop everything while configuring and capture again when exiting the configuration panel
717 m_capture_thread.stopCapture();
718
719 noteRangeChanged();
720 transpositionChanged();
721
722 if(m_capture_thread.getCurrentTransportIndex()<m_config_form.ui_cbTransports->count())
723 m_config_form.ui_cbTransports->setCurrentIndex(m_capture_thread.getCurrentTransportIndex());
724
725 #ifdef CAPTURE_JACK
726 // TODO set descr
727 m_config_form.ui_grpJACK->setTitle(m_capture_thread.getTransport("JACK")->getDescription());
728 m_config_form.ui_lblJACKSamplingRate->setText(QString::number(m_capture_thread.getSamplingRate()));
729 #endif
730
731 #ifdef CAPTURE_PORTAUDIO
732 m_config_form.ui_grpPortAudio->setTitle(m_capture_thread.getTransport("PortAudio")->getDescription());
733 m_config_form.ui_spinPortAudioSamplingRate->setValue(m_capture_thread.getSamplingRate());
734 if(m_capture_thread.getTransport("PortAudio"))
735 {
736 try
737 {
738 PaError err;
739 err = Pa_Initialize();
740 if(err != paNoError)
741 throw QString("PortAudio: CustomInstrumentTunerForm::configure:Pa_Initialize ")+Pa_GetErrorText(err);
742 int numDevices = Pa_GetDeviceCount();
743 int current_index = -1;
744 m_config_form.ui_cbPortAudioDeviceName->clear();
745 const PaDeviceInfo* deviceInfo;
746 for(int i=0; i<numDevices; i++)
747 {
748 deviceInfo = Pa_GetDeviceInfo(i);
749 m_config_form.ui_cbPortAudioDeviceName->addItem(QString(deviceInfo->name));
750 if(QString(deviceInfo->name)==m_capture_thread.getTransport("PortAudio")->getSource())
751 current_index = i;
752 }
753 if(current_index!=-1)
754 m_config_form.ui_cbPortAudioDeviceName->setCurrentIndex(current_index);
755 }
756 catch(QString error)
757 {
758 cout << "CustomInstrumentTunerForm: ERROR: " << error.toStdString() << endl;
759 }
760 Pa_Terminate();
761 }
762 #endif
763
764 #ifdef CAPTURE_ALSA
765 m_config_form.ui_grpALSA->setTitle(m_capture_thread.getTransport("ALSA")->getDescription());
766 m_config_form.ui_spinALSASamplingRate->setValue(m_capture_thread.getSamplingRate());
767 #endif
768 #ifdef CAPTURE_OSS
769 m_config_form.ui_grpOSS->setTitle(m_capture_thread.getTransport("OSS")->getDescription());
770 m_config_form.ui_spinOSSSamplingRate->setValue(m_capture_thread.getSamplingRate());
771 #endif
772 #ifdef CAPTURE_QT
773 m_config_form.ui_grpQt->setTitle(m_capture_thread.getTransport("Qt")->getDescription());
774 m_config_form.ui_spinQtSamplingRate->setValue(m_capture_thread.getSamplingRate());
775 {
776 QString saved_device = m_settings.value(m_config_form.ui_cbQtDeviceName->objectName(), "default").toString();
777 try
778 {
779 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
780
781 int current_index = -1;
782 m_config_form.ui_cbQtDeviceName->clear();
783 for(int i=0; i<devices.count(); i++) {
784 m_config_form.ui_cbQtDeviceName->addItem(devices.at(i).deviceName());
785 if(devices.at(i).deviceName()==m_capture_thread.getTransport("Qt")->getSource())
786 current_index = i;
787 }
788 if(current_index!=-1)
789 m_config_form.ui_cbQtDeviceName->setCurrentIndex(current_index);
790 }
791 catch(QString error)
792 {
793 cout << "CustomInstrumentTunerForm: ERROR: " << error.toStdString() << endl;
794 }
795 }
796 #endif
797
798 m_config_form.adjustSize();
799 m_config_form.show();
800 }
801 void CustomInstrumentTunerForm::configure_cancel()
802 {
803 if(!pauseAction->isChecked() && !m_capture_thread.isCapturing())
804 m_capture_thread.startCapture();
805 }
806 void CustomInstrumentTunerForm::configure_ok()
807 {
808 switch(m_config_form.ui_cbTuning->currentIndex())
809 {
810 case 0:
811 SetTuning(CHROMATIC);
812 break;
813 case 1:
814 SetTuning(WERCKMEISTER3);
815 break;
816 case 2:
817 SetTuning(KIRNBERGER3);
818 break;
819 case 3:
820 SetTuning(DIATONIC);
821 break;
822 case 4:
823 SetTuning(MEANTONE);
824 break;
825 default:
826 SetTuning(CHROMATIC);
827 break;
828 }
829
830 SetTransposition(m_config_form.ui_spinTransposition->value());
831
832 if(m_config_form.ui_cbNotesName->currentIndex()==0) SetNotesName(LOCAL_ANGLO);
833 if(m_config_form.ui_cbNotesName->currentIndex()==1) SetNotesName(LOCAL_LATIN);
834 if(m_config_form.ui_cbNotesName->currentIndex()==2) SetNotesName(LOCAL_GERMAN);
835 if(m_config_form.ui_cbNotesName->currentIndex()==3) SetNotesName(LOCAL_HINDUSTANI);
836 if(m_config_form.ui_cbNotesName->currentIndex()==4) SetNotesName(LOCAL_BYZANTINE);
837 m_microtonalView->notesNameChanged();
838 m_microtonalView->setAFreq(Music::GetAFreq());
839
840 update_localized_note_names();
841
842 SetSemitoneBounds(m_config_form.ui_spinMinHT->value(), m_config_form.ui_spinMaxHT->value());
843
844 ui_spinA3Offset->setVisible(m_config_form.ui_chkShowA4Offset->isChecked());
845 ui_lblA3Offset->setVisible(m_config_form.ui_chkShowA4Offset->isChecked());
846
847 // if(m_note!=-1000)
848 // ui_txtNote->setText(h2n(m_note));
849
850 // m_dialTune->setError(-10.0f);
851
852 // cout << "b" << endl;
853
854 // Capture
855 #ifdef CAPTURE_QT
856 if(m_config_form.ui_cbTransports->currentText()=="Qt")
857 {
858 m_capture_thread.selectTransport("Qt");
859 m_capture_thread.setSource(m_config_form.ui_cbQtDeviceName->currentText());
860 if(m_config_form.ui_chkQtSamplingRateMax->isChecked())
861 m_capture_thread.setSamplingRate(CaptureThread::SAMPLING_RATE_MAX);
862 else
863 m_capture_thread.setSamplingRate(m_config_form.ui_spinQtSamplingRate->value());
864 }
865 #endif
866 #ifdef CAPTURE_ALSA
867 if(m_config_form.ui_cbTransports->currentText()=="ALSA")
868 {
869 m_capture_thread.selectTransport("ALSA");
870 m_capture_thread.setSource(m_config_form.ui_txtALSAPCMName->text());
871 if(m_config_form.ui_chkALSASamplingRateMax->isChecked())
872 m_capture_thread.setSamplingRate(CaptureThread::SAMPLING_RATE_MAX);
873 else
874 m_capture_thread.setSamplingRate(m_config_form.ui_spinALSASamplingRate->value());
875 }
876 #endif
877 #ifdef CAPTURE_JACK
878 if(m_config_form.ui_cbTransports->currentText()=="JACK")
879 {
880 m_capture_thread.selectTransport("JACK");
881 if(m_config_form.ui_chkJACKAutoConnect->isChecked())
882 m_capture_thread.setSource(m_config_form.ui_txtJACKSourcePort->text());
883 else
884 m_capture_thread.setSource("");
885 m_config_form.ui_lblJACKSamplingRate->setText(QString::number(m_capture_thread.getSamplingRate()));
886 }
887 #endif
888 #ifdef CAPTURE_PORTAUDIO
889 if(m_config_form.ui_cbTransports->currentText()=="PortAudio")
890 {
891 m_capture_thread.selectTransport("PortAudio");
892 m_capture_thread.setSource(m_config_form.ui_cbPortAudioDeviceName->currentText());
893 if(m_config_form.ui_chkPortAudioSamplingRateMax->isChecked())
894 m_capture_thread.setSamplingRate(CaptureThread::SAMPLING_RATE_MAX);
895 else
896 m_capture_thread.setSamplingRate(m_config_form.ui_spinPortAudioSamplingRate->value());
897 }
898 #endif
899 #ifdef CAPTURE_OSS
900 if(m_config_form.ui_cbTransports->currentText()=="OSS")
901 {
902 m_capture_thread.selectTransport("OSS");
903 m_capture_thread.setSource(m_config_form.ui_txtOSSPCMName->text());
904 if(m_config_form.ui_chkOSSSamplingRateMax->isChecked())
905 m_capture_thread.setSamplingRate(CaptureThread::SAMPLING_RATE_MAX);
906 else
907 m_capture_thread.setSamplingRate(m_config_form.ui_spinOSSSamplingRate->value());
908 }
909 #endif
910 m_timer_refresh.setInterval(m_config_form.ui_spinRefreshTime->value());
911
912 // Views
913 m_glGraph->m_treshold = invlp(m_config_form.ui_spinVolumeTreshold->value());
914 m_glGraph->clearValues();
915
916 // cout << "c" << endl;
917
918 if(m_config_form.ui_grpRangeFiltering->isChecked())
919 {
920 // cout << "set filters " << GetSamplingRate() << endl;
921 // m_rect_range_filter.reset(int(GetSamplingRate()/6000.0));
922 m_rect_range_filter.reset(int(GetSamplingRate()/h2f(GetSemitoneMin())));
923 // m_fir_range_filter.setImpulseResponse(fir1_bandpass(104, 2*1000.0/GetSamplingRate(), 2*2000.0/GetSamplingRate()));
924 // m_fir_range_filter.setImpulseResponse(fir1_highpass(int(GetSamplingRate()/h2f(GetSemitoneMin())), 0.5));
925
926 if(m_config_form.ui_rdRangeFilteringRectangular->isChecked())
927 m_range_filter = &m_rect_range_filter;
928 else if(m_config_form.ui_rdRangeFilteringFIR->isChecked())
929 m_range_filter = &m_fir_range_filter;
930 }
931 else
932 m_range_filter = &m_dummy_range_filter;
933
934 m_algo_combedfft->setWindowFactor(m_config_form.ui_spinWindowSizeFactor->value());
935 // m_glFT->m_zp_factor = m_config_form.ui_spinWindowSizeFactor->value();
936 m_algo_combedfft->useAudibilityRatio(m_config_form.ui_chkAlgoUseSubHarmTresh->isChecked());
937 m_algo_combedfft->setAudibilityRatio(invlp(-m_config_form.ui_spinCombedFFTAudibilityRatio->value()));
938 m_algo_combedfft->setAmplitudeTreshold(invlp(double(m_config_form.ui_spinVolumeTreshold->value())));
939 m_algo_combedfft->setComponentTreshold(invlp(double(m_config_form.ui_spinVolumeTreshold->value())));
940
941 // cout << "d" << endl;
942
943 // Quantizers
944 m_quantizer->reset();
945 if(m_config_form.ui_grpQuantizer->isChecked())
946 {
947 m_latency_quantizer.setLatency(m_config_form.ui_spinErrorLatency->value());
948 m_quantizer = &m_latency_quantizer;
949 }
950 else
951 m_quantizer = &m_dummy_quantizer;
952
953 // cout << invlp(-m_config_form.ui_spinCombedFFTAudibilityRatio->value()) << endl;
954
955 if(!pauseAction->isChecked() && !m_capture_thread.isCapturing())
956 m_capture_thread.startCapture();
957 }
958
959 void CustomInstrumentTunerForm::saveSettings()
960 {
961 m_settings.save();
962 View::saveAll();
963
964 // views
965 m_settings.setValue("width", width());
966 m_settings.setValue("height", height());
967 //m_settings.setValue("ui_tbViews", QVariant(ui_tbViews->isVisible()));
968 //m_settings.setValue("ui_tbButtons", ui_tbButtons->isVisible());
969
970 // sound capture
971 m_settings.setValue(m_config_form.ui_cbTransports->objectName(), m_config_form.ui_cbTransports->currentText());
972 #ifdef CAPTURE_QT
973 m_settings.setValue(m_config_form.ui_cbQtDeviceName->objectName(), m_config_form.ui_cbQtDeviceName->currentText());
974 #endif
975 #ifdef CAPTURE_PORTAUDIO
976 m_settings.setValue(m_config_form.ui_cbPortAudioDeviceName->objectName(), m_config_form.ui_cbPortAudioDeviceName->currentText());
977 #endif
978 }
979 void CustomInstrumentTunerForm::loadSettings()
980 {
981 // cout << __FILE__ << ":" << __LINE__ << endl;
982
983 m_settings.load();
984 View::loadAll();
985
986 // views
987 resize(m_settings.value("width", 800).toInt(), m_settings.value("height", 600).toInt());
988 //ui_tbViews->setVisible(m_settings.value("ui_tbViews", ui_tbViews->isVisible()).toBool());
989 //ui_tbButtons->setVisible(m_settings.value("ui_tbButtons", ui_tbButtons->isVisible()).toBool());
990
991 // sound capture
992 QString saved_transport = m_settings.value(m_config_form.ui_cbTransports->objectName(), "").toString();
993 if(saved_transport!="")
994 for(int i=0; i<m_config_form.ui_cbTransports->count(); i++)
995 if(m_config_form.ui_cbTransports->itemText(i)==saved_transport)
996 m_config_form.ui_cbTransports->setCurrentIndex(i);
997
998 #ifdef CAPTURE_QT
999 {
1000 QString saved_device = m_settings.value(m_config_form.ui_cbQtDeviceName->objectName(), "default").toString();
1001 try
1002 {
1003 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
1004
1005 int saved_index = -1;
1006 m_config_form.ui_cbQtDeviceName->clear();
1007 for(int i=0; i<devices.count(); i++)
1008 {
1009 m_config_form.ui_cbQtDeviceName->addItem(devices.at(i).deviceName());
1010 if(devices.at(i).deviceName()==saved_device)
1011 saved_index = i;
1012 }
1013 if(saved_index!=-1)
1014 m_config_form.ui_cbQtDeviceName->setCurrentIndex(saved_index);
1015 }
1016 catch(QString error)
1017 {
1018 // cout << "CustomInstrumentTunerForm: ERROR: " << error << endl;
1019 }
1020 }
1021 #endif
1022
1023 #ifdef CAPTURE_PORTAUDIO
1024 {
1025 QString saved_device = m_settings.value(m_config_form.ui_cbPortAudioDeviceName->objectName(), "default").toString();
1026 try
1027 {
1028 PaError err;
1029 err = Pa_Initialize();
1030 if(err != paNoError)
1031 throw QString("PortAudio: CustomInstrumentTunerForm::loadSettings:Pa_Initialize ")+Pa_GetErrorText(err);
1032 int numDevices = Pa_GetDeviceCount();
1033 // cout << "PortAudio devices:"<< endl;
1034 int saved_index = -1;
1035 m_config_form.ui_cbPortAudioDeviceName->clear();
1036 const PaDeviceInfo* deviceInfo;
1037 for(int i=0; i<numDevices; i++)
1038 {
1039 deviceInfo = Pa_GetDeviceInfo(i);
1040 // cout << " " << QString(deviceInfo->name).toStdString() << endl;
1041 m_config_form.ui_cbPortAudioDeviceName->addItem(QString(deviceInfo->name));
1042 if(QString(deviceInfo->name)==saved_device)
1043 saved_index = i;
1044 }
1045 if(saved_index!=-1)
1046 m_config_form.ui_cbPortAudioDeviceName->setCurrentIndex(saved_index);
1047 }
1048 catch(QString error)
1049 {
1050 // cout << "CustomInstrumentTunerForm: ERROR: " << error << endl;
1051 }
1052 Pa_Terminate();
1053 }
1054 #endif
1055 }
1056
1057 void CustomInstrumentTunerForm::restoreFactorySettings()
1058 {
1059 if(QMessageBox::question(this, tr("Restore Factory Settings"), tr("This operation is NOT reversible.\nAre you sure you want to lose all your current settings ?"), QMessageBox::Yes, QMessageBox::No)==QMessageBox::Yes)
1060 {
1061 m_settings.clear();
1062 View::clearAllSettings();
1063
1064 m_settings.remove("width");
1065 m_settings.remove("height");
1066 m_settings.remove("ui_cbTransports");
1067 //m_settings.remove("ui_tbViews");
1068 //m_settings.remove("ui_tbButtons");
1069
1070 m_settings.remove(m_config_form.ui_cbPortAudioDeviceName->objectName());
1071
1072 QMessageBox::information(this, tr("Restore Factory Settings"), tr("You can now restart FMIT to get back factory settings"), QMessageBox::Ok, QMessageBox::NoButton);
1073 }
1074 }
1075
1076 void CustomInstrumentTunerForm::helpAbout()
1077 {
1078 AboutBox box(this);
1079 box.exec();
1080
1081 // QString text;
1082 // text = "<h2>Free Music Instrument Tuner</h2>";
1083
1084 // QString fmitversion;
1085 // QString fmitversiongit(STR(FMITVERSIONGIT));
1086 // if(fmitversiongit.length()>0) {
1087 // fmitversion = tr("Version ") + fmitversiongit;
1088 // }
1089 // else {
1090 // QFile readmefile(":/fmit/README.txt");
1091 // readmefile.open(QFile::ReadOnly | QFile::Text);
1092 // QTextStream readmefilestream(&readmefile);
1093 // readmefilestream.readLine();
1094 // readmefilestream.readLine();
1095 // readmefilestream.readLine();
1096 // fmitversion = readmefilestream.readLine().simplified();
1097 // }
1098 // text += tr("<h3>")+fmitversion;
1099
1100 // text += tr(" (compiled by ")+QString(COMPILER)+tr(" for ");
1101 // #ifdef Q_PROCESSOR_X86_32
1102 // text += "32bits";
1103 // #endif
1104 // #ifdef Q_PROCESSOR_X86_64
1105 // text += "64bits";
1106 // #endif
1107 // text += ")";
1108
1109 // text += "</h3><p><h3>"+tr("Website: ")+"</h3><p>"+tr("Homepage: ")+"<a href=\"http://gillesdegottex.github.io/fmit/\">http://gillesdegottex.github.io/fmit/</a>";
1110 // text += "<p>"+tr("Development site: ")+"<a href=\"http://github.com/gillesdegottex/fmit\">http://github.com/gillesdegottex/fmit</a>";
1111 //// text += tr("<p>donation link: <a href=\"http://gillesdegottex.github.io/fmit/\">http://gillesdegottex.github.io/fmit/</a>");
1112 // text += "<p><h3>"+tr("Author: ")+"</h3><p>Gilles Degottex <a href=\"mailto:[email protected]\">[email protected]</a>";
1113 //#ifdef PACKAGER_STRING
1114 // if(PACKAGER_STRING!="")
1115 // text += "<p><h3>"+tr("Packager: ")+"</h3><p>"+QString(PACKAGER_STRING).replace(QChar('<'),"[").replace(QChar('>'),"]");
1116 //#endif
1117
1118 // QDialog about_dlg(this);
1119
1120 // QTextBrowser* textBrowser1;
1121 // QPushButton* pushButton1;
1122 // QVBoxLayout* Form2Layout;
1123 // QHBoxLayout* layout1;
1124 // QSpacerItem* spacer1;
1125 // QSpacerItem* spacer2;
1126
1127 // about_dlg.setObjectName("about_box");
1128 // about_dlg.setWindowTitle( tr("About Free Music Instrument Tuner") );
1129
1130 // Form2Layout = new QVBoxLayout( &about_dlg );
1131 // Form2Layout->setMargin(11);
1132 // Form2Layout->setSpacing(6);
1133 // about_dlg.setLayout( Form2Layout );
1134
1135 // textBrowser1 = new QTextBrowser( &about_dlg );
1136 // textBrowser1->setHtml(text);
1137 // textBrowser1->setOpenExternalLinks(true);
1138 // textBrowser1->setWordWrapMode(QTextOption::NoWrap);
1139 // textBrowser1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1140 // textBrowser1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1141 // textBrowser1->document()->adjustSize();
1142 // textBrowser1->setMinimumSize(textBrowser1->document()->size().toSize() + QSize(10, 10));
1143 // Form2Layout->addWidget( textBrowser1 );
1144
1145 // layout1 = new QHBoxLayout();
1146 // layout1->setMargin(0);
1147 // layout1->setSpacing(6);
1148
1149 // spacer1 = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
1150 // layout1->addItem( spacer1 );
1151
1152 // pushButton1 = new QPushButton(&about_dlg);
1153 // pushButton1->setText( tr( "&OK" ) );
1154 // layout1->addWidget( pushButton1 );
1155 // connect(pushButton1, SIGNAL(clicked()), &about_dlg, SLOT(close()));
1156
1157 // spacer2 = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
1158 // layout1->addItem( spacer2 );
1159
1160 // Form2Layout->addLayout( layout1 );
1161
1162 // about_dlg.exec();
1163 }
1164
1165 void CustomInstrumentTunerForm::closeEvent( QCloseEvent* event ){
1166 (void)event;
1167 m_config_form.close();
1168 }
1169 CustomInstrumentTunerForm::~CustomInstrumentTunerForm()
1170 {
1171 if(m_config_form.ui_chkAutoSaveOnExit->isChecked())
1172 saveSettings();
1173 else
1174 {
1175 m_settings.beginGroup("Auto/");
1176 m_settings.save(m_config_form.ui_chkAutoSaveOnExit);
1177 m_settings.endGroup();
1178 }
1179 }
1180
1181 void CustomInstrumentTunerForm::update_localized_note_names()
1182 {
1183 QString transpositionToolTip = tr("Transposition in number of semitones relative to %1.\nUseful to convert note names to a corresponding transposing instrument (e.g. %2 for saxophone, %3 for trumpet).")
1184 .arg(QString::fromStdString(h2n(-9, GetNotesName(), 0, CHROMATIC, true))) // C4
1185 .arg(QString::fromStdString(h2n(-18, GetNotesName(), 0, CHROMATIC, true))) // Eb3
1186 .arg(QString::fromStdString(h2n(-11, GetNotesName(), 0, CHROMATIC, true))); // Bb3
1187 m_config_form.textLabel1_7->setToolTip(transpositionToolTip);
1188 m_config_form.textLabel1_7->setWhatsThis(transpositionToolTip);
1189 m_config_form.ui_spinTransposition->setToolTip(transpositionToolTip);
1190 m_config_form.ui_spinTransposition->setWhatsThis(transpositionToolTip);
1191 m_config_form.ui_lblTransposition->setToolTip(transpositionToolTip);
1192 m_config_form.ui_lblTransposition->setWhatsThis(transpositionToolTip);
1193
1194 QString a4Name = QString::fromStdString(h2n(0, GetNotesName(), 0, CHROMATIC, true));
1195
1196 QString a4FreqToolTip = tr("The base tuning frequency (the %1 frequency)")
1197 .arg(a4Name);
1198 textLabel8->setToolTip(a4FreqToolTip);
1199 textLabel8->setWhatsThis(a4FreqToolTip);
1200 ui_spinAFreq->setToolTip(a4FreqToolTip);
1201 ui_spinAFreq->setWhatsThis(a4FreqToolTip);
1202
1203 QString showA4OffsetText = tr("Allows adding an offset to the reference %1")
1204 .arg(a4Name);
1205 m_config_form.ui_chkShowA4Offset->setText(showA4OffsetText);
1206
1207 QString rangeToolTip = tr("Range of possible notes from the lowest to the highest around the %1 reference note.\nSmaller the range, smaller the CPU usage.")
1208 .arg(a4Name);
1209 m_config_form.textLabel7->setToolTip(rangeToolTip);
1210 m_config_form.textLabel7->setWhatsThis(rangeToolTip);
1211
1212 QString minHTToolTip = tr("Lowest semitone from the %1 reference note.\nIt can useful to put it higher than -35 to avoid disturbance from the sector alimentation (~50hz).")
1213 .arg(a4Name);
1214 m_config_form.ui_spinMinHT->setToolTip(minHTToolTip);
1215 m_config_form.ui_spinMinHT->setWhatsThis(minHTToolTip);
1216 m_config_form.ui_txtMinHT->setToolTip(minHTToolTip);
1217 m_config_form.ui_txtMinHT->setWhatsThis(minHTToolTip);
1218
1219 QString maxHTToolTip = tr("Highest semitone from the %1 reference note.")
1220 .arg(a4Name);
1221 m_config_form.ui_spinMaxHT->setToolTip(maxHTToolTip);
1222 m_config_form.ui_spinMaxHT->setWhatsThis(maxHTToolTip);
1223 m_config_form.ui_txtMaxHT->setToolTip(maxHTToolTip);
1224 m_config_form.ui_txtMaxHT->setWhatsThis(maxHTToolTip);
1225 }
1226
1227 /*
1228 bool displayInBrowser(const QString& theURL)
1229 {
1230 #ifdef _WIN32
1231 //TODO replace with less buggy ShellExecuteEx?
1232 if ((unsigned
1233 int)::ShellExecute(qApp->mainWidget()->winId(),NULL,theURL,NULL,NULL,SW_SHOW)
1234 <= 32)
1235 {
1236 OFMessageBox::criticalMessageOK(QMessageBox::tr("Unable to display a
1237 web browser. Ensure that you have a web browser installed."));
1238 }
1239 #else
1240 //TODO warn if netscape not installed
1241 QString aCommand("netscape ");
1242 aCommand += theURL;
1243 aCommand += " &";
1244 if (system(aCommand) != 0)
1245 {
1246 OFMessageBox::criticalMessageOK(QMessageBox::tr("Unable to display a
1247 netscape browser. You need to have netscape installed and in the
1248 path."));
1249 }
1250
1251 return true;
1252 }
1253 */