-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNote.hpp
More file actions
107 lines (95 loc) · 3.04 KB
/
Note.hpp
File metadata and controls
107 lines (95 loc) · 3.04 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
#ifndef NOTE_HPP
#define NOTE_HPP
#include <cctype>
#include <stdexcept>
#include <string>
/**
* @brief Représente une note musicale
*
* Format: lettre (a-g) + altération optionnelle (#/b) + octave (0-8)
* Exemple: c4, d#5, gb3
*/
class Note {
private:
std::string name; ///< Nom de la note (ex: "c", "d#", "gb")
int octave; ///< Octave (0-8)
private:
/**
* @brief Parse une chaîne en note
* @param noteStr Chaîne à parser
* @return true si parsing réussi
*/
bool parse(const std::string& noteStr) {
if (noteStr.empty()) return false;
size_t pos = 0;
// Lettre de base (a-g)
if (noteStr[pos] < 'a' || noteStr[pos] > 'g') return false;
this->name = noteStr[pos++];
// Altération optionnelle
if (pos < noteStr.length() &&
(noteStr[pos] == '#' || noteStr[pos] == 'b')) {
this->name += noteStr[pos++];
}
// Octave
if (pos >= noteStr.length() || !std::isdigit(noteStr[pos]))
return false;
this->octave = noteStr[pos++] - '0';
// Vérifier qu'il n'y a pas de caractères supplémentaires
if (pos != noteStr.length()) return false;
return this->octave >= 0 && this->octave <= 8;
}
public:
/**
* @brief Constructeur par défaut
*/
Note() : name("c"), octave(4) {}
/**
* @brief Constructeur depuis une chaîne
* @param noteStr Chaîne représentant la note (ex: "c4", "d#5")
*/
explicit Note(const std::string& noteStr) {
if (!parse(noteStr))
throw std::invalid_argument("Invalid note format: " + noteStr);
}
/**
* @brief Constructeur avec paramètres
* @param name Nom de la note (ex: "c", "d#", "gb")
* @param octave Octave (0-8)
*/
Note(std::string name, int octave) : name(std::move(name)), octave(octave) {
if (octave < 0 || octave > 8)
throw std::invalid_argument("Octave must be between 0 and 8");
}
/**
* @brief Retourne la représentation en chaîne de la note
* @return Chaîne représentant la note (ex: "c4")
*/
std::string toString() const {
return this->name + std::to_string(this->octave);
}
/**
* @brief Retourne le nom de la note (sans l'octave)
* @return Nom de la note (ex: "c", "d#")
*/
const std::string& getName() const { return this->name; }
/**
* @brief Retourne l'octave de la note
* @return Octave (0-8)
*/
int getOctave() const { return this->octave; }
/**
* @brief Compare deux notes pour l'égalité
* @param other Autre note
* @return true si les notes sont identiques
*/
bool operator==(const Note& other) const {
return this->name == other.name && this->octave == other.octave;
}
/**
* @brief Compare deux notes pour l'inégalité
* @param other Autre note
* @return true si les notes sont différentes
*/
bool operator!=(const Note& other) const { return !(*this == other); }
};
#endif // NOTE_HPP