blepCore

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

Inlets

None

Outlets

None

Declaration
int32_t osc_p;
uint32_t uosc_p;
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];
int16_t *oscp[blepvoices];
int32_t vgain[blepvoices];
uint32_t nextvoice;
int32_t i0;
int32_t wave[BUFSIZE];
int32_t sync[BUFSIZE];
int32_t mod[BUFSIZE];
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_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) << 6);
    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_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) << 6);
    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;
  }
}
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