1 /*****************************************************************************
2 * projectm: visualization module based on libprojectM
3 *****************************************************************************
4 * Copyright © 2009-2011 the VideoLAN team
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 General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc., 51
22 * 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>
36 #include <vlc_vout_wrapper.h>
37 #include <vlc_opengl.h>
38 #include <vlc_filter.h>
41 #include <libprojectM/projectM.hpp>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 #define CONFIG_TEXT N_("projectM configuration file")
50 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
53 #define PRESET_PATH_TXT N_("projectM preset path")
54 #define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
56 #define TITLE_FONT_TXT N_("Title font")
57 #define TITLE_FONT_LONGTXT N_("Font used for the titles")
59 #define MENU_FONT_TXT N_("Font menu")
60 #define MENU_FONT_LONGTXT N_("Font used for the menus")
62 #define WIDTH_TEXT N_("Video width")
63 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
65 #define HEIGHT_TEXT N_("Video height")
66 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
68 #define MESHX_TEXT N_("Mesh width")
69 #define MESHX_LONGTEXT N_("The width of the mesh, in pixels.")
71 #define MESHY_TEXT N_("Mesh height")
72 #define MESHY_LONGTEXT N_("The height of the mesh, in pixels.")
74 #define TEXTURE_TEXT N_("Texture size")
75 #define TEXTURE_LONGTEXT N_("The size of the texture, in pixels.")
78 # define FONT_PATH "C:\\WINDOWS\\Fonts\\arial.ttf"
79 # define FONT_PATH_MENU "C:\\WINDOWS\\Fonts\\arial.ttf"
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"
86 set_shortname( N_("projectM"))
87 set_description( N_("libprojectM effect") )
88 set_capability( "visualization2", 0 )
89 set_category( CAT_AUDIO )
90 set_subcategory( SUBCAT_AUDIO_VISUAL )
91 #ifndef HAVE_PROJECTM2
92 add_loadfile( "projectm-config", "/usr/share/projectM/config.inp",
93 CONFIG_TEXT, CONFIG_LONGTEXT, true )
96 add_directory( "projectm-preset-path", NULL,
98 add_directory( "projectm-preset-path", "/usr/share/projectM/presets",
100 PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
101 add_loadfile( "projectm-title-font", FONT_PATH,
102 TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
103 add_loadfile( "projectm-menu-font", FONT_PATH_MENU,
104 MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
106 add_integer( "projectm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT,
108 add_integer( "projectm-height", 500, HEIGHT_TEXT, HEIGHT_LONGTEXT,
110 add_integer( "projectm-meshx", 32, MESHX_TEXT, MESHX_LONGTEXT,
112 add_integer( "projectm-meshy", 24, MESHY_TEXT, MESHY_LONGTEXT,
114 add_integer( "projectm-texture-size", 1024, TEXTURE_TEXT, TEXTURE_LONGTEXT,
116 add_shortcut( "projectm" )
117 set_callbacks( Open, Close )
121 /*****************************************************************************
123 *****************************************************************************/
132 vout_thread_t *p_vout;
133 vout_display_t *p_vd;
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 /* Test the audio format */
165 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
166 p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
168 msg_Warn( p_filter, "bad input or output format" );
171 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
173 msg_Warn( p_filter, "input and outut are not similar" );
177 p_filter->pf_audio_filter = DoWork;
179 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
183 /* Create the object for the thread */
184 vlc_sem_init( &p_sys->ready, 0 );
185 p_sys->b_error = false;
186 p_sys->b_quit = false;
187 p_sys->i_width = var_InheritInteger( p_filter, "projectm-width" );
188 p_sys->i_height = var_InheritInteger( p_filter, "projectm-height" );
189 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
190 vlc_mutex_init( &p_sys->lock );
191 p_sys->p_buffer = NULL;
192 p_sys->i_buffer_size = 0;
193 p_sys->i_nb_samples = 0;
195 /* Create the thread */
196 if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
199 vlc_sem_wait( &p_sys->ready );
202 vlc_join( p_sys->thread, NULL );
209 vlc_sem_destroy( &p_sys->ready );
217 * @param p_this: the filter object
219 static void Close( vlc_object_t *p_this )
221 filter_t *p_filter = (filter_t *)p_this;
222 filter_sys_t *p_sys = p_filter->p_sys;
225 * XXX vlc_cleanup_push does not seems to work with C++ so no
227 vlc_mutex_lock( &p_sys->lock );
228 p_sys->b_quit = true;
229 vlc_mutex_unlock( &p_sys->lock );
231 vlc_join( p_sys->thread, NULL );
233 /* Free the ressources */
234 vlc_sem_destroy( &p_sys->ready );
235 vlc_mutex_destroy( &p_sys->lock );
236 free( p_sys->p_buffer );
242 * Do the actual work with the new sample
243 * @param p_aout: audio output object
244 * @param p_filter: filter object
245 * @param p_in_buf: input buffer
246 * @param p_out_buf: output buffer
248 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
250 filter_sys_t *p_sys = p_filter->p_sys;
252 vlc_mutex_lock( &p_sys->lock );
253 if( p_sys->i_buffer_size > 0 )
255 p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
256 p_in_buf->i_nb_samples );
258 const float *p_src = (float*)p_in_buf->p_buffer;
259 for( unsigned i = 0; i < p_sys->i_nb_samples; i++ )
262 for( int j = 0; j < p_sys->i_channels; j++ )
263 v += p_src[p_sys->i_channels * i + j];
264 p_sys->p_buffer[i] = v / p_sys->i_channels;
267 vlc_mutex_unlock( &p_sys->lock );
273 * Variable callback for the dummy vout
275 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
276 vlc_value_t oldv, vlc_value_t newv, void *p_data )
278 VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
279 vout_display_t *p_vd = (vout_display_t*)p_data;
281 if( !strcmp(psz_name, "fullscreen") )
283 vout_SetDisplayFullscreen( p_vd, newv.b_bool );
289 * ProjectM update thread which do the rendering
290 * @param p_this: the p_thread object
292 static void *Thread( void *p_data )
294 filter_t *p_filter = (filter_t*)p_data;
295 filter_sys_t *p_sys = p_filter->p_sys;
299 unsigned int i_last_width = 0;
300 unsigned int i_last_height = 0;
304 projectM *p_projectm;
305 #ifndef HAVE_PROJECTM2
308 char *psz_preset_path;
309 char *psz_title_font;
311 projectM::Settings settings;
316 /* Create the openGL provider */
318 (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
323 video_format_Init( &fmt, 0 );
324 video_format_Setup( &fmt, VLC_CODEC_RGB32,
325 p_sys->i_width, p_sys->i_height, 0, 1 );
329 vout_display_state_t state;
330 memset( &state, 0, sizeof(state) );
331 state.cfg.display.sar.num = 1;
332 state.cfg.display.sar.den = 1;
333 state.cfg.is_display_filled = true;
334 state.cfg.zoom.num = 1;
335 state.cfg.zoom.den = 1;
339 p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
343 vlc_object_release( p_sys->p_vout );
346 var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
347 var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
349 gl = vout_GetDisplayOpengl( p_sys->p_vd );
352 var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
353 vout_DeleteDisplay( p_sys->p_vd, NULL );
354 vlc_object_release( p_sys->p_vout );
358 /* Work-around the projectM locale bug */
359 loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
360 oldloc = uselocale (loc);
362 /* Create the projectM object */
363 #ifndef HAVE_PROJECTM2
364 psz_config = var_InheritString( p_filter, "projectm-config" );
365 p_projectm = new projectM( psz_config );
368 psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
370 if ( psz_preset_path == NULL )
372 char *psz_data_path = config_GetDataDir( p_filter );
373 asprintf( &psz_preset_path, "%s" DIR_SEP "visualization", psz_data_path );
374 free( psz_data_path );
378 psz_title_font = var_InheritString( p_filter, "projectm-title-font" );
379 psz_menu_font = var_InheritString( p_filter, "projectm-menu-font" );
381 settings.meshX = var_InheritInteger( p_filter, "projectm-meshx" );
382 settings.meshY = var_InheritInteger( p_filter, "projectm-meshy" );
384 settings.textureSize = var_InheritInteger( p_filter, "projectm-texture-size" );
385 settings.windowWidth = p_sys->i_width;
386 settings.windowHeight = p_sys->i_height;
387 settings.presetURL = psz_preset_path;
388 settings.titleFontURL = psz_title_font;
389 settings.menuFontURL = psz_menu_font;
390 settings.smoothPresetDuration = 5;
391 settings.presetDuration = 30;
392 settings.beatSensitivity = 10;
393 settings.aspectCorrection = 1;
394 settings.easterEgg = 1;
395 settings.shuffleEnabled = 1;
397 p_projectm = new projectM( settings );
399 free( psz_menu_font );
400 free( psz_title_font );
401 free( psz_preset_path );
402 #endif /* HAVE_PROJECTM2 */
404 p_sys->i_buffer_size = p_projectm->pcm()->maxsamples;
405 p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
408 vlc_sem_post( &p_sys->ready );
410 /* Choose a preset randomly or projectM will always show the first one */
411 if ( p_projectm->getPlaylistSize() > 0 )
412 p_projectm->selectPreset( (unsigned)vlc_mrand48() % p_projectm->getPlaylistSize() );
417 const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
418 /* Manage the events */
419 vout_ManageDisplay( p_sys->p_vd, true );
420 if( p_sys->p_vd->cfg->display.width != i_last_width ||
421 p_sys->p_vd->cfg->display.height != i_last_height )
423 /* FIXME it is not perfect as we will have black bands */
424 vout_display_place_t place;
425 vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
426 p_projectm->projectM_resetGL( place.width, place.height );
428 i_last_width = p_sys->p_vd->cfg->display.width;
429 i_last_height = p_sys->p_vd->cfg->display.height;
432 /* Render the image and swap the buffers */
433 vlc_mutex_lock( &p_sys->lock );
434 if( p_sys->i_nb_samples > 0 )
436 p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
437 p_sys->i_nb_samples );
438 p_sys->i_nb_samples = 0;
442 vlc_mutex_unlock( &p_sys->lock );
445 var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
446 vout_DeleteDisplay( p_sys->p_vd, NULL );
447 vlc_object_release( p_sys->p_vout );
448 if (loc != (locale_t)0)
455 vlc_mutex_unlock( &p_sys->lock );
457 p_projectm->renderFrame();
462 if( !vlc_gl_Lock(gl) )
471 p_sys->b_error = true;
472 vlc_sem_post( &p_sys->ready );