Skip to content

Commit

Permalink
add multibar metronome interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gisogrimm committed Sep 19, 2024
1 parent 05654d5 commit eb132e2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
13 changes: 13 additions & 0 deletions libtascar/include/osc_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ namespace TASCAR {
void add_vector_double(const std::string& path, std::vector<double>* data,
const std::string& range = "",
const std::string& comment = "");
/** \brief Register a vector of int32 variable for OSC access
The dimension of the vector specifies the length of the
message. Sending a message of different size will not have any
effect, i.e., the callback handler will not change the size of
the data.
\param path OSC path
\param data Pointer to data
*/
void add_vector_int(const std::string& path, std::vector<int32_t>* data,
const std::string& range = "",
const std::string& comment = "");
/** \brief Register a vector of floats variable for OSC access as dB SPL
The dimension of the vector specifies the length of the
Expand Down
21 changes: 21 additions & 0 deletions libtascar/src/osc_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ int osc_set_vector_double(const char*, const char*, lo_arg** argv, int argc,
return 1;
}

int osc_set_vector_int(const char*, const char*, lo_arg** argv, int argc,
lo_message, void* user_data)
{
if(user_data) {
std::vector<int32_t>* data((std::vector<int32_t>*)user_data);
if(argc == (int)(data->size()))
for(int k = 0; k < argc; ++k)
(*data)[k] = argv[k]->i;
}
return 1;
}

int osc_set_double_db(const char*, const char* types, lo_arg** argv, int argc,
lo_message, void* user_data)
{
Expand Down Expand Up @@ -800,6 +812,15 @@ void osc_server_t::add_vector_double(const std::string& path,
osc_set_vector_double, data, true, false, range, comment);
}

void osc_server_t::add_vector_int(const std::string& path,
std::vector<int32_t>* data,
const std::string& range,
const std::string& comment)
{
add_method(path, std::string(data->size(), 'i').c_str(), osc_set_vector_int,
data, true, false, range, comment);
}

void osc_server_t::add_double_db(const std::string& path, double* data,
const std::string& range,
const std::string& comment)
Expand Down
70 changes: 40 additions & 30 deletions plugins/src/tascar_ap_metronome.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,34 @@ class metronome_t : public TASCAR::audioplugin_base_t {

private:
void update_par();
bool changeonone;
double bpm;
uint32_t bpb;
double a1;
double ao;
double fres1;
double freso;
double q1;
double qo;
bool sync;
bool bypass;
bool bypass_;
int64_t t;
int64_t beat;
bool changeonone = false;
double bpm = 120;
std::vector<int32_t> bpb = {4}; //< beats per bar, for at least one bar
double a1 = 0.002;
double ao = 0.001;
double fres1 = 1000;
double freso = 600;
double q1 = 0.997;
double qo = 0.997;
bool sync = false;
bool bypass = false;
bool bypass_ = false;
int64_t t = 0;
int64_t beat = 0;
int64_t bar = 0;
TASCAR::resonance_filter_t f1;
TASCAR::resonance_filter_t fo;
uint32_t bpb_;
double a1_;
double ao_;
int64_t period;
uint32_t dispatchin;
TASCAR::osc_server_t* srv_;
std::vector<int32_t> bpb_ = {4};
double a1_ = 0.002;
double ao_ = 0.001;
int64_t period = 0; //< number of audio samples per beat
uint32_t dispatchin = 0;
TASCAR::osc_server_t* srv_ = NULL;
TASCAR::msg_t msg;
};

metronome_t::metronome_t(const TASCAR::audioplugin_cfg_t& cfg)
: audioplugin_base_t(cfg), changeonone(false), bpm(120), bpb(4), a1(0.002),
ao(0.001), fres1(1000), freso(600), q1(0.997), qo(0.997), sync(false),
bypass(false), bypass_(false), t(0), beat(0), bpb_(4), a1_(0.002),
ao_(0.001), period(0), dispatchin(0), srv_(NULL),
msg(find_or_add_child("msg"))
: audioplugin_base_t(cfg), msg(find_or_add_child("msg"))
{
GET_ATTRIBUTE_BOOL(changeonone, "Apply OSC parameter changes on next bar");
GET_ATTRIBUTE(bpm, "", "Beats per minute");
Expand All @@ -89,6 +86,11 @@ metronome_t::metronome_t(const TASCAR::audioplugin_cfg_t& cfg)
GET_ATTRIBUTE(q1, "", "Filter resonance of first beat");
GET_ATTRIBUTE(qo, "", "Filter resonance of other beats");
GET_ATTRIBUTE_BOOL(bypass, "Load in bypass mode");
if(bpb.empty())
throw TASCAR::ErrMsg("At least one bar needs to be specified (bpb).");
for(auto b : bpb)
if(b < 1)
throw TASCAR::ErrMsg("A bar needs to contain at least one beat.");
}

void metronome_t::configure()
Expand All @@ -109,7 +111,7 @@ void metronome_t::add_variables(TASCAR::osc_server_t* srv)
TASCAR::strrep(TASCAR::tscbasename(__FILE__), ".cc", ""));
srv->add_bool("/changeonone", &changeonone);
srv->add_double("/bpm", &bpm);
srv->add_uint("/bpb", &bpb);
srv->add_vector_int("/bpb", &bpb);
srv->add_double_dbspl("/a1", &a1);
srv->add_double_dbspl("/ao", &ao);
srv->add_bool("/sync", &sync);
Expand Down Expand Up @@ -162,18 +164,26 @@ void metronome_t::ap_process(std::vector<TASCAR::wave_t>& chunk,
ldiv_t tmp(ldiv(t, period));
t = tmp.rem;
beat = tmp.quot;
if(bpb_ > 0) {
tmp = ldiv(beat, bpb_);
if(bpb_[bar] > 0) {
tmp = ldiv(beat, bpb_[bar]);
beat = tmp.rem;
} else
} else {
beat = 0;
++bar;
if(bar >= (int64_t)(bpb_.size()))
bar = 0;
}
} else {
// free-run mode, in
if(t >= period) {
t = 0;
++beat;
if(beat >= bpb_)
if(beat >= bpb_[bar]) {
beat = 0;
++bar;
if(bar >= (int64_t)(bpb_.size()))
bar = 0;
}
}
}
if(t || (sync && !tp.rolling)) {
Expand Down

0 comments on commit eb132e2

Please sign in to comment.