2013-12-08 22:44:43 +01:00
|
|
|
/*
|
|
|
|
* Author: Harry van Haaren 2013
|
|
|
|
* harryhaaren@gmail.com
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
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.
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
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.
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
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() :
|
2016-12-27 13:47:19 +01:00
|
|
|
TimeObserver(),
|
|
|
|
playBar (false),
|
|
|
|
active (false),
|
|
|
|
playPoint (0)
|
2013-08-15 22:09:42 +02:00
|
|
|
{
|
2018-07-24 22:30:39 +02:00
|
|
|
const uint32_t sr = jack->getSamplerate();
|
|
|
|
const uint32_t bipDuration = (sr / 10);
|
2016-12-27 13:47:19 +01:00
|
|
|
//Create Beat/Bar samples
|
2018-07-24 22:30:39 +02:00
|
|
|
beatSample=new float[bipDuration];
|
|
|
|
barSample=new float[bipDuration];
|
2016-12-27 13:47:19 +01:00
|
|
|
// create beat and bar samples
|
2018-07-24 22:30:39 +02:00
|
|
|
endPoint = bipDuration;
|
2016-12-27 13:47:19 +01:00
|
|
|
// samples per cycle of
|
2018-07-24 22:30:39 +02:00
|
|
|
float scale = 2 * 3.1415 *880 / sr;
|
2016-12-27 13:47:19 +01:00
|
|
|
|
|
|
|
// And fill it up
|
2018-07-24 22:30:39 +02:00
|
|
|
for(int i=0; i < bipDuration; i++) {
|
2016-12-27 13:47:19 +01:00
|
|
|
beatSample[i]= sin(i*scale);
|
|
|
|
barSample [i]= sin(i*scale*2);
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:30:39 +02:00
|
|
|
uint32_t fade_smps = (bipDuration / 10.f);
|
|
|
|
for(int i = 0; i < fade_smps; i++) {
|
|
|
|
float f = (i / (float)fade_smps);
|
|
|
|
f = (f * f * f);
|
|
|
|
beatSample[i] *= f;
|
|
|
|
barSample [i] *= f;
|
|
|
|
|
|
|
|
beatSample[bipDuration-1-i] *= f;
|
|
|
|
barSample [bipDuration-1-i] *= f;
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:00:07 +02:00
|
|
|
setVolume(0.5);
|
|
|
|
|
2016-12-27 13:47:19 +01:00
|
|
|
// don't play after creation
|
|
|
|
playPoint = endPoint + 1;
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:39:13 +02:00
|
|
|
Metronome::~Metronome()
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
if(beatSample)
|
|
|
|
delete [] beatSample;
|
|
|
|
if(barSample)
|
|
|
|
delete [] barSample;
|
2016-09-15 14:39:13 +02:00
|
|
|
}
|
|
|
|
|
2013-08-15 22:09:42 +02:00
|
|
|
void Metronome::setActive(bool a)
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
active = a;
|
|
|
|
// don't play immidiatly
|
|
|
|
playPoint = endPoint + 1;
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|
2014-08-30 17:44:09 +02:00
|
|
|
void Metronome::setVolume( float v )
|
|
|
|
{
|
2017-04-17 18:00:07 +02:00
|
|
|
vol = v * 0.5;
|
|
|
|
//printf(" vol = %f \n", vol );
|
2014-08-30 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2013-11-02 14:49:47 +01:00
|
|
|
void Metronome::bar()
|
2013-08-15 22:09:42 +02:00
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
playPoint = 0;
|
|
|
|
playBar = true;
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Metronome::beat()
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
playPoint = 0;
|
|
|
|
playBar = false;
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:33 +02:00
|
|
|
void Metronome::setFpb(double f)
|
2013-08-15 22:09:42 +02:00
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
fpb = f;
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Metronome::process(int nframes, Buffers* buffers)
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
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];
|
2016-12-27 13:47:19 +01:00
|
|
|
|
|
|
|
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;
|
2016-12-27 13:47:19 +01:00
|
|
|
playPoint++;
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 22:09:42 +02:00
|
|
|
}
|
|
|
|
|