-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
139 lines (114 loc) · 3.97 KB
/
mainwindow.cpp
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
/*
Copyright (C) 2014 Mario Stephan <[email protected]>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopServices>
#include <QFileDialog>
#include <QUrl>
#include <QTimer>
#include <QLabel>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include "trackanalyser.h"
#include "player.h"
//Evaluation project to improve the trackanalyser of Knowthelist
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//the analyser which needs improment
trackanalyser = new TrackAnalyser(this);
connect(trackanalyser, SIGNAL(finishTempo()),this,SLOT(analyseTempoFinished()));
//a player to see and hear
player = new Player(this);
player->prepare();
//visualization
h = ui->graphicsView->sizeHint().height();
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
linePosi = new QGraphicsLineItem(NULL,scene);
//timer for the position drawer
timerPosition = new QTimer(this);
timerPosition->stop();
timerPosition->setInterval(10);
connect( timerPosition, SIGNAL(timeout()), SLOT(timerPosition_timeOut()) );
//latency of the soundcard output
delay = 200; //msec
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::analyseTempoFinished()
{
qDebug() << " resolution:" <<trackanalyser->resolution();
qDebug() << " onset count:" <<trackanalyser->peaks().count();
// Show BPM Result
ui->lblBpm->setText(QString::number(trackanalyser->bpm()));
int interval = 60 * trackanalyser->resolution() / trackanalyser->bpm();
// Draw found onsets
scene->clear();
QPen onsetPen(Qt::blue);
QPen tempoPen(Qt::green);
int j=0;
for ( int i=0;i<trackanalyser->peaks().count();i++,j++ ) {
scene->addLine( QLineF( i, h, i, h-trackanalyser->peaks().at(i)*2 ), onsetPen);
if ( j==interval ){
scene->addLine( QLineF( i, h, i, h + h*.2 ), tempoPen);
j=0;
}
}
}
void MainWindow::timerPosition_timeOut()
{
int posi_ms = QTime(0,0).msecsTo(player->position());
int posi_idx = (posi_ms + delay) * trackanalyser->resolution() / 1000;
qDebug() << " posi_ms:" <<posi_ms<< " posi_idx:" <<posi_idx<<" length"<< QTime(0,0).msecsTo(player->length()) * trackanalyser->resolution() /1000;
//Draw current position while playing
QGraphicsLineItem* li = dynamic_cast<QGraphicsLineItem*>(scene->items().at(0));
if ( li){
li->setPen(QPen(Qt::red));
li->setLine( posi_idx, h, posi_idx, h/2);
}
}
void MainWindow::on_pushAnalyse_clicked()
{
trackanalyser->open(QUrl(ui->lineEdit->text()));
}
void MainWindow::on_pushPlay_clicked()
{
if (player->isPlaying())
{
player->stop();
timerPosition->stop();
}
else
{
player->open(QUrl(ui->lineEdit->text()));
player->play();
timerPosition->start();
}
}
void MainWindow::on_pushOpen_clicked()
{
#if QT_VERSION >= 0x050000
QString pathName = QStandardPaths::standardLocations(QStandardPaths::MusicLocation).at(0);
#else
QString pathName = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
#endif
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Song"), pathName, tr("Music Files (*.mp3)"));
ui->lineEdit->setText(fileName);
}