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 As String
 Dim pl As MediaPipeline
 Dim src, wprs, cnv, lam, snk As MediaControl

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"
 
' 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()
 
 While pl.Duration < 1
   Wait 0.01
 Wend
 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

' E' necessario prevedere sia il Metodo ".Stop" che il Metodo ".Close" della Classe "MediaPipeline", per evitare di ottenere un file audio vuoto:
 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 As String
 Dim pl As MediaPipeline
 Dim src, wprs, cnv, vor, mux, snk As MediaControl

 fileaudio = "/percorso/del/file.wav"
 
 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"
 
 src.LinkTo(wprs)
 wprs.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 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

 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 AAC

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, wav, cnv, aac, snk As MediaControl
 Dim flt As MediaFilter

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

 pl = New MediaPipeline As "PLine"

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

 wav = New MediaControl(pl, "wavparse")
 cnv = New MediaControl(pl, "audioconvert")
 aac = New MediaControl(pl, "faac")   ' oppure anche "voaacenc"
 flt = New MediaFilter(pl, "audio/mpeg,stream-format=adts,channels=2,rate=44100")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".aac"
 
 src.LinkTo(wav)
 wav.LinkTo(cnv)
 cnv.LinkTo(aac)
 aac.LinkTo(flt)
 flt.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 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

 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 OPUS-Ogg

Private bo As Boolean


Public Sub Main()
 
 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, wav, cnv, res, opu, mux, snk As MediaControl

 fileaudio = "/percorso/del/file.wav"
 
 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 
 waw = New MediaControl(pl, "wavparse")
 cnv = New MediaControl(pl, "audioconvert")
 res = New MediaControl(pl, "audioresample")
 opu = New MediaControl(pl, "opusenc")
 
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".opus"
 opu["bitrate"] = 64000
 src.LinkTo(wav)
 wav.LinkTo(cnv)
 cnv.LinkTo(res)
 res.LinkTo(opu)
 opu.LinkTo(mux)
 mux.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 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

 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 MP3 al formato OGG

Private bo As Boolean


Public Sub Main()

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

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 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

 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 MP3 al formato WAV

Private bo As Boolean


Public Sub Main()

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

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 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

 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 MP3 al formato AAC

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, map, mad, aac, snk As MediaControl
 Dim flt As MediaFilter

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

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 map = New MediaControl(pl, "mpegaudioparse")
 mad = New MediaControl(pl, "mpg123audiodec")
 aac = New MediaControl(pl, "faac")   ' oppure anche "voaacenc"
 flt = New MediaFilter(pl, "audio/mpeg,stream-format=adts,channels=2,rate=44100")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".aac"
 
 src.LinkTo(map)
 map.LinkTo(mad)
 mad.LinkTo(aac)
 aac.LinkTo(flt)
 flt.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 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

 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 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 As String
 Dim pl As MediaPipeline
 Dim src, par, dcd, snk As MediaControl
 Dim flt As MediaFilter

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

 pl = New MediaPipeline As "PLine"

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

 par = New MediaControl(pl, "mpegaudioparse")
 dcd = New MediaControl(pl, "mpg123audiodec")
 flt = 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".raw"

 src.LinkTo(par)
 par.LinkTo(dcd)
 dcd.LinkTo(flt)
 flt.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
 
 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 OGG-Vorbis al formato MP3

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


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, ogg, vor, cnv, lam, snk As MediaControl

 fileaudio = "/percorso/del/file.ogg"
 
 pl = New MediaPipeline As "PLine"

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

 ogg = New MediaControl(pl, "oggdemux")
 vor = New MediaControl(pl, "vorbisdec")
 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 src.LinkTo(ogg)
 ogg.LinkLaterTo(vor)
 vor.LinkTo(cnv)
 cnv.LinkTo(lam)
 lam.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

 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 OGG-Vorbis al formato AAC

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, ogg, vor, con, aac, snk As MediaControl
 Dim flt As MediaFilter

 fileaudio = "/percorso/del/file.ogg"
 
 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 ogg = New MediaControl(pl, "oggdemux")
 vor = New MediaControl(pl, "vorbisdec")
 con = New MediaControl(pl, "audioconvert")
 aac = New MediaControl(pl, "faac")   ' oppure anche "voaacenc"
 flt = New MediaFilter(pl, "audio/mpeg,stream-format=adts,channels=2,rate=44100")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".acc"

 src.LinkTo(ogg)
 ogg.LinkLaterTo(vor)
 vor.LinkTo(con)
 con.LinkTo(aac)
 aac.LinkTo(flt)
 flt.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

 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 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 formato-pcm = S16LE.

Private Const GST_AUDIO_FORMAT_S16LE As Integer = 4
Private bo As Boolean


Public Sub Main()

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

 fileaudio = "/percorso/del/file/raw"
 
 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

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

 pl.Play()

 Print "Durata del file raw: "; 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

 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 RAW al formato MP3

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


Public Sub Main()

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

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

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

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file raw: "; 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

 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 RAW al formato OGG-Vorbis

Private Const GST_AUDIO_FORMAT_S16LE As Integer = 4
Private bo As Boolean


Public Sub Main()

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

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 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 raw: "; 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

 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 RAW al formato AAC

Private Const GST_AUDIO_FORMAT_S16LE As Integer = 4
Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, raw, aac, snk As MediaControl
 Dim flt As MediaFilter

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

 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
 aac = New MediaControl(pl, "faac")   ' oppure anche "voaacenc"
 flt = New MediaFilter(pl, "audio/mpeg,stream-format=adts,channels=2,rate=44100")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".aac"

 src.LinkTo(raw)
 raw.LinkTo(aac)
 aac.LinkTo(flt)
 flt.LinkTo(snk)

 pl.Play()

 Print "Durata del file raw: "; 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
 
 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 FLAC al formato WAV

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, flp, fld, wav, snk As MediaControl

 fileaudio = "/percorso/del/file/flac"
 
 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 flp = New MediaControl(pl, "flacparse")
 fld = New MediaControl(pl, "flacdec")
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 src.LinkTo(flp)
 flp.LinkTo(fld)
 fld.LinkTo(wav)
 wav.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file Flac: "; 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
 
 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 FLAC al formato MP3

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


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, flp, fld, lam, snk As MediaControl

 fileaudio = "/percorso/del/file/flac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 flp = New MediaControl(pl, "flacparse")
 fld = New MediaControl(pl, "flacdec")
 lam = New MediaControl(pl, "lamemp3enc")
 lam["target"] = BITRATE
 lam["cbr"] = True
 lam["bitrate"] = 192
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 src.LinkTo(flp)
 flp.LinkTo(fld)
 fld.LinkTo(lam)
 lam.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file Flac: "; 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

 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 FLAC al formato OGG-Vorbis

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, flp, fld, cnv, vor, mux, snk As MediaControl

 fileaudio = "/percorso/del/file/flac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 flp = New MediaControl(pl, "flacparse")
 fld = New MediaControl(pl, "flacdec")
 cnv = New MediaControl(pl, "audioconvert")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 src.LinkTo(flp)
 flp.LinkTo(fld)
 fld.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 flac: "; 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
 
 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 AAC al formato WAV

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, aac, wav, snk As MediaControl

 fileaudio = "/percorso/del/file/aac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "faad")   ' oppure anche "voaacenc"
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 src.LinkTo(aac)
 aac.LinkTo(wav)
 wav.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file aac: "; 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
 
 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 AAC al formato MP3

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


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, aac, lam, snk As MediaControl

 fileaudio = "/percorso/del/file/aac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "faad")   ' oppure anche "voaacenc"
 lam = New MediaControl(pl, "lamemp3enc")
 lam["target"] = BITRATE
 lam["cbr"] = True
 lam["bitrate"] = 192
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 src.LinkTo(aac)
 aac.LinkTo(lam)
 lam.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file Acc: "; 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
 
 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 AAC al formato OGG-Vorbis

Private bo As Boolean


Public Sub Main()

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

 fileaudio = "/percorso/del/file/aac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "faad")   ' oppure anche "voaacenc"
 cnv = New MediaControl(pl, "audioconvert")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 src.LinkTo(aac)
 aac.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 aac: "; 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
 
 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 AAC al formato RAW

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, aac, snk As MediaControl
 Dim flt As MediaFilter

 fileaudio = "/percorso/del/file/aac"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "faad")   ' oppure anche "voaacenc"
 flt = 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".raw"

 src.LinkTo(aac)
 aac.LinkTo(flt)
 flt.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file aac: "; 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

 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 AIFF al formato WAV

Private bo As Boolean


Public Sub Main()

 Dim fileaudio As String
 Dim pl As MediaPipeline
 Dim src, aif, cnv, wav, snk As MediaControl

 fileaudio = "/percorso/del/file/aiff"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aif = New MediaControl(pl, "aiffparse")
 cnv = New MediaControl(pl, "audioconvert")
 wav = New MediaControl(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 src.LinkTo(aif)
 aif.LinkTo(cnv)
 cnv.LinkTo(wav)
 wav.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file aiff: "; 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
 
 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 AIFF al formato MP3

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


Public Sub Main()

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

 fileaudio = "/percorso/del/file/aiff"

 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "aiffparse")
 cnv = New MediaControl(pl, "audioconvert")
 lam = New MediaControl(pl, "lamemp3enc")
 lam["target"] = BITRATE
 lam["cbr"] = True
 lam["bitrate"] = 192
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".mp3"

 src.LinkTo(aif)
 aif.LinkTo(cnv)
 cnv.LinkTo(lam)
 lam.LinkTo(snk)

 pl.Play()

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Durata del file aiff: "; 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
 
 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 AIFF al formato OGG-Vorbis

Private bo As Boolean


Public Sub Main()

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

 fileaudio = "/percorso/del/file/aiff"
 
 pl = New MediaPipeline As "PLine"

 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 aac = New MediaControl(pl, "aiffparse")
 cnv = New MediaControl(pl, "audioconvert")
 vor = New MediaControl(pl, "vorbisenc")
 mux = New MediaControl(pl, "oggmux")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".ogg"

 src.LinkTo(aif)
 aif.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 aiff: "; 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

 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 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 As String
 Dim pl As MediaPipeline
 Dim src, dcb, con, flt, wav, snk As MediaControl

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

 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"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"
 
 src.LinkTo(dcb)
 dcb.LinkLaterTo(con)
 con.LinkTo(flt)
 flt.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

 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

Se si vuole convertire in un file avente caratteristiche (frequenza di campionamento, numero dei canali, risoluzione in bit) dei propri campioni audio diverse da quelle del file originario, da convertire, allora si dovranno usare l'elemento "audioresample" e la Classe MediaFilter.

Mostriamo un esempio pratico, nel quale si convertirà un file MP3 in un file WAV avente frequenza di campionamento uguale a 22050 hertz e risoluzione a 8 bit:

Private bo As Boolean


Public Sub Main()

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

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

 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = fileaudio
 dcb = New MediaControl(pl, "decodebin")
 con = New MediaControl(pl, "audioconvert")
 res = New MediaControl(pl, "audioresample") ' Consente di modificare le caratteristiche del file audio
 flt = New MediaFilter(pl, "audio/x-raw,rate=22050,format=U8")
 wav = New MediaFilter(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = "/tmp" &/ File.BaseName(fileaudio) & ".wav"

 src.LinkTo(dcb)
 dcb.LinkLaterTo(con)
 con.LinkTo(res)
 res.LinkTo(flt)
 flt.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

 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