granular player 4 stereo

cheap granular player, with rectangular envelope for grains (hold parameter instead of attack and decay)
Author: Sputnki
License: BSD
Github: sptnk/table/granular player 4 stereo.axo

Inlets

bool32 hard retrig for a new grain

frac32 modulation inlet for hold parameter

frac32 modulation inlet for density parameter

frac32 modulation inlet for playback parameter

frac32 modulation inlet for pos parameter

Outlets

int32 number of active grains (use for debug)

frac32buffer audio out left

frac32buffer audio out right

Parameters

frac32.u.map how many grains per second are played (not in natural units)

frac32.u.map position in the table from where to read audio

frac32.s.map.ratio playback speed for grains.0=stop; +16 = normal speed; -16 reverse playback

int32 useful in case many grains are played

frac32.s.map.klineartime.exp set the hold time for a grain

Attributes

objref name of the table object to granulize

spinner maximum number of allocated grains

Declaration
bool grain_active[attr_grains];
uint32_t grain_amp[attr_grains];
uint32_t grain_phase[attr_grains];
bool grain_pan[attr_grains];

bool lr = 0;

uint32_t global_phase;
uint32_t global_phase_old;

uint32_t grain_num = 0;

uint32_t temp_32;

uint32_t freq;

uint32_t global_hold;

uint32_t pitchmul =
    66000 *
    ((1 << 27) / attr_table.LENGTH); // this coefficient should be adjusted
                                     // (const*48000*2^27 / LENGTH)

bool rtrig;
Control Rate
bool dostuff = 0;

if (inlet_reset && !rtrig) {
  global_phase_old = 0;
  global_phase = 0;
  dostuff = 1;
  rtrig = 1;
} else {
  if (!inlet_reset)
    rtrig = 0;

  global_phase += param_density + inlet_density << 3;
  if (global_phase < global_phase_old) // time to activate another grain
    dostuff = 1;
  global_phase_old = global_phase;
}

MTOF(-param_hold - inlet_hold, global_hold);

freq = ___SMMUL(param_playback + inlet_playback >> 9, pitchmul);

if (dostuff) {
  lr = !lr;
  grain_num++;
  if (grain_num >= attr_grains)
    grain_num = 0;

  grain_amp[grain_num] = 0;
  grain_pan[grain_num] = lr;
  grain_active[grain_num] = 1;
  grain_phase[grain_num] = param_pos + inlet_pos;
}

int32_t accum[BUFSIZE][2];

for (int j = 0; j < BUFSIZE; j++) {
  accum[j][0] = 0;
  accum[j][1] = 0;
}

int voicealloc = 0;
for (int i = 0; i < attr_grains; i++) {

  if (grain_active[i]) // if in hold phase (active)
  {
    temp_32 = grain_amp[i] + (global_hold >> 1);
    if (temp_32 > grain_amp[i])
      grain_amp[i] = temp_32;
    else {
      grain_active[i] = 0;
      grain_amp[i] = 0;
    }
    voicealloc++;
  }

  for (int j = 0; j < BUFSIZE; j++) {
    if (grain_amp[i]) // if in attack phase (active)
    {
      accum[j][grain_pan[i]] += attr_table.array[__USAT(grain_phase[i], 27) >>
                                                 (27 - attr_table.LENGTHPOW)]
                                    << attr_table.GAIN >>
                                param_gainreduction;
      grain_phase[i] += freq;
    }
  }
}
outlet_alloc = voicealloc;
for (int j = 0; j < BUFSIZE; j++) {
  outlet_l[j] = accum[j][0];
  outlet_r[j] = accum[j][1];
}

Privacy

© 2024 Zrna Research