Eseguire un file Midi mediante le funzioni esterne del API di GStreamer

Da Gambas-it.org - Wikipedia.

Mostriamo un esempio di esecuzione di un file Midi con un'applicazione Gambas a riga di comando mediante l'uso delle risorse esterne della libreria condivisa: "libgstreamer-1.0.so.0.2407.0 ".

Library "libgstreamer-1.0:0.2407.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING
Private Const GST_FORMAT_TIME As Integer = 3

' gst_init (int *argc, char **argv[])
' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Private Extern gst_init(argc As Pointer, argv As Pointer)

' GstElement * gst_element_factory_make(const gchar *factoryname, Const gchar * name)
' Create a new element of the type defined by the given element factory.
Private Extern gst_element_factory_make(factoryname As String, name As String) As Pointer

' gchar * g_filename_to_uri (const gchar *filename, const gchar *hostname, GError **error)
' Converts an absolute filename to an escaped ASCII-encoded URI.
Private Extern g_filename_to_uri(filename As String, hostname As String, GError As Pointer) As String

' void g_object_set(gpointer object, const gchar *first_property_name, ...)
' Sets properties on an object.
Private Extern g_object_set(gobject As Pointer, property_name As String, value_1 As String, value_2 As String, value_3 As Float)

' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)
' Sets the state of the element.
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer

' gboolean gst_element_query_duration (GstElement *element, GstFormat format, gint64 *duration)
' Queries an element for the total stream duration in nanoseconds.
Private Extern gst_element_query_duration(gstelement As Pointer, gstformat As Pointer, duration As Pointer) As Boolean
 
' void gst_object_unref(gpointer object)
' Decrements the reference count on object .
Private Extern gst_object_unref(gobject As Pointer)


Public Sub Main()

 Dim midi As Pointer
 Dim filemidi as string
 Dim durata, l As Long
 Dim volume As Float
 Dim tm As Date

 gst_init(0, 0)
 
 midi = gst_element_factory_make("playbin", "riproduzione")
 If midi == 0 Then Error.Raise("Errore !")

 filemidi = "/percorso/del/file.mid"

' Imposta il percorso del file Midi da eseguire:
 g_object_set(midi, "uri", g_filename_to_uri(filemidi, Null, 0), Null, 0.0)
  
' Imposta il valore del volume (da 0.0 a 10.0) di esecuzione:
 volume = 0.7
 g_object_set(midi, "volume", Null, Null, volume)
  
' Avvia la riproduzione del file Midi:
 gst_element_set_state(midi, GST_STATE_PLAYING)
 
 Repeat
   gst_element_query_duration(midi, GST_FORMAT_TIME, VarPtr(durata))
   Wait 0.01
 Until durata > -1

 tm = Now
 
 Repeat
   l = DateDiff(tm, Now, gb.Millisecond)
   Write "\r\e[0mDurata: " & Time(0, 0, 0, durata / 1000000) & "      Pos. \e[31m" & Time(0, 0, 0, l)
   Wait 0.001
 Until l >= durata / 1000000

' Va in chiusura e libera la memoria precedentemente allocata:
 gst_object_unref(midi)
 Print "\nEsecuzione terminata"
  
End

Va fatto notare che le due proprietà ("uri" e "volume") del plugin playbin potrebbero essere invocate con un'unica funzione g_object_set(). In tal caso della funzione esterna di GStreamer andrebbe dichiarata sostanzialmente come segue con 6 argomenti (il 2° ed il 3° relativi alla propietà "uri", mentre il 4°, il 5° ed il 6° relativi alla proprietà "volume"):

Private Extern g_object_set(gobject As Pointer, property_1 As String, value As String, Property_2 As String, value1 As String, value2 As Float)

e nel codice sarebbe così utilizzata:

g_object_set(midi, "uri", g_filename_to_uri(fileaudio, Null, 0), "volume", Null, volume)


Riferimenti