pwm

pulse width modulation oscillator Bandwith limited
Author: Johannes Taelman
License: BSD
Github: osc/pwm.axo

Inlets

frac32.bipolar pitch

frac32.bipolar pulse width. Zero corresponds to 50% duty cycle.

Outlets

frac32buffer.bipolar pwm wave, anti-aliased

Parameters

frac32.s.map.pitch pitch

Declaration
int32_t osc_p;
static const int blepvoices = 8;
int16_t *oscp[blepvoices];
uint32_t nextvoice;
int32_t pwmp;
Init
int j;
for (j = 0; j < blepvoices; j++)
  oscp[j] = &blept[BLEPSIZE - 1];
nextvoice = 0;
Control Rate
uint32_t freq;
MTOFEXTENDED(param_pitch + inlet_pitch, freq);
int j;
int16_t *lastblep = &blept[BLEPSIZE - 1];
for (j = 0; j < BUFSIZE; j++) {
  int i;
  int p;
  p = osc_p;
  osc_p = p + freq;
  int32_t sum = 0;
  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) + inlet_pw) << 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) + inlet_pw) << 4;
    }
  }
  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;
  outlet_wave[j] = sum << 13;
}

Privacy

© 2021 Zrna Research