// Copyright 2004 "Gilles Degottex" // This file is part of "fmit" // "fmit" is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // "fmit" is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // TODO zoom with a rectangle #include "GLFT.h" #include using namespace std; #include #include #include #include #include #include #include #include using namespace Music; #include using namespace Math; GLFT::GLFT(QWidget* parent) : QOpenGLWidget(parent) , View(tr("Fourier Transform"), this) , m_font("Helvetica") , m_components_max(1.0) { m_start_move_mouse = true; m_os = 2; // over sampling factor // settings setting_show->setIcon(QIcon(":/fmit/ui/images/module_fourier.svg")); setting_show->setChecked(false); hide(); setting_db_scale = new QAction(tr("dB scale"), this); setting_db_scale->setCheckable(true); connect(setting_db_scale, SIGNAL(toggled(bool)), this, SLOT(dBScaleChanged(bool))); setting_db_scale->setChecked(true); m_popup_menu.addAction(setting_db_scale); resetaxis(); QHBoxLayout* sizeActionLayout = new QHBoxLayout(&m_popup_menu); QLabel* sizeActionTitle = new QLabel(tr("Size"), &m_popup_menu); sizeActionLayout->addWidget(sizeActionTitle); setting_winlen = new QSpinBox(&m_popup_menu); setting_winlen->setObjectName("setting_winlen"); setting_winlen->setMinimum(1); setting_winlen->setMaximum(1000); setting_winlen->setSingleStep(1); setting_winlen->setValue(20); setting_winlen->setSuffix(" ms"); setting_winlen->setToolTip(tr("window length")); connect(setting_winlen, SIGNAL(valueChanged(int)), this, SLOT(spinWinLengthChanged(int))); sizeActionLayout->addWidget(setting_winlen); QWidget* sizeActionWidget = new QWidget(&m_popup_menu); sizeActionWidget->setLayout(sizeActionLayout); QWidgetAction* sizeAction = new QWidgetAction(&m_popup_menu); sizeAction->setDefaultWidget(sizeActionWidget); m_popup_menu.addAction(sizeAction); QWidgetAction* helpCaption01 = new QWidgetAction(&m_popup_menu); helpCaption01->setDefaultWidget(new Title(tr("- Press left mouse button to move the view"), &m_popup_menu)); m_popup_menu.addAction(helpCaption01); QWidgetAction* helpCaption02 = new QWidgetAction(&m_popup_menu); helpCaption02->setDefaultWidget(new Title(tr("- Press SHIFT key and left mouse button to zoom in and out"), &m_popup_menu)); m_popup_menu.addAction(helpCaption02); QWidgetAction* helpCaption03 = new QWidgetAction(&m_popup_menu); helpCaption03->setDefaultWidget(new Title(tr("- Double-click to reset the view"), &m_popup_menu)); m_popup_menu.addAction(helpCaption03); s_settings->add(setting_winlen); spinWinLengthChanged(setting_winlen->value()); } void GLFT::refreshGraph() { while(int(buff.size())>m_plan.size()) buff.pop_back(); } void GLFT::setSamplingRate(int sr) { m_maxf=sr/2; } void GLFT::resetaxis() { m_minf=0; m_maxf=Music::GetSamplingRate()/2; // sr is surely -1 because not yet defined if(setting_db_scale->isChecked()) { m_minA = -50; // [dB] m_maxA = 100; // [dB] } else { m_maxA = 1; // [amplitude] } } void GLFT::dBScaleChanged(bool db) { Q_UNUSED(db) resetaxis(); update(); } void GLFT::spinWinLengthChanged(int num) { if(Music::GetSamplingRate()>0) { // Create window int winlen = int(num/1000.0*Music::GetSamplingRate()); win = hann(winlen); double win_sum = 0.0; // normalize the window in energy for(size_t i=0; iposition().x(); old_y = e->position().y(); m_start_move_mouse = false; } int dx = e->position().x() - old_x; int dy = e->position().y() - old_y; if(Qt::LeftButton & e->buttons()) { if(Qt::ShiftModifier & e->modifiers()) { double f = (m_maxf-m_minf)*double(m_press_x)/width()+m_minf; double zx = double(m_press_x-e->position().x())/width(); zx = pow(8, zx); m_minf = f - zx*(f-m_press_minf); m_maxf = f + zx*(m_press_maxf-f); } else { m_minf -= (m_maxf-m_minf)*dx/width(); m_maxf -= (m_maxf-m_minf)*dx/width(); if(setting_db_scale->isChecked()) { m_minA += (m_maxA-m_minA)*dy/height(); m_maxA += (m_maxA-m_minA)*dy/height(); } else m_maxA -= m_maxA*double(dy)/height(); } update(); } old_x = e->position().x(); old_y = e->position().y(); } void GLFT::paintGL() { glClear(GL_COLOR_BUFFER_BIT); if(win.size()>0 && int(buff.size())>=m_plan.size()) { // Use last samples while(int(buff.size())>m_plan.size()) buff.pop_back(); int sr = Music::GetSamplingRate(); // cout << m_plan.size() << endl; // name glColor3f(0.75,0.75,0.75); QPainter painter(this); m_font.setPixelSize(20); painter.setFont(m_font); painter.drawText(2, 20, tr("Fourier Transform")); painter.end(); for(int i=0; i