diff options
Diffstat (limited to 'audio/filter')
-rw-r--r-- | audio/filter/af.c | 8 | ||||
-rw-r--r-- | audio/filter/af_pan.c | 244 | ||||
-rw-r--r-- | audio/filter/af_rubberband.c | 26 |
3 files changed, 159 insertions, 119 deletions
diff --git a/audio/filter/af.c b/audio/filter/af.c index f380459747..e6f19b3e37 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -166,10 +166,10 @@ static struct af_instance *af_create(struct af_stream *s, char *name, .replaygain_data = s->replaygain_data, .out_pool = mp_audio_pool_create(af), }; - struct m_config *config = m_config_from_obj_desc(af, s->log, &desc); - if (m_config_apply_defaults(config, name, s->opts->af_defs) < 0) - goto error; - if (m_config_set_obj_params(config, args) < 0) + struct m_config *config = + m_config_from_obj_desc_and_args(af, s->log, NULL, &desc, + name, s->opts->af_defs, args); + if (!config) goto error; af->priv = config->optstruct; diff --git a/audio/filter/af_pan.c b/audio/filter/af_pan.c index 900d10cc74..de2adf790c 100644 --- a/audio/filter/af_pan.c +++ b/audio/filter/af_pan.c @@ -28,12 +28,11 @@ #include "af.h" // Data for specific instances of this filter -typedef struct af_pan_s -{ - int nch; // Number of output channels; zero means same as input - float level[AF_NCH][AF_NCH]; // Gain level for each channel - char *matrixstr; -}af_pan_t; +typedef struct af_pan_s { + int nch; // Number of output channels; zero means same as input + float level[AF_NCH][AF_NCH]; // Gain level for each channel + char *matrixstr; +} af_pan_t; static void set_channels(struct mp_audio *mpa, int num) { @@ -44,136 +43,151 @@ static void set_channels(struct mp_audio *mpa, int num) mp_audio_set_channels(mpa, &map); } -// Initialization and runtime control -static int control(struct af_instance* af, int cmd, void* arg) +static void parse_matrix(struct af_instance *af, const char *cp) { - af_pan_t* s = af->priv; - - switch(cmd){ - case AF_CONTROL_REINIT: - // Sanity check - if(!arg) return AF_ERROR; + af_pan_t *s = af->priv; + int j = 0, k = 0, n; + while (*cp && k < AF_NCH) { + sscanf(cp, "%f%n" , &s->level[j][k], &n); + MP_VERBOSE(af, "Pan level from channel %i to" + " channel %i = %f\n", k, j, s->level[j][k]); + cp = &cp[n]; + j++; + if (j >= s->nch) { + j = 0; + k++; + } + if (*cp != ',') + break; + cp++; + } - af->data->rate = ((struct mp_audio*)arg)->rate; - mp_audio_set_format(af->data, AF_FORMAT_FLOAT); - set_channels(af->data, s->nch ? s->nch: ((struct mp_audio*)arg)->nch); +} - if((af->data->format != ((struct mp_audio*)arg)->format) || - (af->data->bps != ((struct mp_audio*)arg)->bps)){ - mp_audio_set_format((struct mp_audio*)arg, af->data->format); - return AF_FALSE; +// Initialization and runtime control +static int control(struct af_instance *af, int cmd, void *arg) +{ + af_pan_t* s = af->priv; + + switch(cmd){ + case AF_CONTROL_REINIT: + // Sanity check + if (!arg) + return AF_ERROR; + + af->data->rate = ((struct mp_audio*)arg)->rate; + mp_audio_set_format(af->data, AF_FORMAT_FLOAT); + set_channels(af->data, s->nch ? s->nch : ((struct mp_audio*)arg)->nch); + + if ((af->data->format != ((struct mp_audio*)arg)->format) || + (af->data->bps != ((struct mp_audio*)arg)->bps)) { + mp_audio_set_format((struct mp_audio*)arg, af->data->format); + return AF_FALSE; + } + return AF_OK; + case AF_CONTROL_SET_PAN_LEVEL: { + int i; + int ch = ((af_control_ext_t*)arg)->ch; + float *level = ((af_control_ext_t*)arg)->arg; + if (ch >= AF_NCH) + return AF_FALSE; + for (i = 0; i < AF_NCH; i++) + s->level[ch][i] = level[i]; + return AF_OK; } - return AF_OK; - case AF_CONTROL_SET_PAN_LEVEL:{ - int i; - int ch = ((af_control_ext_t*)arg)->ch; - float* level = ((af_control_ext_t*)arg)->arg; - if (ch >= AF_NCH) - return AF_FALSE; - for(i=0;i<AF_NCH;i++) - s->level[ch][i] = level[i]; - return AF_OK; - } - case AF_CONTROL_SET_PAN_NOUT: - // Reinit must be called after this function has been called - - // Sanity check - if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ - MP_ERR(af, "The number of output channels must be" - " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); - return AF_ERROR; + case AF_CONTROL_SET_PAN_NOUT: + // Reinit must be called after this function has been called + // Sanity check + if (((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH) { + MP_ERR(af, "The number of output channels must be" + " between 1 and %i. Current value is %i\n", + AF_NCH, ((int*)arg)[0]); + return AF_ERROR; + } + s->nch = ((int*)arg)[0]; + return AF_OK; + case AF_CONTROL_SET_PAN_BALANCE: { + float val = *(float*)arg; + if (s->nch) + return AF_ERROR; + if (af->data->nch >= 2) { + s->level[0][0] = MPMIN(1.f, 1.f - val); + s->level[0][1] = MPMAX(0.f, val); + s->level[1][0] = MPMAX(0.f, -val); + s->level[1][1] = MPMIN(1.f, 1.f + val); + } + return AF_OK; } - s->nch=((int*)arg)[0]; - return AF_OK; - case AF_CONTROL_SET_PAN_BALANCE:{ - float val = *(float*)arg; - if (s->nch) - return AF_ERROR; - if (af->data->nch >= 2) { - s->level[0][0] = MPMIN(1.f, 1.f - val); - s->level[0][1] = MPMAX(0.f, val); - s->level[1][0] = MPMAX(0.f, -val); - s->level[1][1] = MPMIN(1.f, 1.f + val); + case AF_CONTROL_GET_PAN_BALANCE: + if (s->nch) + return AF_ERROR; + *(float*)arg = s->level[0][1] - s->level[1][0]; + return AF_OK; + case AF_CONTROL_COMMAND: { + char **args = arg; + if (!strcmp(args[0], "set-matrix")) { + parse_matrix(af, args[1]); + return CONTROL_OK; + } else { + return CONTROL_ERROR; + } } - return AF_OK; - } - case AF_CONTROL_GET_PAN_BALANCE: - if (s->nch) - return AF_ERROR; - *(float*)arg = s->level[0][1] - s->level[1][0]; - return AF_OK; - } - return AF_UNKNOWN; + } + return AF_UNKNOWN; } static int filter_frame(struct af_instance *af, struct mp_audio *c) { - if (!c) - return 0; - struct mp_audio *l = mp_audio_pool_get(af->out_pool, &af->fmt_out, c->samples); - if (!l) { - talloc_free(c); - return -1; - } - mp_audio_copy_attributes(l, c); - - af_pan_t* s = af->priv; // Setup for this instance - float* in = c->planes[0]; // Input audio data - float* out = NULL; // Output audio data - float* end = in+c->samples*c->nch; // End of loop - int nchi = c->nch; // Number of input channels - int ncho = l->nch; // Number of output channels - register int j,k; - - out = l->planes[0]; - // Execute panning - // FIXME: Too slow - while(in < end){ - for(j=0;j<ncho;j++){ - register float x = 0.0; - register float* tin = in; - for(k=0;k<nchi;k++) - x += tin[k] * s->level[j][k]; - out[j] = x; + if (!c) + return 0; + struct mp_audio *l = mp_audio_pool_get(af->out_pool, &af->fmt_out, c->samples); + if (!l) { + talloc_free(c); + return -1; + } + mp_audio_copy_attributes(l, c); + + af_pan_t* s = af->priv; // Setup for this instance + float *in = c->planes[0]; // Input audio data + float *out = NULL; // Output audio data + float *end = in+c->samples * c->nch; // End of loop + int nchi = c->nch; // Number of input channels + int ncho = l->nch; // Number of output channels + register int j, k; + + out = l->planes[0]; + // Execute panning + // FIXME: Too slow + while (in < end) { + for (j = 0; j < ncho; j++) { + register float x = 0.0; + register float *tin = in; + for (k = 0; k < nchi; k++) + x += tin[k] * s->level[j][k]; + out[j] = x; + } + out += ncho; + in += nchi; } - out+= ncho; - in+= nchi; - } - talloc_free(c); - af_add_output_frame(af, l); - return 0; + talloc_free(c); + af_add_output_frame(af, l); + return 0; } // Allocate memory and set function pointers -static int af_open(struct af_instance* af){ - af->control=control; +static int af_open(struct af_instance *af) +{ + af->control = control; af->filter_frame = filter_frame; af_pan_t *s = af->priv; - int n = 0; - int j,k; - int nch = s->nch; - if(nch && AF_OK != control(af,AF_CONTROL_SET_PAN_NOUT, &nch)) + if (nch && AF_OK != control(af, AF_CONTROL_SET_PAN_NOUT, &nch)) return AF_ERROR; // Read pan values - char *cp = s->matrixstr; - j = 0; k = 0; - while(cp && k < AF_NCH){ - sscanf(cp, "%f%n" , &s->level[j][k], &n); - MP_VERBOSE(af, "Pan level from channel %i to" - " channel %i = %f\n",k,j,s->level[j][k]); - cp =&cp[n]; - j++; - if(j>=nch){ - j = 0; - k++; - } - if (*cp != ',') - break; - cp++; - } + if (s->matrixstr) + parse_matrix(af, s->matrixstr); return AF_OK; } diff --git a/audio/filter/af_rubberband.c b/audio/filter/af_rubberband.c index 48bb510679..a4deb3d48c 100644 --- a/audio/filter/af_rubberband.c +++ b/audio/filter/af_rubberband.c @@ -26,6 +26,7 @@ struct priv { RubberBandState rubber; double speed; + double pitch; struct mp_audio *pending; bool needs_reset; // Estimate how much librubberband has buffered internally. @@ -44,6 +45,14 @@ static void update_speed(struct af_instance *af, double new_speed) rubberband_set_time_ratio(p->rubber, 1.0 / p->speed); } +static void update_pitch(struct af_instance *af, double new_pitch) +{ + struct priv *p = af->priv; + + p->pitch = new_pitch; + rubberband_set_pitch_scale(p->rubber, p->pitch); +} + static int control(struct af_instance *af, int cmd, void *arg) { struct priv *p = af->priv; @@ -72,6 +81,7 @@ static int control(struct af_instance *af, int cmd, void *arg) } update_speed(af, p->speed); + update_pitch(af, p->pitch); control(af, AF_CONTROL_RESET, NULL); return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE; @@ -87,6 +97,19 @@ static int control(struct af_instance *af, int cmd, void *arg) p->pending = NULL; p->rubber_delay = 0; return AF_OK; + case AF_CONTROL_COMMAND: { + char **args = arg; + if (!strcmp(args[0], "set-pitch")) { + char *endptr; + double pitch = strtod(args[1], &endptr); + if (*endptr || pitch < 0.01 || pitch > 100.0) + return CONTROL_ERROR; + update_pitch(af, pitch); + return CONTROL_OK; + } else { + return CONTROL_ERROR; + } + } } return AF_UNKNOWN; } @@ -187,9 +210,11 @@ const struct af_info af_info_rubberband = { .priv_size = sizeof(struct priv), .priv_defaults = &(const struct priv) { .speed = 1.0, + .pitch = 1.0, .opt_pitch = RubberBandOptionPitchHighConsistency, .opt_transients = RubberBandOptionTransientsMixed, .opt_formant = RubberBandOptionFormantPreserved, + .opt_channels = RubberBandOptionChannelsTogether, }, .options = (const struct m_option[]) { OPT_CHOICE("transients", opt_transients, 0, @@ -220,6 +245,7 @@ const struct af_info af_info_rubberband = { OPT_CHOICE("channels", opt_channels, 0, ({"apart", RubberBandOptionChannelsApart}, {"together", RubberBandOptionChannelsTogether})), + OPT_DOUBLE("pitch-scale", pitch, M_OPT_RANGE, .min = 0.01, .max = 100), {0} }, }; |