blepcore2

Multi wave oscillator with sync and FM modulation Bandwith limited V2 has more oscillator models
Author: Remco van der Most
License: BSD
Github: sss/osc/blepcore2.axo

Inlets

None

Outlets

None

Declaration
int hrmPst[16][8] = {{1, 1, 1, 1, 1, 1, 1, 1},    {1, 2, 1, 2, 1, 2, 1, 2},
                     {1, 1, 2, 2, 1, 1, 2, 2},    {1, 1, 1, 2, 2, 2, 3, 3},
                     {1, 2, 3, 1, 2, 3, 1, 2},    {1, 2, 1, 3, 1, 2, 1, 3},
                     {1, 1, 2, 2, 3, 3, 4, 4},    {1, 1, 1, 2, 2, 3, 3, 4},
                     {1, 2, 1, 3, 1, 4, 1, 5},    {1, 2, 3, 4, 5, 6, 7, 8},
                     {1, 2, 3, 4, 1, 1, 2, 3},    {1, 1, 3, 3, 5, 5, 7, 7},
                     {1, 1, 2, 2, 4, 4, 6, 6},    {1, 1, 2, 2, 4, 4, 8, 8},
                     {1, 2, 3, 5, 7, 11, 13, 17}, {1, 2, 3, 4, 6, 8, 9, 12}};
int pst;
int core;

static const int blepvoices = 8;
const int16_t *uoscp[blepvoices];

/////////// all these values have to be updated from the oscillator voice module
///for correct functioning of the oscillators!///////////////
int16_t *oscp[blepvoices];
int32_t vgain[blepvoices];
int32_t osc_p;
uint32_t uosc_p;
uint32_t nextvoice;
int32_t i0;
int32_t Pwm[BUFSIZE]; // pwm control for pulse oscillator->refer to this name to
                      // control pulsewidth
int32_t pwmp; //
int32_t wave[BUFSIZE];
int32_t sync[BUFSIZE];
int32_t mod[BUFSIZE];
/////////// all these values have to be updated from the oscillator voice module
///for correct functioning of the oscillators!///////////////

int32_t v27 = 1 << 27;
int32_t v28 = 1 << 28;

int32_t OSC_SIN(int32_t pitch) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  freq = freq * hrmPst[pst][core];
  int j;
  for (j = 0; j < BUFSIZE; j++) {

    osc_p += freq + (___SMMUL(freq, mod[j] << 3) << 6);
    if ((sync[j] > 0) && !nextvoice) {
      nextvoice = 1;
      osc_p = 0;
    } else if (sync[j] < 0) {
      nextvoice = 0;
    }
    int32_t r;
    SINE2TINTERP(osc_p, r)
    wave[j] = r >> 5;
  }
}

int32_t OSC_NSI(int32_t pitch) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  freq = freq * hrmPst[pst][core] >> 4;
  int j;
  int32_t LP;

  for (j = 0; j < BUFSIZE; j++) {
    MTOF(Pwm[j], LP)
    osc_p += freq + (___SMMUL(freq, pwmp + mod[j] << 3) << 6);
    if ((sync[j] > 0) && !nextvoice) {
      nextvoice = 1;
      osc_p = 0;
    } else if (sync[j] < 0) {
      nextvoice = 0;
    }
    int32_t r;
    SINE2TINTERP(osc_p, r)
    pwmp += ___SMMUL((int32_t)(GenerateRandomNumber() >> 4) - pwmp, LP);
    wave[j] = ___SMMUL(r, pwmp);
  }
}

int32_t OSC_HRM(int32_t pitch) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  freq = freq * hrmPst[pst][core];
  int j;
  for (j = 0; j < BUFSIZE; j++) {
    int32_t mix, stp1, stp2;
    mix = Pwm[j];
    mix = mix > 0 ? mix : -mix;
    stp1 = mix >> 22;
    mix = mix - (stp1 << 22) << 5;
    stp1 = stp1 & 31;
    stp2 = (stp1 + 1) & 31;
    stp1 += 1;
    stp2 += 1;
    osc_p += freq + (___SMMUL(freq, mod[j] << 3) << 6);
    if ((sync[j] > 0) && !nextvoice) {
      nextvoice = 1;
      osc_p = 0;
    } else if (sync[j] < 0) {
      nextvoice = 0;
    }
    int32_t r1, r2;
    SINE2TINTERP(osc_p * stp1, r1)
    SINE2TINTERP(osc_p * stp2, r2)
    r1 = r1 >> 5;
    r2 = r2 >> 5;
    wave[j] = r1 + (___SMMUL(r2 - r1, mix) << 5);
  }
}

int32_t OSC_VOW(int32_t pitch) {
  int32_t freq;
  MTOFEXTENDED(pitch, freq);
  freq = freq * hrmPst[pst][core];
  int j;
  for (j = 0; j < BUFSIZE; j++) {

    uosc_p += freq + (___SMMUL(freq, mod[j] << 3) << 6);
    if ((sync[j] > 0) && !nextvoice) {
      nextvoice = 1;
      uosc_p = 0;
    } else if (sync[j] < 0) {
      nextvoice = 0;
    }
    int32_t r;
    SINE2TINTERP(uosc_p + (___SMMUL(Pwm[j], uosc_p >> 1) << 8), r)
    wave[j] = ___SMMUL((((1 << 32) - uosc_p >> 1)), r >> 3);
  }
}

int32_t OSC_TRI(int32_t pitch) {
  uint32_t Freq;
  MTOFEXTENDED(pitch, Freq);
  Freq = Freq * hrmPst[pst][core];
  int j;
  const int16_t *lastblep = &blt[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int32_t freq = Freq + (___SMMUL(Freq >> 1, mod[j] << 3) << 4);
    int dir = freq > 0 ? 1 : 0;
    int32_t frq = freq;
    freq = freq > 0 ? freq : -freq;
    int i;
    uint32_t p;
    p = uosc_p;
    int32_t p3 = p - 2 * frq;
    int32_t tri;
    if (p3 > 0) {
      tri = ((1 << 30) - (p3)) >> 4;
    } else {
      tri = (p3 + (1 << 30)) >> 4;
    }
    uosc_p = p + freq;

    int i1 = sync[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      nextvoice = (nextvoice + 1) & (3);
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      uoscp[nextvoice] = &blt[x];
      vgain[nextvoice] = (((x * (frq >> 7)) + (((uint32_t)p) >> 1))) >> 18;
      uosc_p = x * (frq >> 6);
    } else if ((((int32_t)uosc_p) > 0) ^ (((int32_t)p) > 0)) { // dispatch
      if ((freq >> 6) > 0) {
        nextvoice = (nextvoice + 1) & (3);
        int32_t x = (uosc_p & 0x7FFFFFFF) / (((uint32_t)freq) >> 6);
        uoscp[nextvoice] = &blt[x];
        vgain[nextvoice] = (((int32_t)uosc_p) < 0) ? frq >> 16 : -(frq >> 16);
      }
    }
    i0 = i1;
    int32_t sum = 0;
    for (i = 0; i < 4; i++) { // sample
      const int16_t *t = uoscp[i];
      sum += (*t) * vgain[i];
      t += 64;
      if (t >= lastblep)
        t = lastblep;
      uoscp[i] = t;
    }
    wave[j] = tri + (sum >> 3);
  }
}

int32_t OSC_PEK(int32_t pitch) {
  uint32_t Freq;
  MTOFEXTENDED(pitch, Freq);
  Freq = Freq * hrmPst[pst][core];
  int j;
  const int16_t *lastblep = &blt[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int32_t freq = Freq + (___SMMUL(Freq >> 1, mod[j] << 3) << 4);
    int dir = freq > 0 ? 1 : 0;
    int32_t frq = freq;
    freq = freq > 0 ? freq : -freq;
    int i;
    uint32_t p;
    p = uosc_p;
    int32_t p3 = p - 2 * frq;
    int32_t tri;
    if (p3 > 0) {
      tri = ((1 << 30) - (p3)) >> 4;
    } else {
      tri = (p3 + (1 << 30)) >> 4;
    }
    uosc_p = p + freq;

    int i1 = sync[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      nextvoice = (nextvoice + 1) & (3);
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      uoscp[nextvoice] = &blt[x];
      vgain[nextvoice] = (((x * (frq >> 7)) + (((uint32_t)p) >> 1))) >> 18;
      uosc_p = x * (frq >> 6);
    } else if ((((int32_t)uosc_p) > 0) ^ (((int32_t)p) > 0)) { // dispatch
      if ((freq >> 6) > 0) {
        nextvoice = (nextvoice + 1) & (3);
        int32_t x = (uosc_p & 0x7FFFFFFF) / (((uint32_t)freq) >> 6);
        uoscp[nextvoice] = &blt[x];
        vgain[nextvoice] = (((int32_t)uosc_p) < 0) ? frq >> 16 : -(frq >> 16);
      }
    }
    i0 = i1;
    int32_t sum = 0;
    for (i = 0; i < 4; i++) { // sample
      const int16_t *t = uoscp[i];
      sum += (*t) * vgain[i];
      t += 64;
      if (t >= lastblep)
        t = lastblep;
      uoscp[i] = t;
    }
    if (tri > 0) {
      SINE2TINTERP(tri - v27 << 3, tri)
      tri = (tri >> 3) + v28;
    } else {
      SINE2TINTERP(v27 + tri << 3, tri)
      tri = (tri >> 3) - v28;
    }
    wave[j] = tri + (sum >> 3);
  }
}

int32_t OSC_SAW(int32_t pitch) {
  int32_t Freq;
  MTOFEXTENDED(pitch, Freq);
  Freq = Freq * hrmPst[pst][core];
  int j;
  int16_t *lastblep = &blept[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int i;
    int p;
    int32_t freq = Freq + (___SMMUL(Freq, mod[j] << 3) << 4);
    int dir = freq > 0 ? 1 : -1;
    freq = freq > 0 ? freq : -freq;
    p = osc_p;
    osc_p = p + freq;
    int i1 = sync[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      nextvoice = (nextvoice + 1) & (3);
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      oscp[nextvoice] = &blept[x];
      vgain[nextvoice] = vgain[nextvoice] =
          (((x * (freq * dir >> 7)) + (((uint32_t)p) >> 1))) >> 18;
      osc_p = x * (freq * dir >> 6);
    } else if ((osc_p > 0) && !(p > 0)) { // dispatch
      nextvoice = (nextvoice + 1) & (3);
      int32_t x = osc_p / (freq >> 6);
      oscp[nextvoice] = &blept[x];
      vgain[nextvoice] = 1 << 13;
    }
    i0 = i1;
    int32_t sum = 0;
    for (i = 0; i < 4; 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;
    wave[j] = (g >> 5) + sum - (1 << 26);
  }
}

int32_t OSC_SQR(int32_t pitch) {
  int32_t Freq;
  MTOFEXTENDED(pitch, Freq);
  Freq = Freq * hrmPst[pst][core];
  int j;
  int16_t *lastblep = &blept[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int i;
    int p;
    uint32_t freq = Freq + (___SMMUL(Freq, mod[j] << 3) << 6);
    p = osc_p;
    osc_p = p + (freq << 1);
    int32_t sum = 0;
    int i1 = sync[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      osc_p = x * (freq >> 6);
      if (nextvoice & 1) {
        nextvoice = (nextvoice + 1) & (7);
        oscp[nextvoice] = &blept[x];
      }
    } else if ((osc_p > 0) && !(p > 0)) { // dispatch
      nextvoice = (nextvoice + 1) & (7);
      int32_t x = osc_p / (freq >> 5);
      oscp[nextvoice] = &blept[x];
    }
    i0 = i1;
    for (i = 0; i < 8; 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;
    wave[j] = sum << 13;
  }
}

int32_t OSC_PLS(int32_t pitch) {
  int32_t Freq;
  MTOFEXTENDED(pitch, Freq);
  Freq = Freq * hrmPst[pst][core];
  int j;
  int16_t *lastblep = &blept[BLEPSIZE - 1];
  for (j = 0; j < BUFSIZE; j++) {
    int i;
    int p;
    int32_t freq = Freq + (___SMMUL(Freq, mod[j] << 3) << 6);
    freq = freq > 0 ? freq : -freq;
    p = osc_p;
    osc_p = p + (freq);
    int32_t sum = 0;
    int i1 = sync[j] >> 2;
    if ((i1 > 0) && !(i0 > 0)) { // phase reset
      int32_t x = 64 - ((-i0 << 6) / (i1 - i0));
      osc_p = x * (freq >> 6);
      if (nextvoice & 1) {
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        oscp[nextvoice] = &blept[x];
      }
    } else if (((int32_t)osc_p) >= ((int32_t)(osc_p - pwmp))) {
      if ((osc_p > 0) && !(p > 0)) { // dispatch
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        int32_t x = 0;
        if (freq >> 24)
          x = osc_p / (freq >> 6);
        else if (freq)
          x = (osc_p << 6) / freq;
        oscp[nextvoice] = &blept[x];
        pwmp = ((1 << 27) + Pwm[j]) << 4;
      }
      if (((osc_p - pwmp) > 0) && !((p - pwmp) > 0)) { // dispatch
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        uint32_t x = 0;
        if (freq >> 24)
          x = (osc_p - pwmp) / (freq >> 6);
        else if (freq)
          x = ((osc_p - pwmp) << 6) / (freq);
        oscp[nextvoice] = &blept[x];
      }
    } else {
      if (((osc_p - pwmp) > 0) && !((p - pwmp) > 0)) { // dispatch
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        uint32_t x = 0;
        if (freq >> 24)
          x = (osc_p - pwmp) / (freq >> 6);
        else if (freq)
          x = ((osc_p - pwmp) << 6) / (freq);
        oscp[nextvoice] = &blept[x];
      }
      if ((osc_p > 0) && !(p > 0)) { // dispatch
        nextvoice = (nextvoice + 1) & (blepvoices - 1);
        int32_t x = 0;
        if (freq >> 24)
          x = osc_p / (freq >> 6);
        else if (freq)
          x = (osc_p << 6) / freq;
        oscp[nextvoice] = &blept[x];
        pwmp = ((1 << 27) + Pwm[j]) << 4;
      }
    }
    i0 = i1;
    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;
    wave[j] = sum << 13;
  }
}
Init
int j;
for (j = 0; j < blepvoices; j++) {
  oscp[j] = &blept[BLEPSIZE - 1];
  nextvoice = 0;
  i0 = 0;
  uoscp[j] = &blt[BLEPSIZE - 1];
  vgain[j] = 0;
}
pst = 0;
core = 0;

Privacy

© 2024 Zrna Research