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...")
 
Riga 44: Riga 44:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim fileaudio as string
+
   Dim filemidi as string
 
   Dim posizione, durata As Long
 
   Dim posizione, durata As Long
 
   Dim dur, pos As Date
 
   Dim dur, pos As Date
Riga 56: Riga 56:
 
   
 
   
 
  <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(elem, "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>

Versione delle 10:15, 22 set 2016

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 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 stato As Integer = GST_STATE_PLAYING

' 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 *cur)
' Queries an element for the stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur 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 filemidi as string
 Dim posizione, durata As Long
 Dim dur, pos As Date
 Dim volume As Float

  gst_init(0, 0)

  elem = gst_element_factory_make("playbin", "riproduzione")

  fileaudio = "/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)
 
  While stato = GST_STATE_PLAYING
    gst_element_query_duration(elem, GST_FORMAT_TIME, VarPtr(durata))
    gst_element_query_position(elem, GST_FORMAT_TIME, VarPtr(posizione))
    dur = Date(0, 0, 0, 0, 0, 0, durata / 1000000)
    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


Public Sub Application_Read()
 
 Dim s As String
 
  Input #File.In, s
   
  Select Case s
    Case "p"                                               ' Pone in pausa la riproduzione del file Midi
      gst_element_set_state(elem, GST_STATE_PAUSED)
    Case "r"
      gst_element_set_state(elem, GST_STATE_PLAYING)       ' Riprende la riproduzione del file Midi
    Case "s"
      stato = gst_element_set_state(elem, GST_STATE_NULL)  ' Arresta la riproduzione del file Midi
  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"):

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