smplr save

Author:
License: GPL
Github: rbrt/testing/smplr save.axo

Inlets

bool32.rising load

bool32.rising save

charptr32 filename

Outlets

None

Attributes

objref smplr

combo directory

Declaration
bool trig;
bool load;
bool save;
int rem_sz;
uint8_t bytes;

char fn[16];

FILINFO fno;
DIR dir;
FIL FileObject;
FRESULT err;
UINT bytes_written;
UINT bytes_read;

char fname_flt[16];
char fname_raw[16];

// load file from SD:: fload (filename,destination table adress,start
// index,length)
void fload(const char *fn, char *table, int start, int bytes_rem)
    __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (bytes_rem > 0) {
    if (bytes_rem > sizeof(fbuff)) {
      err = f_read(&FileObject, fbuff, sizeof(fbuff), &bytes_read);
      if (bytes_read == 0)
        break;
      memcpy(table + offset + start, (char *)fbuff, bytes_read);
      bytes_rem -= bytes_read;
      offset += bytes_read;
    } else {
      err = f_read(&FileObject, fbuff, bytes_rem, &bytes_read);
      memcpy(table + offset + start, (char *)fbuff, bytes_read);
      bytes_rem = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

// save array to SD::fsave (filename,source table adress,start index,length)
void fsave(const char *fn, char *table, int start, int bytes_rem)
    __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_WRITE | FA_CREATE_ALWAYS);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (bytes_rem > 0) {
    if (bytes_rem > sizeof(fbuff)) {
      memcpy((char *)fbuff, table + offset + start, sizeof(fbuff));
      err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
      bytes_rem -= sizeof(fbuff);
      offset += sizeof(fbuff);
    } else {
      memcpy((char *)fbuff, table + offset + start, bytes_rem);
      err = f_write(&FileObject, fbuff, bytes_rem, &bytes_written);
      bytes_rem = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

// get file length in 8-bit bytes::fsize (filename)
int fsize(const char *fn) __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_READ);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return 0;
  } else {
    return f_size(&FileObject);
    err = f_close(&FileObject);
  }
}

void getdir(const char *path, const char *filter, int start_index)
    __attribute__((noinline)) {
  memset(&fname_flt[0], 0, 16);
  memset(&fname_raw[0], 0, 16);
  int count = -1;
  int fltlen = strlen(filter);
  f_opendir(&dir, path);
  for (;;) {
    err = f_readdir(&dir, &fno);
    if (err != FR_OK || fno.fname[0] == 0)
      break;
    int filelen = strlen(fno.fname);
    if (!memcmp(&filter[0], &fno.fname[filelen - fltlen], fltlen)) {
      count++;
      if (count == start_index) {
        memcpy(&fname_flt[0], fno.fname, filelen - fltlen);
        memcpy(&fname_raw[0], fno.fname, filelen - fltlen);
      }
    }
  }
}

// rename a file
void frename(const char *old_n, const char *new_n) __attribute__((noinline)) {
  f_rename(old_n, new_n);
}

// delete a file
void fdelete(const char *del_f) __attribute__((noinline)) { f_unlink(del_f); }

// prepare filename
void prep_name(const char *fname, const char *suffix)
    __attribute__((noinline)) {
  memset(&fn[0], 0, 16);
  if (attr_directory)
    strcpy(&fn[0], "/");
  memcpy(&fn[attr_directory], &fname[0], 8);
  strcat(&fn[0], suffix);
}
Init
bytes = sizeof(attr_smplr.WAVE);
Control Rate
if (inlet_load && !trig) {
  trig = 1;
  load = 1;
}
if (inlet_save && !trig) {
  trig = 1;
  save = 1;
}

if (load) {
  load = 0;
  prep_name(inlet_filename, ".raw");
  int filesize = fsize(fn) / bytes;
  attr_smplr.REC_HEAD =
      (filesize > attr_smplr.W_LENGTH) ? attr_smplr.W_LENGTH : filesize;
  rem_sz = attr_smplr.REC_HEAD * bytes;
  fload(fn, (char *)&attr_smplr.WAVE[0], 0, rem_sz);
  prep_name(inlet_filename, ".dat");
  fload(fn, (char *)&attr_smplr.array[0], 0, 16384);
}
if (save) {
  save = 0;
  attr_smplr.defrag();
  rem_sz = attr_smplr.REC_HEAD * bytes;
  prep_name(inlet_filename, ".raw");
  fsave(fn, (char *)&attr_smplr.WAVE[0], 0, rem_sz);
  prep_name(inlet_filename, ".dat");
  fsave(fn, (char *)&attr_smplr.array[0], 0, 16384);
}

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

Privacy

© 2024 Zrna Research