Loopp/src/metronome.cxx

84 lines
1.4 KiB
C++
Raw Normal View History

2013-08-15 22:09:42 +02:00
#include "metronome.hxx"
#include <cmath>
#include <iostream>
2013-09-17 14:11:11 +02:00
#include "jack.hxx"
2013-08-15 22:09:42 +02:00
#include "buffers.hxx"
#include "observer/time.hxx"
2013-09-17 14:11:11 +02:00
extern Jack* jack;
2013-08-15 22:09:42 +02:00
using namespace std;
Metronome::Metronome() :
TimeObserver(),
playBar (false),
active (false),
playPoint (0)
{
// create beat and bar samples
2013-09-17 14:11:11 +02:00
endPoint = ( jack->getSamplerate() / 441 );
2013-08-15 22:09:42 +02:00
// 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);
}
// don't play after creation
playPoint = endPoint + 1;
}
void Metronome::setActive(bool a)
{
active = a;
// don't play immidiatly
playPoint = endPoint + 1;
}
void Metronome::bar()
{
playPoint = 0;
playBar = true;
}
void Metronome::beat()
{
playPoint = 0;
}
void Metronome::setFpb(int f)
{
fpb = f;
// disable play until next beat
playPoint = endPoint + 1;
}
void Metronome::process(int nframes, Buffers* buffers)
{
if ( not active )
return;
float* outL = buffers->audio[Buffers::MASTER_OUT_L];
float* outR = buffers->audio[Buffers::MASTER_OUT_R];
float* sample = &beatSample[0];
if( playBar ) { sample = &barSample[0]; playBar = false; }
for(int i = 0; i < nframes; i++)
{
if ( playPoint < endPoint )
{
outL[i] += sample[playPoint];
outR[i] += sample[playPoint];
playPoint++;
}
}
}