Differenze tra le versioni di "La gestione dei file MIDI mediante le funzioni esterne del API di VLC"

Da Gambas-it.org - Wikipedia.
Riga 13: Riga 13:
 
  Private inst As Pointer
 
  Private inst As Pointer
 
  Private mp As Pointer
 
  Private mp As Pointer
 
+
Private m As Pointer
 +
 
   
 
   
 
  Library "libvlc:5.5.0"
 
  Library "libvlc:5.5.0"
Riga 65: Riga 66:
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 +
 +
<FONT color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)''
 +
' ''Decrement the reference count of a media descriptor object.''</font>
 +
Private Extern libvlc_media_release(p_md As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
Riga 72: Riga 77:
 
   
 
   
 
  '''Public''' Sub Button1_Click()
 
  '''Public''' Sub Button1_Click()
 
  Dim m As Pointer
 
 
 
 
      
 
      
 
  <FONT color=gray>' ''Inizializza la libreria VLC:''</font>
 
  <FONT color=gray>' ''Inizializza la libreria VLC:''</font>
Riga 109: Riga 111:
 
  '''Private''' Procedure Chiude()
 
  '''Private''' Procedure Chiude()
 
   
 
   
  <FONT color=gray>' ''Rilascia e chiude il media player:''</font>
+
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
   libvlc_media_player_release(mp)
 
   libvlc_media_player_release(mp)
    
+
   libvlc_media_release(m)
<FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
 
   libvlc_release(inst)
 
   libvlc_release(inst)
   
+
 
 
  '''End'''
 
  '''End'''
 
   
 
   
Riga 185: Riga 186:
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 +
 +
<FONT Color=gray>' ''void libvlc_media_player_release (libvlc_media_player_t * p_mi)''
 +
' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 +
Private Extern libvlc_media_player_release(p_mi As Pointer)
 +
 +
<FONT color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)''
 +
' ''Decrement the reference count of a media descriptor object.''</font>
 +
Private Extern libvlc_media_release(p_md As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
Riga 238: Riga 247:
 
  '''Private''' Procedure Chiude()
 
  '''Private''' Procedure Chiude()
 
      
 
      
  <FONT color=gray>' ''Rilascia e chiude il media player:''</font>
+
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
   libvlc_media_player_release(mp)
 
   libvlc_media_player_release(mp)
      
+
     libvlc_media_release(m)
<FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
 
   libvlc_release(inst)
 
   libvlc_release(inst)
 
      
 
      
Riga 295: Riga 303:
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 
  Private Extern libvlc_media_player_release(p_mi As Pointer)
 +
 +
<FONT color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)''
 +
' ''Decrement the reference count of a media descriptor object.''</font>
 +
Private Extern libvlc_media_release(p_md As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
 
  <FONT color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)''
Riga 306: Riga 318:
 
        
 
        
 
  <FONT color=gray>' ''Inizializza la libreria VLC:''</font>
 
  <FONT color=gray>' ''Inizializza la libreria VLC:''</font>
    inst = libvlc_new(0, Null)
+
  inst = libvlc_new(0, Null)
 
      
 
      
 
  <FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font>
 
  <FONT color=gray>' ''Crea un nuovo oggetto multimedia:''</font>
    m = libvlc_media_new_path(inst, "<FONT color=gray>''/percorso/del/file.mid''</font>")
+
  m = libvlc_media_new_path(inst, "<FONT color=gray>''/percorso/del/file.mid''</font>")
 
      
 
      
 
  <FONT color=gray>' ''Crea un media player:''</font>
 
  <FONT color=gray>' ''Crea un media player:''</font>
    mp = libvlc_media_player_new_from_media(m)
+
  mp = libvlc_media_player_new_from_media(m)
 
      
 
      
 
   <FONT color=gray>' ''Avvia l'esecuzione del file Midi da parte del media player:''</font>
 
   <FONT color=gray>' ''Avvia l'esecuzione del file Midi da parte del media player:''</font>
    libvlc_media_player_play(mp)
+
  libvlc_media_player_play(mp)
+
 
 
   While libvlc_media_player_get_state(mp) < libvlc_Stopped
 
   While libvlc_media_player_get_state(mp) < libvlc_Stopped
 
     Write #File.Out, Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp))) & "\r"
 
     Write #File.Out, Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp))) & "\r"
 
   Wend
 
   Wend
+
 
+
 
<FONT color=gray>' ''Va in chiusura:''</font>
 
 
<FONT color=gray>' ''Rilascia e chiude il media player:''</font>
 
    libvlc_media_player_release(mp)
 
   
 
 
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>
 
  <FONT color=gray>' ''Chiude la libreria VLC:''</font>
    libvlc_release(inst)
+
  libvlc_media_player_release(mp)
+
  libvlc_media_release(m)
 +
  libvlc_release(inst)
 +
 
 
  '''End'''
 
  '''End'''
  

Versione delle 03:55, 31 lug 2017

La libreria LibVLC consente di eseguire anche file MIDI, purché si abbiano installati nel sistema anche:

  • vlc-plugin-fluidsynth - le risorse dei plugin di FluidSynth per VLC;
  • fluid-soundfont-gm - Fluid (R3) General MIDI SoundFont (GM);
  • ovviamente lo stesso FluidSynth.

E' possibile eseguire dati Midi anche direttamente da internet.

Per creare in Gambas un'applicazione con la presente risorsa, si dovrà installare e richiamare la libreria dinamica e condivisa "libvlc.so.5.5.0".


Esempio con applicazione grafica

Mostriamo di seguito un semplice esempio, nel quale si potrà avviare, arrestare, porre in pausa e riprendere l'esecuzione del file Midi:

Private inst As Pointer
Private mp As Pointer
Private m As Pointer


Library "libvlc:5.5.0"

Private Enum libvlc_NothingSpecial = 0,
        libvlc_Opening,
        libvlc_Buffering,
        libvlc_Playing,
        libvlc_Paused,
        libvlc_Stopped,
        libvlc_Ended,
        libvlc_Error

' libvlc_instance_t * libvlc_new (int argc, const char *const *argv)
' Create And initialize a libvlc instance.
Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer

' libvlc_media_t * libvlc_media_new_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' int libvlc_media_player_play (libvlc_media_player_t * p_mi)
' Play the audio file.
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer

' void libvlc_media_player_stop (libvlc_media_player_t * p_mi)
' Stop the audio file.
Private Extern libvlc_media_player_stop(p_mi As Pointer)

' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)
' Get the current movie length (in ms).
Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Integer

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer

' void   libvlc_media_player_pause (libvlc_media_player_t *p_mi)
' Toggle pause.
Private Extern libvlc_media_player_pause(p_mi As Pointer)

' void libvlc_media_player_release (libvlc_media_player_t * p_mi)
' Release a media_player after use Decrement the reference count of a media player object.
Private Extern libvlc_media_player_release(p_mi As Pointer)

' void libvlc_media_release (libvlc_media_t *p_md)
' Decrement the reference count of a media descriptor object.
Private Extern libvlc_media_release(p_md As Pointer)

' libvlc_release (libvlc_instance_t * p_instance)
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
Private Extern libvlc_release(p_instance As Pointer)
 

Public Sub Button1_Click()
   
' Inizializza la libreria VLC:
  inst = libvlc_new(0, Null)
   
' Crea un nuovo oggetto multimedia:
  m = libvlc_media_new_path(inst, "/percorso/del/file.mid")
   
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
   
 ' Avvia l'esecuzione del file Midi da parte del media player:
  libvlc_media_player_play(mp)

  Do
    TextLabel1.Text = "Durata: " & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_length(mp, 0)))
    TextLabel2.Text = "<FONT Color=red>" & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
    Wait 0.01
  Loop Until libvlc_media_player_get_state(mp) > libvlc_Paused

  Chiude()

End


Public Sub Button2_Click()

' Arresta l'esecuzione del file Midi:
  libvlc_media_player_stop(mp)

End


Private Procedure Chiude()

' Chiude la libreria VLC:
  libvlc_media_player_release(mp)
  libvlc_media_release(m)
  libvlc_release(inst)
 
End


Public Sub ToggleButton1_Click()

' Pone in pausa o riprende (se già in pausa) l'esecuzione del file Midi:
  libvlc_media_player_pause(mp)

End


In quest'altro esempio mostreremo un effetto video (Spectrum analyzer) di VLC, che rappresenta un "Analizzatore di spettro", all'interno di una DrawingArea

Private inst As Pointer
Private mp As Pointer
 

Library "libvlc:5.5.0"

Private Enum libvlc_NothingSpecial = 0,
        libvlc_Opening,
        libvlc_Buffering,
        libvlc_Playing,
        libvlc_Paused,
        libvlc_Stopped,
        libvlc_Ended,
        libvlc_Error

' libvlc_instance_t * libvlc_new (int argc, const char *const *argv)
' Create And initialize a libvlc instance.
Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer

' libvlc_media_t * libvlc_media_new_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable)
' Set an X Window System drawable where the media player should render its video output.
Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer)

' int libvlc_media_player_play (libvlc_media_player_t * p_mi)
' Play the audio file.
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer

' void libvlc_media_player_stop (libvlc_media_player_t * p_mi)
' Stop the audio file.
Private Extern libvlc_media_player_stop(p_mi As Pointer)

' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *)
' Get the current movie length (in ms).
Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex As Pointer) As Integer

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer

' void   libvlc_media_player_pause (libvlc_media_player_t *p_mi)
' Toggle pause.
Private Extern libvlc_media_player_pause(p_mi As Pointer)

' void libvlc_media_player_release (libvlc_media_player_t * p_mi)
' Release a media_player after use Decrement the reference count of a media player object.
Private Extern libvlc_media_player_release(p_mi As Pointer)

' void libvlc_media_player_release (libvlc_media_player_t * p_mi)
' Release a media_player after use Decrement the reference count of a media player object.
Private Extern libvlc_media_player_release(p_mi As Pointer)

' void libvlc_media_release (libvlc_media_t *p_md)
' Decrement the reference count of a media descriptor object.
Private Extern libvlc_media_release(p_md As Pointer)

' libvlc_release (libvlc_instance_t * p_instance)
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
Private Extern libvlc_release(p_instance As Pointer)


Public Sub Button1_Click()

 Dim ss As String[]
 Dim m As Pointer
 Dim id As Integer
  
  ss = ["--audio-visual=visual", "--effect-list=spectrum"]
  
' Inizializza la libreria VLC:
  inst = libvlc_new(ss.Count, ss)
   
' Crea un nuovo oggetto multimedia:
  m = libvlc_media_new_path(inst, "/percorso/del/file.mid")
   
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
  
' Per far mostrare l'effetto video "Analizzatore di spettro" nella "DrawingArea", ricaviamo il suo identificativo:
  id = DrawingArea1.Id
  
' Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato l'effetto "Spectrum analyzer":
  libvlc_media_player_set_xwindow(mp, id)
   
 ' Avvia l'esecuzione del file Midi da parte del media player:
  libvlc_media_player_play(mp)

  Do
    TextLabel1.Text = "Durata: " & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_length(mp, 0)))
    TextLabel2.Text = "<FONT Color=red>" & Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
    Wait 0.01
  Loop Until libvlc_media_player_get_state(mp) > libvlc_Paused

  Chiude()

End


Public Sub Button2_Click()

' Arresta l'esecuzione del file Midi:
  libvlc_media_player_stop(mp)

End


Private Procedure Chiude()
   
' Chiude la libreria VLC:
  libvlc_media_player_release(mp)
    libvlc_media_release(m)
  libvlc_release(inst)
   
End


Public Sub ToggleButton1_Click()

' Pone in pausa o riprende (se già in pausa) l'esecuzione del file Midi:
  libvlc_media_player_pause(mp)

End


Esempio con applicazione a riga di comando

Library "libvlc:5.5.0"

Enum libvlc_NothingSpecial = 0,
     libvlc_Opening,
     libvlc_Buffering,
     libvlc_Playing,
     libvlc_Paused,
     libvlc_Stopped,
     libvlc_Ended,
     libvlc_Error

' libvlc_instance_t * libvlc_new (int argc, const char *const *argv)
' Create And initialize a libvlc instance.
Private Extern libvlc_new(argc As Integer, argv As String) As Pointer

' libvlc_media_t * libvlc_media_new_path (libvlc_instance_t *p_instance, const char *path)
' Create a media for a certain Midi file path.
Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md)
' Create a Media Player object from a Media.
Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer

' int libvlc_media_player_play (libvlc_media_player_t * p_mi)
' Play the Midi file.
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer

' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi)
' Get current movie state.
Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer
 
' void libvlc_media_player_release (libvlc_media_player_t * p_mi)
' Release a media_player after use Decrement the reference count of a media player object.
Private Extern libvlc_media_player_release(p_mi As Pointer)

' void libvlc_media_release (libvlc_media_t *p_md)
' Decrement the reference count of a media descriptor object.
Private Extern libvlc_media_release(p_md As Pointer)

' libvlc_release (libvlc_instance_t * p_instance)
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
Private Extern libvlc_release(p_instance As Pointer)


Public Sub Main()

 Dim m, inst, mp As Pointer
      
' Inizializza la libreria VLC:
  inst = libvlc_new(0, Null)
   
' Crea un nuovo oggetto multimedia:
  m = libvlc_media_new_path(inst, "/percorso/del/file.mid")
   
' Crea un media player:
  mp = libvlc_media_player_new_from_media(m)
   
 ' Avvia l'esecuzione del file Midi da parte del media player:
  libvlc_media_player_play(mp)
  
  While libvlc_media_player_get_state(mp) < libvlc_Stopped
    Write #File.Out, Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp))) & "\r"
  Wend
  
  
' Chiude la libreria VLC:
  libvlc_media_player_release(mp)
  libvlc_media_release(m)
  libvlc_release(inst)
  
End


Riferimenti