Loopp/src/metronome.cxx

120 lines
2.4 KiB
C++
Raw Normal View History

2013-12-08 22:44:43 +01:00
/*
* Author: Harry van Haaren 2013
* harryhaaren@gmail.com
*
2013-12-08 22:44:43 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2013-12-08 22:44:43 +01:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2013-12-08 22:44:43 +01:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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),
vol (1),
playPoint (0)
2013-08-15 22:09:42 +02:00
{
//Create Beat/Bar samples
beatSample=new float[jack->getSamplerate()];
barSample=new float[jack->getSamplerate()];
// create beat and bar samples
endPoint = ( jack->getSamplerate()/10 );
// samples per cycle of
float scale = 2 * 3.1415 *880/jack->getSamplerate();
// And fill it up
for(int i=0; i < jack->getSamplerate(); i++) {
beatSample[i]= sin(i*scale);
barSample [i]= sin(i*scale*2);
}
// don't play after creation
playPoint = endPoint + 1;
2013-08-15 22:09:42 +02:00
}
Metronome::~Metronome()
{
if(beatSample)
delete [] beatSample;
if(barSample)
delete [] barSample;
}
2013-08-15 22:09:42 +02:00
void Metronome::setActive(bool a)
{
active = a;
// don't play immidiatly
playPoint = endPoint + 1;
2013-08-15 22:09:42 +02:00
}
void Metronome::setVolume( float v )
{
vol = v;
printf(" vol = %f \n", vol );
}
void Metronome::bar()
2013-08-15 22:09:42 +02:00
{
playPoint = 0;
playBar = true;
2013-08-15 22:09:42 +02:00
}
void Metronome::beat()
{
playPoint = 0;
playBar = false;
2013-08-15 22:09:42 +02:00
}
void Metronome::setFpb(int f)
{
fpb = f;
// disable play until next beat
playPoint = endPoint + 1;
2013-08-15 22:09:42 +02:00
}
void Metronome::process(int nframes, Buffers* buffers)
{
if ( not active )
return;
2017-03-19 15:58:28 +01:00
float* outL = buffers->audio[Buffers::HEADPHONES_OUT_L];
float* outR = buffers->audio[Buffers::HEADPHONES_OUT_R];
float* sample = &beatSample[0];
if( playBar ) {
sample = &barSample[0];
}
for(int i = 0; i < nframes; i++) {
if ( playPoint < endPoint ) {
2017-03-19 15:58:28 +01:00
outL[i] += sample[playPoint] * vol;
outR[i] += sample[playPoint] * vol;
playPoint++;
}
}
2013-08-15 22:09:42 +02:00
}