-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabxtester.cpp
177 lines (160 loc) · 3.98 KB
/
abxtester.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "abxtester.h"
ABXTester::ABXTester(QString filePath1, QString filePath2, int trials, qint64 segmentStart,
qint64 segmentStop, QObject *parent)
: QObject(parent)
{
this->filePath1 = filePath1;
this->filePath2 = filePath2;
this->trials = trials;
this->segmentStart = segmentStart;
this->segmentStop = segmentStop;
APlayer = new QMediaPlayer(this);
BPlayer = new QMediaPlayer(this);
connect(APlayer, &QMediaPlayer::positionChanged, this, [&](qint64 position) {
if (position >= this->segmentStop)
APlayer->setPosition(this->segmentStart);
});
connect(BPlayer, &QMediaPlayer::positionChanged, this, [&](qint64 position) {
if (position >= this->segmentStop)
BPlayer->setPosition(this->segmentStart);
});
}
ABXTester::~ABXTester()
{
delete APlayer;
delete BPlayer;
}
int ABXTester::getCurrentTrial()
{
return currentTrial;
}
int ABXTester::getTrials()
{
return trials;
}
qint64 ABXTester::getPlaybackPosition()
{
if (testIsRunning)
return currentPlayer->position();
return 0;
}
void ABXTester::startTest()
{
if (!testIsStarted) {
startNextTrial();
}
}
void ABXTester::startNextTrial()
{
if (currentTrial >= trials) {
emit testFinished(results);
return;
}
if (!testIsStarted) {
testIsStarted = true;
emit testStarted();
}
++currentTrial;
emit nextTrialStarted();
testIsRunning = true;
if (QRandomGenerator::global()->bounded(2)) {
BPlayer->setMedia(QUrl::fromLocalFile(filePath1));
APlayer->setMedia(QUrl::fromLocalFile(filePath2));
} else {
APlayer->setMedia(QUrl::fromLocalFile(filePath1));
BPlayer->setMedia(QUrl::fromLocalFile(filePath2));
}
if (QRandomGenerator::global()->bounded(2))
XPlayer = BPlayer;
else
XPlayer = APlayer;
currentPlayer = XPlayer;
currentPlayer->setPosition(segmentStart);
startCurrentPlayer();
}
void ABXTester::finishCurrentTrial()
{
if (testIsRunning) {
APlayer->stop();
BPlayer->stop();
currentPlaybackPosition = segmentStart;
testIsRunning = false;
emit currentTrialFinished();
}
}
void ABXTester::stopCurrentPlayer()
{
if (testIsRunning) {
currentPlaybackPosition = currentPlayer->position();
if (currentPlayer->state() == QMediaPlayer::PlayingState) {
currentPlayer->stop();
}
}
}
void ABXTester::startCurrentPlayer()
{
if (testIsRunning) {
if (currentPlayer->state() == QMediaPlayer::StoppedState) {
currentPlayer->setPosition(currentPlaybackPosition);
currentPlayer->play();
}
}
}
void ABXTester::chooseA()
{
if (testIsRunning) {
emit AChosen();
if (APlayer == XPlayer)
results.append(true);
else
results.append(false);
finishCurrentTrial();
}
}
void ABXTester::chooseB()
{
if (testIsRunning) {
emit BChosen();
if (BPlayer == XPlayer)
results.append(true);
else
results.append(false);
finishCurrentTrial();
}
}
void ABXTester::listenToA()
{
if (testIsRunning) {
stopCurrentPlayer();
currentPlayer = APlayer;
startCurrentPlayer();
emit AIsListenedTo();
}
}
void ABXTester::listenToB()
{
if (testIsRunning) {
stopCurrentPlayer();
currentPlayer = BPlayer;
startCurrentPlayer();
emit BIsListenedTo();
}
}
void ABXTester::listenToX()
{
if (testIsRunning) {
stopCurrentPlayer();
currentPlayer = XPlayer;
startCurrentPlayer();
emit XIsListenedTo();
}
}
void ABXTester::changePlaybackPosition(qint64 position)
{
if (testIsRunning) {
if (position < segmentStart || position >= segmentStop)
position = segmentStart;
currentPlayer->setPosition(position);
emit playbackPositionChanged(position);
}
}