Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Last active November 20, 2023 19:27
Show Gist options
  • Save Xenakios/819bae786a91e6476d2bc2f46edab2e4 to your computer and use it in GitHub Desktop.
Save Xenakios/819bae786a91e6476d2bc2f46edab2e4 to your computer and use it in GitHub Desktop.
// based on event list from free-audio clap-wrapper
class SortingEventList
{
public:
union clap_multi_event
{
clap_event_header_t header;
clap_event_note_t note;
clap_event_midi_t midi;
clap_event_midi2_t midi2;
clap_event_midi_sysex_t sysex;
clap_event_param_value_t param;
clap_event_param_mod_t parammod;
clap_event_param_gesture_t paramgest;
clap_event_note_expression_t noteexpression;
clap_event_transport_t transport;
};
SortingEventList()
{
events.reserve(512);
event_indices.reserve(512);
}
void clear()
{
events.clear();
event_indices.clear();
sorted = false;
}
uint32_t size() const { return events.size(); }
const clap_event_header_t *get(uint32_t index) const
{
if (events.size() > index)
{
assert(sorted);
auto realindex = event_indices[index];
return &events[realindex].header;
}
return nullptr;
}
template<typename EventType>
void pushEvent(EventType *ev)
{
event_indices.push_back(events.size());
events.push_back(*(clap_multi_event*)ev);
sorted = false;
}
void sortEvents()
{
// just sorting the index
// an item must be sorted to front of
// if the timestamp if event[a] is earlier than
// the timestamp of event[b].
// if they have the same timestamp, the index must be preserved
std::sort(event_indices.begin(), event_indices.end(),
[&](size_t const &a, size_t const &b) {
auto t1 = events[a].header.time;
auto t2 = events[b].header.time;
return (t1 == t2) ? (a < b) : (t1 < t2);
});
sorted = true;
}
private:
std::vector<clap_multi_event> events;
std::vector<size_t> event_indices;
bool sorted = false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment