2016 project-added to repo
[EroBeats.git] / Release / TempoTools.cpp
blob5b0c4138f3cbb5c14bc3c134d238bb1de822c8be
1 #include "TempoTools.h"
3 using namespace std::chrono;
5 TempoTools::TempoTools(RhythmIO* rhythmio)
7 rio = rhythmio;
9 errorMarginPerf = 16;
10 errorMarginGood = 32;
11 errorMarginOK = 48;
12 errorMarginBad = 64;
16 TempoTools::~TempoTools()
20 void TempoTools::createTimeData(std::string levelPath) {
21 clearTimeData();
22 timeKeys = rio->createBeatPatterns(levelPath);
24 void TempoTools::clearTimeData() {
25 timeKeys.clear();
26 hitData.clear();
27 cueData.clear();
28 rio->keyPositions = 0;
30 void TempoTools::proccessDataSlow() {
31 rio->bufferBeatData(timeKeys, musicStart, &hitData, &cueData);
33 void TempoTools::setMusicStart(long long mStart) {
34 musicStart = mStart;
37 void TempoTools::countDown(float* time, float duration) {
38 milliseconds ms = duration_cast<milliseconds>(
39 system_clock::now().time_since_epoch());
40 long long expiredTime = ms.count();
41 *time = duration - (expiredTime - musicStart) / 1000.0;
44 double TempoTools::clickTempo(int time) {
45 int limit = 3;
46 clickTimes.push_back(time);
47 int size = clickTimes.size();
49 if (size == 1) {
50 return 1;
52 else if (size > limit) {
53 clickTimes.erase(clickTimes.begin());
54 size = limit;
56 double combined = 0;
57 for (int t = 0; t < size - 1; t++) {
58 combined += clickTimes.at(t + 1) - clickTimes.at(t);
60 //time seperation
61 double combinedAv = combined / (size - 1);
62 //CPM
63 double clickRate = 60000 / combinedAv;
64 double factor = clickRate / tempo;
65 if (combinedAv < 1) {
66 return 1;
68 else {
69 return factor;
74 short TempoTools::autoPlay(int type){
75 milliseconds ms = duration_cast<milliseconds>(
76 system_clock::now().time_since_epoch());
77 long long time = ms.count();
78 long long difference = 0;
79 for (int i = 1; i < 10; i++) {
80 if (type == i * 10) {
81 difference = cueFileTime[i] - time;
82 if (difference < 8 && difference > -8 && !autoplayed[i]) {
83 autoplayed[i] = true;
84 return 1;
86 else if (difference < 8 && difference > -8 && autoplayed[i]) {}
87 else {
88 autoplayed[i] = false;
92 return 0;
95 short TempoTools::clickAccuracy(int type) {
96 milliseconds ms = duration_cast<milliseconds>(
97 system_clock::now().time_since_epoch());
99 long long clickTime = ms.count();
100 short hit;
101 int difference;
102 int oldDifference;
103 for (int i = 1; i < 10; i++) {
104 if (type == i) {
105 //Ahead
106 difference = fileTime[i] - clickTime;
107 //Behind
108 oldDifference = clickTime - oldTime[i];
112 if (difference < errorMarginPerf || oldDifference < errorMarginPerf) {
113 return 0;
115 else if (difference > oldDifference) {
116 return 1;
117 //ahead
119 else if (difference < oldDifference) {
120 return -1;
121 //behind
123 else return 9;
125 short TempoTools::clickAccuracyRange(int type) {
126 milliseconds ms = duration_cast<milliseconds>(
127 system_clock::now().time_since_epoch());
129 long long clickTime = ms.count();
130 short hit;
131 int difference;
132 int oldDifference;
133 for (int i = 1; i < 10; i++) {
134 if (type == i) {
135 //Ahead
136 difference = fileTime[i] - clickTime;
137 //Behind
138 oldDifference = clickTime - oldTime[i];
142 if (difference < errorMarginPerf || oldDifference < errorMarginPerf) {
143 return 0;
145 else if (difference < errorMarginGood || oldDifference < errorMarginGood) {
146 return 1;
147 //ahead
149 else if (difference < errorMarginOK || oldDifference < errorMarginOK) {
150 return 2;
151 //ahead
153 else if (difference < errorMarginBad || oldDifference < errorMarginBad) {
154 return 3;
155 //ahead
157 else return 9;
160 //when a certain vector's time has expired, switch to the next one and keep the old one in an int.
161 void TempoTools::moveTicker() {
162 milliseconds ms = duration_cast<milliseconds>(
163 system_clock::now().time_since_epoch());
164 long long ct = ms.count();
166 for (int i = 1; i < 10; i++) {
167 if (hitData.size() != 0) {
168 if (ct > fileTime[i] && hitData.at(0).at(1) == 1) {
169 oldTime[i] = fileTime[i];
170 fileTime[i] = (hitData.at(0)).at(0);
171 hitData.erase(hitData.begin());
172 std::cout << "\tupdate Hit " << hitData.size() << "\n";
176 for (int i = 1; i < 10; i++) {
177 if (cueData.size() != 0) {
178 if (ct > cueFileTime[i] && cueData.at(0).at(1) == i * 10) {
179 cueFileTime[i] = cueData.at(0).at(0);
180 cueData.erase(cueData.begin());
181 std::cout << "\tupdate Cue" << cueData.size() << "\n";
184 else {
185 done = true;