project-kyoku/include/core/timeline.h

159 lines
3.8 KiB
C
Raw Normal View History

#pragma once
#include <algorithm>
#include <memory>
#include <set>
#include "core/note.h"
#include "core/time.h"
2021-12-29 09:59:18 -05:00
namespace kku
{
template <class TNote,
class = std::enable_if_t<std::is_base_of<Note, TNote>::value>>
class Timeline
{
public:
explicit Timeline() : _current_offset(0) {}
typedef typename std::set<TNote *>::const_iterator Iterator;
void recalculate(const microsec &offset)
{
_current_offset = offset;
expire(_top_note);
if (!_timeline.empty())
{
Iterator head_iterator = _timeline.begin();
while (!isExpired(head_iterator))
{
2021-12-29 09:59:18 -05:00
if ((*head_iterator)->getPerfectOffset() >= offset)
{
Iterator pre_head = head_iterator;
--pre_head;
_top_note =
!isExpired(pre_head) && (*pre_head)->isActive(offset)
? pre_head
: head_iterator;
break;
}
++head_iterator;
}
if (isExpired(_top_note))
_top_note = _timeline.begin();
}
}
void setNotes(const std::set<TNote *, NotePtrComparator> &notes)
{
_timeline = std::move(notes);
recalculate(_current_offset);
if (isExpired(_top_note))
return;
}
void insertNote(TNote *note)
2021-10-03 11:23:28 -04:00
{
2021-12-06 14:18:04 -05:00
_top_note = _timeline.insert(note).first;
recalculate(_current_offset);
2021-10-03 11:23:28 -04:00
update(_current_offset);
}
void insertNotes(const std::set<TNote *, NotePtrComparator> &notes)
2021-10-03 11:23:28 -04:00
{
_timeline.insert(notes.begin(), notes.end());
recalculate(_current_offset);
2021-10-03 11:23:28 -04:00
update(_current_offset);
}
inline void clear()
{
for (auto &note : _timeline)
delete note;
_timeline.clear();
}
void update(const microsec &offset)
{
_current_offset = offset;
updateTopNote(_current_offset);
}
Iterator getActiveNote(const microsec &music_offset) noexcept
{
Iterator return_note = _timeline.end();
auto note_iterator = _top_note;
while (!isExpired(note_iterator))
{
const auto &note = *note_iterator;
if (note->isActive(music_offset))
{
return_note = note_iterator;
break;
}
2021-12-29 09:59:18 -05:00
else if (note->getPerfectOffset() > music_offset)
break;
++note_iterator;
}
return return_note;
}
inline Iterator getNoteBy(const microsec &music_offset) noexcept
{
return std::find_if(_timeline.begin(), _timeline.end(),
[music_offset](const auto &note) {
2021-12-29 09:59:18 -05:00
return note->getPerfectOffset() == music_offset;
});
}
inline bool isExpired(const Iterator &iterator) const noexcept
{
return iterator == _timeline.end();
}
inline void expire(Iterator &iterator) noexcept
{
iterator = _timeline.end();
}
inline Iterator getTopNote() const noexcept { return _top_note; }
2021-12-21 12:07:19 -05:00
inline Iterator begin() const noexcept { return _timeline.begin(); }
inline Iterator end() const noexcept { return _timeline.end(); }
2022-02-23 17:45:43 -05:00
private:
std::set<TNote *, NotePtrComparator> _timeline;
microsec _current_offset;
inline void updateTopNote(const microsec &music_offset) noexcept
{
if (isExpired(_top_note))
return;
const auto &top_note = *_top_note;
bool already_played = top_note->getPerfectOffset() < music_offset &&
!top_note->isActive(music_offset);
if (already_played)
++_top_note;
}
Iterator _top_note;
};
2021-12-29 09:59:18 -05:00
} // namespace kku