resonance 1.0.1
Loading...
Searching...
No Matches
sequencer.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5
6#include "app/msgsink.h"
7#include "mid/tick.h"
8#include "msg/musemsg.h"
9#include "os/mem.h"
11#include "sch/sequencercmd.h"
12#include "sch/tickclock.h"
13
28template <typename T>
30public:
39 Sequencer(T begin, T finish) : mBegin(begin), mFinish(finish) {
40 }
41
48 virtual ~Sequencer() {
49 Withdraw();
50 if (mCommand != nullptr) {
51 mCommand->Release();
52 }
53 }
54
61 static void *operator new(size_t nSize) {
62 return AllocateTaggedMemory(nSize, "Sequencer");
63 }
64
70 static void operator delete(void *pBlock) {
71 OperatorDeleteOverride(pBlock, "Sequencer");
72 }
73
82 virtual void Dispatch() {
83 MuseMsg *pMsg = mCursor->mValue->CloneAndShift(mNextTick.mTick);
84 mSink->Dispatch(pMsg);
85 delete pMsg;
86 ++mCursor;
88 }
89
100 void Post(Sch::TickClock *pClock, MsgSink *pSink) {
101 mSink = pSink;
102 mClock = pClock;
103 mStartTick.mTick = pClock->SongTick();
104 mCursor = mBegin;
105 ScheduleNext();
106 }
107
120 if (mCursor == mFinish) {
121 return;
122 }
123
124 const Sch::Tick at(ClampTick(mStartTick.mTick + mCursor->mPosition.mTick));
125 mNextTick = Sch::Tick(ClampTick(at.mTick - mOffset.mTick));
126 if (mNextTick.mTick == mStartTick.mTick) {
127 Dispatch();
128 return;
129 }
130
131 if (mCommand == nullptr) {
132 mCommand = new SequencerCmd(this);
133 }
134 mClock->PostAtSongTick(mCommand, mNextTick.mTick, mCmdId);
135 }
136
144
145private:
146 static int ClampTick(int nTick) {
147 return std::min(std::max(nTick, kTickMinimum), kTickMaximum);
148 }
149
150 // The tick of the message at the cursor.
151 Sch::Tick mNextTick;
152
153public:
156};
GenericSequencer()
Prepare a sequencer before any post.
Definition genericsequencer.h:33
Sch::Tick mStartTick
Definition genericsequencer.h:77
SequencerCmd * mCommand
Definition genericsequencer.h:73
Sch::TickClock * mClock
Definition genericsequencer.h:75
Sch::CmdID mCmdId
Definition genericsequencer.h:71
MsgSink * mSink
Definition genericsequencer.h:82
Sch::Tick mOffset
Definition genericsequencer.h:80
void Withdraw()
Withdraw every command queued under the handle.
Definition genericsequencer.h:61
Receiver of engine messages.
Definition msgsink.h:37
Abstract base shared by a family of messages.
Definition musemsg.h:28
Pausable view of scheduler time, together with the tempo that maps it to a song position.
Definition tickclock.h:40
int SongTick()
Report the song position the clock has arrived at.
Song position in MIDI ticks, printed as measure, beat, and tick.
Definition tick.h:70
int mTick
The position, in MIDI ticks at 480 per quarter note.
Definition tick.h:153
Scheduler command that runs a sequencer's next dispatch.
Definition sequencercmd.h:18
void Post(Sch::TickClock *pClock, MsgSink *pSink)
Record the clock and the sink and start sending the range.
Definition sequencer.h:100
T mCursor
The next object to send.
Definition sequencer.h:143
virtual ~Sequencer()
Withdraw the queued command, release it, and free the sequencer.
Definition sequencer.h:48
T mFinish
Definition sequencer.h:155
virtual void Dispatch()
Send a copy of the message at the cursor stamped with its tick, then schedule the next.
Definition sequencer.h:82
void ScheduleNext()
Queue or send the message at the cursor.
Definition sequencer.h:119
Sequencer(T begin, T finish)
Prepare a sequencer over a range, before any post.
Definition sequencer.h:39
T mBegin
Definition sequencer.h:154
void OperatorDeleteOverride(void *pBlock, const char *pszClass)
Release a block that AllocateTaggedMemory() handed out.
void * AllocateTaggedMemory(size_t nSize, const char *pszClass)
Allocate a block for one object of a named class.
constexpr int kTickMaximum
Largest finite position, from the bound Sch::Tick::IsInRange() accepts.
Definition tick.h:18
constexpr int kTickMinimum
Smallest finite position, from the bound Sch::Tick::IsInRange() accepts.
Definition tick.h:21