Ottenere la durata di un file Midi con le funzioni esterne del API di VLC

Da Gambas-it.org - Wikipedia.

Per ottenere la durata di un file Midi con l'API di VLC, è possibile utilizzare due funzioni esterne.

E' necessario avere installata nel sistema e richiamare nel programma Gambas la libreria condivisa: "libvlc.so.5.6.1 ".

Uso della funzione esterna "libvlc_media_get_duration()"

Mostriamo un esempio con il semplice uso della funzione esterna "libvlc_media_get_duration()":

Library "libvlc:5.6.1"

Private Enum libvlc_media_parse_local = 0,
             libvlc_media_parse_network,
             libvlc_media_fetch_local,
             libvlc_media_fetch_network = 4,
             libvlc_media_do_interact = 8

' 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

' int libvlc_media_parse_with_options (libvlc_media_t *p_md, libvlc_media_parse_flag_t parse_flag, int timeout)
' Parse the media asynchronously with options.
Private Extern libvlc_media_parse_with_options(p_md As Pointer, parse_flag As Integer, timeout As Integer) As Integer

' libvlc_time_t libvlc_media_get_duration(libvlc_media_t *p_md)
' Get duration (in ms) of media descriptor object item.
Private Extern libvlc_media_get_duration(p_md As Pointer) As Long

' 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 inst, md As Pointer
 Dim tm As Long
 
 inst = libvlc_new(0, Null)
 
 md = libvlc_media_new_path(inst, "/percorso/del/file.mid")
 
 libvlc_media_parse_with_options(md, libvlc_media_parse_local, 0)
 
 Repeat
   tm = libvlc_media_get_duration(md)
   Wait 0.01
 Until tm > 0
 
 Print "Durata: "; Time(0, 0, 0, tm)
 
 libvlc_media_release(md)
 libvlc_release(inst)
 
End


Uso della funzione esterna "libvlc_media_player_get_length()"

Si potrà utilizzare anche la funzione "libvlc_media_player_get_length()". L'uso di tale funzione esterna, però, è preferibile nel caso di contestuale esecuzione di un file Midi.

Mostriamo un esempio pratico, nel quale si eseguirà un file Midi e si otterrà in Console - oltre che il tempo trascorso dall'inizio dell'esecuzione - anche la sua durata:

Library "libvlc:5.6.1"

' 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

' 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

' 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 inst, md, mp As Pointer
 Dim lg, dd As Integer
 Dim tm As Date
 
 inst = libvlc_new(0, Null)

 md = libvlc_media_new_path(inst, "/percorso/del/file.mid")
 
' Crea un media player:
 mp = libvlc_media_player_new_from_media(md)

' Esegue il file Midi:
 libvlc_media_player_play(mp)
 
 Repeat
   lg = libvlc_media_player_get_length(mp, 0)
   Wait 0.01
 Until lg > 0
 
 tm = Now

 Repeat 
   dd = DateDiff(tm, Now, gb.Millisecond)
   Write "\e[0m\r" & "Durata: " & Str(Time(0, 0, 0, lg)) &
         " - \e[31m" & Str(Time(0, 0, 0, dd))
 Until dd >= lg

 libvlc_media_player_release(mp)
 libvlc_media_release(md)
 libvlc_release(inst)

End