La gestione dei file MIDI mediante le funzioni esterne del API di GStreamer

Da Gambas-it.org - Wikipedia.

Rinviamo all'analogo capitolo sulla gestione delle funzioni delle API di GStreamer per l'audio: per l'esecuzione dei file Midi si adotteranno le medesime funzioni e risorse lì descritte, precisando che è necessario aver installato il file banco di suoni di default di Timidity: "/usr/share/sounds/sf2/TimGM6mb.sf2", oppure di FluidSynth: "/usr/share/sounds/sf2/FluidR3_GM.sf2".


Pro memoria riportiamo l'esempio per un'applicazione a riga di comando, utilizzando però in questo caso un file Midi:

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, key As String, value As String, tertium As String)

' 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 fileaudio as string
 Dim posizione, durata As Long

  gst_init(0, 0)

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

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

  g_object_set(elem, "uri", g_filename_to_uri(fileaudio, Null, 0), Null)

' Avvia la riproduzione del file Midi:
  gst_element_set_state(elem, GST_STATE_PLAYING)
 
  While (posizione = 0) Or (durata > posizione)
    gst_element_query_duration(elem, GST_FORMAT_TIME, VarPtr(durata))
    gst_element_query_position(elem, GST_FORMAT_TIME, VarPtr(posizione))
    Write #File.Out, "\rDurata: " & Date(0, 0, 0, 0, 0, 0, durata / 1000000) & "      Pos. " & Date(0, 0, 0, 0, 0, 0, posizione / 1000000)
    If stato <> 4 Then Break
    Wait 0.01
  Wend
   
  gst_object_unref(elem)
  
  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


Esecuzione mediante una linea di pipeline con la funzione gst parse launch()

Un file Midi può essere eseguito anche mediante una linea di pipeline gestita dalla funzione gst parse launch(), avendo l'accortezza di inserire alla fine della pipeline uno dei seguenti plugin: alsasink, pulsesink, openalsink.

Mostriamo un esempio pratico:

Private audio 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

' 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_parse_launch (const gchar *pipeline_description, GError **error)
' Create a new pipeline based on command line syntax.
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer

' 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_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 tempus As Long
     
  gst_init(0, 0)
  
  audio = gst_parse_launch("filesrc location=/percorso/del/file.mid ! decodebin ! audioconvert ! audioresample ! volume volume=7.0 ! alsasink", 0)
       
' Avviamo la riproduzione del file Midi:
  gst_element_set_state(audio, GST_STATE_PLAYING)
  
  While True
    gst_element_query_position(audio, GST_FORMAT_TIME, VarPtr(tempus))
    Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
    Wait 0.01
  Wend
      
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(audio, GST_STATE_PAUSED)
   Case "r"
' Riprende la riproduzione del file Midi
     gst_element_set_state(audio, GST_STATE_PLAYING)
   Case "s"
' Arresta la riproduzione del file Midi
     gst_element_set_state(audio, GST_STATE_NULL)
     gst_object_unref(audio)
     Print "\nEsecuzione terminata."
     Quit
 End Select

End


Va precisato che la riga di comando afferente alla funzione esterna gst parse launch() può essere impostata più brevemente anche così:

audio = gst_parse_launch("playbin uri=file:///percorso/del/file.mid", 0)

ma non sarà possibile utilizare il plugin per la gestione del volume.



Riferimenti