summaryrefslogtreecommitdiff
path: root/song.cpp
blob: 14ac92d5af9247d83f28478506492d2494968b45 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <vector>

#include "song.h"
#include "timestamp.h"
#include "common.h"

Song::Song(std::string chartFile): chartFile(chartFile){
  // substitute "notes.chart" for "song.wav"
  string audioFile = chartFile;
  audioFile.replace(audioFile.find("notes.chart"), 12, "song.wav");
  parseInfo();
  parseSync();
  /* for(int i = 0; i < 4; i++){ */
  /*   // Mark the difficulty as available if it has chords */
  /*   difficulty[i] = parseChords(i); */
  /* } */
}

void Song::print(){
  std::cout << "Title: "      << title << "\n"
            << "Artist: "     << artist << "\n"
            << "Album: "      << album << "\n"
            << "Year: "       << year << "\n"
            << "Charter: "    << album << "\n"
            << "Resolution: " << resolution << "\n";
}

Song::~Song(){
}

void Song::parseInfo(){
  // String based information
  std::string search[5]= {
    "Name", "Artist",  "Album",
    "Year", "Charter"
  };
  std::string *values[5] = {
    &title, &artist,  &album,
    &year,  &charter};
  std::string boilerplate[5] = {
    "Untitled", "Unknown artist", "Unknown album",
    "Unknown year", "Unknown charter"
  };
  bool insideSong = false; // true while inside the "Song" info delimiter
  // open the chart file
  std::ifstream file(chartFile);
  if (!file.is_open()) {
    std::cerr << "There was a problem opening the file";
    return;
  }
  std::string line;
  while (getline(file,line)){
    insideSong = line.find("[Song]");
    if (insideSong) // We can start searching for info fields once inside
    while (getline(file,line)){
      // For every pattern check if this line contains it
      for (int i=0; i<5; i++){
        if (line.find(search[i]) != std::string::npos){
          // If it does, find the pattern contained inside two quotes
          std::size_t start = line.find("\"");
          std::size_t end   = line.find("\"", start+1);
          // Match the pattern to the value
          *values[i] = line.substr(start+1, end-start-1);
          break;
        }
      }
      // Resolution is not inside quotes and must be assigned to an int
      if (line.find("Resolution") != std::string::npos) {
        resolution = std::stoi( line.substr(line.find_last_of(' ')) );
      }
      // Year value often has a comma inside. Let's fix that:
      if (year.size() > 4 )
      year = year.substr(year.find(' ')+1);
      if (line.find("[SyncTrack]") != std::string::npos) break;
    }
  }
  file.close();
};

void Song::parseSync(){
  // We want to prevent offsets in timings.
  // Timeline will store timings in nanoseconds and the different chords
  // will round down to the lower millisecond.
  long int currentTime = 0; // ns
  // Open the chart file
  std::ifstream file(chartFile);
  if (!file.is_open()) {
    std::cerr << "There was a problem opening the file";
    return;
  }
  int lineNb = 0;
  std::string line;
  while (getline(file,line)){
    lineNb++;
    // We are only interested in the SyncTrack
    if (line.find("[SyncTrack]") != std::string::npos)
    while (getline(file,line)) {
      lineNb++;
      std::regex pattern("([0-9]+) = B ([0-9]+)");
      std::smatch match;
      if (std::regex_search(line, match, pattern)){
        // We found a match, let's parse the integers
        int tick  = std::stoi(match[1]);
        int nbpm  = std::stoi(match[2]);
        if (tick == 0){
          // First timestamp, we need to add a default one
          timestamps.push_back(Timestamp(0, tick, nbpm));
        } else {
          // We can calculate the time passed since the last timestamp
          // and add it to the current time and create a new timestamp
          int tickDiff = tick - timestamps.back().getTick();
          long int timeDiff = nspt(nbpm, resolution) * tickDiff;
          currentTime += timeDiff;
          #ifdef DEBUG
          if (currentTime < 0) {
            std::cerr << "---------------------\n"
                      << "NEGATIVE TIMESTAMP:\n"
                      << "Last Timestamp:"
                      << " tick=" << timestamps.back().getTick()
                      << " bpm=" << timestamps.back().getBPM()
                      << " time=" << timestamps.back().getTime() << "ns\n"
                      << "Current Timestamp:"
                      << " tick=" << tick
                      << " bpm=" << nbpm
                      << " time=" << currentTime << "ns\n"
                      << "Other:"
                      << " tickDiff=" << tickDiff << std::endl;
          }
          #endif // DEBUG
          timestamps.push_back(Timestamp(currentTime, tick, nbpm));
          #ifdef DEBUG
            std::cout << "BPM: " << timestamps.back().getNbpm() << " "
                      << "Tick: " << timestamps.back().getTick() << " "
                      << "Line: " << lineNb << " "
                      << "TickDiff: " << tickDiff << " "
                      << "TimeDiff: " << timeDiff << "ns "
                      << "Current time: " << currentTime << std::endl;
          #endif // DEBUG
        }
      }
      if (line.find("[Events]") != std::string::npos) break;
    }
  }
  file.close();
};

void Song::printTimestamps(){
  for (int i=0; i<timestamps.size(); i++){
    std::cout << " Tick: " << timestamps[i].getTick()
              << " BPM: " << timestamps[i].getBPM()
              << " Time: " << timestamps[i].getTime() << "ns"
              << std::endl;
  }
  if (timestamps.size() == 0) std::cout << "No timestamps found" << std::endl;
}

bool Song::parseChords(int difficulty){
  // We set a chord vector to store point to the correct difficulty vector
  std::vector<ChordNote>*chords;
  // We set the stringPattern to match depending on the difficulty
  std::string stringPattern;
  switch (difficulty){
    case DIFFICULTY_EASY:
      stringPattern = "EasySingle";
      std::vector<ChordNote>*chords = &easy;
      break;
    case DIFFICULTY_MEDIUM:
      stringPattern = "MediumSingle";
      std::vector<ChordNote>*chords = &medium;
      break;
    case DIFFICULTY_HARD:
      stringPattern = "HardSingle";
      std::vector<ChordNote>*chords = &hard;
      break;
    case DIFFICULTY_EXPERT:
      stringPattern = "ExpertSingle";
      std::vector<ChordNote>*chords = &expert;
      break;
    default:
      std::cerr << "Invalid difficulty" << std::endl;
      return false;
  }
  // We open the chart file
  std::ifstream file(chartFile);
  if (!file.is_open()) {
    std::cerr << "There was a problem opening the file";
    return false;
  }
  std::string line;
  while (getline(file,line)){
    // We want to find the correct difficulty
    if (line.find(stringPattern) != std::string::npos){
      // We found the correct difficulty, let's parse the chords
      while (getline(file,line)){
        // Notes in chords have this format: ` tick = N fret duration`
        // We want to match the tick, fret and duration using regex
        std::regex pattern("([0-9]+) = N ([0-4]+) ([0-9]+)");
        std::smatch match;
        if (std::regex_search(line, match, pattern)){
          // We found a match, let's parse the integers
          int tick    = std::stoi(match[1]);
          int fret    = std::stoi(match[2]);
          int duration= std::stoi(match[3]);
          // We need to find the timestamp with the closest tick that's smaller
          int timestampIndex = 0;
          for (int i=0; i<timestamps.size(); i++){
            if (timestamps[i].getTick() < tick) timestampIndex = i;
            else break;
          }
          // Get the tick of that timestamp
          const int tsTick = timestamps[timestampIndex].getTick();
          // Get the current bpm at that timestamp
          const int tsBPM  = timestamps[timestampIndex].getNbpm();
          // Get the time of that timestamp
          const long int tsTime = timestamps[timestampIndex].getTime();
          // Calculate the start time of the chord
          const long int chordTime = tsTime+(nspt(tsBPM, resolution) * (tick - tsTick));
          int chordEnd = 0;
          if (duration != 0){
            // Calculate the duration of the chord
            const long int chordDuration = nspt(tsBPM, resolution) * duration;
            chordEnd = chordTime + chordDuration;
          }
          #ifdef DEBUG
          if (chordTime < 0) {
            std::cerr << "----------------------------\n"
                      << "Chord start time is negative\n"
                      << "LTI:"
                      << " i=" << timestampIndex
                      << " tick=" << tsTick
                      << " time=" << tsTime
                      << " bpm=" << tsBPM << "\n"
                      << "Chord Info:"
                      << " res=" << resolution
                      << " tick=" << tick
                      << " time=" << chordTime
                      << " delta=" << (tick - tsTick) << "\n"
                      << "Other: nspt=" << nspt(tsBPM,resolution) << std::endl;
          }
          std::cout << line << std::endl;
          std::cout << "Note ajoutée: " << fret
                    << " Tick: " << tick << std::endl;
          #endif // DEBUG
          // Create a new chord and add it to the vector
          chords->push_back(ChordNote(fret, chordTime, chordEnd));
          /* switch (difficulty){ */
          /*   case DIFFICULTY_EASY: */
          /*     easy.push_back(ChordNote(fret, chordTime, chordEnd)); */
          /*     break; */
          /*   case DIFFICULTY_MEDIUM: */
          /*     medium.push_back(ChordNote(fret, chordTime, chordEnd)); */
          /*     break; */
          /*   case DIFFICULTY_HARD: */
          /*     hard.push_back(ChordNote(fret, chordTime, chordEnd)); */
          /*     break; */
          /*   case DIFFICULTY_EXPERT: */
          /*     expert.push_back(ChordNote(fret, chordTime, chordEnd)); */
          /*     break; */
          /*   default: */
          /*     std::cerr << "Invalid difficulty" << std::endl; */
          /*     return false; */
          /* } */
        }
        else if (line.find("[") != std::string::npos) return true;
      }
    }
  }
  return false;
};


void Song::consolidateChords(int difficulty){
  std::vector<ChordNote>*chords;
  switch (difficulty){
    case DIFFICULTY_EASY:
      std::vector<ChordNote>*chords = &easy;
      break;
    case DIFFICULTY_MEDIUM:
      std::vector<ChordNote>*chords = &medium;
      break;
    case DIFFICULTY_HARD:
      std::vector<ChordNote>*chords = &hard;
      break;
    case DIFFICULTY_EXPERT:
      std::vector<ChordNote>*chords = &expert;
      break;
    default:
      std::cerr << "Invalid difficulty" << std::endl;
      return;
  }
  // Number of chords in the vector
  int chordSize = chords->size();
  // Go through all the chords
  for (int i=0; i<chordSize; i++){
    ChordNote currentChord = chords->at(i);
    // Check if any other chord past the current one
    // has the same start and end time
    for (int j=i+1; j<chordSize; j++){
      ChordNote nextChord = chords->at(j);
      if (currentChord.getStart() == nextChord.getStart() &&
          currentChord.getEnd() == nextChord.getEnd()){
        // If so:
        // - add the fret to the current chord
        // - remove the next one
        // - decrement the chordSize
        // - decrement the j counter
        #ifdef DEBUG
        std::cout << "----------------------------\n"
                  << "Chords merged: ";
        currentChord.print();
        std::cout << " + ";
        nextChord.print();
        std::cout << " =";
        #endif // DEBUG
        currentChord.merge(nextChord);
        #ifdef DEBUG
        currentChord.print();
        #endif
        chords->erase(chords->begin()+j);
        chordSize--;
        j--;
      }
    }
    // Shrink the vector to the new size
    chords->shrink_to_fit();
  }
}

void Song::printChords(int difficulty){
  // TODO: make this fuction use the difficulty parameter
  for (int i=0; i<expert.size(); i++){
    expert[i].print();
  }
  if (expert.size() == 0) std::cout << "No chords found" << std::endl;
}


std::string Song::getChartFile(){
  return chartFile;
}

std::string Song::getTitle(){
  return title;
}

std::string Song::getArtist(){
  return artist;
}

std::string Song::getCharter(){
  return charter;
}

std::string Song::getAlbum(){
  return album;
}

std::string Song::getYear(){
  return year;
}

std::string Song::getGenre(){
  return genre;
}

std::string Song::getAudioFile(){
  return audioFile;
}

void Song::trim(int difficulty){
  // TODO: make this fuction use the difficulty parameter
  //std::vector<ChordNote> *chords = &expert;
  for (int i = 0; i < expert.size(); i++)
  {
      expert[i].trim();
  }
}


// vim: syntax=cpp.doxygen