libmetro
libmetro.h
1 #ifndef LIBMETRO_H
2 #define LIBMETRO_H
3 
4 #include <chrono>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
20 // forward declare the private implementation of Metronome
21 namespace metro_private {
22 class MetronomePrivate;
23 };
24 
26 namespace metro {
27 
29 
33 const float SampleRateHz = 48000.0;
34 
36 class MetroException : public std::runtime_error {
37 public:
38  MetroException(std::string msg)
39  : std::runtime_error(msg){};
40 };
41 
43 
54 class Note {
55 public:
57  enum Timbre {
58  Sine,
59  Drum,
60  };
61 
63 
67  Note()
68  : frames(std::vector<float>(2 * SampleRateHz)){};
69 
71 
79  Note(Timbre timbre, float frequency, float volume);
80 
82  //
83  // Parses a string in the format timbre,freq,vol
84  // e.g. sine,440.0,50.0
85  //
89  Note(std::string triplet);
90 
92  float& operator[](size_t index) { return frames[index]; };
93 
95  const float& operator[](size_t index) const { return frames[index]; };
96 
98  std::vector<float>& get_frames() { return frames; };
99 
101  size_t size() { return frames.size(); };
102 
104  Note operator+(const Note& other)
105  {
106  metro::Note ret;
107  for (size_t i = 0; i < ret.size(); ++i)
108  ret[i] = (*this)[i] + other[i];
109  return ret;
110  }
111 
112 private:
113  std::vector<float> frames;
114 };
115 
117 
125 class Measure {
126 public:
128  enum FileFormat {
129  One,
130  Two,
131  };
132 
134 
144  Measure(int num_notes)
145  : notes(num_notes){};
146 
148 
177  Measure(const char* path, FileFormat file_format);
178 
180  Note& operator[](size_t index) { return notes[index]; };
181 
183  const Note& operator[](size_t index) const { return notes[index]; };
184 
186  std::vector<Note>& get_notes() { return notes; };
187 
189  size_t size() { return notes.size(); };
190 
191 private:
192  std::vector<Note> notes;
193 };
194 
196 
209 class Metronome {
210 public:
212 
217  Metronome(int bpm);
218 
219  ~Metronome();
220 
222 
229  void add_measure(Measure& measure);
230 
232 
237  void start();
238 
240  void start_and_loop();
241 
242 private:
243  metro_private::MetronomePrivate*
244  p_impl; // https://en.cppreference.com/w/cpp/language/pimpl
245 };
246 }; // namespace metro
247 
248 #endif /* LIBMETRO_H */
float & operator[](size_t index)
index into underlying vector of floats.
Definition: libmetro.h:92
If anything goes wrong in libmetro, a MetroException is thrown.
Definition: libmetro.h:36
Measure(int num_notes)
Measure constructor.
Definition: libmetro.h:144
void start()
start the loop
FileFormat
Definition: libmetro.h:128
metro namespace contains everything defined in libmetro
Definition: libmetro.h:26
Note operator+(const Note &other)
sum underlying vectors and return new Note
Definition: libmetro.h:104
size_t size()
size of underlying vector of floats.
Definition: libmetro.h:101
Metronome(int bpm)
Metronome constructor.
const float & operator[](size_t index) const
index into underlying vector of floats.
Definition: libmetro.h:95
std::vector< Note > & get_notes()
get a reference to the underlying vector of Notes.
Definition: libmetro.h:186
std::vector< float > & get_frames()
get a reference to the underlying vector of floats.
Definition: libmetro.h:98
Note class.
Definition: libmetro.h:54
Definition: libmetro.h:59
Metronome class.
Definition: libmetro.h:209
Measure class.
Definition: libmetro.h:125
Note & operator[](size_t index)
index into underlying vector of Notes.
Definition: libmetro.h:180
void start_and_loop()
start the loop and block
Definition: libmetro.h:129
Definition: libmetro.h:58
size_t size()
size of underlying vector of Notes.
Definition: libmetro.h:189
const Note & operator[](size_t index) const
index into underlying vector of Notes.
Definition: libmetro.h:183
const float SampleRateHz
Sample rate used throughout libmetro.
Definition: libmetro.h:33
Definition: libmetro.h:130
Timbre
Definition: libmetro.h:57
void add_measure(Measure &measure)
add a measure
Note()
Empty Note constructor.
Definition: libmetro.h:67