-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiChannel.cpp
62 lines (48 loc) · 1.64 KB
/
MidiChannel.cpp
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
//
// Created by madsy on 22.03.17.
//
#include <algorithm>
#include "MidiChannel.h"
MidiChannel::MidiChannel() : pChannelIndex(0), pStartTick(0), pEndTick(0) {
}
MidiChannel::~MidiChannel() {
}
std::vector<std::shared_ptr<MidiEvent>> MidiChannel::getEventsWithinInterval(unsigned int startTick, unsigned int endTick) const {
std::vector<std::shared_ptr<MidiEvent>> result;
for(size_t i = 0; i < pEvents.size(); i++){
unsigned int t = pEvents[i]->getTimestamp();
if(t >= startTick && t < endTick){
result.push_back(pEvents[i]);
}
}
return pEvents;
}
const std::vector<std::shared_ptr<MidiEvent>> &MidiChannel::getEvents() const {
return pEvents;
}
void MidiChannel::addEvent(const std::shared_ptr<MidiEvent> &evt) {
pEvents.push_back(evt);
}
void MidiChannel::sort() {
std::stable_sort(pEvents.begin(),
pEvents.end(),
[](const std::shared_ptr<MidiEvent>& a, const std::shared_ptr<MidiEvent>& b) -> bool {
return a->getTimestamp() < b->getTimestamp();
}
);
}
unsigned int MidiChannel::getStartTick() const {
return pStartTick;
}
unsigned int MidiChannel::getEndTick() const {
return pEndTick;
}
void MidiChannel::update() {
//refresh start and end tick after adding all events
pStartTick = 0xFFFFFFFF;
pEndTick = 0;
for(size_t i = 0; i < pEvents.size(); i++){
if(pEvents[i] && pEvents[i]->getTimestamp() < pStartTick){ pStartTick = pEvents[i]->getTimestamp(); }
if(pEvents[i] && pEvents[i]->getTimestamp() > pEndTick){ pEndTick = pEvents[i]->getTimestamp(); }
}
}