blepped2

triangle oscillator Bandwith limited
Author: Johannes Taelman
License: BSD
Github: sss/osc/blepped2.axo

Inlets

frac32.bipolar pitch

frac32buffer mod

Outlets

frac32buffer.bipolar triangle wave, anti-aliased

Parameters

frac32.s.map.pitch pitch

frac32.s.map mod

int32 mode

Declaration
uint32_t osc_p;
int32_t Osc_p;
static const int blepvoices = 8;
const int16_t *oscp[blepvoices];
int16_t *Oscp[blepvoices];
int16_t amp[blepvoices];
uint32_t nextvoice;
int32_t vgain[blepvoices];
int32_t i0;

int32_t in[BUFSIZE];
int32_t out[BUFSIZE];
int i;

int32_t SIN(int32_t pitch, int32_t fm) {
  uint32_t freq;
  MTOFEXTENDED(pitch, freq);
  int32_t FM;
  int32_t fM = ___SMMUL(fm << 4, freq) << 3;
  int j;
  for (j = 0; j < BUFSIZE; j++) {
    FM = ___SMMUL(in[j] << 3, fM << 2);
    int32_t tfreq = freq + FM;
    osc_p += tfreq;
    int32_t sine;
    SINE2TINTERP(osc_p, sine)
    out[j] = sine >> 5;
  }
}
int32_t TRI(int32_t pitch, int32_t fm) {
  uint32_t freq;
  MTOFEXTENDED(pitch, freq);
  int32_t FM;
  int32_t fM = ___SMMUL(fm << 4, freq) << 3;
  int j;
  const int16_t *lastblep = &blt[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    FM = ___SMMUL(in[j] << 3, fM << 2);
    int32_t tfreq = freq + FM;
    int i;
    uint32_t p;
    p = osc_p;
    int32_t p3 = p - 2 * tfreq;
    int32_t tri;
    if (p3 > 0) {
      tri = ((1 << 30) - (p3)) >> 4;
    } else {
      tri = (p3 + (1 << 30)) >> 4;
    }
    osc_p = p + tfreq;
    if ((((int32_t)osc_p) > 0) ^ (((int32_t)p) > 0)) { // dispatch
      if ((freq >> 6) > 0) {
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        int32_t x = (osc_p & 0x7FFFFFFF) / (((uint32_t)tfreq) >> 6);
        oscp[nextvoice] = &blt[x];
        amp[nextvoice] = (((int32_t)osc_p) < 0) ? tfreq >> 16 : -(tfreq >> 16);
      }
    }
    int32_t sum = 0;
    for (i = 0; i < blepvoices; i++) { // sample
      const int16_t *t = oscp[i];
      sum += (*t) * amp[i];
      t += 64;
      if (t >= lastblep)
        t = lastblep;
      oscp[i] = t;
    }
    out[j] = tri + (sum >> 3);
  }
};

int32_t SQR(int32_t pitch, int32_t fm) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  int32_t FM;
  int32_t fM = ___SMMUL(fm << 4, freq) << 3;
  int j;
  int16_t *lastblep = &blept[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    FM = ___SMMUL(in[j] << 3, fM << 2);
    int32_t tfreq = freq + FM;
    tfreq = tfreq > 0 ? tfreq : -tfreq - 1;
    int i;
    int p;
    p = Osc_p;
    Osc_p = p + (tfreq << 1);
    int32_t sum = 0;
    if ((Osc_p > 0) && !(p > 0)) { // dispatch
      nextvoice = (nextvoice + 1) & (blepvoices - 1);
      int32_t x = Osc_p / (tfreq >> 5);
      Oscp[nextvoice] = &blept[x];
    }
    for (i = 0; i < blepvoices; i++) { // sample
      int16_t *t = Oscp[i];
      if (i & 1)
        sum += *t;
      else
        sum -= *t;
      t += 64;
      if (t >= lastblep)
        t = lastblep;
      Oscp[i] = t;
    }
    sum -= ((((nextvoice + 1) & 1) << 1) - 1) << 13;
    out[j] = sum << 13;
  }
}

int32_t SAW(int32_t pitch, int32_t fm) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  int32_t FM;
  int32_t fM = ___SMMUL(fm << 4, freq) << 3;
  int j;
  int16_t *lastblep = &blept[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int i;
    int p;
    FM = ___SMMUL(in[j] << 3, fM << 2);
    int32_t tfreq = freq + FM;
    tfreq = tfreq > 0 ? tfreq : -tfreq - 1;
    p = Osc_p;
    Osc_p = p + tfreq;
    int i1 = in[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      nextvoice = (nextvoice + 1) & (blepvoices - 1);
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      Oscp[nextvoice] = &blept[x];
      vgain[nextvoice] = vgain[nextvoice] =
          (((x * (tfreq >> 7)) + (((uint32_t)p) >> 1))) >> 18;
      Osc_p = x * (tfreq >> 6);
    } else if ((Osc_p > 0) && !(p > 0)) { // dispatch
      nextvoice = (nextvoice + 1) & (blepvoices - 1);
      int32_t x = Osc_p / (tfreq >> 6);
      Oscp[nextvoice] = &blept[x];
      vgain[nextvoice] = 1 << 13;
    }
    i0 = i1;
    int32_t sum = 0;
    for (i = 0; i < blepvoices; i++) { // sample
      int16_t *t = Oscp[i];
      sum += (16384 - (*t)) * vgain[i];
      t += 64;
      if (t >= lastblep)
        t = lastblep;
      Oscp[i] = t;
    }
    // sum = -sum;
    uint32_t g = Osc_p;
    out[j] = (g >> 5) + sum - (1 << 26);
  }
}
Init
int j;
for (j = 0; j < blepvoices; j++) {
  oscp[j] = &blt[BLEPSIZE - 1];
  Oscp[j] = &blept[BLEPSIZE - 1];
  amp[j] = 0;
  i0 = 0;
}
nextvoice = 0;
Control Rate
for (i = 0; i < BUFSIZE; i++) {
  in[i] = inlet_mod[i];
}
if (param_mode == 0) {
  SIN(inlet_pitch + param_pitch, param_mod);
}
if (param_mode == 1) {
  TRI(inlet_pitch + param_pitch, param_mod);
}
if (param_mode == 2) {
  SQR(inlet_pitch + param_pitch, param_mod);
}
if (param_mode == 3) {
  SAW(inlet_pitch + param_pitch, param_mod);
}
for (i = 0; i < BUFSIZE; i++) {
  outlet_wave[i] = out[i];
}

Privacy

© 2024 Zrna Research