1 /*****************************************************************************
2 * projectm.cpp: visualization module based on libprojectM
3 *****************************************************************************
4 * Copyright © 2009-2011 VLC authors and VideoLAN
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #ifndef __STDC_CONSTANT_MACROS
29 # define __STDC_CONSTANT_MACROS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_vout_window.h>
36 #include <vlc_opengl.h>
37 #include <vlc_filter.h>
40 #include <libprojectM/projectM.hpp>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 #define CONFIG_TEXT N_("projectM configuration file")
49 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
52 #define PRESET_PATH_TXT N_("projectM preset path")
53 #define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
55 #define TITLE_FONT_TXT N_("Title font")
56 #define TITLE_FONT_LONGTXT N_("Font used for the titles")
58 #define MENU_FONT_TXT N_("Font menu")
59 #define MENU_FONT_LONGTXT N_("Font used for the menus")
61 #define WIDTH_TEXT N_("Video width")
62 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
64 #define HEIGHT_TEXT N_("Video height")
65 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
67 #define MESHX_TEXT N_("Mesh width")
68 #define MESHX_LONGTEXT N_("The width of the mesh, in pixels.")
70 #define MESHY_TEXT N_("Mesh height")
71 #define MESHY_LONGTEXT N_("The height of the mesh, in pixels.")
73 #define TEXTURE_TEXT N_("Texture size")
74 #define TEXTURE_LONGTEXT N_("The size of the texture, in pixels.")
77 # define FONT_PATH "C:\\WINDOWS\\Fonts\\arial.ttf"
78 # define FONT_PATH_MENU "C:\\WINDOWS\\Fonts\\arial.ttf"
79 # define PRESET_PATH NULL
81 # define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"
82 # define FONT_PATH_MENU "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
83 # define PRESET_PATH "/usr/share/projectM/presets"
86 #ifdef DEFAULT_FONT_FILE
88 #define FONT_PATH DEFAULT_FONT_FILE
91 #ifdef DEFAULT_MONOSPACE_FONT_FILE
93 #define FONT_PATH_MENU DEFAULT_MONOSPACE_FONT_FILE
97 set_shortname( N_("projectM"))
98 set_description( N_("libprojectM effect") )
99 set_capability( "visualization", 0 )
100 set_category( CAT_AUDIO )
101 set_subcategory( SUBCAT_AUDIO_VISUAL )
102 #ifndef HAVE_PROJECTM2
103 add_loadfile( "projectm-config", "/usr/share/projectM/config.inp",
104 CONFIG_TEXT, CONFIG_LONGTEXT, true )
106 add_directory( "projectm-preset-path", PRESET_PATH,
107 PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
108 add_loadfile( "projectm-title-font", FONT_PATH,
109 TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
110 add_loadfile( "projectm-menu-font", FONT_PATH_MENU,
111 MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
113 add_integer( "projectm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT,
115 add_integer( "projectm-height", 500, HEIGHT_TEXT, HEIGHT_LONGTEXT,
117 add_integer( "projectm-meshx", 32, MESHX_TEXT, MESHX_LONGTEXT,
119 add_integer( "projectm-meshy", 24, MESHY_TEXT, MESHY_LONGTEXT,
121 add_integer( "projectm-texture-size", 1024, TEXTURE_TEXT, TEXTURE_LONGTEXT,
123 add_shortcut( "projectm" )
124 set_callbacks( Open, Close )
128 /*****************************************************************************
130 *****************************************************************************/
146 unsigned i_buffer_size;
147 unsigned i_nb_samples;
151 static block_t *DoWork( filter_t *, block_t * );
152 static void *Thread( void * );
156 * @param p_this: the filter object
157 * @return VLC_SUCCESS or vlc error codes
159 static int Open( vlc_object_t * p_this )
161 filter_t *p_filter = (filter_t *)p_this;
164 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
168 /* Create the object for the thread */
169 p_sys->b_quit = false;
170 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
171 vlc_mutex_init( &p_sys->lock );
172 p_sys->p_buffer = NULL;
173 p_sys->i_buffer_size = 0;
174 p_sys->i_nb_samples = 0;
176 /* Create the OpenGL context */
177 vout_window_cfg_t cfg;
179 memset(&cfg, 0, sizeof (cfg));
180 cfg.width = var_CreateGetInteger( p_filter, "projectm-width" );
181 cfg.height = var_CreateGetInteger( p_filter, "projectm-height" );
183 p_sys->gl = vlc_gl_surface_Create( VLC_OBJECT(p_filter), &cfg, NULL );
184 if( p_sys->gl == NULL )
187 /* Create the thread */
188 if( vlc_clone( &p_sys->thread, Thread, p_filter,
189 VLC_THREAD_PRIORITY_LOW ) )
191 vlc_gl_surface_Destroy( p_sys->gl );
195 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
196 p_filter->fmt_out.audio = p_filter->fmt_in.audio;
197 p_filter->pf_audio_filter = DoWork;
201 vlc_mutex_destroy( &p_sys->lock );
209 * @param p_this: the filter object
211 static void Close( vlc_object_t *p_this )
213 filter_t *p_filter = (filter_t *)p_this;
214 filter_sys_t *p_sys = p_filter->p_sys;
217 * XXX vlc_cleanup_push does not seems to work with C++ so no
219 vlc_mutex_lock( &p_sys->lock );
220 p_sys->b_quit = true;
221 vlc_mutex_unlock( &p_sys->lock );
223 vlc_join( p_sys->thread, NULL );
225 /* Free the ressources */
226 vlc_gl_surface_Destroy( p_sys->gl );
227 vlc_mutex_destroy( &p_sys->lock );
228 free( p_sys->p_buffer );
234 * Do the actual work with the new sample
235 * @param p_aout: audio output object
236 * @param p_filter: filter object
237 * @param p_in_buf: input buffer
238 * @param p_out_buf: output buffer
240 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
242 filter_sys_t *p_sys = p_filter->p_sys;
244 vlc_mutex_lock( &p_sys->lock );
245 if( p_sys->i_buffer_size > 0 )
247 p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
248 p_in_buf->i_nb_samples );
250 const float *p_src = (float*)p_in_buf->p_buffer;
251 for( unsigned i = 0; i < p_sys->i_nb_samples; i++ )
254 for( int j = 0; j < p_sys->i_channels; j++ )
255 v += p_src[p_sys->i_channels * i + j];
256 p_sys->p_buffer[i] = v / p_sys->i_channels;
259 vlc_mutex_unlock( &p_sys->lock );
265 * ProjectM update thread which do the rendering
266 * @param p_this: the p_thread object
268 static void *Thread( void *p_data )
270 filter_t *p_filter = (filter_t*)p_data;
271 filter_sys_t *p_sys = p_filter->p_sys;
272 vlc_gl_t *gl = p_sys->gl;
276 projectM *p_projectm;
277 #ifndef HAVE_PROJECTM2
280 char *psz_preset_path;
281 char *psz_title_font;
283 projectM::Settings settings;
286 vlc_gl_MakeCurrent( gl );
288 /* Work-around the projectM locale bug */
289 loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
290 oldloc = uselocale (loc);
292 /* Create the projectM object */
293 #ifndef HAVE_PROJECTM2
294 psz_config = var_InheritString( p_filter, "projectm-config" );
295 p_projectm = new projectM( psz_config );
298 psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
300 if ( psz_preset_path == NULL )
302 char *psz_data_path = config_GetDataDir();
303 asprintf( &psz_preset_path, "%s" DIR_SEP "visualization", psz_data_path );
304 free( psz_data_path );
308 psz_title_font = var_InheritString( p_filter, "projectm-title-font" );
309 psz_menu_font = var_InheritString( p_filter, "projectm-menu-font" );
311 settings.meshX = var_InheritInteger( p_filter, "projectm-meshx" );
312 settings.meshY = var_InheritInteger( p_filter, "projectm-meshy" );
314 settings.textureSize = var_InheritInteger( p_filter, "projectm-texture-size" );
315 settings.windowWidth = var_InheritInteger( p_filter, "projectm-width" );
316 settings.windowHeight = var_CreateGetInteger( p_filter, "projectm-height" );
317 settings.presetURL = psz_preset_path;
318 settings.titleFontURL = psz_title_font;
319 settings.menuFontURL = psz_menu_font;
320 settings.smoothPresetDuration = 5;
321 settings.presetDuration = 30;
322 settings.beatSensitivity = 10;
323 settings.aspectCorrection = 1;
324 settings.easterEgg = 1;
325 settings.shuffleEnabled = 1;
327 p_projectm = new projectM( settings );
329 free( psz_menu_font );
330 free( psz_title_font );
331 free( psz_preset_path );
332 #endif /* HAVE_PROJECTM2 */
334 p_sys->i_buffer_size = p_projectm->pcm()->maxsamples;
335 p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
338 /* Choose a preset randomly or projectM will always show the first one */
339 if ( p_projectm->getPlaylistSize() > 0 )
340 p_projectm->selectPreset( (unsigned)vlc_mrand48() % p_projectm->getPlaylistSize() );
345 const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
347 /* Manage the events */
348 unsigned width, height;
351 if( vlc_gl_surface_CheckSize( gl, &width, &height ) )
352 p_projectm->projectM_resetGL( width, height );
354 /* Render the image and swap the buffers */
355 vlc_mutex_lock( &p_sys->lock );
356 if( p_sys->i_nb_samples > 0 )
358 p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
359 p_sys->i_nb_samples );
360 p_sys->i_nb_samples = 0;
362 quit = p_sys->b_quit;
363 vlc_mutex_unlock( &p_sys->lock );
368 p_projectm->renderFrame();
373 if( !vlc_gl_Lock(gl) )
382 if (loc != (locale_t)0)
388 vlc_gl_ReleaseCurrent( gl );