Differenze tra le versioni di "Eseguire un file Midi mediante le funzioni esterne del API di GStreamer"

Da Gambas-it.org - Wikipedia.
Riga 28: Riga 28:
 
  ' ''Queries an element for the total stream duration in nanoseconds.''</font>
 
  ' ''Queries an element for the total stream duration in nanoseconds.''</font>
 
  Private Extern gst_element_query_duration(gstelement As Pointer, gstformat As Pointer, duration As Pointer) As Boolean
 
  Private Extern gst_element_query_duration(gstelement As Pointer, gstformat As Pointer, duration As Pointer) As Boolean
 +
 +
<FONT color=gray>' ''gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *duration)''
 +
' ''Queries an element for the total stream position in nanoseconds.''</font>
 +
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, position As Pointer) As Boolean
 
   
 
   
 
  <FONT color=gray>' ''void gst_object_unref(gpointer object)''
 
  <FONT color=gray>' ''void gst_object_unref(gpointer object)''
Riga 34: Riga 38:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
 
   Dim elem As Pointer
 
   Dim elem As Pointer
 
   Dim filemidi as string
 
   Dim filemidi as string
   Dim durata As Long
+
   Dim durata, pos As Long
  Dim dur, tempus As Date
 
  Dim dd As Integer
 
 
   Dim volume As Float
 
   Dim volume As Float
 
   
 
   
Riga 48: Riga 50:
 
   If elem == 0 Then Error.Raise("Errore !")
 
   If elem == 0 Then Error.Raise("Errore !")
 
   
 
   
   filemidi = "<FONT color=gray>''/percorso/del/file.mid''</font>"
+
   filemidi = "<FONT color=darkgreen>''/percorso/del/file.mid''</font>"
 
   
 
   
 
  <FONT color=gray>' ''Imposta il percorso del file Midi da eseguire:''</font>
 
  <FONT color=gray>' ''Imposta il percorso del file Midi da eseguire:''</font>
Riga 62: Riga 64:
 
   Repeat
 
   Repeat
 
     gst_element_query_duration(elem, GST_FORMAT_TIME, VarPtr(durata))
 
     gst_element_query_duration(elem, GST_FORMAT_TIME, VarPtr(durata))
     dur = Date(0, 0, 0, 0, 0, 0, durata / 1000000)
+
     Wait 0.01
   Until dur > 0
+
   Until durata > -1
  Print "Durata: "; dur
+
 
 
  tempus = Now
 
 
 
 
   Repeat
 
   Repeat
     dd = DateDiff(tempus, Now, gb.Millisecond)
+
     gst_element_query_position(elem, GST_FORMAT_TIME, VarPtr(pos))
     Write "\r\e[0mPosiz.: \e[31m" & CStr(Time(0, 0, 0, dd))
+
     Write "\r\e[0mDurata: " & Time(0, 0, 0, durata / 1000000) & "      Pos. \e[31m" & Time(0, 0, 0, pos / 1000000)
 
     Wait 0.001
 
     Wait 0.001
   Until dd > (durata / 1000000)
+
   Until pos >= durata
 
+
 
  <FONT color=gray>' ''Va in chiusura e libera la memoria precedentemente allocata:''</font>
 
  <FONT color=gray>' ''Va in chiusura e libera la memoria precedentemente allocata:''</font>
 
   gst_object_unref(elem)
 
   gst_object_unref(elem)
 
   Print "\nEsecuzione terminata"
 
   Print "\nEsecuzione terminata"
 
    
 
    
  '''End'''
+
  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''"):
 
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)
 
  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)

Versione delle 04:53, 12 dic 2023

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 ".

Library "libgstreamer-1.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

' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *duration)
' Queries an element for the total stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, position 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 elem As Pointer
 Dim filemidi as string
 Dim durata, pos As Long
 Dim volume As Float

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

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

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

 Repeat
   gst_element_query_position(elem, GST_FORMAT_TIME, VarPtr(pos))
   Write "\r\e[0mDurata: " & Time(0, 0, 0, durata / 1000000) & "      Pos. \e[31m" & Time(0, 0, 0, pos / 1000000)
   Wait 0.001
 Until pos >= durata

' Va in chiusura e libera la memoria precedentemente allocata:
 gst_object_unref(elem)
 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(elem, "uri", g_filename_to_uri(fileaudio, Null, 0), "volume", Null, volume)


Riferimenti