-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.cpp
More file actions
121 lines (100 loc) · 3.78 KB
/
Copy pathgui.cpp
File metadata and controls
121 lines (100 loc) · 3.78 KB
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
#include "gui.h"
#include "ui_gui.h"
#include <QGraphicsScene>
#include <stdio.h>
#include <iostream>
#include <QImage>
#include <QPixmap>
const float MainWindow::TRACK_WIDTH_PIXELS = 20000.0f;
const float MainWindow::END_BAR_POSITION = 20.0f;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->resize(800, 600);
this->setWindowTitle("Beat Detector v0.1A");
this->timer = new QTimer(this);
this->connect(timer, SIGNAL(timeout()), this, SLOT(updateGraphics()));
timer->setInterval( 10 );
timer->start();
// set up graphics
this->graphicsScene = new QGraphicsScene();
this->graphicsView = new QGraphicsView(this->graphicsScene, this);
this->graphicsView->resize(760, 540);
this->graphicsView->move(20, 40);
this->endBar = this->graphicsScene->addRect(
QRectF(MainWindow::END_BAR_POSITION, 0, 10, 500),
QPen(Qt::black), QBrush(Qt::red));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setAlgorithmResults(std::vector<std::vector<float>*>* results) {
this->algorithmResults = results;
this->beatFlags = new std::vector<std::vector<QRect*>*>();
// calculate height for each algo row based on graphics view height
int algoRowHeight = (this->graphicsView->height() - 40)/results->size();
for(int i = 0; i < results->size(); i++) {
std::vector<float>* currentFlags = (*results)[i];
std::vector<QRect*> * newRects = new std::vector<QRect*>();
for(int j = 0; j < currentFlags->size(); j++) {
float leftPixelPos = (*currentFlags)[j] * TRACK_WIDTH_PIXELS + END_BAR_POSITION;
newRects->push_back(new QRect(leftPixelPos, algoRowHeight*i, 10, algoRowHeight-10));
this->graphicsScene->addRect(*(*newRects)[j], QPen(Qt::black), QBrush(Qt::green));
}
this->beatFlags->push_back(newRects);
}
}
void MainWindow::setAlgorithmSamples(unsigned char * samples) {
this->samples = samples;
}
void MainWindow::setAlgorithmSampleSize(long numSamples) {
this->numSamples = numSamples;
}
void MainWindow::drawSamples() {
short sampleOne, sampleTwo;
long sampleIndex = 0;
double sampleWidth = (double)this->numSamples / MainWindow::TRACK_WIDTH_PIXELS / 2;
QImage sampleImage((int)MainWindow::TRACK_WIDTH_PIXELS, 500, QImage::Format_ARGB32);
QPainter painter;
QPen pen(Qt::blue);
pen.setWidth(2);
painter.setPen(pen);
painter.begin(&sampleImage);
for(long i = 2; i < TRACK_WIDTH_PIXELS; i++) {
sampleOne = ((short*)this->samples)[sampleIndex];
sampleIndex = (((long)(sampleWidth*i) >> 1) << 1);
sampleTwo = ((short*)this->samples)[sampleIndex];
painter.drawLine(i-1 + END_BAR_POSITION, (sampleOne >> 8)+250, i + END_BAR_POSITION, (sampleTwo >> 8)+250);
}
painter.end();
this->graphicsScene->addPixmap(QPixmap::fromImage(sampleImage));
}
void MainWindow::setMusicPlayer(Phonon::MediaObject* music) {
this->music = music;
}
void MainWindow::updateGraphics() {
float r = 0;
if(this->music != NULL)
r = float(this->music->currentTime())/this->music->totalTime();
updateAlgorithmRendering(r);
//printf("moving to: %f\n", r);
//std::cout.flush();
}
/**
* Draw the beat flags at the current location.
*
*/
void MainWindow::updateAlgorithmRendering(float seekPosition) {
QRectF rect = this->graphicsView->sceneRect();
QPointF point = rect.topLeft();
QPointF newPoint(seekPosition * rect.width(), point.y());
QRectF newRect(rect);
newRect.moveTo(newPoint);
this->graphicsView->setSceneRect(newRect);
// move the baseline rectangle
float newX = seekPosition * rect.width() + MainWindow::END_BAR_POSITION;
this->endBar->moveBy(newX - this->endBar->x() ,0);
}