-Working on observer pattern for TimeManager-> looper bpm

main
Harry van Haaren 2013-05-15 02:55:51 +01:00
parent d00f988165
commit 42465a7b18
9 changed files with 99 additions and 36 deletions

View File

@ -2,6 +2,7 @@
#ifndef LUPPP_BUFFERS_H
#define LUPPP_BUFFERS_H
#include <cstring>
#include <jack/transport.h>
class Buffers

View File

@ -11,19 +11,13 @@ using namespace std;
Gui::Gui()
{
window = new Fl_Double_Window(600,280);
window = new Fl_Double_Window(650,280);
window->color(FL_BLACK);
window->label("Luppp 5");
Avtk::Image* header = new Avtk::Image(0,0,600,36,"header.png");
/*
box = new Fl_Box(5, 5, 200, 60, "Luppp 5");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
*/
for (int i = 0; i < 5; i++ )
{
stringstream s;
@ -31,6 +25,11 @@ Gui::Gui()
tracks.push_back( new GTrack(8 + i * 118, 40, 110, 230, s.str().c_str() ) );
}
box = new Fl_Box(655, 5, 200, 60, "BPM = 120");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
window->end();
}

View File

@ -48,8 +48,10 @@ Jack::Jack()
}
for(int i = 0; i < 5; i++)
{
loopers.push_back( new Looper(i) );
timeManager.registerObserver( loopers.back() );
}
jack_transport_start(client);
}

View File

@ -35,6 +35,8 @@ class Jack
{
loopers.at(t)->setState(s);
}
TimeManager* getTimeManager(){return &timeManager;}
private:
Buffers buffers;

View File

@ -0,0 +1,39 @@
#include "looper.hxx"
#include "jack.hxx"
void Looper::setState(State s)
{
// before update, check if we recording, if so, print info
if ( state == STATE_RECORDING )
{
int newBpm = lastWrittenSampleIndex / 44100;
cout << "Looper " << track << " ending record: endPoint @ " << lastWrittenSampleIndex
<< ". Bpm " << newBpm << " perhaps?" << endl;
jack->getTimeManager()->setBpm( newBpm );
}
// quantize?!
state = s;
if ( state == STATE_PLAYING ) // setup PLAY
{
endPoint = lastWrittenSampleIndex;
playPoint = 0;
//cout << "State = PLAYING, endPoint = " << endPoint << endl;
}
else if ( state == STATE_RECORDING ) // setup REC
{
//cout << "State = RECORDING" << endl;
playPoint = 0;
endPoint = 0;
lastWrittenSampleIndex = 0;
}
else if ( state == STATE_STOPPED ) //
{
//cout << "State = STOPPED" << endl;
}
}

View File

@ -6,9 +6,14 @@
#include "buffers.hxx"
#include "observer/observer.hxx"
class Jack;
extern Jack* jack;
using namespace std;
class Looper
class Looper : public Observer // for notifications
{
public:
enum State {
@ -26,31 +31,13 @@ class Looper
}
void setState(State s)
void setFpb(int fpb)
{
// quantize?!
state = s;
if ( state == STATE_PLAYING ) // setup PLAY
{
endPoint = lastWrittenSampleIndex;
playPoint = 0;
//cout << "State = PLAYING, endPoint = " << endPoint << endl;
}
else if ( state == STATE_RECORDING ) // setup REC
{
//cout << "State = RECORDING" << endl;
playPoint = 0;
endPoint = 0;
lastWrittenSampleIndex = 0;
}
else if ( state == STATE_STOPPED ) //
{
//cout << "State = STOPPED" << endl;
}
cout << "Looper " << track << " got fpb of " << fpb << endl;
}
void setState(State s);
void process(int nframes, Buffers* buffers)
{
float* in = buffers->audio[Buffers::MASTER_INPUT];

11
src/observer/observer.hxx Normal file
View File

@ -0,0 +1,11 @@
#ifndef LUPPP_OBSERVER_H
#define LUPPP_OBSERVER_H
class Observer
{
public:
virtual void setFpb(int fpb){};
};
#endif // LUPPP_OBSERVER_H

View File

@ -6,6 +6,8 @@
#include "buffers.hxx"
#include "observer/observer.hxx"
using namespace std;
// inherits from ObserverSubject
@ -13,21 +15,31 @@ class TimeManager
{
public:
TimeManager():
oldBeat(0)
oldBeat(0),
bpm(120)
{
}
void setBpm(int b)
{
bpm = b;
}
void registerObserver(Observer* o)
{
observers.push_back(o);
}
void process(Buffers* buffers)
{
/*
float bpm = 160;
int framesPerBeat = (int) buffers->samplerate / (bpm / 60.0);
// time signature?
buffers->transportPosition->beats_per_bar = 4;
buffers->transportPosition->beat_type = 4;
int beatFloat = buffers->transportFrame / framesPerBeat;
int beat = buffers->transportFrame / framesPerBeat;
//int beat = int(beat);
//int tick = int( (beatFloat - beat) * 1920 );
@ -35,7 +47,14 @@ class TimeManager
if ( beat != oldBeat )
{
if ( beat % (int)buffers->transportPosition->beats_per_bar == 0 )
{
bpm++;
for(int i = 0; i < observers.size(); i++)
{
observers.at(i)->setFpb(bpm);
}
buffers->transportPosition->bar++;
}
oldBeat = beat;
}
@ -48,12 +67,14 @@ class TimeManager
buffers->transportPosition->ticks_per_beat = 1920;
buffers->transportPosition->beats_per_minute = bpm;
*/
}
private:
float bpm;
int oldBeat;
std::vector<Observer*> observers;
// list of Observers of this TimeManager Subject, "beat", "bar" updates?
/*
for(int i = 0; i < numObservers; i++ )

View File

@ -22,6 +22,7 @@ def build(bld):
sources = ['src/gui.cxx',
'src/main.cxx',
'src/jack.cxx',
'src/looper.cxx',
'src/eventhandlerdsp.cxx']
bld.program(source = sources, target='luppp5', use='JACK NTK')