Skip to content

Instantly share code, notes, and snippets.

@upsuper
Created October 12, 2012 16:27
Show Gist options
  • Select an option

  • Save upsuper/3880098 to your computer and use it in GitHub Desktop.

Select an option

Save upsuper/3880098 to your computer and use it in GitHub Desktop.
Interesting Audio Filters for MPlayer
/*
* Copyright (C) 2012 Xidorn Quan <quanxunzhen@gmail.com>
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include "mp_msg.h"
#include "af.h"
#ifndef M_PI
# define M_PI 3.141592653589793
#endif
typedef struct af_alternate_s
{
float freq;
float amin, amax;
float pos;
} af_alternate_t;
static int control(af_instance_t *af, int cmd, void *arg)
{
af_data_t *data;
af_alternate_t *s = af->setup;
float period;
switch (cmd) {
case AF_CONTROL_REINIT:
if (!arg) return AF_ERROR;
data = arg;
af->data->rate = data->rate;
af->data->nch = 2;
af->data->format = AF_FORMAT_FLOAT_NE;
af->data->bps = 4;
return af_test_output(af, data);
case AF_CONTROL_COMMAND_LINE:
sscanf((char *)arg, "%f:%f:%f",
&period, &s->amin, &s->amax);
s->freq = 1.0 / period;
return AF_OK;
}
return AF_UNKNOWN;
}
static void uninit(af_instance_t *af)
{
free(af->data);
free(af->setup);
}
static af_data_t *play(af_instance_t *af, af_data_t *data)
{
af_alternate_t *s = af->setup;
double pos = s->pos;
int rate = af->data->rate;
float freq = s->freq;
float amin = s->amin, amax = s->amax,
aspan = (amax - amin) / 2;
size_t i, ch;
float *a = data->audio;
int len = data->len / 4;
for (i = 0; i < len / 2; i++) {
float x = sin(pos) * aspan;
pos += freq * M_PI * 2 / rate;
for (ch = 0; ch < 2; ch++) {
size_t p = i * 2 + ch;
float t = a[p] * (amin + aspan + x * (ch ? -1 : 1));
a[p] = clamp(t, -1.0, 1.0);
}
}
pos -= floor(pos / (M_PI * 2)) * M_PI * 2;
s->pos = pos;
return data;
}
static int af_open(af_instance_t *af)
{
af_alternate_t *s;
af->control = control;
af->uninit = uninit;
af->play = play;
af->mul = 1.0f;
af->data = calloc(1, sizeof(af_data_t));
af->setup = s = calloc(1, sizeof(af_alternate_t));
if (!af->data || !af->setup)
return AF_ERROR;
s->freq = 0.2;
s->amin = 0.0;
s->amax = 1.0;
s->pos = 0.0;
return AF_OK;
}
af_info_t af_info_alternate = {
"Alternating sound between two channals",
"alternate",
"Xidorn",
"",
AF_FLAGS_NOT_REENTRANT,
af_open
};
Index: Makefile
===================================================================
--- Makefile (revision 35239)
+++ Makefile (working copy)
@@ -322,6 +322,7 @@
libaf/af_tools.c \
libaf/af_volnorm.c \
libaf/af_volume.c \
+ libaf/af_alternate.c \
libaf/filter.c \
libaf/format.c \
libaf/reorder_ch.c \
Index: libaf/af.c
===================================================================
--- libaf/af.c (revision 35239)
+++ libaf/af.c (working copy)
@@ -53,6 +53,7 @@
extern const af_info_t af_info_scaletempo;
extern const af_info_t af_info_stats;
extern const af_info_t af_info_bs2b;
+extern const af_info_t af_info_alternate;
static const af_info_t * const filter_list[] = {
&af_info_dummy,
@@ -89,6 +90,7 @@
#ifdef CONFIG_LIBBS2B
&af_info_bs2b,
#endif
+ &af_info_alternate,
NULL
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment