summaryrefslogtreecommitdiffstats
path: root/demos/mobile/guitartuner/src/guitartunerui.cpp
blob: 24357b3b1244ca2b59f2ac5990c61c415a78d507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QTimer>

#include "guitartunerui.h"
#include "ui_guitartunerui.h"

GuitarTunerUI::GuitarTunerUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GuitarTunerUI),
    m_maximumPrecision(0)
{
    ui->setupUi(this);

    // Set up the class attributes to proper values.
    m_outputActive = false;
    m_muted = false;
    m_outputVolumeLevel = getVolumeFromSoundSlider();
    m_inputVolumeLevel = 1.0 - m_outputVolumeLevel;

    // Set up the current tone, the frequency, and the name for it.
    m_currentToneIndex = 5;
    updateFrequencyByToneIndex(m_currentToneIndex);

    // Connect the signals from UI into proper slots.
    connect(ui->soundSlider, SIGNAL(valueChanged(int)),
            SLOT(changeVolume()));
    connect(ui->soundButton, SIGNAL(toggled(bool)),
            SLOT(toggleSound(bool)));
    connect(ui->modeButton, SIGNAL(clicked()),
            SLOT(toggleInputOrOutput()));
    connect(ui->buttonNext, SIGNAL(clicked()), SLOT(next()));
    connect(ui->buttonPrev, SIGNAL(clicked()), SLOT(prev()));

    // Initialise up the UI by calling toggleInputOrOutput
    // for the first time.
    toggleInputOrOutput();
}

GuitarTunerUI::~GuitarTunerUI()
{
    delete ui;
}

void GuitarTunerUI::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/**
  * Returns a value from 0 to 1, representing the volume.
  */
qreal GuitarTunerUI::getVolumeFromSoundSlider() const
{
    qreal value = ui->soundSlider->value();
    return value/ui->soundSlider->maximum();
}

/**
  * Updates the m_currentToneFrequency and m_currentToneString,
  * according to the given index. Also updates the UI.
  */
void GuitarTunerUI::updateFrequencyByToneIndex(int index)
{

    switch (index) {
    case 0: {
            m_currentToneFrequency = FrequencyE;
            m_currentToneString = "E";
            break;
        }
    case 1: {
            m_currentToneFrequency = FrequencyA;
            m_currentToneString = "A";
            break;
        }
    case 2: {
            m_currentToneFrequency = FrequencyD;
            m_currentToneString = "D";
            break;
        }
    case 3: {
            m_currentToneFrequency = FrequencyG;
            m_currentToneString = "G";
            break;
        }
    case 4: {
            m_currentToneFrequency = FrequencyB;
            m_currentToneString = "B";
            break;
        }
    case 5: {
            m_currentToneFrequency = FrequencyE2;
            m_currentToneString = "e";
            break;
        }
    default: {
            qDebug() << "invalid index!" << index;
        }
    }
    // Set the noteLabel text according to the current tone.
    ui->noteLabel->setText(m_currentToneString);
}

/**
  * Returns the volume.
  */
qreal GuitarTunerUI::getVolume() const
{
    return m_outputVolumeLevel;
}

/**
  * Returns true if the sound is muted.
  */
bool GuitarTunerUI::getMuteState() const
{
    return m_muted;
}


/**
  * Returns the microphone sensitivity.
  */
qreal GuitarTunerUI::getMicrophoneSensitivity() const
{
    return m_inputVolumeLevel;
}

/**
  * Returns whether the input mode is active.
  */
bool GuitarTunerUI::isInputModeActive() const
{
    return !m_outputActive;
}

/**
  * Returns the current target frequency.
  */
qreal GuitarTunerUI::getFrequency() const
{
    return m_currentToneFrequency;
}

/**
  * Toggles the sound according to the parameter.
  * Has no effect if output is not active.
  */
void GuitarTunerUI::toggleSound(bool noSound)
{
    if (!m_outputActive) {
        return;
    }
    m_muted = noSound;
    emit muteChanged(m_muted);
}

/**
  * Changes the volume or microphone sensitivity.
  */
void GuitarTunerUI::changeVolume()
{
    qreal resultingAmplitude = getVolumeFromSoundSlider();
    qDebug() << "resultingAmplitude" << resultingAmplitude;
    if (m_outputActive) {
        m_outputVolumeLevel = resultingAmplitude;
        emit volumeChanged(resultingAmplitude);
    }
    else {
        m_inputVolumeLevel = resultingAmplitude;
        emit microphoneSensitivityChanged(1.0-resultingAmplitude);
    }
}

/**
  * Toggles input or output, depending of the current state.
  */
void GuitarTunerUI::toggleInputOrOutput()
{
    // If output mode is active:
    if (m_outputActive) {
        // Change UI to correspond to the input mode.
        m_outputActive = false;
        ui->soundSlider->setValue(m_inputVolumeLevel*100);
        ui->soundButton->setDisabled(true);
        ui->soundButton->hide();
        ui->micSensitivityLabel->show();
        emit modeChanged(true);
        ui->modeButton->setText("To tone mode");
    }
    // Else:
    else {
        // Change UI to correspond to the output mode.
        m_outputActive = true;
        ui->soundSlider->setValue(m_outputVolumeLevel*100);
        ui->soundButton->setDisabled(false);
        ui->micSensitivityLabel->hide();
        ui->soundButton->show();
        emit modeChanged(false);
        ui->modeButton->setText("To listen mode");
    }
}

/**
  * Receives the low voice signal.
  */
void GuitarTunerUI::lowVoice()
{
    if (ui->noteLabel->font().bold()) {
        QFont font;
        font.setBold(false);
        font.setUnderline(false);
        ui->noteLabel->setFont(font);
    }
}

/**
  * Receives the voice difference signal.
  * The difference is qreal, where increase of 1 corresponds
  * to increase of 1 tone to the target frequency.
  */
void GuitarTunerUI::voiceDifference(qreal difference)
{
    if (ui->noteLabel->font().bold()) {
        QFont font;
        font.setBold(false);
        font.setUnderline(false);
        ui->noteLabel->setFont(font);
    }
    ui->correctSoundSlider->setValue(difference*m_maximumPrecision);
}

/**
  * Receives the correct frequency signal.
  * Makes the UI to visualize correct frequency event.
  */
void GuitarTunerUI::correctFrequencyObtained()
{
    qDebug() << "CORRECT FREQUENCY";
    QFont font;
    font.setBold(true);
    font.setUnderline(true);
    ui->noteLabel->setFont(font);
}

/**
  * Sets up the maximum voice difference.
  */
void GuitarTunerUI::setMaximumVoiceDifference(int max)
{
    // Assert that the maximum precision is known.
    Q_ASSERT(m_maximumPrecision != 0);
    // Set the maximum and minimum values of the correctSoundSlider
    // to the +- max*m_maximumPrecision, and set the tick interval
    // to be m_maximumPrecision.
    ui->correctSoundSlider->setMaximum(max*m_maximumPrecision);
    ui->correctSoundSlider->setMinimum(-max*m_maximumPrecision);
    ui->correctSoundSlider->setTickInterval(max*m_maximumPrecision);
}

/**
  * Stores the maximum precision per note. Used to setup the
  * correct sound slider.
  */
void GuitarTunerUI::setMaximumPrecisionPerNote(int max)
{
    m_maximumPrecision = max;
}

/**
  * Changes the tone to the next value.
  */
void GuitarTunerUI::next()
{
    changeTone((m_currentToneIndex + 1) % 6);
}

/**
  * Changes the tone to the previous value.
  */
void GuitarTunerUI::prev()
{
    changeTone((m_currentToneIndex + 5) % 6);
}

/**
  * Changes the tone according to the new index.
  */
void GuitarTunerUI::changeTone(int newIndex)
{
    m_currentToneIndex = newIndex;
    updateFrequencyByToneIndex(m_currentToneIndex);
    qDebug() << "targetFrequencyChanged" << m_currentToneFrequency;
    emit targetFrequencyChanged(m_currentToneFrequency);
}