1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
43 #include "guitartunerui.h"
44 #include "ui_guitartunerui.h"
46 GuitarTunerUI::GuitarTunerUI(QWidget *parent) :
48 ui(new Ui::GuitarTunerUI),
53 // Set up the class attributes to proper values.
54 m_outputActive = false;
56 m_outputVolumeLevel = getVolumeFromSoundSlider();
57 m_inputVolumeLevel = 1.0 - m_outputVolumeLevel;
59 // Set up the current tone, the frequency, and the name for it.
60 m_currentToneIndex = 5;
61 updateFrequencyByToneIndex(m_currentToneIndex);
63 // Connect the signals from UI into proper slots.
64 connect(ui->soundSlider, SIGNAL(valueChanged(int)),
65 SLOT(changeVolume()));
66 connect(ui->soundButton, SIGNAL(toggled(bool)),
67 SLOT(toggleSound(bool)));
68 connect(ui->modeButton, SIGNAL(clicked()),
69 SLOT(toggleInputOrOutput()));
70 connect(ui->buttonNext, SIGNAL(clicked()), SLOT(next()));
71 connect(ui->buttonPrev, SIGNAL(clicked()), SLOT(prev()));
73 // Initialise up the UI by calling toggleInputOrOutput
74 // for the first time.
75 toggleInputOrOutput();
78 GuitarTunerUI::~GuitarTunerUI()
83 void GuitarTunerUI::changeEvent(QEvent *e)
85 QWidget::changeEvent(e);
87 case QEvent::LanguageChange:
88 ui->retranslateUi(this);
96 * Returns a value from 0 to 1, representing the volume.
98 qreal GuitarTunerUI::getVolumeFromSoundSlider() const
100 qreal value = ui->soundSlider->value();
101 return value/ui->soundSlider->maximum();
105 * Updates the m_currentToneFrequency and m_currentToneString,
106 * according to the given index. Also updates the UI.
108 void GuitarTunerUI::updateFrequencyByToneIndex(int index)
113 m_currentToneFrequency = FrequencyE;
114 m_currentToneString = "E";
118 m_currentToneFrequency = FrequencyA;
119 m_currentToneString = "A";
123 m_currentToneFrequency = FrequencyD;
124 m_currentToneString = "D";
128 m_currentToneFrequency = FrequencyG;
129 m_currentToneString = "G";
133 m_currentToneFrequency = FrequencyB;
134 m_currentToneString = "B";
138 m_currentToneFrequency = FrequencyE2;
139 m_currentToneString = "e";
143 qDebug() << "invalid index!" << index;
146 // Set the noteLabel text according to the current tone.
147 ui->noteLabel->setText(m_currentToneString);
151 * Returns the volume.
153 qreal GuitarTunerUI::getVolume() const
155 return m_outputVolumeLevel;
159 * Returns true if the sound is muted.
161 bool GuitarTunerUI::getMuteState() const
168 * Returns the microphone sensitivity.
170 qreal GuitarTunerUI::getMicrophoneSensitivity() const
172 return m_inputVolumeLevel;
176 * Returns whether the input mode is active.
178 bool GuitarTunerUI::isInputModeActive() const
180 return !m_outputActive;
184 * Returns the current target frequency.
186 qreal GuitarTunerUI::getFrequency() const
188 return m_currentToneFrequency;
192 * Toggles the sound according to the parameter.
193 * Has no effect if output is not active.
195 void GuitarTunerUI::toggleSound(bool noSound)
197 if (!m_outputActive) {
201 emit muteChanged(m_muted);
205 * Changes the volume or microphone sensitivity.
207 void GuitarTunerUI::changeVolume()
209 qreal resultingAmplitude = getVolumeFromSoundSlider();
210 qDebug() << "resultingAmplitude" << resultingAmplitude;
211 if (m_outputActive) {
212 m_outputVolumeLevel = resultingAmplitude;
213 emit volumeChanged(resultingAmplitude);
216 m_inputVolumeLevel = resultingAmplitude;
217 emit microphoneSensitivityChanged(1.0-resultingAmplitude);
222 * Toggles input or output, depending of the current state.
224 void GuitarTunerUI::toggleInputOrOutput()
226 // If output mode is active:
227 if (m_outputActive) {
228 // Change UI to correspond to the input mode.
229 m_outputActive = false;
230 ui->soundSlider->setValue(m_inputVolumeLevel*100);
231 ui->soundButton->setDisabled(true);
232 ui->soundButton->hide();
233 ui->micSensitivityLabel->show();
234 emit modeChanged(true);
235 ui->modeButton->setText("To tone mode");
239 // Change UI to correspond to the output mode.
240 m_outputActive = true;
241 ui->soundSlider->setValue(m_outputVolumeLevel*100);
242 ui->soundButton->setDisabled(false);
243 ui->micSensitivityLabel->hide();
244 ui->soundButton->show();
245 emit modeChanged(false);
246 ui->modeButton->setText("To listen mode");
251 * Receives the low voice signal.
253 void GuitarTunerUI::lowVoice()
255 if (ui->noteLabel->font().bold()) {
258 font.setUnderline(false);
259 ui->noteLabel->setFont(font);
264 * Receives the voice difference signal.
265 * The difference is qreal, where increase of 1 corresponds
266 * to increase of 1 tone to the target frequency.
268 void GuitarTunerUI::voiceDifference(qreal difference)
270 if (ui->noteLabel->font().bold()) {
273 font.setUnderline(false);
274 ui->noteLabel->setFont(font);
276 ui->correctSoundSlider->setValue(difference*m_maximumPrecision);
280 * Receives the correct frequency signal.
281 * Makes the UI to visualize correct frequency event.
283 void GuitarTunerUI::correctFrequencyObtained()
285 qDebug() << "CORRECT FREQUENCY";
288 font.setUnderline(true);
289 ui->noteLabel->setFont(font);
293 * Sets up the maximum voice difference.
295 void GuitarTunerUI::setMaximumVoiceDifference(int max)
297 // Assert that the maximum precision is known.
298 Q_ASSERT(m_maximumPrecision != 0);
299 // Set the maximum and minimum values of the correctSoundSlider
300 // to the +- max*m_maximumPrecision, and set the tick interval
301 // to be m_maximumPrecision.
302 ui->correctSoundSlider->setMaximum(max*m_maximumPrecision);
303 ui->correctSoundSlider->setMinimum(-max*m_maximumPrecision);
304 ui->correctSoundSlider->setTickInterval(max*m_maximumPrecision);
308 * Stores the maximum precision per note. Used to setup the
309 * correct sound slider.
311 void GuitarTunerUI::setMaximumPrecisionPerNote(int max)
313 m_maximumPrecision = max;
317 * Changes the tone to the next value.
319 void GuitarTunerUI::next()
321 changeTone((m_currentToneIndex + 1) % 6);
325 * Changes the tone to the previous value.
327 void GuitarTunerUI::prev()
329 changeTone((m_currentToneIndex + 5) % 6);
333 * Changes the tone according to the new index.
335 void GuitarTunerUI::changeTone(int newIndex)
337 m_currentToneIndex = newIndex;
338 updateFrequencyByToneIndex(m_currentToneIndex);
339 qDebug() << "targetFrequencyChanged" << m_currentToneFrequency;
340 emit targetFrequencyChanged(m_currentToneFrequency);