samplePlayer

read from table, nearest neighbour
Author: Remco van der Most
License: BSD
Github: sss/table/samplePlayer.axo

Inlets

int32 startsample

int32 loopsample

int32 loopmode

int32 endsample

int32 samplelength

frac32 startpitch

frac32 startStrt

frac32 startEnd

frac32 looppitch

frac32 loopStrt

frac32 loopEnd

frac32 endpitch

frac32 endStrt

frac32 endEnd

frac32 FMwidth

frac32buffer fm

bool32 trig

bool32 LOOP

Outlets

int32 stage

frac32 pitch

frac32buffer table[a]

Parameters

int32 samplelength

bool32.tgl intExt

Attributes

objref table

Declaration
uint32_t count;
int ntrig;
int stage;
int direction1;
int direction2;
int LOOP;
int sample;
float32_t pitch;
int32_t out;
uint32_t phase;
uint32_t Phase;
int etrig;
Init
count = 0;
direction1 = 1;
direction2 = 1;
Control Rate
int32_t samplelength;
int32_t Samplelength;
int32_t MASK;
if (param_intExt > 0) {
  samplelength = (1 << param_samplelength);
  Samplelength = param_samplelength;
  MASK = ((1 << (20 - param_samplelength)) - 1);
} else {
  samplelength = (1 << inlet_samplelength);
  Samplelength = inlet_samplelength;
  MASK = ((1 << (20 - inlet_samplelength)) - 1);
}

outlet_pitch = pitch;
outlet_stage = sample;
Audio Rate
phase += ___SMMUL(inlet_fm >> 3, inlet_FMwidth << 2);
Phase = phase >> 19;
pitch = pitch / (1 << 21) / 12;
int32_t totalcount = (samplelength)*powf(2, pitch);
float32_t ratio = (float32_t)(samplelength) / (float32_t)(totalcount);
LOOP = inlet_LOOP;
int32_t start = ___SMMUL(inlet_startStrt << 2, totalcount << 3);
int32_t stend = ___SMMUL(inlet_startEnd << 2, totalcount << 3);
int32_t enstart = ___SMMUL(inlet_endStrt << 2, totalcount << 3);
int32_t end = ___SMMUL(inlet_endEnd << 2, totalcount << 3);
int32_t loopstart = ___SMMUL(inlet_loopStrt << 2, totalcount << 3);
int32_t loopend = ___SMMUL(inlet_loopEnd << 2, totalcount << 3);

int DIR1 = end > start ? 1 : -1;
int DIR2 = stend > start ? 1 : -1;
int DIR3 = loopstart < loopend ? 1 : -1;

if ((inlet_trig > 0) && !ntrig) {
  stage = 1;
  phase = 0;
  if (LOOP == 0) {
    direction1 = DIR1;
  }
  if (LOOP == 1) {
    direction1 = DIR2;
  }
  ntrig = 1;
  count = start;
} else if (inlet_trig == 0) {
  ntrig = 0;
  stage = 0;
}

if (stage == 1) {
  sample = inlet_startsample;
  pitch = -inlet_startpitch;
}
if (stage == 2) {
  sample = inlet_loopsample;
  pitch = -inlet_looppitch;
}
if (stage == 0) {
  sample = inlet_endsample;
  pitch = -inlet_endpitch;
}

sample = sample & MASK;

if (inlet_trig > 0) {
  if (stage == 1) {
    count += direction1;
    if (LOOP == 0) {
      if (start < end) {
        if (count >= end) {
          stage = 0;
        }
      }
      if (start > end) {
        if (count <= end) {
          stage = 0;
        }
      }
    }

    if (LOOP == 1) {

      if ((inlet_loopmode == 0) || (inlet_loopmode == 2)) {
        if (start < stend) {
          if (count >= stend) {
            stage = 2;
            direction2 = DIR3;
            count = loopstart;
          }
        }
        if (start > stend) {
          if (count <= stend) {
            stage = 2;
            direction2 = DIR3;
            count = loopstart;
          }
        }
      }

      if (inlet_loopmode == 1) {
        if (start < stend) {
          if (count >= stend) {
            stage = 2;
            direction2 = -DIR3;
            count = loopend;
          }
        }
        if (start > stend) {
          if (count <= stend) {
            stage = 2;
            direction2 = DIR3;
            count = loopend;
          }
        }
      }
    }
  }

  if (stage == 2) {
    count += direction2;
    if (inlet_loopmode == 0) {
      if (loopstart < loopend) {
        if (count >= loopend) {
          count = loopstart;
        }
      }
      if (loopstart > loopend) {
        if (count <= loopend) {
          count = loopstart;
        }
      }
    }
    if (inlet_loopmode == 1) {
      if (loopstart < loopend) {
        if (count <= loopstart) {
          count = loopend;
        }
      }
      if (loopstart > loopend) {
        if (count >= loopstart) {
          count = loopend;
        }
      }
    }
    if (inlet_loopmode == 2) {
      if (count == loopstart) {
        direction2 = DIR3;
      }
      if (count == loopend) {
        direction2 = -DIR3;
      }
    }
  }

  out = (attr_table.array[((((int32_t)(count * ratio)) & (samplelength - 1)) +
                           Phase + (sample << Samplelength)) &
                          attr_table.LENGTHMASK]
         << attr_table.GAIN) >>
        5;
}

if ((stage == 0) && !etrig) {
  etrig = 1;
  if (LOOP > 0) {
    count = enstart;
  }
} else if (stage > 0) {
  etrig = 0;
}

if (stage == 0) {
  if (!(count == end)) {
    count += count < end ? 1 : -1;
    out = (attr_table.array[((((int32_t)(count * ratio)) & (samplelength - 1)) +
                             Phase + (sample << Samplelength)) &
                            attr_table.LENGTHMASK]
           << attr_table.GAIN) >>
          5;
  }
  if (count == end) {
    outlet_o = 0;
  }
}

outlet_o = out;

Privacy

© 2024 Zrna Research