From a30d540b4931a0c728ef6633b0b954892971d780 Mon Sep 17 00:00:00 2001 From: Harry van Haaren Date: Wed, 15 May 2013 22:48:43 +0100 Subject: [PATCH] -Added metronome class, plays back synthesized sins on beat & bar --- src/jack.cxx | 6 ++++ src/jack.hxx | 4 ++- src/looper.hxx | 9 +++--- src/metronome.hxx | 79 +++++++++++++++++++++++++++++++++++++++++++++ src/timemanager.hxx | 16 ++++----- 5 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 src/metronome.hxx diff --git a/src/jack.cxx b/src/jack.cxx index 3155b5c..f4cbb39 100644 --- a/src/jack.cxx +++ b/src/jack.cxx @@ -52,6 +52,9 @@ Jack::Jack() loopers.push_back( new Looper(i) ); timeManager.registerObserver( loopers.back() ); } + + timeManager.registerObserver( &metronome ); + jack_transport_start(client); } @@ -77,6 +80,9 @@ int Jack::process (jack_nframes_t nframes) for(uint i = 0; i < loopers.size(); i++) loopers.at(i)->process( nframes, &buffers ); + if (true) + metronome.process( nframes, &buffers ); + /* float* input = buffers.audio[Buffers::MASTER_INPUT]; float* output = buffers.audio[Buffers::MASTER_OUTPUT]; diff --git a/src/jack.hxx b/src/jack.hxx index b48195e..49282e4 100644 --- a/src/jack.hxx +++ b/src/jack.hxx @@ -18,6 +18,7 @@ #include #include "looper.hxx" +#include "metronome.hxx" #include "timemanager.hxx" using namespace std; @@ -39,7 +40,8 @@ class Jack TimeManager* getTimeManager(){return &timeManager;} private: - Buffers buffers; + Buffers buffers; + Metronome metronome; TimeManager timeManager; vector loopers; diff --git a/src/looper.hxx b/src/looper.hxx index 968772a..29b5ffa 100644 --- a/src/looper.hxx +++ b/src/looper.hxx @@ -34,19 +34,19 @@ class Looper : public Observer // for notifications void bar() { - cout << "Looper " << track << " got bar()" << flush; + //cout << "Looper " << track << " got bar()" << flush; playPoint = 0; if ( state == STATE_PLAY_QUEUED ) { - cout << " Q->Playing endpoint = " << endPoint; + cout << " Q->Playing endpoint = " << endPoint << endl; state = STATE_PLAYING; playPoint = 0; endPoint = lastWrittenSampleIndex; } if ( state == STATE_RECORD_QUEUED ) { - cout << " Q->Recording "; + cout << " Q->Recording " << endl; state = STATE_RECORDING; playPoint = 0; endPoint = 0; @@ -54,11 +54,10 @@ class Looper : public Observer // for notifications } if ( state == STATE_PLAY_QUEUED ) { - cout << " Q->Stopped "; + cout << " Q->Stopped " << endl; state = STATE_STOPPED; endPoint = lastWrittenSampleIndex; } - cout << endl; } void beat() diff --git a/src/metronome.hxx b/src/metronome.hxx new file mode 100644 index 0000000..506b812 --- /dev/null +++ b/src/metronome.hxx @@ -0,0 +1,79 @@ + +#ifndef LUPPP_METRONOME_H +#define LUPPP_METRONOME_H + +#include +#include + +#include "buffers.hxx" + +#include "observer/observer.hxx" + +using namespace std; + +// simple metronome class +class Metronome : public Observer +{ + public: + Metronome() : + playPoint (0), + playBar (false) + { + // create beat and bar samples + endPoint = (44100.f/441); + // samples per cycle of + float scale = 2 * 3.1415 / endPoint; + + /*And fill it up*/ + for(int i=0;i < endPoint*40;i++){ + beatSample[i]= sin(i*scale); + barSample [i]= sin(i*scale*1.5); + } + } + + void bar() + { + playPoint = 0; + playBar = true; + } + + void beat() + { + playPoint = 0; + //cout << "Looper " << track << " got beat()" << flush; + } + + void setFpb(int f) + { + fpb = f; + //cout << "Looper " << track << " got fpb of " << fpb << endl; + } + + void process(int nframes, Buffers* buffers) + { + float* out = buffers->audio[Buffers::MASTER_OUTPUT]; + + float* sample = &beatSample[0]; + if( playBar ) { sample = &barSample[0]; playBar = false; } + + for(int i = 0; i < nframes; i++) + { + if ( playPoint < endPoint ) + { + out[i] += sample[playPoint++]; + } + + } + } + + private: + int fpb; + bool playBar; + + int playPoint, endPoint; + float barSample[44100]; + float beatSample[44100]; + +}; + +#endif // LUPPP_METRONOME_H diff --git a/src/timemanager.hxx b/src/timemanager.hxx index 3bf5716..a2fb56b 100644 --- a/src/timemanager.hxx +++ b/src/timemanager.hxx @@ -49,19 +49,19 @@ class TimeManager if ( beat != oldBeat ) { - if ( beat % (int)buffers->transportPosition->beats_per_bar == 0 ) - { - // inform observers of new bar - for(uint i = 0; i < observers.size(); i++) { observers.at(i)->bar(); } - buffers->transportPosition->bar++; - } - - // inform observers of new beat + // inform observers of new beat FIRST for(uint i = 0; i < observers.size(); i++) { observers.at(i)->beat(); } + if ( beat % (int)buffers->transportPosition->beats_per_bar == 0 ) + { + // inform observers of new bar SECOND + for(uint i = 0; i < observers.size(); i++) { observers.at(i)->bar(); } + buffers->transportPosition->bar++; + } + oldBeat = beat; }