brain+

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/old/brainPLUS.axo

Inlets

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.risingfalling copy the selected slot

bool32.risingfalling move the selected slot

bool32.risingfalling delete the selected slot

charptr32 filename

Outlets

int32 test

bool32.pulse pulse on delete

Attributes

objref wave

objref params

combo blocksize

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 busy;
bool trig;
uint8_t m_count;
int rem_sz;
char *t_adr;
bool error;
char fn[32];

void _save() {
  error = 0;
  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);
    error = 1;
    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);
    error = 1;
    return;
  }
  err = f_close(&FileObject);
}

void _load() {
  error = 0;
  FIL FileObject;
  FRESULT err;
  UINT bytes_read;
  err = f_open(&FileObject, fn, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    error = 1;
    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);
    error = 1;
    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;
    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]++;
    }
  }
}
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;
}
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;
}

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;
}

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

busy = load || save;

if (busy) {
  switch (index ? index : 0) {
  case 0: {
    rem_sz = 1024 << 2;
    t_adr = (char *)(&array[0]);
    strcpy(&fn[0], inlet_filename);
    strcat(&fn[0], ".slt");
    break;
  }
  case 1: {
    rem_sz = sizeof(*attr_wave.array) * array[1022];
    t_adr = (char *)(&attr_wave.array[0]);
    strcpy(&fn[0], inlet_filename);
    strcat(&fn[0], ".raw");
    break;
  }
  case 2: {
    rem_sz = sizeof(*attr_params.array) * attr_params.LENGTH;
    t_adr = (char *)(&attr_params.array[0]);
    strcpy(&fn[0], inlet_filename);
    strcat(&fn[0], ".prm");
    break;
  }
  }
}

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

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

if (!inlet_load && !inlet_save && !busy)
  trig = 0;
outlet_test = sizeof(*attr_wave.array);

Privacy

© 2024 Zrna Research