Exemple #1
0
/*
 * Start processing
 */
static int sox_trim_start(sox_effect_t * effp)
{
    priv_t * trim = (priv_t *) effp->priv;

    if (lsx_parsesamples(effp->in_signal.rate, trim->start_str,
                         &trim->start, 't') == NULL)
        return lsx_usage(effp);
    /* Account for # of channels */
    trim->start *= effp->in_signal.channels;

    if (trim->length_str)
    {
        if (lsx_parsesamples(effp->in_signal.rate, trim->length_str,
                             &trim->length, 't') == NULL)
            return lsx_usage(effp);
    }
    else
        trim->length = 0;

    /* Account for # of channels */
    trim->length *= effp->in_signal.channels;

    trim->index = 0;
    trim->trimmed = 0;

    return (SOX_SUCCESS);
}
Exemple #2
0
/*
 * Process options
 */
static int sox_trim_getopts(sox_effect_t * effp, int argc, char **argv)
{
    char *end;
    priv_t * trim = (priv_t *) effp->priv;
  --argc, ++argv;

    /* Do not know sample rate yet so hold off on completely parsing
     * time related strings.
     */
    switch (argc) {
        case 2:
            end = argv[1];
            if (*end == '=') {
                trim->end_is_absolute = sox_true;
                end++;
            } else trim->end_is_absolute = sox_false;
            trim->end_str = lsx_malloc(strlen(end)+1);
            strcpy(trim->end_str, end);
            /* Do a dummy parse to see if it will fail */
            if (lsx_parsesamples(0., trim->end_str, &trim->length, 't') == NULL)
              return lsx_usage(effp);
        case 1:
            trim->start_str = lsx_malloc(strlen(argv[0])+1);
            strcpy(trim->start_str,argv[0]);
            /* Do a dummy parse to see if it will fail */
            if (lsx_parsesamples(0., trim->start_str, &trim->start, 't') == NULL)
              return lsx_usage(effp);
            break;
        default:
            return lsx_usage(effp);

    }
    return (SOX_SUCCESS);
}
Exemple #3
0
/*
 * Process options
 */
static int sox_trim_getopts(sox_effect_t * effp, int n, char **argv)
{
    priv_t * trim = (priv_t *) effp->priv;

    /* Do not know sample rate yet so hold off on completely parsing
     * time related strings.
     */
    switch (n) {
    case 2:
        trim->length_str = lsx_malloc(strlen(argv[1])+1);
        strcpy(trim->length_str,argv[1]);
        /* Do a dummy parse to see if it will fail */
        if (lsx_parsesamples(0., trim->length_str, &trim->length, 't') == NULL)
            return lsx_usage(effp);
    case 1:
        trim->start_str = lsx_malloc(strlen(argv[0])+1);
        strcpy(trim->start_str,argv[0]);
        /* Do a dummy parse to see if it will fail */
        if (lsx_parsesamples(0., trim->start_str, &trim->start, 't') == NULL)
            return lsx_usage(effp);
        break;
    default:
        return lsx_usage(effp);

    }
    return (SOX_SUCCESS);
}
Exemple #4
0
static int sox_silence_start(sox_effect_t * effp)
{
    priv_t *silence = (priv_t *)effp->priv;
    uint64_t temp;

    /* When you want to remove silence, small window sizes are
     * better or else RMS will look like non-silence at
     * aburpt changes from load to silence.
     */
    silence->window_size = (effp->in_signal.rate / 50) * 
        effp->in_signal.channels;
    silence->window = lsx_malloc(silence->window_size * sizeof(double));

    clear_rms(effp);

    /* Now that we know sample rate, reparse duration. */
    if (silence->start)
    {
        if (lsx_parsesamples(effp->in_signal.rate, silence->start_duration_str,
                             &temp, 's') == NULL)
            return lsx_usage(effp);
        silence->start_duration = temp * effp->in_signal.channels;
    }
    if (silence->stop)
    {
        if (lsx_parsesamples(effp->in_signal.rate,silence->stop_duration_str,
                             &temp,'s') == NULL)
            return lsx_usage(effp);
        silence->stop_duration = temp * effp->in_signal.channels;
    }

    if (silence->start)
        silence->mode = SILENCE_TRIM;
    else
        silence->mode = SILENCE_COPY;

    silence->start_holdoff = lsx_malloc(sizeof(sox_sample_t)*silence->start_duration);
    silence->start_holdoff_offset = 0;
    silence->start_holdoff_end = 0;
    silence->start_found_periods = 0;

    silence->stop_holdoff = lsx_malloc(sizeof(sox_sample_t)*silence->stop_duration);
    silence->stop_holdoff_offset = 0;
    silence->stop_holdoff_end = 0;
    silence->stop_found_periods = 0;

    effp->out_signal.length = SOX_UNKNOWN_LEN; /* depends on input data */

    return(SOX_SUCCESS);
}
Exemple #5
0
static int start(sox_effect_t * effp)
{
  priv_t * p = (priv_t *)effp->priv;
  size_t max_delay;

  if (!p->max_arg)
    return SOX_EFF_NULL;
  if (effp->flow < p->argc)
    lsx_parsesamples(effp->in_signal.rate, p->argv[effp->flow], &p->buffer_size, 't');
  lsx_parsesamples(effp->in_signal.rate, p->max_arg, &max_delay, 't');
  p->buffer_index = p->delay = 0;
  p->pad = max_delay - p->buffer_size;
  p->buffer = lsx_malloc(p->buffer_size * sizeof(*p->buffer));
  return SOX_SUCCESS;
}
Exemple #6
0
static int parse(sox_effect_t * effp, char * * argv, sox_rate_t rate)
{
  priv_t * p = (priv_t *)effp->priv;
  char const * next;
  unsigned i;
  uint64_t last_seen = 0;
  const uint64_t in_length = argv ? 0 :
    (effp->in_signal.length != SOX_UNKNOWN_LEN ?
     effp->in_signal.length / effp->in_signal.channels : SOX_UNKNOWN_LEN);
  uint64_t pad_len = 0;
  uint64_t *arg;
  char *str;

  for (i = 0; i < p->npads; ++i) {
    if (argv) /* 1st parse only */
      p->pads[i].str = lsx_strdup(argv[i]);
    str = p->pads[i].str;
    if (*str == '%') {
      str++;
      arg = &p->pads[i].align;
    } else {
      arg = &p->pads[i].pad;
    }
    next = lsx_parsesamples(rate, str, arg, 't');
    if (next == NULL) break;
    if (*next == '\0')
      p->pads[i].start = i? in_length : 0;
    else {
      if (*next != '@') break;
      next = lsx_parseposition(rate, next+1, argv ? NULL : &p->pads[i].start,
               last_seen, in_length, '=');
      if (next == NULL || *next != '\0') break;
      last_seen = p->pads[i].start;
      if (p->pads[i].start == SOX_UNKNOWN_LEN)
        p->pads[i].start = UINT64_MAX; /* currently the same value, but ... */
    }
    if (!argv) {
      if (p->pads[i].align && p->pads[i].start != UINT64_MAX) {
        p->pads[i].pad =
          pad_align(p->pads[i].start + pad_len, p->pads[i].align);
        p->pads[i].align = 0;
      }

      pad_len += p->pads[i].pad;

      /* Do this check only during the second pass when the actual
         sample rate is known, otherwise it might fail on legal
         commands like
           pad [email protected] 1@30000s
         if the rate is, e.g., 48k. */
      if (i > 0 && p->pads[i].start <= p->pads[i-1].start) break;
    }
  }
  if (i < p->npads)
    return lsx_usage(effp);
  return SOX_SUCCESS;
}
Exemple #7
0
/*
 * Start processing
 */
static int sox_trim_start(sox_effect_t * effp)
{
    priv_t * trim = (priv_t *) effp->priv;

    if (lsx_parsesamples(effp->in_signal.rate, trim->start_str,
                        &trim->start, 't') == NULL)
      return lsx_usage(effp);

    if (trim->end_str)
    {
        if (lsx_parsesamples(effp->in_signal.rate, trim->end_str,
                    &trim->length, 't') == NULL)
          return lsx_usage(effp);
        if (trim->end_is_absolute) {
            if (trim->length < trim->start) {
                lsx_warn("end earlier than start");
                trim->length = 0;
                  /* with trim->end_str != NULL, this really means zero */
            } else
                trim->length -= trim->start;
        }
    }
    else
        trim->length = 0;
          /* with trim->end_str == NULL, this means indefinite length */

    lsx_debug("start at %lus, length %lu", trim->start, trim->length);

    /* Account for # of channels */
    trim->start *= effp->in_signal.channels;
    trim->length *= effp->in_signal.channels;

    trim->index = 0;
    trim->trimmed = 0;

    effp->out_signal.length = trim->length;
    return (SOX_SUCCESS);
}
Exemple #8
0
static int parse(sox_effect_t * effp, char * * argv, sox_rate_t rate)
{
    priv_t * p = (priv_t *)effp->priv;
    char const * next;
    unsigned i;

    for (i = 0; i < p->npads; ++i) {
        if (argv) /* 1st parse only */
            p->pads[i].str = lsx_strdup(argv[i]);
        next = lsx_parsesamples(rate, p->pads[i].str, &p->pads[i].pad, 't');
        if (next == NULL) break;
        if (*next == '\0')
            p->pads[i].start = i? SOX_SIZE_MAX : 0;
        else {
            if (*next != '@') break;
            next = lsx_parsesamples(rate, next+1, &p->pads[i].start, 't');
            if (next == NULL || *next != '\0') break;
        }
        if (i > 0 && p->pads[i].start <= p->pads[i-1].start) break;
    }
    if (i < p->npads)
        return lsx_usage(effp);
    return SOX_SUCCESS;
}
Exemple #9
0
static int parse(sox_effect_t * effp, char * * argv, sox_rate_t rate)
{
  priv_t * p = (priv_t *)effp->priv;
  char const * s, * q;
  int i;

  for (i = p->argc - 1; i == 0 || i == 1; --i) {
    if (argv) /* 1st parse only */
      p->pos[i].str = lsx_strdup(argv[i]);
    s = p->pos[i].str;
    if (strchr("+-" + 1 - i, *s))
      p->pos[i].flag = *s++;
    if (!(q = lsx_parsesamples(rate, s, &p->pos[i].at, 't')) || *q)
      break;
  }
  return i >= 0 ? lsx_usage(effp) : SOX_SUCCESS;
}
Exemple #10
0
static int create(sox_effect_t * effp, int argc, char * * argv)
{
  priv_t * p = (priv_t *)effp->priv;
  size_t delay, max_samples = 0;
  unsigned i;

  --argc, ++argv;
  p->argv = lsx_calloc(p->argc = argc, sizeof(*p->argv));
  for (i = 0; i < p->argc; ++i) {
    char const * next = lsx_parsesamples(1e5, p->argv[i] = lsx_strdup(argv[i]), &delay, 't');
    if (!next || *next) {
      kill(effp);
      return lsx_usage(effp);
    }
    if (delay > max_samples) {
      max_samples = delay;
      p->max_arg = p->argv[i];
    }
  }
  return SOX_SUCCESS;
}
Exemple #11
0
static int sox_fade_getopts(sox_effect_t * effp, int argc, char **argv)
{

    priv_t * fade = (priv_t *) effp->priv;
    char t_char[2];
    int t_argno;
    uint64_t samples;
    const char *n;
  --argc, ++argv;

    if (argc < 1 || argc > 4)
         return lsx_usage(effp);

    /* because sample rate is unavailable at this point we store the
     * string off for later computations.
     */

    if (sscanf(argv[0], "%1[qhltp]", t_char))
    {
        fade->in_fadetype = *t_char;
        fade->out_fadetype = *t_char;

        argv++;
        argc--;
    }
    else
    {
        /* No type given. */
        fade->in_fadetype = 'l';
        fade->out_fadetype = 'l';
    }

    fade->in_stop_str = lsx_strdup(argv[0]);
    /* Do a dummy parse to see if it will fail */
    n = lsx_parsesamples(0., fade->in_stop_str, &samples, 't');
    if (!n || *n)
      return lsx_usage(effp);

    fade->in_stop = samples;
    fade->out_start_str = fade->out_stop_str = 0;

    for (t_argno = 1; t_argno < argc && t_argno < 3; t_argno++)
    {
        /* See if there is fade-in/fade-out times/curves specified. */
        if(t_argno == 1)
        {
            fade->out_stop_str = lsx_strdup(argv[t_argno]);

            /* Do a dummy parse to see if it will fail */
            n = lsx_parseposition(0., fade->out_stop_str, NULL, (uint64_t)0, (uint64_t)0, '=');
            if (!n || *n)
              return lsx_usage(effp);
            fade->out_stop = samples;
        }
        else
        {
            fade->out_start_str = lsx_strdup(argv[t_argno]);

            /* Do a dummy parse to see if it will fail */
            n = lsx_parsesamples(0., fade->out_start_str, &samples, 't');
            if (!n || *n)
              return lsx_usage(effp);
            fade->out_start = samples;
        }
    } /* End for(t_argno) */

    return(SOX_SUCCESS);
}
Exemple #12
0
/*
 * Prepare processing.
 * Do all initializations.
 */
static int sox_fade_start(sox_effect_t * effp)
{
    priv_t * fade = (priv_t *) effp->priv;
    sox_bool truncate = sox_false;
    uint64_t samples;
    uint64_t in_length = effp->in_signal.length != SOX_UNKNOWN_LEN ?
      effp->in_signal.length / effp->in_signal.channels : SOX_UNKNOWN_LEN;

    /* converting time values to samples */
    fade->in_start = 0;
    if (lsx_parsesamples(effp->in_signal.rate, fade->in_stop_str,
                        &samples, 't') == NULL)
      return lsx_usage(effp);

    fade->in_stop = samples;
    fade->do_out = 0;
    /* See if user specified a stop time */
    if (fade->out_stop_str)
    {
        fade->do_out = 1;
        if (!lsx_parseposition(effp->in_signal.rate, fade->out_stop_str,
                            &samples, (uint64_t)0, in_length, '=') ||
            samples == SOX_UNKNOWN_LEN) {
          lsx_fail("audio length is unknown");
          return SOX_EOF;
        }
        fade->out_stop = samples;

        if (!(truncate = !!fade->out_stop)) {
          fade->out_stop = effp->in_signal.length != SOX_UNKNOWN_LEN ?
              effp->in_signal.length / effp->in_signal.channels :
              0;
          if (!fade->out_stop) {
            lsx_fail("cannot fade out: audio length is neither known nor given");
            return SOX_EOF;
          }
        }

        /* See if user wants to fade out. */
        if (fade->out_start_str)
        {
            if (lsx_parsesamples(effp->in_signal.rate, fade->out_start_str,
                        &samples, 't') == NULL)
              return lsx_usage(effp);
            /* Fade time is relative to stop time. */
            fade->out_start = fade->out_stop - samples;
        }
        else
            /* If user doesn't specify fade out length then
             * use same length as input side.  This is stored
             * in in_stop.
             */
            fade->out_start = fade->out_stop - fade->in_stop;
    }
    else
        /* If not specified then user wants to process all
         * of file.  Use a value of zero to indicate this.
         */
        fade->out_stop = 0;

    if (fade->out_start) {              /* Sanity check */
      if (fade->in_stop > fade->out_start)
        --fade->in_stop;                /* 1 sample grace for rounding error. */
      if (fade->in_stop > fade->out_start) {
        lsx_fail("fade-out overlaps fade-in");
        return SOX_EOF;
      }
    }

    fade->samplesdone = fade->in_start;
    fade->endpadwarned = 0;

    lsx_debug("in_start = %" PRIu64 " in_stop = %" PRIu64 " "
      "out_start = %" PRIu64 " out_stop = %" PRIu64,
      fade->in_start, fade->in_stop, fade->out_start, fade->out_stop);

    if (fade->in_start == fade->in_stop && !truncate &&
        fade->out_start == fade->out_stop)
      return SOX_EFF_NULL;

    effp->out_signal.length = truncate ?
        fade->out_stop * effp->in_signal.channels : effp->in_signal.length;

    return SOX_SUCCESS;
}
Exemple #13
0
static int sox_silence_getopts(sox_effect_t * effp, int argc, char **argv)
{
    priv_t *   silence = (priv_t *) effp->priv;
    int parse_count;
    uint64_t temp;
    const char *n;
  --argc, ++argv;

    /* check for option switches */
    silence->leave_silence = sox_false;
    if (argc > 0)
    {
        if (!strcmp("-l", *argv)) {
            argc--; argv++;
            silence->leave_silence = sox_true;
        }
    }

    if (argc < 1)
      return lsx_usage(effp);

    /* Parse data related to trimming front side */
    silence->start = sox_false;
    if (sscanf(argv[0], "%d", &silence->start_periods) != 1)
      return lsx_usage(effp);
    if (silence->start_periods < 0)
    {
        lsx_fail("Periods must not be negative");
        return(SOX_EOF);
    }
    argv++;
    argc--;

    if (silence->start_periods > 0)
    {
        silence->start = sox_true;
        if (argc < 2)
          return lsx_usage(effp);

        /* We do not know the sample rate so we can not fully
         * parse the duration info yet.  So save argument off
         * for future processing.
         */
        silence->start_duration_str = lsx_strdup(argv[0]);
        /* Perform a fake parse to do error checking */
        n = lsx_parsesamples(0.,silence->start_duration_str,&temp,'s');
        if (!n || *n)
          return lsx_usage(effp);
        silence->start_duration = temp;

        parse_count = sscanf(argv[1], "%lf%c", &silence->start_threshold,
                &silence->start_unit);
        if (parse_count < 1)
          return lsx_usage(effp);
        else if (parse_count < 2)
            silence->start_unit = '%';

        argv++; argv++;
        argc--; argc--;
    }

    silence->stop = sox_false;
    /* Parse data needed for trimming of backside */
    if (argc > 0)
    {
        if (argc < 3)
          return lsx_usage(effp);
        if (sscanf(argv[0], "%d", &silence->stop_periods) != 1)
          return lsx_usage(effp);
        if (silence->stop_periods < 0)
        {
            silence->stop_periods = -silence->stop_periods;
            silence->restart = 1;
        }
        else
            silence->restart = 0;
        silence->stop = sox_true;
        argv++;
        argc--;

        /* We do not know the sample rate so we can not fully
         * parse the duration info yet.  So save argument off
         * for future processing.
         */
        silence->stop_duration_str = lsx_strdup(argv[0]);
        /* Perform a fake parse to do error checking */
        n = lsx_parsesamples(0.,silence->stop_duration_str,&temp,'s');
        if (!n || *n)
          return lsx_usage(effp);
        silence->stop_duration = temp;

        parse_count = sscanf(argv[1], "%lf%c", &silence->stop_threshold,
                             &silence->stop_unit);
        if (parse_count < 1)
          return lsx_usage(effp);
        else if (parse_count < 2)
            silence->stop_unit = '%';

        argv++; argv++;
        argc--; argc--;
    }

    /* Error checking */
    if (silence->start)
    {
        if ((silence->start_unit != '%') && (silence->start_unit != 'd'))
        {
            lsx_fail("Invalid unit specified");
            return lsx_usage(effp);
        }
        if ((silence->start_unit == '%') && ((silence->start_threshold < 0.0)
            || (silence->start_threshold > 100.0)))
        {
            lsx_fail("silence threshold should be between 0.0 and 100.0 %%");
            return (SOX_EOF);
        }
        if ((silence->start_unit == 'd') && (silence->start_threshold >= 0.0))
        {
            lsx_fail("silence threshold should be less than 0.0 dB");
            return(SOX_EOF);
        }
    }

    if (silence->stop)
    {
        if ((silence->stop_unit != '%') && (silence->stop_unit != 'd'))
        {
            lsx_fail("Invalid unit specified");
            return(SOX_EOF);
        }
        if ((silence->stop_unit == '%') && ((silence->stop_threshold < 0.0) ||
                    (silence->stop_threshold > 100.0)))
        {
            lsx_fail("silence threshold should be between 0.0 and 100.0 %%");
            return (SOX_EOF);
        }
        if ((silence->stop_unit == 'd') && (silence->stop_threshold >= 0.0))
        {
            lsx_fail("silence threshold should be less than 0.0 dB");
            return(SOX_EOF);
        }
    }
    return(SOX_SUCCESS);
}
Exemple #14
0
/*
 * Prepare processing.
 * Do all initializations.
 */
static int sox_fade_start(sox_effect_t * effp)
{
    priv_t * fade = (priv_t *) effp->priv;
    sox_bool truncate = sox_false;

    /* converting time values to samples */
    fade->in_start = 0;
    if (lsx_parsesamples(effp->in_signal.rate, fade->in_stop_str,
                        &fade->in_stop, 't') == NULL)
      return lsx_usage(effp);

    fade->do_out = 0;
    /* See if user specified a stop time */
    if (fade->out_stop_str)
    {
        fade->do_out = 1;
        if (lsx_parsesamples(effp->in_signal.rate, fade->out_stop_str,
                            &fade->out_stop, 't') == NULL)
          return lsx_usage(effp);

        if (!(truncate = !!fade->out_stop)) {
          fade->out_stop = effp->in_signal.length / effp->in_signal.channels;
          if (!fade->out_stop) {
            lsx_fail("cannot fade out: audio length is neither known nor given");
            return SOX_EOF;
          }
        }

        /* See if user wants to fade out. */
        if (fade->out_start_str)
        {
            if (lsx_parsesamples(effp->in_signal.rate, fade->out_start_str,
                        &fade->out_start, 't') == NULL)
              return lsx_usage(effp);
            /* Fade time is relative to stop time. */
            fade->out_start = fade->out_stop - fade->out_start;

        }
        else
            /* If user doesn't specify fade out length then
             * use same length as input side.  This is stored
             * in in_stop.
             */
            fade->out_start = fade->out_stop - fade->in_stop;
    }
    else
        /* If not specified then user wants to process all
         * of file.  Use a value of zero to indicate this.
         */
        fade->out_stop = 0;

    /* Sanity check for fade times vs total time */
    if (fade->in_stop > fade->out_start && fade->out_start != 0)
    { /* Fades too long */
        lsx_fail("Fade: End of fade-in should not happen before beginning of fade-out");
        return(SOX_EOF);
    } /* endif fade time sanity */

    fade->samplesdone = fade->in_start;
    fade->endpadwarned = 0;

    lsx_debug("fade: in_start = %lu in_stop = %lu out_start = %lu out_stop = %lu", (unsigned long)fade->in_start, (unsigned long)fade->in_stop, (unsigned long)fade->out_start, (unsigned long)fade->out_stop);

    if (fade->in_start == fade->in_stop && !truncate &&
        fade->out_start == fade->out_stop)
      return SOX_EFF_NULL;

    return SOX_SUCCESS;
}