Convertire i formati audio mediante il Componente gb.media

Da Gambas-it.org - Wikipedia.

Utilizzando i plugin di GStreamer con le Classi MediaControl e MediaPipeline del Componente gb.media, è possibile effettuare diverse conversioni di file audio da un formato ad un altro.

Mostriamo di seguito qualche esempio pratico.

Conversione audio dal formato WAV al formato MP3

Private Const QUALITY As String = "quality"
Private Const BITRATE As String = "bitrate"
Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, wprs, cnv, lam, snk As MediaControl

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio

 wprs = New MediaControl(pl, "wavparse")
 cnv = New MediaControl(pl, "audioconvert")
 lam = New MediaControl(pl, "lamemp3enc")
 lam["target"] = BITRATE    ' Imposta il Bitrate.
 lam["cbr"] = True          ' Imposta il Bitrate "Costante".
 lam["bitrate"] = 192       ' Imposta il valore del Bitrate "Costante".
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio
 
' Collega fra loro all'interno della pipeline gli oggetti MediaControl impostati:
 src.LinkTo(wprs)
 wprs.LinkTo(cnv)
 cnv.LinkTo(lam)
 lam.LinkTo(snk)

 pl.Play()
 
 Print "Durata del file wav: "; Time(0, 0, 0, pl.Duration * 1000)

 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End() ' Questo Evento viene sollevato, quando la conversione è terminata

 bo = True

End


Conversione audio dal formato WAV al formato OGG

Private bo As Boolean


Public Sub Main()
 
 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, wprs, cnv, vor, mux, snk As MediaControl

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 wprs = New MediaControl(pl, "wavparse")
 cnv = New MediaControl(pl, "audioconvert")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio
 
 src.LinkTo(wprs)
 wprs.LinkTo(cnv)
 cnv.LinkTo(vor)
 vor.LinkTo(mux)
 mux.LinkTo(snk)

 pl.Play()

 Print "Durata del file wav: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".ogg"
 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio dal formato MP3 al formato OGG

Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, map, mad, vor, mux, snk As MediaControl

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
  
 map = New MediaControl(pl, "mpegaudioparse")
 mad = New MediaControl(pl, "mpg123audiodec")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio
 
 src.LinkTo(map)
 map.LinkTo(mad)
 mad.LinkTo(vor)
 vor.LinkTo(mux)
 mux.LinkTo(snk)
 
 pl.Play()
 
 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file mp3: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio dal formato MP3 al formato WAV

Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, map, mad, wav, snk As MediaControl

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 map = New MediaControl(pl, "mpegaudioparse")
 mad = New MediaControl(pl, "mpg123audiodec")
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio
 
 src.LinkTo(map)
 map.LinkTo(mad)
 mad.LinkTo(wav)
 wav.LinkTo(snk)
 
 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file mp3: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio dal formato MP3 al formato RAW

Con il seguente codice si otterranno da un file MP3 i dati audio grezzi nel formato: frequenza di campionamento = 44100, risoluzione = 16 bit, canali = 2.

Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, par, dcd, snk As MediaControl
 Dim fil As MediaFilter

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 par = New MediaControl(pl, "mpegaudioparse")
 dcd = New MediaControl(pl, "mpg123audiodec")
 fil = New MediaFilter(pl, "audio/x-raw, format=S16LE, rate=44100, channels=2, endianness=1234, width=16, depth=16")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio

 src.LinkTo(par)
 par.LinkTo(dcd)
 dcd.LinkTo(fil)
 fil.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file mp3: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".raw"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True
  
End


Conversione audio dal formato RAW al formato WAV

Il seguente codice prevede che i dati del file audio RAW abbiano le seguenti caratteristiche: frequenza di campionamento = 44100; numero di canali = 2; modalità interleaved e [https://gstreamer.freedesktop.org/documentation/audio/audio-format.html?gi-language=c#GstAudioFormat formato-pcm = S16LE.

Private Const GST_AUDIO_FORMAT_S16LE As Integer = 4
Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, raw, wav, snk As MediaControl

 fileaudio = "/percorso/del/file/raw"

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 raw = New MediaControl(pl, "rawaudioparse")
 raw["interleaved"] = True
 raw["num-channels"] = 2
 raw["pcm-format"] = GST_AUDIO_FORMAT_S16LE
 raw["sample-rate"] = 44100
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio

 src.LinkTo(raw)
 raw.LinkTo(wav)
 wav.LinkTo(snk)

 pl.Play()

 Print "Durata del file wav finale: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio dal formato RAW al formato MP3

Private Const QUALITY As String = "quality"
Private Const BITRATE As String = "bitrate"
Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, raw, lam, snk As MediaControl

 fileaudio = "/percorso/del/file/raw"

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 raw = New MediaControl(pl, "rawaudioparse")
 raw["interleaved"] = True
 raw["num-channels"] = 2
 raw["pcm-format"] = GST_AUDIO_FORMAT_S16LE
 raw["sample-rate"] = 44100
 lam = New MediaControl(pl, "lamemp3enc")
 lam["target"] = BITRATE    ' Imposta il Bitrate
 lam["cbr"] = True          ' Imposta il Bitrate "Costante"
 lam["bitrate"] = 192       ' Imposta il valore del Bitrate "Costante"
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio

 src.LinkTo(raw)
 raw.LinkTo(lam)
 lam.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file mp3 finale: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio dal formato RAW al formato OGG-Vorbis

Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, raw, cnv, vor, mux, snk As MediaControl

 fileaudio = "/percorso/del/file/raw"

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 raw = New MediaControl(pl, "rawaudioparse")
 raw["interleaved"] = True
 raw["num-channels"] = 2
 raw["pcm-format"] = GST_AUDIO_FORMAT_S16LE
 raw["sample-rate"] = 44100
 cnv = New MediaControl(pl, "audioconvert")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio

 src.LinkTo(raw)
 raw.LinkTo(cnv)
 cnv.LinkTo(vor)
 vor.LinkTo(mux)
 mux.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file ogg finale: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True

End


Conversione audio con il plugin "decodebin"

Usando il plugin "decodebin" è possibile convertire qualsiasi formato audio in un altro desiderato.
Mostriamo un esempio, nel quale sarà convertito un file audio "OGG" in un file audio "WAV", avente il seguente formato: frequenza di campionamento = 44100, risoluzione = 16 bit, canali = 2.

Private bo As Boolean


Public Sub Main()

 Dim fileaudio, appoggio As String
 Dim pl As MediaPipeline
 Dim src, dcb, con, flt, wav, snk As MediaControl

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

 appoggio = Temp

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio

 dcb = New MediaControl(pl, "decodebin")
 con = New MediaControl(pl, "audioconvert")
 flt = New MediaFilter(pl, "audio/x-raw, format=S16LE, rate=44100, channels=2, depth=16")
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio
 
 src.LinkTo(dcb)
 dcb.LinkLaterTo(con)
 con.LinkTo(fil)
 fil.LinkTo(wav)
 wav.LinkTo(snk)

 pl.Play()

 Print "Durata del file ogg: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
   Write "\r\e[0mTempo trascorso:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy appoggio To "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 pl.Stop
 pl.Close

 Print "\n\e[0mConversione terminata !"

End


Public Sub PLine_End()

 bo = True
  
End