Subversion Repositories DIN Is Noise

Rev

Rev 2311 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* beat2value.cc
* DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath
* DIN Is Noise is released under GNU Public License 2.0
* For more information, please visit https://dinisnoise.org/
*/


#include "beat2value.h"
#include "utils.h"
#include "console.h"
#include <string>
using namespace std;

extern int SAMPLE_RATE;

beat2value::beat2value (const string& n, const string& c) : name (n), crvname (c)  {
  crv = new multi_curve (c);
  crv_made_here = 1;
  swing = 0;
  setup ();
}

beat2value::beat2value () { crv = 0; crv_made_here = 0; swing = 0;}

void beat2value::setup (int delta_mult) {
  min_bpm = 0;
  bpm = 45;
  delta = 0;
  set_bpm (bpm, delta_mult);
  style = "loop";
  xmin = &_loopmin;
  xmax = &_loopmax;
  sol (crv);
  now = sol.firstx;
}

void beat2value::reverse () {
  delta = -delta;
  if (style == "loop") {
    if (delta < 0) {
      xmin = &_tomax;
      xmax = &_atmax;
    } else {
      xmax = &_tomin;
      xmin = &_atmin;
    }
  } /* else {
    // nothing todo for pong
  }*/

}

beat2value::~beat2value () {
  if (crv_made_here) {
    crv->save (crvname);
    delete crv;
  }
}

int beat2value::gen_and_mix (float* soln, float* mixb, float* mixa, int n) {
  //xmax->called = 0;

 
  float now1 = now, delta1 = delta;
  /*if (swing) {
    swingsol (now, delta, n, soln, *xmin, *xmax);
    sol (soln, n);
  } else {*/

    sol (now, delta, n, soln, *xmin, *xmax);
  //}

  /*if (xmax->called == 1) {
    xmax->called = 0;
    cons ("beatend");
  }*/


  if (mixer.active) {
    mixer.gen_mix (mixb, n, *xmin, *xmax);
    mixer.do_mix (soln, mixb, mixa, n);
  }

  /*if (accent) {
    accentsol (now1, delta1, n, mixb, *xmin, *xmax);
    multiply (soln, mixb, n);
  }*/


  return mixer.active;
}

int beat2value::modulate_and_mix (float* in, float* mixb, float* mixa, int n, float depth) {
  sol (now, delta, n, in, *xmin, *xmax);
  multiply (in, n, depth);
  if (mixer.active) {
    mixer.gen_mix (mixb, n, *xmin, *xmax);
    multiply (mixb, n, depth);
    mixer.do_mix (in, mixb, mixa, n);
  }
  return mixer.active;
}

int beat2value::modulate_and_mix (float* in, float* mixb, float* mixa, int n, float* depth) {
  sol (now, delta, n, in, *xmin, *xmax);
  multiply (in, depth, n);
  if (mixer.active) {
    mixer.gen_mix (mixb, n, *xmin, *xmax);
    multiply (mixb, depth, n);
    mixer.do_mix (in, mixb, mixa, n);
  }
  return mixer.active;
}


float beat2value::set_bpm (float n, int nsamples) {

  if (n < min_bpm) n = min_bpm;

  bpm = n;
  bps = bpm / 60.0f;
   
  // delta used in solvers
  int sgn = 1;
  if (delta < 0) sgn = -1;
  delta = sgn * bps * nsamples * 1.0f / SAMPLE_RATE;

  return bpm;

}

float beat2value::get_bpm () {
  return bpm;
}

void beat2value::set_mix (multi_curve& c, const string& nam) {
  mixer.setup (c, now, delta);
  mixer.name = nam;
}

void beat2value::setswing (multi_curve& sw) {
  swing = &sw;
  swingsol (swing);
}


void beat2value::setaccent (multi_curve& ac) {
  accent = &ac;
  accentsol (accent);
}

void powbeat (const string& name, beat2value& bv, float p) {
  float b = bv.get_bpm () * p;
  bv.set_bpm (b);
  cons << GREEN << name << " bpm = " << b << eol;
}