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