guitartunerui.cpp Example File
demos/mobile/guitartuner/src/guitartunerui.cpp
 
 
 #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);
     
     m_outputActive = false;
     m_muted = false;
     m_outputVolumeLevel = getVolumeFromSoundSlider();
     m_inputVolumeLevel = 1.0 - m_outputVolumeLevel;
     
     m_currentToneIndex = 5;
     updateFrequencyByToneIndex(m_currentToneIndex);
     
     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()));
     
     
     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;
     }
 }
 
 qreal GuitarTunerUI::getVolumeFromSoundSlider() const
 {
     qreal value = ui->soundSlider->value();
     return value/ui->soundSlider->maximum();
 }
 
 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;
         }
     }
     
     ui->noteLabel->setText(m_currentToneString);
 }
 
 qreal GuitarTunerUI::getVolume() const
 {
     return m_outputVolumeLevel;
 }
 
 bool GuitarTunerUI::getMuteState() const
 {
     return m_muted;
 }
 
 qreal GuitarTunerUI::getMicrophoneSensitivity() const
 {
     return m_inputVolumeLevel;
 }
 
 bool GuitarTunerUI::isInputModeActive() const
 {
     return !m_outputActive;
 }
 
 qreal GuitarTunerUI::getFrequency() const
 {
     return m_currentToneFrequency;
 }
 
 void GuitarTunerUI::toggleSound(bool noSound)
 {
     if (!m_outputActive) {
         return;
     }
     m_muted = noSound;
     emit muteChanged(m_muted);
 }
 
 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);
     }
 }
 
 void GuitarTunerUI::toggleInputOrOutput()
 {
     
     if (m_outputActive) {
         
         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 {
         
         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");
     }
 }
 
 void GuitarTunerUI::lowVoice()
 {
     if (ui->noteLabel->font().bold()) {
         QFont font;
         font.setBold(false);
         font.setUnderline(false);
         ui->noteLabel->setFont(font);
     }
 }
 
 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);
 }
 
 void GuitarTunerUI::correctFrequencyObtained()
 {
     qDebug() << "CORRECT FREQUENCY";
     QFont font;
     font.setBold(true);
     font.setUnderline(true);
     ui->noteLabel->setFont(font);
 }
 
 void GuitarTunerUI::setMaximumVoiceDifference(int max)
 {
     
     Q_ASSERT(m_maximumPrecision != 0);
     
     
     
     ui->correctSoundSlider->setMaximum(max*m_maximumPrecision);
     ui->correctSoundSlider->setMinimum(-max*m_maximumPrecision);
     ui->correctSoundSlider->setTickInterval(max*m_maximumPrecision);
 }
 
 void GuitarTunerUI::setMaximumPrecisionPerNote(int max)
 {
     m_maximumPrecision = max;
 }
 
 void GuitarTunerUI::next()
 {
     changeTone((m_currentToneIndex + 1) % 6);
 }
 
 void GuitarTunerUI::prev()
 {
     changeTone((m_currentToneIndex + 5) % 6);
 }
 
 void GuitarTunerUI::changeTone(int newIndex)
 {
     m_currentToneIndex = newIndex;
     updateFrequencyByToneIndex(m_currentToneIndex);
     qDebug() << "targetFrequencyChanged" << m_currentToneFrequency;
     emit targetFrequencyChanged(m_currentToneFrequency);
 }