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

Da Gambas-it.org - Wikipedia.
(Creata pagina con "Mostriamo un esempio di esecuzione di un file Midi con un'applicazione Gambas ''a riga di comando'': Private elem As Pointer Library "libgstreamer-1.0" Private Enum G...")
 
 
(11 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
Mostriamo un esempio di esecuzione di un file Midi con un'applicazione Gambas ''a riga di comando'':
+
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'' ".
Private elem As Pointer
 
 
 
 
  Library "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 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
 
  Private Const GST_FORMAT_TIME As Integer = 3
Private stato As Integer = GST_STATE_PLAYING
 
 
   
 
   
 
  <FONT color=gray>' ''gst_init (int *argc, char **argv[])''
 
  <FONT color=gray>' ''gst_init (int *argc, char **argv[])''
Riga 32: 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 *cur)''
 
' ''Queries an element for the stream position in nanoseconds.''</font>
 
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean
 
 
 
  <FONT color=gray>' ''void gst_object_unref(gpointer object)''
 
  <FONT color=gray>' ''void gst_object_unref(gpointer object)''
 
  ' ''Decrements the reference count on object .''</font>
 
  ' ''Decrements the reference count on object .''</font>
Riga 42: Riga 34:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
   Dim fileaudio as string
+
   Dim midi As Pointer
   Dim posizione, durata As Long
+
  Dim filemidi as string
  Dim dur, pos As Date
+
   Dim durata, l As Long
 
   Dim volume As Float
 
   Dim volume As Float
 +
  Dim tm As Date
 
   
 
   
  gst_init(0, 0)
+
  gst_init(0, 0)
 +
 
 +
  midi = gst_element_factory_make("playbin", "riproduzione")
 +
  If midi == 0 Then Error.Raise("Errore !")
 
   
 
   
  elem = gst_element_factory_make("playbin", "riproduzione")
+
  filemidi = "<FONT color=darkgreen>''/percorso/del/file.mid''</font>"
 
  fileaudio = "<FONT color=gray>''/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>
  g_object_set(elem, "uri", g_filename_to_uri(fileaudio, Null, 0), Null, 0.0)
+
  g_object_set(midi, "uri", g_filename_to_uri(filemidi, Null, 0), Null, 0.0)
 
    
 
    
 
  <FONT color=gray>' ''Imposta il valore del volume (da 0 a 10) di esecuzione:''</font>
 
  <FONT color=gray>' ''Imposta il valore del volume (da 0 a 10) di esecuzione:''</font>
  volume = 7.0
+
  volume = 7.0
  g_object_set(elem, "volume", Null, Null, volume)
+
  g_object_set(midi, "volume", Null, Null, volume)
 
    
 
    
 
  <FONT color=gray>' ''Avvia la riproduzione del file Midi:''</font>
 
  <FONT color=gray>' ''Avvia la riproduzione del file Midi:''</font>
  gst_element_set_state(elem, GST_STATE_PLAYING)
+
  gst_element_set_state(midi, GST_STATE_PLAYING)
 
    
 
    
  While stato = GST_STATE_PLAYING
+
  Repeat
    gst_element_query_duration(elem, GST_FORMAT_TIME, VarPtr(durata))
+
    gst_element_query_duration(midi, GST_FORMAT_TIME, VarPtr(durata))
    gst_element_query_position(elem, GST_FORMAT_TIME, VarPtr(posizione))
+
    Wait 0.01
    dur = Date(0, 0, 0, 0, 0, 0, durata / 1000000)
+
  Until durata > -1
    pos = Date(0, 0, 0, 0, 0, 0, posizione / 1000000)
 
    If (posizione > 0) And (pos >= dur) Then Break
 
    Write #File.Out, "\rDurata: " & dur & "      Pos. " & pos
 
    Wait 0.01
 
  Wend
 
   
 
  gst_object_unref(elem)
 
  Print "\nEsecuzione terminata."
 
  Quit
 
 
 
'''End'''
 
 
   
 
   
 +
  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
 
   
 
   
  '''Public''' Sub Application_Read()
+
  <FONT color=gray>' ''Va in chiusura e libera la memoria precedentemente allocata:''</font>
 
+
  gst_object_unref(midi)
  Dim s As String
+
  Print "\nEsecuzione terminata"
 
+
    
  Input #File.In, s
+
  End
   
 
  Select Case s
 
    Case "p"                                              <FONT color=gray>' ''Pone in pausa la riproduzione del file Midi''</font>
 
      gst_element_set_state(elem, GST_STATE_PAUSED)
 
    Case "r"
 
      gst_element_set_state(elem, GST_STATE_PLAYING)      <FONT color=gray>' ''Riprende la riproduzione del file Midi''</font>
 
    Case "s"
 
      stato = gst_element_set_state(elem, GST_STATE_NULL)  <FONT color=gray>' ''Arresta la riproduzione del file Midi''</font>
 
   End Select
 
 
 
  '''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)
 
e nel codice sarebbe così utilizzata:
 
e nel codice sarebbe così utilizzata:
  g_object_set(elem, "uri", g_filename_to_uri(fileaudio, Null, 0), "volume", Null, volume)
+
  g_object_set(midi, "uri", g_filename_to_uri(fileaudio, Null, 0), "volume", Null, volume)
 
 
  
  

Versione attuale delle 13:58, 13 gen 2024

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
 
' 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 a 10) di esecuzione:
 volume = 7.0
 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