2013-05-14 01:03:43 +02:00
|
|
|
|
|
|
|
#ifndef LUPPP_TIME_H
|
|
|
|
#define LUPPP_TIME_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "buffers.hxx"
|
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
#include "observer/observer.hxx"
|
|
|
|
|
2013-05-14 01:03:43 +02:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// inherits from ObserverSubject
|
|
|
|
class TimeManager
|
|
|
|
{
|
|
|
|
public:
|
2013-05-15 03:04:39 +02:00
|
|
|
TimeManager():
|
2013-05-15 05:05:36 +02:00
|
|
|
bpm(120),
|
|
|
|
oldBeat(0)
|
2013-05-14 01:03:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
void setBpm(int b)
|
|
|
|
{
|
|
|
|
bpm = b;
|
2013-05-15 05:05:36 +02:00
|
|
|
for(uint i = 0; i < observers.size(); i++)
|
|
|
|
{
|
|
|
|
observers.at(i)->setFpb(bpm);
|
|
|
|
}
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerObserver(Observer* o)
|
|
|
|
{
|
|
|
|
observers.push_back(o);
|
|
|
|
}
|
|
|
|
|
2013-05-14 01:03:43 +02:00
|
|
|
void process(Buffers* buffers)
|
|
|
|
{
|
2013-05-15 03:04:39 +02:00
|
|
|
int framesPerBeat = (int) buffers->samplerate / (bpm / 60.0);
|
2013-05-14 01:03:43 +02:00
|
|
|
|
2013-05-15 03:04:39 +02:00
|
|
|
// time signature?
|
|
|
|
buffers->transportPosition->beats_per_bar = 4;
|
|
|
|
buffers->transportPosition->beat_type = 4;
|
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
int beat = buffers->transportFrame / framesPerBeat;
|
2013-05-15 03:04:39 +02:00
|
|
|
//int beat = int(beat);
|
|
|
|
|
|
|
|
//int tick = int( (beatFloat - beat) * 1920 );
|
|
|
|
|
|
|
|
if ( beat != oldBeat )
|
2013-05-14 01:03:43 +02:00
|
|
|
{
|
2013-05-15 03:04:39 +02:00
|
|
|
if ( beat % (int)buffers->transportPosition->beats_per_bar == 0 )
|
2013-05-15 03:55:51 +02:00
|
|
|
{
|
2013-05-15 05:05:36 +02:00
|
|
|
// inform observers of new bar
|
|
|
|
for(uint i = 0; i < observers.size(); i++) { observers.at(i)->bar(); }
|
2013-05-15 03:04:39 +02:00
|
|
|
buffers->transportPosition->bar++;
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
2013-05-15 03:04:39 +02:00
|
|
|
|
2013-05-15 05:05:36 +02:00
|
|
|
// inform observers of new beat
|
|
|
|
for(uint i = 0; i < observers.size(); i++)
|
|
|
|
{
|
|
|
|
observers.at(i)->beat();
|
|
|
|
}
|
|
|
|
|
2013-05-15 03:04:39 +02:00
|
|
|
oldBeat = beat;
|
2013-05-14 01:03:43 +02:00
|
|
|
}
|
2013-05-15 03:04:39 +02:00
|
|
|
|
|
|
|
buffers->transportPosition->valid = JackPositionBBT;
|
|
|
|
buffers->transportPosition->tick += (buffers->nframes / buffers->samplerate);
|
|
|
|
|
|
|
|
buffers->transportPosition->beat = beat % 4;
|
|
|
|
buffers->transportPosition->tick = 0;
|
|
|
|
|
|
|
|
buffers->transportPosition->ticks_per_beat = 1920;
|
|
|
|
buffers->transportPosition->beats_per_minute = bpm;
|
2013-05-14 01:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-15 03:55:51 +02:00
|
|
|
float bpm;
|
2013-05-15 03:04:39 +02:00
|
|
|
int oldBeat;
|
2013-05-14 01:03:43 +02:00
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
std::vector<Observer*> observers;
|
|
|
|
|
2013-05-14 01:03:43 +02:00
|
|
|
// list of Observers of this TimeManager Subject, "beat", "bar" updates?
|
|
|
|
/*
|
|
|
|
for(int i = 0; i < numObservers; i++ )
|
|
|
|
{
|
|
|
|
observers[i]->notifyNewBeat(int beat);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bar != oldBar )
|
|
|
|
{
|
|
|
|
for(int i = 0; i < numObservers; i++ )
|
|
|
|
{
|
|
|
|
observers[i]->notifyNewBar(int bar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LUPPP_TIME_H
|