2013-05-14 01:03:43 +02:00
|
|
|
|
|
|
|
#ifndef LUPPP_TIME_H
|
|
|
|
#define LUPPP_TIME_H
|
|
|
|
|
|
|
|
#include <iostream>
|
2013-05-16 16:17:49 +02:00
|
|
|
#include <cstdio>
|
2013-05-14 01:03:43 +02:00
|
|
|
|
|
|
|
#include "buffers.hxx"
|
2013-05-16 16:17:49 +02:00
|
|
|
#include "eventhandler.hxx"
|
2013-05-14 01:03:43 +02:00
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
#include "observer/observer.hxx"
|
|
|
|
|
2013-05-14 01:03:43 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2013-05-16 16:17:49 +02:00
|
|
|
|
2013-05-14 01:03:43 +02:00
|
|
|
// 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-16 18:16:18 +02:00
|
|
|
void setBpm(float b)
|
2013-05-15 03:55:51 +02:00
|
|
|
{
|
2013-05-16 16:17:49 +02:00
|
|
|
char buffer [50];
|
|
|
|
sprintf (buffer, "%d", b);
|
|
|
|
//printf ("[%s] is a string %d chars long\n",buffer,n);
|
|
|
|
|
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
|
2013-05-15 03:55:51 +02:00
|
|
|
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-16 16:17:49 +02:00
|
|
|
int fpb = 44100 / bpm * 60;
|
|
|
|
char buffer [50];
|
|
|
|
sprintf (buffer, "TM, bpm = %i, fpb = %i", int(bpm), fpb );
|
|
|
|
EventGuiPrint e( buffer );
|
|
|
|
writeToGuiRingbuffer( &e );
|
|
|
|
|
|
|
|
// triggers newly registered object to have bpm set
|
|
|
|
o->setFpb( fpb );
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
|
|
|
|
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 23:48:43 +02:00
|
|
|
// inform observers of new beat FIRST
|
|
|
|
for(uint i = 0; i < observers.size(); i++)
|
2013-05-15 03:55:51 +02:00
|
|
|
{
|
2013-05-15 23:48:43 +02:00
|
|
|
observers.at(i)->beat();
|
2013-05-15 03:55:51 +02:00
|
|
|
}
|
2013-05-15 03:04:39 +02:00
|
|
|
|
2013-05-15 23:48:43 +02:00
|
|
|
if ( beat % (int)buffers->transportPosition->beats_per_bar == 0 )
|
2013-05-15 05:05:36 +02:00
|
|
|
{
|
2013-05-15 23:48:43 +02:00
|
|
|
// inform observers of new bar SECOND
|
|
|
|
for(uint i = 0; i < observers.size(); i++) { observers.at(i)->bar(); }
|
|
|
|
buffers->transportPosition->bar++;
|
2013-05-15 05:05:36 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LUPPP_TIME_H
|