Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
optionspageaudioinputoutputpage.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
21 #include <QMessageBox>
22 #include "../../../core/core.h"
23 #include "../../../core/audio/player/audioplayeradapter.h"
24 #include "core/config.h"
25 #include "../../../core/audio/recorder/audiorecorderadapter.h"
26 #include "../../volumecontrollevel.h"
27 #include "../../mainwindow.h"
28 #include "../../settingsforqt.h"
29 
30 namespace options {
31 
33  : mOptionsDialog(optionsDialog),
34  mAudioBase(nullptr),
35  mMode(mode) {
36 
37  if (mode == QAudio::AudioInput) {
38  mAudioBase = optionsDialog->getCore()->getAudioRecorder();
39  } else {
40  mAudioBase = optionsDialog->getCore()->getAudioPlayer();
41  }
42 
43  QGridLayout *inputLayout = new QGridLayout;
44  this->setLayout(inputLayout);
45 
46  inputLayout->setColumnStretch(1, 1);
47 
48  mDeviceSelection = new QComboBox;
49 
50  QList<QAudioDeviceInfo> deviceInfos(QAudioDeviceInfo::availableDevices(mode));
51  for (QAudioDeviceInfo info : deviceInfos) {
52  if (!info.isFormatSupported(info.preferredFormat())) {
53  // no supported formats, dont list
54  continue;
55  }
56  mDeviceSelection->addItem(info.deviceName(), QVariant::fromValue<QAudioDeviceInfo>(info));
57  }
58 
59  QPushButton *selectDefaultInputDeviceButton = new QPushButton(tr("Default"));
60 
61  inputLayout->addWidget(new QLabel(tr("Input device")), 0, 0);
62  inputLayout->addWidget(mDeviceSelection, 0, 1);
63  inputLayout->addWidget(selectDefaultInputDeviceButton, 0, 2);
64 
65  // sampling rate
66  QPushButton *selectDefaultSamplingRateButton = new QPushButton(tr("Default"));
67  inputLayout->addWidget(new QLabel(tr("Sampling rates")), 1, 0);
68  inputLayout->addWidget(mSamplingRates = new QComboBox, 1, 1);
69  inputLayout->addWidget(selectDefaultSamplingRateButton, 1, 2);
70 
71  // add auto configure input device to input page
72  /*if (mode == QAudio::AudioInput) {
73  QPushButton *autoConfig = new QPushButton(tr("Auto configure"));
74  VolumeControlLevel *controlLevel = new VolumeControlLevel(this);
75  controlLevel->setTextVisible(false);
76 
77  inputLayout->addWidget(new QLabel(tr("Input level")), 5, 0);
78  inputLayout->addWidget(controlLevel, 5, 1);
79  inputLayout->addWidget(autoConfig, 5, 2);
80 
81  QObject::connect(autoConfig, SIGNAL(clicked()), mOptionsDialog->getMainWindow(), SLOT(onAutoConfigureInputLevel()));
82  }*/
83 
84  // add open sound settings button
85 
87  QPushButton *openSystemSettingsButton = new QPushButton(tr("Open system settings"));
88  inputLayout->addWidget(openSystemSettingsButton, 10, 0);
89  QObject::connect(openSystemSettingsButton, SIGNAL(clicked()), mOptionsDialog->getMainWindow(), SLOT(onOpenSoundControl()));
90  }
91 
92  // add stretch
93  inputLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding), 20, 0);
94 
95 
96  // set current values
97  mDeviceSelection->setCurrentText(QString::fromStdString(mAudioBase->getDeviceName()));
99  mSamplingRates->setCurrentText(QString("%1").arg(mAudioBase->getSamplingRate()));
100 
101  // connect widgets
102  QObject::connect(mDeviceSelection, SIGNAL(currentIndexChanged(int)), this, SLOT(onDeviceSelectionChanged(int)));
103 
104  QObject::connect(selectDefaultInputDeviceButton, SIGNAL(clicked()), this, SLOT(onDefaultDevice()));
105  QObject::connect(selectDefaultSamplingRateButton, SIGNAL(clicked()), this, SLOT(onDefaultSamplingRate()));
106 
107  // notify if changes are made
108  QObject::connect(mDeviceSelection, SIGNAL(currentIndexChanged(int)), optionsDialog, SLOT(onChangesMade()));
109  QObject::connect(mSamplingRates, SIGNAL(currentIndexChanged(int)), optionsDialog, SLOT(onChangesMade()));
110 }
111 
113  assert(mSamplingRates->currentIndex() != -1);
114  assert(mSamplingRates->currentText().isEmpty() == false);
115 
116  mAudioBase->stop();
117  mAudioBase->exit();
118  if (mMode == QAudio::AudioOutput) {
121  } else {
124  }
125  mAudioBase->setDeviceName(mDeviceSelection->currentText().toStdString());
126  mAudioBase->setSamplingRate(mSamplingRates->currentText().toInt());
127  mAudioBase->init();
128  mAudioBase->start();
129 }
130 
132  QAudioDeviceInfo info(mDeviceSelection->itemData(row).value<QAudioDeviceInfo>());
133 
134  mSamplingRates->clear();
135 
136  for (int rate : info.supportedSampleRates()) {
137  if (rate < 9000) {
138  // drop out 8000 sampling rate. Frequencies above 4000 Hz cant be recorded/played
139  // elsewise. This is the uppermost key!
140  continue;
141  }
142  mSamplingRates->addItem(QString("%1").arg(rate));
143  }
145 }
146 
148  if (mMode == QAudio::AudioInput) {
149  mDeviceSelection->setCurrentText(QAudioDeviceInfo::defaultInputDevice().deviceName());
150  }
151  else {
152  mDeviceSelection->setCurrentText(QAudioDeviceInfo::defaultOutputDevice().deviceName());
153  }
155 }
156 
158  QAudioDeviceInfo info(mDeviceSelection->currentData().value<QAudioDeviceInfo>());
159  if (info.deviceName().isEmpty()) {
160  // no device found
161  mSamplingRates->setCurrentText(QString());
162  return;
163  }
164 
165  // use 22050/44100 as default sampling rate if the device supports it,
166  // since the synthesizer cant handle too many samples
167  // and we need at least 11025 samples for the fft.
168  if (mMode == QAudio::AudioOutput && info.supportedSampleRates().contains(22050)) {
169  mSamplingRates->setCurrentText(QString("22050"));
170  } else if (mMode == QAudio::AudioInput && info.supportedSampleRates().contains(44100)) {
171  mSamplingRates->setCurrentText(QString("44100"));
172  } else {
173  // select prefered
174  mSamplingRates->setCurrentText(QString("%1").arg(info.preferredFormat().sampleRate()));
175  if (info.preferredFormat().sampleRate() < 9000) {
176  QMessageBox::warning(this, tr("Warning"), tr("You need at least a sampling rate of %1 to record and play all keys.").arg(11025));
177  }
178  }
179 }
180 
181 } // namespace options
virtual void start()=0
Start/restart the audio device.
PageAudioInputOutput(OptionsDialog *optionsDialog, QAudio::Mode mode)
const std::string & getDeviceName() const
Get a readable string of the name of the audio device.
Definition: audiobase.cpp:52
MainWindow * getMainWindow() const
Definition: optionsdialog.h:63
AudioPlayerAdapter * getAudioPlayer()
Definition: core.h:66
void setOutputDeviceName(const QString &s)
Setter function for mOutputDeviceName.
virtual void stop()=0
Stop the audio device.
void setDeviceName(const std::string &n)
Set the device name.
Definition: audiobase.cpp:65
int getSamplingRate() const
Get the actual sampling rate.
Definition: audiobase.cpp:78
bool isSoundControlSupported()
Definition: mainwindow.cpp:559
void setInputDeviceName(const QString &s)
Setter function for mInputDeviceName.
virtual void exit()=0
Destroy the audio device.
Core * getCore() const
Definition: optionsdialog.h:62
void setInputDeviceSamplingRate(int rate)
Setter function for mInputDeviceSamplingRate.
virtual void init()=0
Inizialize the audio device.
virtual void setSamplingRate(int rate)
Allow the implementation to change the sampling rate during operation.
Definition: audiobase.cpp:96
void setOutputDeviceSamplingRate(int rate)
Setter function for mOutputDeviceSamplingRate.
static SettingsForQt & getSingleton()
Getter function for the singleton.
AudioRecorderAdapter * getAudioRecorder()
Definition: core.h:65