Пример #1
0
static int sox_datstartread(sox_format_t * ft)
{
    char inpstr[LINEWIDTH];
    long rate;
    int chan;
    int status;
    char sc;

    /* Read lines until EOF or first non-comment line */
    while ((status = lsx_reads(ft, inpstr, LINEWIDTH-1)) != SOX_EOF) {
      inpstr[LINEWIDTH-1] = 0;
      if ((sscanf(inpstr," %c", &sc) != 0) && (sc != ';')) break;
      if (sscanf(inpstr," ; Sample Rate %ld", &rate)) {
        ft->signal.rate=rate;
      } else if (sscanf(inpstr," ; Channels %d", &chan)) {
        ft->signal.channels=chan;
      }
    }
    /* Hold a copy of the last line we read (first non-comment) */
    if (status != SOX_EOF) {
      strncpy(((priv_t *)ft->priv)->prevline, inpstr, (size_t)LINEWIDTH);
      ((priv_t *)ft->priv)->buffered = 1;
    } else {
      ((priv_t *)ft->priv)->buffered = 0;
    }

    /* Default channels to 1 if not found */
    if (ft->signal.channels == 0)
       ft->signal.channels = 1;

    ft->encoding.encoding = SOX_ENCODING_FLOAT_TEXT;

    return (SOX_SUCCESS);
}
Пример #2
0
static size_t sox_datread(sox_format_t * ft, sox_sample_t *buf, size_t nsamp)
{
    char inpstr[LINEWIDTH];
    int  inpPtr = 0;
    int  inpPtrInc = 0;
    double sampval = 0.0;
    int retc = 0;
    char sc = 0;
    size_t done = 0;
    size_t i=0;

    /* Always read a complete set of channels */
    nsamp -= (nsamp % ft->signal.channels);

    while (done < nsamp) {

      /* Read a line or grab the buffered first line */
      if (((priv_t *)ft->priv)->buffered) {
        strncpy(inpstr, ((priv_t *)ft->priv)->prevline, (size_t)LINEWIDTH);
        inpstr[LINEWIDTH-1] = 0;
        ((priv_t *)ft->priv)->buffered=0;
      } else {
        lsx_reads(ft, inpstr, LINEWIDTH-1);
        inpstr[LINEWIDTH-1] = 0;
        if (lsx_eof(ft)) return (done);
      }

      /* Skip over comments - ie. 0 or more whitespace, then ';' */
      if ((sscanf(inpstr," %c", &sc) != 0) && (sc==';')) continue;

      /* Read a complete set of channels */
      sscanf(inpstr," %*s%n", &inpPtr);
      for (i=0; i<ft->signal.channels; i++) {
        SOX_SAMPLE_LOCALS;
        retc = sscanf(&inpstr[inpPtr]," %lg%n", &sampval, &inpPtrInc);
        inpPtr += inpPtrInc;
        if (retc != 1) {
          lsx_fail_errno(ft,SOX_EOF,"Unable to read sample.");
          return 0;
        }
        *buf++ = SOX_FLOAT_64BIT_TO_SAMPLE(sampval, ft->clips);
        done++;
      }
    }

    return (done);
}
Пример #3
0
static int startread(sox_format_t * ft)
{
    priv_t * avr = (priv_t *)ft->priv;
    int rc;

    lsx_reads(ft, avr->magic, (size_t)4);

    if (strncmp (avr->magic, AVR_MAGIC, (size_t)4)) {
        lsx_fail_errno(ft,SOX_EHDR,"AVR: unknown header");
        return(SOX_EOF);
    }

    lsx_readbuf(ft, avr->name, sizeof(avr->name));

    lsx_readw (ft, &(avr->mono));
    if (avr->mono) {
        ft->signal.channels = 2;
    }
    else {
        ft->signal.channels = 1;
    }

    lsx_readw (ft, &(avr->rez));
    if (avr->rez == 8) {
        ft->encoding.bits_per_sample = 8;
    }
    else if (avr->rez == 16) {
        ft->encoding.bits_per_sample = 16;
    }
    else {
        lsx_fail_errno(ft,SOX_EFMT,"AVR: unsupported sample resolution");
        return(SOX_EOF);
    }

    lsx_readw (ft, &(avr->sign));
    if (avr->sign) {
        ft->encoding.encoding = SOX_ENCODING_SIGN2;
    }
    else {
        ft->encoding.encoding = SOX_ENCODING_UNSIGNED;
    }

    lsx_readw (ft, &(avr->loop));

    lsx_readw (ft, &(avr->midi));

    lsx_readdw (ft, &(avr->rate));
    /*
     * No support for AVRs created by ST-Replay,
     * Replay Proffesional and PRO-Series 12.
     *
     * Just masking the upper byte out.
     */
    ft->signal.rate = (avr->rate & 0x00ffffff);

    lsx_readdw (ft, &(avr->size));

    lsx_readdw (ft, &(avr->lbeg));

    lsx_readdw (ft, &(avr->lend));

    lsx_readw (ft, &(avr->res1));

    lsx_readw (ft, &(avr->res2));

    lsx_readw (ft, &(avr->res3));

    lsx_readbuf(ft, avr->ext, sizeof(avr->ext));

    lsx_readbuf(ft, avr->user, sizeof(avr->user));

    rc = lsx_rawstartread (ft);
    if (rc)
        return rc;

    return(SOX_SUCCESS);
}
Пример #4
0
static int start_read(sox_format_t * ft)
{
  unsigned long header_size_ul = 0, num_samples_ul = 0;
  sox_encoding_t encoding = SOX_ENCODING_SIGN2;
  size_t     header_size, bytes_read;
  size_t     num_samples = 0;
  unsigned       bytes_per_sample = 0;
  unsigned       channels = 1;
  unsigned       rate = 16000;
  char           fldname[64], fldtype[16], fldsval[128];
  char           * buf;

  /* Magic header */
  if (lsx_reads(ft, fldname, (size_t)8) || strncmp(fldname, "NIST_1A", (size_t)7) != 0) {
    lsx_fail_errno(ft, SOX_EHDR, "Sphere header does not begin with magic word `NIST_1A'");
    return (SOX_EOF);
  }

  if (lsx_reads(ft, fldsval, (size_t)8)) {
    lsx_fail_errno(ft, SOX_EHDR, "Error reading Sphere header");
    return (SOX_EOF);
  }

  /* Determine header size, and allocate a buffer large enough to hold it. */
  sscanf(fldsval, "%lu", &header_size_ul);
  buf = lsx_malloc(header_size = header_size_ul);

  /* Skip what we have read so far */
  header_size -= 16;

  if (lsx_reads(ft, buf, header_size) == SOX_EOF) {
    lsx_fail_errno(ft, SOX_EHDR, "Error reading Sphere header");
    free(buf);
    return (SOX_EOF);
  }

  header_size -= (strlen(buf) + 1);

  while (strncmp(buf, "end_head", (size_t)8) != 0) {
    if (strncmp(buf, "sample_n_bytes", (size_t)14) == 0)
      sscanf(buf, "%63s %15s %u", fldname, fldtype, &bytes_per_sample);
    else if (strncmp(buf, "channel_count", (size_t)13) == 0)
      sscanf(buf, "%63s %15s %u", fldname, fldtype, &channels);
    else if (strncmp(buf, "sample_count ", (size_t)13) == 0)
      sscanf(buf, "%53s %15s %lu", fldname, fldtype, &num_samples_ul);
    else if (strncmp(buf, "sample_rate ", (size_t)12) == 0)
      sscanf(buf, "%53s %15s %u", fldname, fldtype, &rate);
    else if (strncmp(buf, "sample_coding", (size_t)13) == 0) {
      sscanf(buf, "%63s %15s %127s", fldname, fldtype, fldsval);
      if (!strcasecmp(fldsval, "ulaw") || !strcasecmp(fldsval, "mu-law"))
        encoding = SOX_ENCODING_ULAW;
      else if (!strcasecmp(fldsval, "pcm"))
        encoding = SOX_ENCODING_SIGN2;
      else {
        lsx_fail_errno(ft, SOX_EFMT, "sph: unsupported coding `%s'", fldsval);
        free(buf);
        return SOX_EOF;
      }
    }
    else if (strncmp(buf, "sample_byte_format", (size_t)18) == 0) {
      sscanf(buf, "%53s %15s %127s", fldname, fldtype, fldsval);
      if (strcmp(fldsval, "01") == 0)         /* Data is little endian. */
        ft->encoding.reverse_bytes = MACHINE_IS_BIGENDIAN;
      else if (strcmp(fldsval, "10") == 0)    /* Data is big endian. */
        ft->encoding.reverse_bytes = MACHINE_IS_LITTLEENDIAN;
      else if (strcmp(fldsval, "1")) {
        lsx_fail_errno(ft, SOX_EFMT, "sph: unsupported coding `%s'", fldsval);
        free(buf);
        return SOX_EOF;
      }
    }

    if (lsx_reads(ft, buf, header_size) == SOX_EOF) {
      lsx_fail_errno(ft, SOX_EHDR, "Error reading Sphere header");
      free(buf);
      return (SOX_EOF);
    }

    header_size -= (strlen(buf) + 1);
  }

  if (!bytes_per_sample)
    bytes_per_sample = encoding == SOX_ENCODING_ULAW? 1 : 2;

  while (header_size) {
    bytes_read = lsx_readbuf(ft, buf, header_size);
    if (bytes_read == 0) {
      free(buf);
      return (SOX_EOF);
    }
    header_size -= bytes_read;
  }
  free(buf);

  if (ft->seekable) {
    /* Check first four bytes of data to see if it's shorten compressed. */
    char           shorten_check[4];

    if (lsx_readchars(ft, shorten_check, sizeof(shorten_check)))
      return SOX_EOF;
    lsx_seeki(ft, -(off_t)sizeof(shorten_check), SEEK_CUR);

    if (!memcmp(shorten_check, "ajkg", sizeof(shorten_check))) {
      lsx_fail_errno(ft, SOX_EFMT,
                     "File uses shorten compression, cannot handle this.");
      return (SOX_EOF);
    }
  }

  num_samples = num_samples_ul;
  return lsx_check_read_params(ft, channels, (sox_rate_t)rate, encoding,
      bytes_per_sample << 3, (uint64_t)num_samples * channels, sox_true);
}