[vlc-devel] [PATCH] Vovoid VSXu audio visualization integration

Rémi Denis-Courmont remi at remlab.net
Tue Mar 13 09:29:25 CET 2012


diff --git a/configure.ac b/configure.ac
index cc1500e..af780d9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3834,6 +3834,24 @@ AS_IF([test "${enable_projectm}" != "no"],
   ])
 
 dnl
+dnl Vovoid VSXu visualization plugin
+dnl
+AC_ARG_ENABLE(vsxu,
+  [  --enable-vsxu           Vovoid VSXu visualization plugin (default 
enabled)])
+AS_IF([test "${enable_vsxu}" != "no"],
+  [
+    PKG_CHECK_MODULES(VSXU, libvsxu,
+    [
+      VLC_ADD_PLUGIN([vsxu])
+      VLC_ADD_CXXFLAGS([vsxu],[$VSXU_CFLAGS])
+      VLC_ADD_LIBS([vsxu],[$VSXU_LIBS $PROJECTM_LIBS])

I have a feeling this won't work if ProjectM is not present.

+    ],[
+      AC_MSG_WARN([${VSXU_PKG_ERRORS}.])
+    ])
+  ])
+
+
+dnl
 dnl  AtmoLight (homemade Philips Ambilight clone)
 dnl
 AC_ARG_ENABLE(atmo,
diff --git a/modules/visualization/Modules.am 
b/modules/visualization/Modules.am
index 3e62eb0..b67cc6b 100644
--- a/modules/visualization/Modules.am
+++ b/modules/visualization/Modules.am
@@ -1,3 +1,4 @@
 SUBDIRS = visual
 SOURCES_goom = goom.c
 SOURCES_projectm = projectm.cpp
+SOURCES_vsxu = vsxu.cpp
\ No newline at end of file
diff --git a/modules/visualization/cyclic_buffer.h 
b/modules/visualization/cyclic_buffer.h
new file mode 100644
index 0000000..4610aed
--- /dev/null
+++ b/modules/visualization/cyclic_buffer.h
@@ -0,0 +1,99 @@
+/*****************************************************************************
+ * cyclic_buffer.h cyclic buffer helper class for audio visualizations
+ 
*****************************************************************************
+ * Copyright © 2012 Vovoid Media Technologies
+ *
+ * Authors: Jonatan "jaw" Wallmander
+ *
+ * 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 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.
+ 
*****************************************************************************/
+
+#ifndef CYCLIC_BUFFER_H
+#define CYCLIC_BUFFER_H
+
+
+class block_holder
+{
+public:
+    float data[512]; // data holder
+    mtime_t pts; // machine time when this is to be played
+//    size_t size; // data elements written
+    block_holder()
+    {
+        pts = 0; // max_int 64-bit
+//        size = 0;
+    }
+};
+
+#define CYCLIC_BUFFER_SIZE 128
+class cyclic_block_queue
+{
+    block_holder cycl_buffer[CYCLIC_BUFFER_SIZE];
+    size_t consumer_pos;
+    size_t insertion_pos;
+public:
+    cyclic_block_queue()
+    {
+        consumer_pos = 0;
+        insertion_pos = 0;
+    }
+
+    block_holder* consume()
+    {
+        mtime_t cur_machine_time = mdate();
+        size_t steps = 0;
+        while (
+               (cycl_buffer[consumer_pos].pts < cur_machine_time)
+               &&
+               (steps++ < CYCLIC_BUFFER_SIZE)
+              )
+        {
+            consumer_pos++;
+            //printf("----------------------step %f\n",
(float)cycl_buffer[consumer_pos].pts*0.000001f);
+            if (consumer_pos == CYCLIC_BUFFER_SIZE)
+            {
+                consumer_pos = 0;
+            }
+        }
+        //printf("consumer_pos                               %d                     
%f\n",consumer_pos,(float)cur_machine_time*0.000001);
+        //printf("                                                                  
%f\n",(float)cycl_buffer[consumer_pos].pts*0.000001);
+        return &cycl_buffer[consumer_pos];
+    }
+
+  block_holder* get_insertion_object()
+  {
+      insertion_pos++;
+      if ( insertion_pos == CYCLIC_BUFFER_SIZE )
+      {
+          insertion_pos = 0;
+      }
+      //printf("insertion_pos                %d\n",insertion_pos);
+      return &cycl_buffer[insertion_pos];
+  }
+
+  void reset()
+  {
+      for (size_t i = 0; i < CYCLIC_BUFFER_SIZE; i++)
+      {
+          cycl_buffer[i].pts = 0;
+//          cycl_buffer[i].size = 0;
+          consumer_pos = 0;
+          insertion_pos = 0;
+      }
+  }
+};
+#undef CYCLIC_BUFFER_SIZE
+
+#endif // CYCLIC_BUFFER_H
diff --git a/modules/visualization/vsxu.cpp b/modules/visualization/vsxu.cpp
new file mode 100644
index 0000000..13f6c0f
--- /dev/null
+++ b/modules/visualization/vsxu.cpp
@@ -0,0 +1,466 @@
+/*****************************************************************************
+ * vsxu.cpp: visualization module wrapper for Vovoid VSXu
+ 
*****************************************************************************
+ * Copyright © 2009-2011 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Rémi Duraffort <ivoire at videolan.org>
+ *          Laurent Aimar
+ *          Jonatan "jaw" Wallmander
+ *
+ * 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 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.
+ 
*****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#ifndef __STDC_CONSTANT_MACROS
+# define __STDC_CONSTANT_MACROS
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_aout.h>
+#include <vlc_vout.h>
+#include <vlc_vout_wrapper.h>
+#include <vlc_opengl.h>
+#include <vlc_filter.h>
+#include <vlc_rand.h>
+
+#include <string>
+#include <stdlib.h>
+#include <sys/time.h>

You should not need that.

+
+// vsxu manager include
+#include <vsx_manager.h>
+#include <logo_intro.h>
+
+// class to handle cyclic buffer
+#include "cyclic_buffer.h"
+
+/*****************************************************************************
+ * Module descriptor
+ 
*****************************************************************************/
+static int  Open         ( vlc_object_t * );
+static void Close        ( vlc_object_t * );
+
+#define CONFIG_TEXT N_("vsxu share path")
+#define CONFIG_LONGTEXT N_("Path to vsxu data folder")
+
+#define WIDTH_TEXT N_("Video width")
+#define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
+
+#define HEIGHT_TEXT N_("Video height")
+#define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
+
+vlc_module_begin ()
+    set_shortname( N_("vsxu"))
+    set_description( N_("vsxu") )
+    set_capability( "visualization2", 0 )
+    set_category( CAT_AUDIO )
+    set_subcategory( SUBCAT_AUDIO_VISUAL )
+    add_directory( "vsxu-asset-path", "/usr/share/vsxu",

This should probably be read from the .pc file rather than hard-coded.

+                  CONFIG_TEXT, CONFIG_LONGTEXT, true )
+    add_integer( "vsxu-width", 1280, WIDTH_TEXT, WIDTH_LONGTEXT,
+                 false )
+    add_integer( "vsxu-height", 720, HEIGHT_TEXT, HEIGHT_LONGTEXT,
+                 false )
+    add_shortcut( "vsxu" )
+    set_callbacks( Open, Close )
+vlc_module_end ()
+
+
+/*****************************************************************************
+ * Local prototypes
+ 
*****************************************************************************/
+struct filter_sys_t
+{
+    /* */
+    vlc_thread_t thread;
+    vlc_sem_t    ready;
+    bool         b_error;
+
+    /* Opengl */
+    vout_thread_t  *p_vout;
+    vout_display_t *p_vd;
+
+    /* Window size */
+    int i_width;
+    int i_height;
+
+    /* audio info */
+    int i_channels;
+
+    /* */
+    vlc_mutex_t lock;
+    bool  b_quit;
+    float *p_buffer;
+    unsigned i_buffer_size;
+    unsigned i_nb_samples;
+};
+
+
+static block_t *DoWork( filter_t *, block_t * );
+static void *Thread( void * );
+
+/**
+ * Open the module
+ * @param p_this: the filter object
+ * @return VLC_SUCCESS or vlc error codes
+ */
+static int Open( vlc_object_t * p_this )
+{
+    filter_t     *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys;
+
+    /* Test the audio format */
+    if( 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 
) )
+    {
+        msg_Warn( p_filter, "input and outut are not similar" );
+        return VLC_EGENERIC;
+    }
+
+    p_filter->pf_audio_filter = DoWork;
+
+    p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
+
+    /* Create the object for the thread */
+    vlc_sem_init( &p_sys->ready, 0 );
+    p_sys->b_error       = false;
+    p_sys->b_quit        = false;
+    p_sys->i_width       = var_InheritInteger( p_filter, "vsxu-width" );
+    p_sys->i_height      = var_InheritInteger( p_filter, "vsxu-height" );
+    p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
+    vlc_mutex_init( &p_sys->lock );
+    p_sys->p_buffer      = NULL;
+    p_sys->i_buffer_size = 0;
+    p_sys->i_nb_samples  = 0;
+
+    /* Create the thread */
+    if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW 
) )
+        goto error;
+
+    vlc_sem_wait( &p_sys->ready );
+    if( p_sys->b_error )
+    {
+        vlc_join( p_sys->thread, NULL );
+        goto error;
+    }
+
+    return VLC_SUCCESS;
+
+error:
+    vlc_sem_destroy( &p_sys->ready );
+    free (p_sys );
+    return VLC_EGENERIC;
+}
+
+
+/**
+ * Close the module
+ * @param p_this: the filter object
+ */
+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;
+
+    /* Stop the thread
+     * XXX vlc_cleanup_push does not seems to work with C++ so no
+     * vlc_cancel()... */
+    vlc_mutex_lock( &p_sys->lock );
+    p_sys->b_quit = true;
+    vlc_mutex_unlock( &p_sys->lock );
+
+    vlc_join( p_sys->thread, NULL );
+
+    /* Free the ressources */
+    vlc_sem_destroy( &p_sys->ready );
+    vlc_mutex_destroy( &p_sys->lock );
+    free( p_sys->p_buffer );
+    free( p_sys );
+}
+
+
+// our abstract holder
+vsx_manager_abs* manager;
+
+// keep track of first frame
+bool first = true;
+
+// keep track of iterations
+int i_iterations = 0;
+
+// mutex around the cyclic block
+vlc_mutex_t cyclic_block_mutex;
+
+// cyclic buffer to cache sound frames in
+cyclic_block_queue vsxu_cyclic_buffer;
+
+// small logo intro
+vsx_logo_intro* intro;

Unless this data is shared across all concurrent instances of the 
visualization, it shouldn't be static.


+
+/**
+ * Do the actual work with the new sample
+ * @param p_filter: filter object
+ * @param p_in_buf: input buffer
+ */
+static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
+{
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    vlc_mutex_lock( &p_sys->lock );
+    vlc_mutex_lock( &cyclic_block_mutex );
+
+    if( p_sys->i_buffer_size > 0 )
+    {
+        p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
+                                     p_in_buf->i_nb_samples );
+        //printf("dowork in samples: %d\n",p_in_buf->i_nb_samples);
+
+        const float *p_src = (float*)p_in_buf->p_buffer;
+        // iterate block holder
+        size_t i_bh_data_iter = 0;
+
+        // calc 512-byte-aligned sample count to grab, we don't need more
+        unsigned i_num_samples = p_sys->i_nb_samples - p_sys->i_nb_samples % 
512;
+        //printf("num samples: %d\n",num_samples);
+
+        // muls are cheaper than divs
+        float f_onedivchannels = 1.0f / p_sys->i_channels;
+
+        block_holder* p_block_holder = 
vsxu_cyclic_buffer.get_insertion_object();
+        p_block_holder->pts = p_in_buf->i_pts;
+        for( unsigned i = 0; i < i_num_samples; i++ )
+        {
+            float f_v = 0;
+            for( int j = 0; j < p_sys->i_channels; j++ )
+            {
+                f_v += p_src[p_sys->i_channels * i + j];
+            }
+
+            // insert into our little cyclic buffer
+            p_block_holder->data[i_bh_data_iter] = f_v * f_onedivchannels;
+            i_bh_data_iter++;
+            if (i_bh_data_iter == 512 && i < i_num_samples-256)
+            {
+                //printf("starting on next 512\n");
+                p_block_holder = vsxu_cyclic_buffer.get_insertion_object();
+                p_block_holder->pts = p_in_buf->i_pts + 11609;
+                //printf("eob %f\n",(float)p_block_holder->pts*0.000001);
+
+                i_bh_data_iter = 0;
+            }
+            p_sys->p_buffer[i] = f_v * f_onedivchannels;
+        }
+        //printf("eob %f\n",(float)bh->pts*0.000001);
+    }
+
+    vlc_mutex_unlock( &cyclic_block_mutex );
+    vlc_mutex_unlock( &p_sys->lock );
+    return p_in_buf;
+}
+
+/**
+ * Variable callback for the dummy vout
+ */
+static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
+                         vlc_value_t oldv, vlc_value_t newv, void *p_data )
+{
+    VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
+    vout_display_t *p_vd = (vout_display_t*)p_data;
+
+    if( !strcmp(psz_name, "fullscreen") )
+    {
+        vout_SetDisplayFullscreen( p_vd, newv.b_bool );
+    }
+    return VLC_SUCCESS;
+}
+
+/**
+ * VSXu update thread which do the rendering
+ * @param p_this: the p_thread object
+ */
+static void *Thread( void *p_data )
+{
+    filter_t  *p_filter = (filter_t*)p_data;
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    video_format_t fmt;
+    vlc_gl_t *gl;
+    unsigned int i_last_width  = 0;
+    unsigned int i_last_height = 0;
+
+    char *psz_asset_path;
+
+    vlc_savecancel();
+
+    /* Create the openGL provider */
+    p_sys->p_vout =
+        (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) 
);
+    if( !p_sys->p_vout )
+        goto error;
+
+    /* */
+    video_format_Init( &fmt, 0 );
+    video_format_Setup( &fmt, VLC_CODEC_RGB32,
+                        p_sys->i_width, p_sys->i_height, 0, 1 );
+    fmt.i_sar_num = 1;
+    fmt.i_sar_den = 1;
+
+    vout_display_state_t state;
+    memset( &state, 0, sizeof(state) );
+    state.cfg.display.sar.num = 1;
+    state.cfg.display.sar.den = 1;
+    state.cfg.is_display_filled = true;
+    state.cfg.zoom.num = 1;
+    state.cfg.zoom.den = 1;
+    state.sar.num = 1;
+    state.sar.den = 1;
+
+    p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
+                                   300000, 1000000 );
+    if( !p_sys->p_vd )
+    {
+        vlc_object_release( p_sys->p_vout );
+        goto error;
+    }
+    var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
+    var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd 
);
+
+    gl = vout_GetDisplayOpengl( p_sys->p_vd );
+    if( !gl )
+    {
+        var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys-
>p_vd );
+        vout_DeleteDisplay( p_sys->p_vd, NULL );
+        vlc_object_release( p_sys->p_vout );
+        goto error;
+    }
+
+    psz_asset_path = var_InheritString( p_filter, "vsxu-asset-path" );
+
+    p_sys->i_buffer_size = 1024;
+    p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
+                                      sizeof( float ) );
+    vlc_sem_post( &p_sys->ready );
+
+    for( ;; )
+    {
+        /* Manage the events */
+        vout_ManageDisplay( p_sys->p_vd, true );
+        if( p_sys->p_vd->cfg->display.width  != i_last_width ||
+            p_sys->p_vd->cfg->display.height != i_last_height )
+        {
+            /* FIXME it is not perfect as we will have black bands */
+            vout_display_place_t place;
+            vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys-
>p_vd->cfg, false );
+
+            i_last_width  = p_sys->p_vd->cfg->display.width;
+            i_last_height = p_sys->p_vd->cfg->display.height;
+        }
+
+        /* Render the image and swap the buffers */
+        vlc_mutex_lock( &p_sys->lock );
+        //printf("number of samples gotten: %d\n",p_sys->i_nb_samples);
+        if( p_sys->i_nb_samples > 0 )
+        {
+            p_sys->i_nb_samples = 0;
+        }
+        if( p_sys->b_quit )
+        {
+            vlc_mutex_unlock( &p_sys->lock );
+
+            // stop vsxu
+            manager->stop();
+            // call abstract factory to destruct our manager object
+            manager_destroy( manager );
+
+            var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, 
p_sys->p_vd );
+
+            // clean out vlc opengl stuff
+            vout_DeleteDisplay( p_sys->p_vd, NULL );
+            vlc_object_release( p_sys->p_vout );
+
+            // clean up the cyclic buffer
+            vlc_mutex_lock( &cyclic_block_mutex );
+                vsxu_cyclic_buffer.reset();
+            vlc_mutex_unlock( &cyclic_block_mutex );
+
+            // delete the intro
+            delete intro;
+            // reset first
+            first = true;
+            // die
+            return NULL;
+        }
+        vlc_mutex_unlock( &p_sys->lock );
+
+        if (first)
+        {
+            // only run this once
+            first = false;
+            // create a new manager
+            manager = manager_factory();
+
+            // init manager with the shared path and sound input type.
+            manager->init( psz_asset_path, "media_player" );
+
+            // only show logo once
+            if ( i_iterations++ < 1 )
+            {
+                intro = new vsx_logo_intro;
+                intro->set_destroy_textures( false );
+            } else
+            {
+                intro = 0;
+            }
+        }
+
+        // lock mutex and copy floats
+        vlc_mutex_lock( &cyclic_block_mutex );
+            block_holder* bh = vsxu_cyclic_buffer.consume();
+            float f_sample_buf[512];
+            memcpy( &f_sample_buf[0], (void*)(&bh->data[0]), sizeof(float) * 
512 );
+        vlc_mutex_unlock( &cyclic_block_mutex );
+
+        manager->set_sound_wave( &f_sample_buf[0] );
+
+        // render
+        if (manager) manager->render();
+        if (intro) intro->draw();
+
+        // swap buffers etc.
+        if( !vlc_gl_Lock(gl) )
+        {
+            vlc_gl_Swap( gl );
+            vlc_gl_Unlock( gl );
+        }
+    }
+    abort();
+
+error:
+    p_sys->b_error = true;
+    vlc_sem_post( &p_sys->ready );
+    return NULL;
+}
+
diff --git a/src/audio_output/common.c b/src/audio_output/common.c
index b8900ab..89098fa 100644
--- a/src/audio_output/common.c
+++ b/src/audio_output/common.c
@@ -110,6 +110,13 @@ audio_output_t *aout_New( vlc_object_t * p_parent )
         text.psz_string = (char*)"projectM";
         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
     }
+    /* Look for VSXu plugin */
+    if (module_exists ("vsxu"))
+    {
+        val.psz_string = (char *)"vsxu";
+        text.psz_string = (char*)"Vovoid VSXu";
+        var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
+    }
     str = var_GetNonEmptyString (aout, "effect-list");
     if (str != NULL)
     {
diff --git a/src/audio_output/input.c b/src/audio_output/input.c
index 42b67b7..228132b 100644
--- a/src/audio_output/input.c
+++ b/src/audio_output/input.c
@@ -615,18 +615,28 @@ static int VisualizationCallback (vlc_object_t *obj, 
char const *var,
         ChangeFiltersString (obj, "audio-visual", "goom", false);
         ChangeFiltersString (obj, "audio-visual", "visual", false);
         ChangeFiltersString (obj, "audio-visual", "projectm", false);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
     }
     else if (!strcmp ("goom", mode))
     {
         ChangeFiltersString (obj, "audio-visual", "visual", false );
         ChangeFiltersString (obj, "audio-visual", "goom", true );
         ChangeFiltersString (obj, "audio-visual", "projectm", false );
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
     }
     else if (!strcmp ("projectm", mode))
     {
         ChangeFiltersString (obj, "audio-visual", "visual", false);
         ChangeFiltersString (obj, "audio-visual", "goom", false);
         ChangeFiltersString (obj, "audio-visual", "projectm", true);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
+    }
+    else if (!strcmp ("vsxu", mode))
+    {
+        ChangeFiltersString (obj, "audio-visual", "visual", false);
+        ChangeFiltersString (obj, "audio-visual", "goom", false);
+        ChangeFiltersString (obj, "audio-visual", "projectm", false);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", true);
     }
     else
     {
-- 
1.7.5.4

-- 
Rémi Denis-Courmont
http://www.remlab.net/
http://fi.linkedin.com/in/remidenis



More information about the vlc-devel mailing list