00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef TSE3_EVENTTRACK_H
00018 #define TSE3_EVENTTRACK_H
00019
00020 #include "tse3/listen/EventTrack.h"
00021
00022 #include "tse3/Notifier.h"
00023 #include "tse3/Midi.h"
00024 #include "tse3/Playable.h"
00025
00026 #include <vector>
00027 #include <algorithm>
00028 #include <functional>
00029 #include <cstddef>
00030
00031 namespace TSE3
00032 {
00057 template <class etype>
00058 class EventTrack : public Playable,
00059 public Notifier<EventTrackListener<etype> >
00060 {
00061 public:
00062
00072 EventTrack(bool allowDuplicates = false) : dup(allowDuplicates) {}
00073 ~EventTrack() {}
00074
00075 typedef Event<etype> event_type;
00076
00082 size_t size() const { return data.size(); }
00083
00094 event_type const &operator[](const size_t n) const
00095 {
00096 return data[n];
00097 }
00098
00110 int index(const Clock c, bool roundup = true);
00111
00127 size_t insert(const event_type &event);
00128
00137 void erase(const event_type &event);
00138
00147 void erase(size_t index);
00148
00149 protected:
00150
00151 std::vector<event_type> data;
00152
00153 private:
00154
00155 EventTrack &operator=(const EventTrack &);
00156 EventTrack(const EventTrack &);
00157
00158 bool dup;
00159 };
00160
00161 template <class etype>
00162 int EventTrack<etype>::index(const Clock c, bool roundup)
00163 {
00164 typename std::vector< TSE3::Event<etype> >::iterator i = data.begin();
00165 while (i != data.end() && !(c <= (*i).time))
00166 {
00167 ++i;
00168 }
00169 if (!roundup
00170 && i != data.begin()
00171 && (i == data.end() || (*i).time != c))
00172 {
00173 --i;
00174 }
00175 return i - data.begin();
00176 }
00177
00178 template <class etype>
00179 size_t EventTrack<etype>::insert(const event_type &event)
00180 {
00181 typename std::vector<event_type>::iterator i = data.begin();
00182 while (i != data.end() && *i <= event) ++i;
00183 if (!dup && i != data.begin() && (i-1)->time == event.time)
00184 {
00185 *(i-1) = event;
00186 size_t index = i - data.begin();
00187 Notifier<EventTrackListener<etype> >::notify
00188 (&EventTrackListener<etype>::EventTrack_EventAltered, index);
00189 return index;
00190 }
00191 else
00192 {
00193 size_t index = i - data.begin();
00194 data.insert(i, event);
00195 Notifier<EventTrackListener<etype> >::notify
00196 (&EventTrackListener<etype>::EventTrack_EventInserted, index);
00197 return index;
00198 }
00199 }
00200
00201 template <class etype>
00202 void EventTrack<etype>::erase(const event_type &event)
00203 {
00204 typename std::vector<event_type>::iterator i =
00205 std::find_if(data.begin(), data.end(),
00206 typename event_type::equal_to(event));
00207 if (i != data.end())
00208 {
00209 size_t index = i - data.begin();
00210 data.erase(i);
00211 Notifier<EventTrackListener<etype> >::notify
00212 (&EventTrackListener<etype>::EventTrack_EventErased, index);
00213 }
00214 }
00215
00216 template <class etype>
00217 void EventTrack<etype>::erase(size_t index)
00218 {
00219 if (index < data.size())
00220 {
00221 data.erase(data.begin()+index);
00222 Notifier<EventTrackListener<etype> >::notify
00223 (&EventTrackListener<etype>::EventTrack_EventErased, index);
00224 }
00225 }
00226 }
00227
00228 #endif