2 #include <msp/core/algorithm.h>
9 Sequencer::Sequencer(float bpm):
14 set_beats_per_minute(bpm);
17 void Sequencer::set_beats_per_minute(float bpm)
19 secs_per_beat = Time::min/bpm;
22 void Sequencer::add_static_action(Action &act)
24 static_actions.push_back(&act);
27 void Sequencer::add_action(Action &act, float sb, float eb)
30 throw invalid_argument("Sequencer::add_action");
33 seq_act.action = &act;
34 seq_act.start_beat = sb;
35 seq_act.end_beat = eb;
36 auto i = find_if(segments, [=](const Segment &s){ return s.start_beat>sb; });
37 segments.insert(i, seq_act);
40 void Sequencer::start()
43 end = begin = segments.begin();
44 for(auto a: static_actions)
45 a->start(0, numeric_limits<float>::max());
46 float start_beat = beat;
48 advance_to(start_beat);
51 void Sequencer::seek(float b)
54 throw logic_error("Sequencer::seek");
62 void Sequencer::tick(const Time::TimeDelta &dt)
66 if(begin==segments.end())
69 advance_to(beat+dt/secs_per_beat);
72 void Sequencer::advance_to(float target_beat)
74 while(target_beat>next_event)
75 advance_to(next_event);
77 float prev_beat = beat;
80 while(end!=segments.end() && end->start_beat<=beat)
83 int ibeat = static_cast<int>(floor(beat));
84 bool do_beat = (ibeat!=static_cast<int>(floor(prev_beat)));
86 for(auto a: static_actions)
90 a->tick(beat, beat-prev_beat);
93 for(auto i=begin; i!=end; ++i)
95 if(i->start_beat>prev_beat)
96 i->action->start(i->start_beat, i->end_beat-i->start_beat);
98 i->action->beat(ibeat);
99 if(i->end_beat>=prev_beat)
101 float tick_beat = min(beat, i->end_beat);
102 i->action->tick(tick_beat, tick_beat-max(prev_beat, i->start_beat));
103 if(i->end_beat<=beat)
104 i->action->end(i->end_beat);
108 while(begin!=end && begin->end_beat<=beat)
111 if(target_beat>=next_event)
114 if(begin==segments.end())
115 signal_finished.emit();
118 void Sequencer::update_next_event()
120 next_event = numeric_limits<float>::max();
122 if(end!=segments.end())
123 next_event = min(next_event, end->start_beat);
125 for(auto i=begin; i!=end; ++i)
127 next_event = min(next_event, i->end_beat);