GuitarTuner::GuitarTuner(QWidget *parent) : QMainWindow(parent) { // Set up the QML. m_guitarTunerUI = new QDeclarativeView(QUrl("qrc:/src/application.qml"), this); setCentralWidget(m_guitarTunerUI); m_guitarTunerUI->setResizeMode(QDeclarativeView::SizeRootObjectToView); qmlObject = m_guitarTunerUI->rootObject(); // Init audio output and input. initAudioOutput(); initAudioInput(); // Connect the quit signal of m_guitarTunerUI // into the close slot of this. connect(m_guitarTunerUI->engine(), SIGNAL(quit()), SLOT(close())); // Connect the signals from qmlObject into proper slots // of this and m_voicegenerator. connect(qmlObject, SIGNAL(muteStateChanged(bool)), SLOT(muteStateChanged(bool))); connect(qmlObject, SIGNAL(volumeChanged(qreal)), m_voicegenerator, SLOT(setAmplitude(qreal))); connect(qmlObject, SIGNAL(volumeChanged(qreal)), SLOT(setMaxVolumeLevel(qreal))); // Connect the modeChanged signal from qmlObject // into modeChanged slot of this class. connect(qmlObject, SIGNAL(modeChanged(bool)), SLOT(modeChanged(bool))); // Connect the microphoneSensitivityChanged signal from // m_guitarTunerUI into setCutOffPercentage slot of m_analyzer class. connect(qmlObject, SIGNAL(microphoneSensitivityChanged(qreal)), m_analyzer, SLOT(setCutOffPercentage(qreal))); // Connect the signals from m_analyzer into slots of qmlObject. connect(m_analyzer, SIGNAL(lowVoice()), qmlObject, SLOT(lowVoice())); connect(m_analyzer, SIGNAL(correctFrequency()), qmlObject, SLOT(correctFrequencyObtained())); connect(m_analyzer, SIGNAL(voiceDifference(QVariant)), qmlObject, SLOT(voiceDifferenceChanged(QVariant))); // Initialise the MaximumVoiceDifference // value of qmlObject with the value obtained from m_analyzer. qmlObject->setProperty("maxVoiceDifference", m_analyzer->getMaximumVoiceDifference()); // Connect the targetFrequencyChanged signal of qmlObject // into targetFrequencyChanged slot of this class. connect(qmlObject, SIGNAL(targetFrequencyChanged(qreal)), SLOT(targetFrequencyChanged(qreal))); // Start voice output or input by using the modeChanged function, // depending of the current mode. modeChanged(qmlObject->property("isInput").toBool()); }
GuitarTuner::GuitarTuner(QWidget *parent) : QWidget(parent), ui(new Ui::GuitarTuner), m_maximumPrecision(0) { ui->setupUi(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(setNoteInChromatic())); timer->start(5); 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->modeButton, SIGNAL(clicked()), SLOT(toggleInputOrOutput())); connect(ui->buttonNext, SIGNAL(clicked()), SLOT(next())); connect(ui->buttonPrev, SIGNAL(clicked()), SLOT(prev())); initAudioInput(); initAudioOutput(); connect(this, SIGNAL(volumeChanged(qreal)), m_voicegenerator, SLOT(setAmplitude(qreal))); connect(this, SIGNAL(microphoneSensitivityChanged(qreal)),m_analyzer, SLOT(setCutOffPercentage(qreal))); connect(m_analyzer, SIGNAL(lowVoice()),this, SLOT(lowVoice())); connect(m_analyzer, SIGNAL(correctFrequency()), this, SLOT(correctFrequencyObtained())); modeChanged(m_outputActive); toggleInputOrOutput(); }
void VoiceAnalyzer::analyzeVoice() { m_fftHelper->calculateFFT(m_samples); int index = m_fftHelper->getMaximumDensityIndex(); if(index == -1) { emit lowVoice(); qDebug() << "low voice"; return; } qreal stepSizeInFrequency = (qreal)m_format.sampleRate() / (m_totalSampleCount * m_stepSize); qreal newFrequency = (qreal)(index) * stepSizeInFrequency; m_currentFrequency = newFrequency; int correctIndex = qRound(m_frequency / stepSizeInFrequency); qreal value = 0; if (m_frequency > newFrequency * TargetFrequencyParameter){ qDebug() << "compare" << "low" << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << (m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; value = -m_maximumVoiceDifference; } else if (m_frequency * TargetFrequencyParameter < newFrequency){ qDebug() << "compare" << "high" << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << ( m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; value = m_maximumVoiceDifference; } else { value = log(newFrequency / (stepSizeInFrequency * correctIndex)) * 12 / M_LN2; qDebug() << "compare" << value << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << ( m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; } QVariant valueVar(value); emit voiceDifference(valueVar); if(correctIndex == index){ emit(correctFrequency()); } }
/** * Analyzes the voice frequency and emits appropriate signals. */ void VoiceAnalyzer::analyzeVoice() { m_fftHelper->calculateFFT(m_samples); int index = m_fftHelper->getMaximumDensityIndex(); // If index == -1 if (index == -1) { // The voice is to be filtered away. // Emit the lowVoice signal and return. emit lowVoice(); qDebug() << "low voice"; return; } // Else, continue // Let the correctIndex to be // the nearest index corresponding to the correct frequency. qreal stepSizeInFrequency = (qreal)m_format.sampleRate() / (m_totalSampleCount * m_stepSize); qreal newFrequency = qreal(index) * stepSizeInFrequency; // Calculate the nearest index corresponding to the correct frequency. int correctIndex = qRound(m_frequency / stepSizeInFrequency); qreal value = 0; // If the obtained frequency is more than // log_2(TargetFrequencyParameter) octaves less than the m_frequency: // Note: // Instead of m_frequency/TargetFrequencyParameter > newFrequency, // the comparison is done without a div instructions by // m_frequency > newFrequency * TargetFrequencyParameter. if (m_frequency > newFrequency * TargetFrequencyParameter) { // Set the difference value to be -m_maximumVoiceDifference. qDebug() << "compare" << "low" << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << (m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; value = -m_maximumVoiceDifference; } // Else, if the obtained frequency is more than // log_2(TargetFrequencyParameter) octaves more than the m_frequency: else if (m_frequency*TargetFrequencyParameter < newFrequency) { // Set the difference value to be m_maximumVoiceDifference. qDebug() << "compare" << "high" << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << (m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; value = m_maximumVoiceDifference; } // Else: else { // Calculate the difference between the obtained and the correct // frequency in tones. // Use stepSizeInFrequency * correctIndex instead of // m_frequency so that the value is zero when there is correct // voice obtained. Set the difference value to be // log(frequency / target frequency) * 12 / log(2). value = log(newFrequency / (stepSizeInFrequency * correctIndex)) * 12 / M_LN2; qDebug() << "compare" << value << newFrequency << m_frequency - stepSizeInFrequency * correctIndex << (m_frequency - stepSizeInFrequency * correctIndex) / stepSizeInFrequency; } // Emit voiceDifference signal. QVariant valueVar(value); //Has to be QVariant for QML emit voiceDifference(valueVar); // If the correctIndex is index, emit the correctFrequency signal. if (correctIndex == index) { emit(correctFrequency()); } }