brain_lpr

edits a bank of samples recorded with 'smplr'. 'slot' sets the slot to work on, 'copy','move' and 'delete' have to be triggered via 'trig'. 'defrag' will clear unused sample data and compact the referred table. 'clear' will clear the data.
Author: Robert Schirmer
License: BSD
Github: rbrt/testing/brain_lpr.axo

Inlets

bool32.risingfalling copy the selected slot

bool32.risingfalling move the selected slot

bool32.risingfalling delete the selected slot

int32.positive slot to work on

bool32.rising trigger an editing action

bool32.rising defragment the sample data

bool32.rising clear all data

bool32.rising load

bool32.rising save

bool32.rising delFile

Outlets

bool32.pulse pulse on delete

Attributes

objref wave

objref params

objref aux1

objref aux2

objref aux3

combo blocksize

spinner auxes

Declaration
bool ctrig;
bool dtrig;
bool cpytrig;
bool cpytemptrig;
bool del;
bool clrtrig;

bool move;

bool mtrig;
uint8_t mslot;
uint8_t srcslot;

bool deletetrig;
bool copytrig;

uint32_t sstart;
uint32_t send;
uint32_t slength;

uint32_t slottemp[4];
int32_t paramtemp[1 << attr_blocksize];

static const uint16_t LENGTH = 1024;
uint32_t *array;
bool REC;
uint8_t RECSLOT;

uint8_t index;
bool save;
bool load;
bool dftrig;
bool busy;
bool trig;
uint8_t m_count;
int rem_sz;
char *t_adr;
uint8_t *fout;
char fn[16];
uint8_t fload[8];
uint8_t fsave[8];
char oldn[16];
char newn[16];

int count;

void _save() {
  FIL FileObject;
  FRESULT err;
  UINT bytes_written;
  err = f_open(&FileObject, fn, FA_WRITE | FA_CREATE_ALWAYS);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (rem_sz > 0) {
    if (rem_sz > sizeof(fbuff)) {
      memcpy((char *)fbuff, t_adr + offset, sizeof(fbuff));
      err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
      rem_sz -= sizeof(fbuff);
      offset += sizeof(fbuff);
    } else {
      memcpy((char *)fbuff, t_adr + offset, rem_sz);
      err = f_write(&FileObject, fbuff, rem_sz, &bytes_written);
      rem_sz = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

void _load() {
  UINT bytes_read;
  FIL FileObject;
  FRESULT err;
  err = f_open(&FileObject, fn, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (rem_sz > 0) {
    if (rem_sz > sizeof(fbuff)) {
      err = f_read(&FileObject, fbuff, sizeof(fbuff), &bytes_read);
      if (bytes_read == 0)
        break;
      memcpy(t_adr + offset, (char *)fbuff, bytes_read);
      rem_sz -= bytes_read;
      offset += bytes_read;
    } else {
      err = f_read(&FileObject, fbuff, rem_sz, &bytes_read);
      memcpy(t_adr + offset, (char *)fbuff, bytes_read);
      rem_sz = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

void delete_slot(uint8_t target) {
  if (array[(target << 2) + 2]) {
    sstart = array[target << 2];
    send = array[(target << 2) + 1];
    int i;
    for (i = 0; i < 4; i++)
      array[(target << 2) + i] = 0;
    for (i = 0; i < (1 << attr_blocksize); i++)
      attr_params.array[(target << attr_blocksize) + i] = 0;
    bool check = 1;
    for (i = 0; i < 127; i++)
      if (array[(i << 2) + 1] == send)
        check = 0;
    if (check) {
      array[array[1023] + 512] = sstart;
      array[array[1023] + 767] = send;
      array[1023]++;
    }
  }
}

void get_dir() {
  FILINFO fno;
  DIR dir;
  FRESULT err;
  memset(&fout[0], 0, 512);
  f_opendir(&dir, "");
  for (;;) {
    err = f_readdir(&dir, &fno);
    if (err != FR_OK || fno.fname[0] == 0)
      break;
    if (count < 64) {
      uint8_t len = strlen(fno.fname);
      if ((fno.fname[len - 3] == 'l') && (fno.fname[len - 2] == 'p') &&
          (fno.fname[len - 1] == 'r')) {
        memcpy(&fout[count << 3], fno.fname, ((len - 4) > 0) ? (len - 4) : 1);
        count++;
      }
    }
  }
}

void del_file() {
  memcpy(&oldn[0], &fload[0], 8);
  memcpy(&newn[0], &fload[0], 8);
  strcat(&oldn[0], ".lpr");
  strcat(&newn[0], ".xxx");
  f_rename(&oldn[0], &newn[0]);
}
Init
static uint32_t _array[attr_poly][1024] __attribute__((section(".sdram")));
array = &_array[parent->polyIndex][0];
int i;
for (i = 0; i < 1024; i++)
  array[i] = 0;

static uint8_t _fout[attr_poly][512] __attribute__((section(".sdram")));
fout = &_fout[parent->polyIndex][0];
for (i = 0; i < 512; i++)
  fout[i] = 0;

get_dir();
Control Rate
mslot = inlet_slot % 126;
outlet_delete = 0;

// delete
if (inlet_delete && inlet_trig && !dtrig) {
  delete_slot(mslot);
  dtrig = 1;
  outlet_delete = 1;
}
// compact
if (inlet_defrag && !ctrig) {
  ctrig = 1;
  int i;
  for (i = 0; i < array[1023]; i++) {
    send = array[i + 767];
    sstart = array[i + 512];
    slength = (send - sstart);
    int shift_index;
    for (shift_index = 0; shift_index < 512; shift_index += 4)
      if (array[shift_index] >= send) {
        array[shift_index] -= slength;
        array[shift_index + 1] -= slength;
      }
    int move_samples;
    for (move_samples = send; move_samples < (array[1022]); move_samples++)
      attr_wave.array[move_samples - slength] = attr_wave.array[move_samples];
    int shift_chunks;
    for (shift_chunks = 0; shift_chunks < array[1023]; shift_chunks++)
      if (array[shift_chunks + 512] >= send) {
        array[shift_chunks + 512] -= slength;
        array[shift_chunks + 767] -= slength;
      }

    array[1022] -= slength;
  }
  array[1023] = 0;
}
if (!inlet_defrag)
  ctrig = 0;

// paste
if (inlet_trig && cpytemptrig && !cpytrig && (inlet_copy || move)) {
  cpytrig = 1;
  delete_slot(mslot);
  int i;
  for (i = 0; i < 4; i++)
    array[(mslot << 2) + i] = slottemp[i];
  for (i = 0; i < (1 << attr_blocksize); i++)
    attr_params.array[(mslot << attr_blocksize) + i] = paramtemp[i];
  if (move) {
    delete_slot(srcslot);
    outlet_delete = 1;
    move = 0;
  }
}

// copy
if (inlet_trig && !cpytemptrig && (inlet_copy || move)) {
  cpytemptrig = 1;
  srcslot = mslot;
  int i;
  for (i = 0; i < 4; i++)
    slottemp[i] = array[(mslot << 2) + i];
  for (i = 0; i < (1 << attr_blocksize); i++)
    paramtemp[i] = attr_params.array[(mslot << attr_blocksize) + i];
}

// move
if (inlet_move && !mtrig) {
  mtrig = 1;
  move = 1;
}

// clear
if (inlet_clear && !clrtrig) {
  clrtrig = 1;
  int i;
  for (i = 0; i < 1024; i++)
    array[i] = 0;
  for (i = 0; i < ((1 << attr_blocksize) << 7); i++)
    attr_params.array[i] = 0;
}

if (!inlet_copy && !inlet_move)
  cpytemptrig = 0;
if (!inlet_move) {
  mtrig = 0;
  move = 0;
}
if (!inlet_trig) {
  cpytrig = 0;
  dtrig = 0;
}
if (!inlet_clear)
  clrtrig = 0;

// IO
if (inlet_save && !trig) {
  save = 1;
  index = 0;
  trig = 1;
  m_count = 3 + attr_auxes;
}

if (inlet_load && !trig) {
  load = 1;
  index = 0;
  trig = 1;
  m_count = 3 + attr_auxes;
}

busy = load || save;

if (busy) {
  memset(&fn[0], 0, 16);
  if (load)
    memcpy(&fn[0], &fload[0], 8);
  if (save)
    memcpy(&fn[0], &fsave[0], 8);
  switch (index ? index : 0) {
  case 0: {
    rem_sz = 1024 << 2;
    t_adr = (char *)(&array[0]);
    strcat(&fn[0], ".lpr");
    break;
  }
  case 1: {
    rem_sz = sizeof(*attr_wave.array) * array[1022];
    t_adr = (char *)(&attr_wave.array[0]);
    strcat(&fn[0], ".raw");
    break;
  }
  case 2: {
    rem_sz = sizeof(*attr_params.array) * attr_params.LENGTH;
    t_adr = (char *)(&attr_params.array[0]);
    strcat(&fn[0], ".prm");
    break;
  }
  case 3: {
    rem_sz = sizeof(*attr_aux1.array) * attr_aux1.LENGTH;
    t_adr = (char *)(&attr_aux1.array[0]);
    strcat(&fn[0], ".ax1");
    break;
  }
  case 4: {
    rem_sz = sizeof(*attr_aux2.array) * attr_aux2.LENGTH;
    t_adr = (char *)(&attr_aux2.array[0]);
    strcat(&fn[0], ".ax2");
    break;
  }
  case 5: {
    rem_sz = sizeof(*attr_aux3.array) * attr_aux3.LENGTH;
    t_adr = (char *)(&attr_aux3.array[0]);
    strcat(&fn[0], ".ax3");
    break;
  }
  }
}

if (save) {
  _save();
  index++;
  if (index >= m_count) {
    save = 0;
    get_dir();
  }
}

if (load) {
  _load();
  index++;
  if (index >= m_count) {
    load = 0;
  }
}

if (inlet_delFile && !dftrig) {
  dftrig = 1;
  del_file();
  get_dir();
}
if (!inlet_delFile)
  dftrig = 0;

if (!inlet_load && !inlet_save && !busy)
  trig = 0;

Privacy

© 2024 Zrna Research