1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*****************************************************************************
* spatializer.cpp: sound reverberation
*****************************************************************************
* Copyright (C) 2004, 2006, 2007 VLC authors and VideoLAN
*
* Google Summer of Code 2007
*
* Authors: Biodun Osunkunle <biodun@videolan.org>
*
* Mentor : Jean-Baptiste Kempf <jb@videolan.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h> /* malloc(), free() */
#include <math.h>
#include <new>
using std::nothrow;
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>
#include <vlc_filter.h>
#include "revmodel.hpp"
#define SPAT_AMP 0.3
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close( vlc_object_t * );
#define ROOMSIZE_TEXT N_("Room size")
#define ROOMSIZE_LONGTEXT N_("Defines the virtual surface of the room" \
" emulated by the filter." )
#define WIDTH_TEXT N_("Room width")
#define WIDTH_LONGTEXT N_("Width of the virtual room")
#define WET_TEXT N_("Wet")
#define WET_LONGTEXT NULL
#define DRY_TEXT N_("Dry")
#define DRY_LONGTEXT NULL
#define DAMP_TEXT N_("Damp")
#define DAMP_LONGTEXT NULL
vlc_module_begin ()
set_description( N_("Audio Spatializer") )
set_shortname( N_("Spatializer" ) )
set_capability( "audio filter", 0 )
set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_AFILTER )
set_callbacks( Open, Close )
add_shortcut( "spatializer" )
add_float_with_range( "spatializer-roomsize", 0.85, 0., 1.1,
ROOMSIZE_TEXT, ROOMSIZE_LONGTEXT, false )
add_float_with_range( "spatializer-width", 1, 0., 1.,
WIDTH_TEXT,WIDTH_LONGTEXT, false )
add_float_with_range( "spatializer-wet", 0.4, 0., 1.,
WET_TEXT,WET_LONGTEXT, false )
add_float_with_range( "spatializer-dry", 0.5, 0., 1.,
DRY_TEXT,DRY_LONGTEXT, false )
add_float_with_range( "spatializer-damp", 0.5, 0., 1.,
DAMP_TEXT,DAMP_LONGTEXT, false )
vlc_module_end ()
/*****************************************************************************
* Local prototypes
*****************************************************************************/
struct filter_sys_t
{
vlc_mutex_t lock;
revmodel *p_reverbm;
};
#define DECLARECB(fn) static int fn (vlc_object_t *,char const *, \
vlc_value_t, vlc_value_t, void *)
DECLARECB( RoomCallback );
DECLARECB( WetCallback );
DECLARECB( DryCallback );
DECLARECB( DampCallback );
DECLARECB( WidthCallback );
#undef DECLARECB
struct callback_s {
const char *psz_name;
int (*fp_callback)(vlc_object_t *,const char *,
vlc_value_t,vlc_value_t,void *);
void (revmodel::* fp_set)(float);
};
static const callback_s callbacks[] = {
{ "spatializer-roomsize", RoomCallback, &revmodel::setroomsize },
{ "spatializer-width", WidthCallback, &revmodel::setwidth },
{ "spatializer-wet", WetCallback, &revmodel::setwet },
{ "spatializer-dry", DryCallback, &revmodel::setdry },
{ "spatializer-damp", DampCallback, &revmodel::setdamp }
};
enum { num_callbacks=sizeof(callbacks)/sizeof(callback_s) };
static block_t *DoWork( filter_t *, block_t * );
/*****************************************************************************
* Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
filter_t *p_filter = (filter_t *)p_this;
filter_sys_t *p_sys;
vlc_object_t *p_aout = p_filter->obj.parent;
/* Allocate structure */
p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
if( !p_sys )
return VLC_ENOMEM;
/* Force new to return 0 on failure instead of throwing, since we don't
want an exception to leak back to C code. Bad things would happen. */
p_sys->p_reverbm = new (nothrow) revmodel;
if( !p_sys->p_reverbm )
{
free( p_sys );
return VLC_ENOMEM;
}
vlc_mutex_init( &p_sys->lock );
for(unsigned i=0;i<num_callbacks;++i)
{
/* NOTE: C++ pointer-to-member function call from table lookup. */
(p_sys->p_reverbm->*(callbacks[i].fp_set))
(var_CreateGetFloatCommand(p_aout,callbacks[i].psz_name));
var_AddCallback( p_aout, callbacks[i].psz_name,
callbacks[i].fp_callback, p_sys );
}
p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
aout_FormatPrepare(&p_filter->fmt_in.audio);
p_filter->fmt_out.audio = p_filter->fmt_in.audio;
p_filter->pf_audio_filter = DoWork;
return VLC_SUCCESS;
}
/*****************************************************************************
* Close: close the plugin
*****************************************************************************/
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;
vlc_object_t *p_aout = p_filter->obj.parent;
/* Delete the callbacks */
for(unsigned i=0;i<num_callbacks;++i)
{
var_DelCallback( p_aout, callbacks[i].psz_name,
callbacks[i].fp_callback, p_sys );
}
delete p_sys->p_reverbm;
vlc_mutex_destroy( &p_sys->lock );
free( p_sys );
msg_Dbg( p_this, "Closing filter spatializer" );
}
/*****************************************************************************
* SpatFilter: process samples buffer
* DoWork: call SpatFilter
*****************************************************************************/
static void SpatFilter( filter_t *p_filter, float *out, float *in,
unsigned i_samples, unsigned i_channels )
{
filter_sys_t *p_sys = p_filter->p_sys;
vlc_mutex_locker locker( &p_sys->lock );
for( unsigned i = 0; i < i_samples; i++ )
{
for( unsigned ch = 0 ; ch < 2; ch++)
{
in[ch] = in[ch] * SPAT_AMP;
}
p_sys->p_reverbm->processreplace( in, out , 1, i_channels);
in += i_channels;
out += i_channels;
}
}
static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
{
SpatFilter( p_filter, (float*)p_in_buf->p_buffer,
(float*)p_in_buf->p_buffer, p_in_buf->i_nb_samples,
aout_FormatNbChannels( &p_filter->fmt_in.audio ) );
return p_in_buf;
}
/*****************************************************************************
* Variables callbacks
*****************************************************************************/
static int RoomCallback( vlc_object_t *p_this, char const *,
vlc_value_t, vlc_value_t newval, void *p_data )
{
filter_sys_t *p_sys = (filter_sys_t*)p_data;
vlc_mutex_locker locker( &p_sys->lock );
p_sys->p_reverbm->setroomsize(newval.f_float);
msg_Dbg( p_this, "room size is now %3.1f", newval.f_float );
return VLC_SUCCESS;
}
static int WidthCallback( vlc_object_t *p_this, char const *,
vlc_value_t, vlc_value_t newval, void *p_data )
{
filter_sys_t *p_sys = (filter_sys_t*)p_data;
vlc_mutex_locker locker( &p_sys->lock );
p_sys->p_reverbm->setwidth(newval.f_float);
msg_Dbg( p_this, "width is now %3.1f", newval.f_float );
return VLC_SUCCESS;
}
static int WetCallback( vlc_object_t *p_this, char const *,
vlc_value_t, vlc_value_t newval, void *p_data )
{
filter_sys_t *p_sys = (filter_sys_t*)p_data;
vlc_mutex_locker locker( &p_sys->lock );
p_sys->p_reverbm->setwet(newval.f_float);
msg_Dbg( p_this, "'wet' value is now %3.1f", newval.f_float );
return VLC_SUCCESS;
}
static int DryCallback( vlc_object_t *p_this, char const *,
vlc_value_t, vlc_value_t newval, void *p_data )
{
filter_sys_t *p_sys = (filter_sys_t*)p_data;
vlc_mutex_locker locker( &p_sys->lock );
p_sys->p_reverbm->setdry(newval.f_float);
msg_Dbg( p_this, "'dry' value is now %3.1f", newval.f_float );
return VLC_SUCCESS;
}
static int DampCallback( vlc_object_t *p_this, char const *,
vlc_value_t, vlc_value_t newval, void *p_data )
{
filter_sys_t *p_sys = (filter_sys_t*)p_data;
vlc_mutex_locker locker( &p_sys->lock );
p_sys->p_reverbm->setdamp(newval.f_float);
msg_Dbg( p_this, "'damp' value is now %3.1f", newval.f_float );
return VLC_SUCCESS;
}
|