synthberry
Software MIDI synthesizer for the Raspberry Pi.
 All Classes Functions
notes.cpp
1 #include <algorithm>
2 #include "notes.h"
3 
4 Notes::Notes()
5 {
6 }
7 
8 Notes::~Notes()
9 {
10 }
11 
12 void Notes::addNote(uint8_t pitch, uint8_t velocity)
13 {
14  bool present = false;
15  for(auto &note : notes) {
16  if(note.getPitch() == pitch) {
17  present = true;
18  break;
19  }
20  }
21 
22  if(!present) {
23  Note note(pitch, velocity);
24  notes.push_back(note);
25 
26  for(auto &observer : observers) {
27  observer->notify(PNoteObservable(this));
28  observer->noteAdded(note);
29  }
30  }
31 }
32 
33 void Notes::removeNote(uint8_t pitch)
34 {
35  // As we're providing a lambda to filter out which Note objects should be
36  // removed, we can do the observer notification here as well to avoid
37  // looping over the notes twice.
38  auto notifyingPredicate = [this, pitch](Note note) {
39  if(note.getPitch() == pitch) {
40  for(auto &observer : this->observers) {
41  observer->notify(PNoteObservable(this));
42  observer->noteRemoved(note);
43  }
44 
45  return true;
46  }
47  return false;
48  };
49 
50  notes.erase(std::remove_if(notes.begin(), notes.end(), notifyingPredicate),
51  notes.end());
52 }
53 
54 const std::vector<Note> Notes::getNotes() const
55 {
56  return notes;
57 }
58 
59 void Notes::registerObserver(PNoteObserver &observer)
60 {
61  observers.push_back(observer);
62 }
63 
64 void Notes::removeObserver(PNoteObserver &observer)
65 {
66  observers.erase(std::remove(observers.begin(), observers.end(), observer),
67  observers.end());
68 }