]> git.sesse.net Git - vlc/blob - modules/visualization/projectm.cpp
fingerprinter: don't hardcode acoustid module selection
[vlc] / modules / visualization / projectm.cpp
1 /*****************************************************************************
2  * projectm.cpp: visualization module based on libprojectM
3  *****************************************************************************
4  * Copyright © 2009-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rémi Duraffort <ivoire@videolan.org>
8  *          Laurent Aimar
9  *
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)
13  * any later version.
14  *
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
18  * more details.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #ifndef __STDC_CONSTANT_MACROS
29 # define __STDC_CONSTANT_MACROS
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35 #include <vlc_vout.h>
36 #include <vlc_vout_wrapper.h>
37 #include <vlc_opengl.h>
38 #include <vlc_filter.h>
39 #include <vlc_rand.h>
40
41 #include <libprojectM/projectM.hpp>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open         ( vlc_object_t * );
47 static void Close        ( vlc_object_t * );
48
49 #define CONFIG_TEXT N_("projectM configuration file")
50 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
51                            "module.")
52
53 #define PRESET_PATH_TXT N_("projectM preset path")
54 #define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
55
56 #define TITLE_FONT_TXT N_("Title font")
57 #define TITLE_FONT_LONGTXT N_("Font used for the titles")
58
59 #define MENU_FONT_TXT N_("Font menu")
60 #define MENU_FONT_LONGTXT N_("Font used for the menus")
61
62 #define WIDTH_TEXT N_("Video width")
63 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
64
65 #define HEIGHT_TEXT N_("Video height")
66 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
67
68 #define MESHX_TEXT N_("Mesh width")
69 #define MESHX_LONGTEXT N_("The width of the mesh, in pixels.")
70
71 #define MESHY_TEXT N_("Mesh height")
72 #define MESHY_LONGTEXT N_("The height of the mesh, in pixels.")
73
74 #define TEXTURE_TEXT N_("Texture size")
75 #define TEXTURE_LONGTEXT N_("The size of the texture, in pixels.")
76
77 #ifdef _WIN32
78 # define FONT_PATH      "C:\\WINDOWS\\Fonts\\arial.ttf"
79 # define FONT_PATH_MENU "C:\\WINDOWS\\Fonts\\arial.ttf"
80 # define PRESET_PATH    NULL
81 #else
82 # define FONT_PATH      "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"
83 # define FONT_PATH_MENU "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
84 # define PRESET_PATH    "/usr/share/projectM/presets"
85 #endif
86
87 #ifdef DEFAULT_FONT_FILE
88 #undef FONT_PATH
89 #define FONT_PATH DEFAULT_FONT_FILE
90 #endif
91
92 #ifdef DEFAULT_MONOSPACE_FONT_FILE
93 #undef FONT_PATH_MENU
94 #define FONT_PATH_MENU DEFAULT_MONOSPACE_FONT_FILE
95 #endif
96
97 vlc_module_begin ()
98     set_shortname( N_("projectM"))
99     set_description( N_("libprojectM effect") )
100     set_capability( "visualization", 0 )
101     set_category( CAT_AUDIO )
102     set_subcategory( SUBCAT_AUDIO_VISUAL )
103 #ifndef HAVE_PROJECTM2
104     add_loadfile( "projectm-config", "/usr/share/projectM/config.inp",
105                   CONFIG_TEXT, CONFIG_LONGTEXT, true )
106 #else
107     add_directory( "projectm-preset-path", PRESET_PATH,
108                   PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
109     add_loadfile( "projectm-title-font", FONT_PATH,
110                   TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
111     add_loadfile( "projectm-menu-font", FONT_PATH_MENU,
112                   MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
113 #endif
114     add_integer( "projectm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT,
115                  false )
116     add_integer( "projectm-height", 500, HEIGHT_TEXT, HEIGHT_LONGTEXT,
117                  false )
118     add_integer( "projectm-meshx", 32, MESHX_TEXT, MESHX_LONGTEXT,
119                  false )
120     add_integer( "projectm-meshy", 24, MESHY_TEXT, MESHY_LONGTEXT,
121                  false )
122     add_integer( "projectm-texture-size", 1024, TEXTURE_TEXT, TEXTURE_LONGTEXT,
123                  false )
124     add_shortcut( "projectm" )
125     set_callbacks( Open, Close )
126 vlc_module_end ()
127
128
129 /*****************************************************************************
130  * Local prototypes
131  *****************************************************************************/
132 struct filter_sys_t
133 {
134     /* */
135     vlc_thread_t thread;
136     vlc_sem_t    ready;
137     bool         b_error;
138
139     /* Opengl */
140     vout_thread_t  *p_vout;
141     vout_display_t *p_vd;
142
143     /* Window size */
144     int i_width;
145     int i_height;
146
147     /* audio info */
148     int i_channels;
149
150     /* */
151     vlc_mutex_t lock;
152     bool  b_quit;
153     float *p_buffer;
154     unsigned i_buffer_size;
155     unsigned i_nb_samples;
156 };
157
158
159 static block_t *DoWork( filter_t *, block_t * );
160 static void *Thread( void * );
161
162 /**
163  * Open the module
164  * @param p_this: the filter object
165  * @return VLC_SUCCESS or vlc error codes
166  */
167 static int Open( vlc_object_t * p_this )
168 {
169     filter_t     *p_filter = (filter_t *)p_this;
170     filter_sys_t *p_sys;
171
172     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
173     if( !p_sys )
174         return VLC_ENOMEM;
175
176     /* Create the object for the thread */
177     vlc_sem_init( &p_sys->ready, 0 );
178     p_sys->b_error       = false;
179     p_sys->b_quit        = false;
180     p_sys->i_width       = var_InheritInteger( p_filter, "projectm-width" );
181     p_sys->i_height      = var_InheritInteger( p_filter, "projectm-height" );
182     p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
183     vlc_mutex_init( &p_sys->lock );
184     p_sys->p_buffer      = NULL;
185     p_sys->i_buffer_size = 0;
186     p_sys->i_nb_samples  = 0;
187
188     /* Create the thread */
189     if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
190         goto error;
191
192     vlc_sem_wait( &p_sys->ready );
193     if( p_sys->b_error )
194     {
195         vlc_join( p_sys->thread, NULL );
196         goto error;
197     }
198
199     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
200     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
201     p_filter->pf_audio_filter = DoWork;
202     return VLC_SUCCESS;
203
204 error:
205     vlc_sem_destroy( &p_sys->ready );
206     free (p_sys );
207     return VLC_EGENERIC;
208 }
209
210
211 /**
212  * Close the module
213  * @param p_this: the filter object
214  */
215 static void Close( vlc_object_t *p_this )
216 {
217     filter_t  *p_filter = (filter_t *)p_this;
218     filter_sys_t *p_sys = p_filter->p_sys;
219
220     /* Stop the thread
221      * XXX vlc_cleanup_push does not seems to work with C++ so no
222      * vlc_cancel()... */
223     vlc_mutex_lock( &p_sys->lock );
224     p_sys->b_quit = true;
225     vlc_mutex_unlock( &p_sys->lock );
226
227     vlc_join( p_sys->thread, NULL );
228
229     /* Free the ressources */
230     vlc_sem_destroy( &p_sys->ready );
231     vlc_mutex_destroy( &p_sys->lock );
232     free( p_sys->p_buffer );
233     free( p_sys );
234 }
235
236
237 /**
238  * Do the actual work with the new sample
239  * @param p_aout: audio output object
240  * @param p_filter: filter object
241  * @param p_in_buf: input buffer
242  * @param p_out_buf: output buffer
243  */
244 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
245 {
246     filter_sys_t *p_sys = p_filter->p_sys;
247
248     vlc_mutex_lock( &p_sys->lock );
249     if( p_sys->i_buffer_size > 0 )
250     {
251         p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
252                                      p_in_buf->i_nb_samples );
253
254         const float *p_src = (float*)p_in_buf->p_buffer;
255         for( unsigned i = 0; i < p_sys->i_nb_samples; i++ )
256         {
257             float v = 0;
258             for( int j = 0; j < p_sys->i_channels; j++ )
259                 v += p_src[p_sys->i_channels * i + j];
260             p_sys->p_buffer[i] = v / p_sys->i_channels;
261         }
262     }
263     vlc_mutex_unlock( &p_sys->lock );
264
265     return p_in_buf;
266 }
267
268 /**
269  * Variable callback for the dummy vout
270  */
271 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
272                          vlc_value_t oldv, vlc_value_t newv, void *p_data )
273 {
274     VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
275     vout_display_t *p_vd = (vout_display_t*)p_data;
276
277     if( !strcmp(psz_name, "fullscreen") )
278     {
279         vout_SetDisplayFullscreen( p_vd, newv.b_bool );
280     }
281     return VLC_SUCCESS;
282 }
283
284 /**
285  * ProjectM update thread which do the rendering
286  * @param p_this: the p_thread object
287  */
288 static void *Thread( void *p_data )
289 {
290     filter_t  *p_filter = (filter_t*)p_data;
291     filter_sys_t *p_sys = p_filter->p_sys;
292
293     video_format_t fmt;
294     vlc_gl_t *gl;
295     unsigned int i_last_width  = 0;
296     unsigned int i_last_height = 0;
297     locale_t loc;
298     locale_t oldloc;
299
300     projectM *p_projectm;
301 #ifndef HAVE_PROJECTM2
302     char *psz_config;
303 #else
304     char *psz_preset_path;
305     char *psz_title_font;
306     char *psz_menu_font;
307     projectM::Settings settings;
308 #endif
309
310     vlc_savecancel();
311
312     /* Create the openGL provider */
313     p_sys->p_vout =
314         (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
315     if( !p_sys->p_vout )
316         goto error;
317
318     /* */
319     video_format_Init( &fmt, 0 );
320     video_format_Setup( &fmt, VLC_CODEC_RGB32,
321                         p_sys->i_width, p_sys->i_height, 0, 1 );
322     fmt.i_sar_num = 1;
323     fmt.i_sar_den = 1;
324
325     vout_display_state_t state;
326     memset( &state, 0, sizeof(state) );
327     state.cfg.display.sar.num = 1;
328     state.cfg.display.sar.den = 1;
329     state.cfg.is_display_filled = true;
330     state.cfg.zoom.num = 1;
331     state.cfg.zoom.den = 1;
332     state.sar.num = 1;
333     state.sar.den = 1;
334
335     p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
336                                    300000, 1000000 );
337     if( !p_sys->p_vd )
338     {
339         vlc_object_release( p_sys->p_vout );
340         goto error;
341     }
342     var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
343     var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
344
345     gl = vout_GetDisplayOpengl( p_sys->p_vd );
346     if( !gl )
347     {
348         var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
349         vout_DeleteDisplay( p_sys->p_vd, NULL );
350         vlc_object_release( p_sys->p_vout );
351         goto error;
352     }
353
354     /* Work-around the projectM locale bug */
355     loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
356     oldloc = uselocale (loc);
357
358     /* Create the projectM object */
359 #ifndef HAVE_PROJECTM2
360     psz_config = var_InheritString( p_filter, "projectm-config" );
361     p_projectm = new projectM( psz_config );
362     free( psz_config );
363 #else
364     psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
365 #ifdef _WIN32
366     if ( psz_preset_path == NULL )
367     {
368         char *psz_data_path = config_GetDataDir();
369         asprintf( &psz_preset_path, "%s" DIR_SEP "visualization", psz_data_path );
370         free( psz_data_path );
371     }
372 #endif
373
374     psz_title_font                = var_InheritString( p_filter, "projectm-title-font" );
375     psz_menu_font                 = var_InheritString( p_filter, "projectm-menu-font" );
376
377     settings.meshX                = var_InheritInteger( p_filter, "projectm-meshx" );
378     settings.meshY                = var_InheritInteger( p_filter, "projectm-meshy" );
379     settings.fps                  = 35;
380     settings.textureSize          = var_InheritInteger( p_filter, "projectm-texture-size" );
381     settings.windowWidth          = p_sys->i_width;
382     settings.windowHeight         = p_sys->i_height;
383     settings.presetURL            = psz_preset_path;
384     settings.titleFontURL         = psz_title_font;
385     settings.menuFontURL          = psz_menu_font;
386     settings.smoothPresetDuration = 5;
387     settings.presetDuration       = 30;
388     settings.beatSensitivity      = 10;
389     settings.aspectCorrection     = 1;
390     settings.easterEgg            = 1;
391     settings.shuffleEnabled       = 1;
392
393     p_projectm = new projectM( settings );
394
395     free( psz_menu_font );
396     free( psz_title_font );
397     free( psz_preset_path );
398 #endif /* HAVE_PROJECTM2 */
399
400     p_sys->i_buffer_size = p_projectm->pcm()->maxsamples;
401     p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
402                                       sizeof( float ) );
403
404     vlc_sem_post( &p_sys->ready );
405
406     /* Choose a preset randomly or projectM will always show the first one */
407     if ( p_projectm->getPlaylistSize() > 0 )
408         p_projectm->selectPreset( (unsigned)vlc_mrand48() % p_projectm->getPlaylistSize() );
409
410     /* */
411     for( ;; )
412     {
413         const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
414         /* Manage the events */
415         vout_ManageDisplay( p_sys->p_vd, true );
416         if( p_sys->p_vd->cfg->display.width  != i_last_width ||
417             p_sys->p_vd->cfg->display.height != i_last_height )
418         {
419             /* FIXME it is not perfect as we will have black bands */
420             vout_display_place_t place;
421             vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
422             p_projectm->projectM_resetGL( place.width, place.height );
423
424             i_last_width  = p_sys->p_vd->cfg->display.width;
425             i_last_height = p_sys->p_vd->cfg->display.height;
426         }
427
428         /* Render the image and swap the buffers */
429         vlc_mutex_lock( &p_sys->lock );
430         if( p_sys->i_nb_samples > 0 )
431         {
432             p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
433                                             p_sys->i_nb_samples );
434             p_sys->i_nb_samples = 0;
435         }
436         if( p_sys->b_quit )
437         {
438             vlc_mutex_unlock( &p_sys->lock );
439
440             delete p_projectm;
441             var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
442             vout_DeleteDisplay( p_sys->p_vd, NULL );
443             vlc_object_release( p_sys->p_vout );
444             if (loc != (locale_t)0)
445             {
446                 uselocale (oldloc);
447                 freelocale (loc);
448             }
449             return NULL;
450         }
451         vlc_mutex_unlock( &p_sys->lock );
452
453         p_projectm->renderFrame();
454
455         /* */
456         mwait( i_deadline );
457
458         if( !vlc_gl_Lock(gl) )
459         {
460             vlc_gl_Swap( gl );
461             vlc_gl_Unlock( gl );
462         }
463     }
464     abort();
465
466 error:
467     p_sys->b_error = true;
468     vlc_sem_post( &p_sys->ready );
469     return NULL;
470 }
471