From fc00ccb2b1eca2d0e2ff720afb855ff51c048aa7 Mon Sep 17 00:00:00 2001
From: Pankaj Yadav <pankajdnapster@gmail.com>
Date: Sun, 20 Mar 2011 11:03:15 +0100
Subject: [PATCH] KARAOKE Audio Filter Module for VLC

---
 modules/audio_filter/karaoke.c |  156 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 156 insertions(+), 0 deletions(-)
 create mode 100644 modules/audio_filter/karaoke.c

diff --git a/modules/audio_filter/karaoke.c b/modules/audio_filter/karaoke.c
new file mode 100644
index 0000000..2205eef
--- /dev/null
+++ b/modules/audio_filter/karaoke.c
@@ -0,0 +1,156 @@
+/*****************************************************************************
+ * karaoke.c : Karaoke Audio filter
+ *****************************************************************************
+ * Copyright (C) 20011 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Pankaj Yadav <pk@videolan.org>
+ *
+ * This program 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.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*
+ * TODO:
+ *
+ * We should detect fast power increases and react faster to these
+ * This way, we can increase the buffer size to get a more stable filter */
+
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <math.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+
+#include <vlc_aout.h>
+#include <vlc_filter.h>
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+
+static int  Open     ( vlc_object_t * );
+static void Close    ( vlc_object_t * );
+static block_t *DoWork( filter_t *, block_t * );
+
+struct filter_sys_t
+{
+    float f_level;
+};
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+
+#define LEVEL_TEXT N_("Karaoke Level" )
+#define LEVEL_LONGTEXT N_("Karaoke Level" )
+
+vlc_module_begin ()
+    set_description( N_("Karaoke") )
+    set_shortname( N_("Karaoke") )
+    set_category( CAT_AUDIO )
+    set_subcategory( SUBCAT_AUDIO_AFILTER )
+    add_shortcut( "karaoke" )
+    add_float( "karaoke-level", 0.7, LEVEL_TEXT,
+               LEVEL_LONGTEXT, true )
+    set_capability( "audio filter", 0 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
+
+/*****************************************************************************
+ * Open: initialize and create stuff
+ *****************************************************************************/
+static int Open( vlc_object_t *p_this )
+{
+    filter_t *p_filter = (filter_t*)p_this;
+    unsigned i_channels;
+    filter_sys_t *p_sys;
+
+    if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
+        p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
+    {
+        p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
+        p_filter->fmt_out.audio.i_format = VLC_CODEC_FL32;
+        msg_Warn( p_filter, "bad input or output format" );
+        return VLC_EGENERIC;
+    }
+
+    if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
+    {
+        memcpy( &p_filter->fmt_out.audio, &p_filter->fmt_in.audio,
+                sizeof(audio_sample_format_t) );
+        msg_Warn( p_filter, "input and output formats are not similar" );
+        return VLC_EGENERIC;
+    }
+
+    p_filter->pf_audio_filter = DoWork;
+
+    i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
+
+    p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
+
+	p_sys->f_level = var_CreateGetFloat( p_filter->p_parent, "karaoke-level" );
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * DoWork : normalizes and sends a buffer
+ *****************************************************************************/
+static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
+{
+    int i, i_chan;
+
+    int i_samples = p_in_buf->i_nb_samples;
+    int i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
+    float *p_out = (float*)p_in_buf->p_buffer;
+    float *p_in =  (float*)p_in_buf->p_buffer;
+
+    struct filter_sys_t *p_sys = p_filter->p_sys;
+
+	p_sys->f_level = var_GetFloat( p_filter->p_parent, "karaoke-level" );
+
+    /* Calculate the average power level on this buffer */
+    for( i = 0 ; i < i_samples; i++ )
+    {
+        p_out[0] = (p_in[0] - p_in[1]) * p_sys->f_level;
+        p_out[1] = p_out[0];
+        p_in += i_channels;
+        p_out += i_channels;
+    }
+
+    return p_in_buf;
+}
+
+/**********************************************************************
+ * Close
+ **********************************************************************/
+static void Close( vlc_object_t *p_this )
+{
+    filter_t *p_filter = (filter_t*)p_this;
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    free( p_sys );
+}
+
-- 
1.7.4.1

