Playing MP3 audio file by using MediaPipeline and MediaControl Classes of gb.media Component

Spezielle Fragen zu 2D- und 3D-Grafik, Sound und Video
Antworten
vuott
Foriker
Beiträge: 73
Registriert: Fr 16. Jan 2015, 01:27
Kontaktdaten:

Playing MP3 audio file by using MediaPipeline and MediaControl Classes of gb.media Component

Beitrag von vuott » Mo 21. Aug 2017, 00:02

Hello,
here a my simple code to play MP3 audio file by using MediaPipeline and MediaControl Classes of gb.media Component.
gambas code
Public Sub Main()
 
 Dim pl As MediaPipeline
 Dim src, dcd, snk As MediaControl
 Dim fileMP3 As String
 Dim dur, i As Integer
 Dim tm As Date
  
  fileMP3 = "/path/of/file.mp3"
  If Not Exist(fileMP3) Then Error.Raise("Error, your mp3 file doesn't exist !")
  
  pl = New MediaPipeline
  
  src = New MediaControl(pl, "filesrc")
  src["location"] = fileMP3
  dcd = New MediaControl(pl, "mad")
  snk = New MediaControl(pl, "alsasink")
  src.LinkTo(dcd)
  dcd.LinkTo(snk)
  
  pl.Play()
  
' Calls the function to get mp3 file duration:
  dur = DurataMP3(fileMP3)
  
  tm = Now
  
  Repeat
    i = DateDiff(tm, Now, gb.Millisecond)
    Write "\rTime: " & Date(0, 0, 0, 0, 0, 0, i)
    Wait 0.01
  Until i >= dur
  
' Frees memory and goes to end:
   pl.Stop
   pl.Close
  
   Write "\nPlay completed !"
  
End


Private Function DurataMP3(mp3 As String) As Integer
 
 Dim s, ver_mp3, layer As String
 Dim j, frequenza, durata As Integer
 Dim vB, lB, brB, frB As Byte
 Dim initium, bitrate As Short
  
  Print "Audio file mp3: "; File.Name(mp3)
  
  s = File.Load(mp3)
  Print "\nDimension: "; Len(s); " byte"
  
  initium = 1
  
  For j = initium To Len(s) - 1
    If (Asc(s, j) = 255) And (Asc(s, j + 1) > 241) And (Asc(s, j + 2) > 15) Then
 
' Find the MP3 file version:
      vB = Asc(s, j + 1) And 24
      Select Case vB
        Case 0
          ver_mp3 = "2.5"
        Case 16
          ver_mp3 = "2"
        Case 24
          ver_mp3 = "1"
      End Select
      
' Find the "Layer" of the mp3 file:
      lB = Asc(s, j + 1) And 6
      Select Case lB
        Case 2
          layer = "III"
        Case 4
          layer = "II"
        Case 6
          layer = "I"
      End Select
    
      Print "Version MPEG = "; ver_mp3, "Layer = "; layer
      
' Extract the frequency and bitrate values of MP3 file:
      brB = Asc(s, j + 2) And 240
      bitrate = EstraeBitRate(ver_mp3, layer, brB)
      
      frB = Asc(s, j + 2) And 12
      frequenza = EstraeFrequenza(ver_mp3, frB)
      
      Exit
      
    Endif
  Next
  
  durata = Fix((Len(s) / bitrate) * 8)
  
  Print "BitRate = "; bitrate; " kbps"
  Print "Frequency = hz "; frequenza
  Print "Duration = "; Date(0, 0, 0, 0, 0, 0, durata)
  
  Return durata
  
End


Private Function EstraeBitRate(Vmpeg As String, layB As String, bitB As Byte) As Short
 
 Dim velCamp As Short
 
  If Vmpeg = "1" Then   ' In case of Mpeg vers. 1
    Select Case layB    ' Check the Layer
      Case "I"
        Select Case bitB
          Case 16
            velCamp = 32
          Case 32
            velCamp = 64
          Case 48
            velCamp = 96
          Case 64
            velCamp = 128
          Case 80
            velCamp = 160
          Case 96
            velCamp = 192
          Case 112
            velCamp = 224
          Case 128
            velCamp = 256
          Case 144
            velCamp = 288
          Case 160
            velCamp = 320
          Case 176
            velCamp = 352
          Case 192
            velCamp = 384
          Case 208
            velCamp = 416
          Case 224
            velCamp = 448
        End Select
      Case "II"
        Select Case bitB
          Case 16
            velCamp = 32
          Case 32
            velCamp = 48
          Case 48
            velCamp = 56
          Case 64
            velCamp = 64
          Case 80
            velCamp = 80
          Case 96
            velCamp = 96
          Case 112
            velCamp = 112
          Case 128
            velCamp = 128
          Case 144
            velCamp = 160
          Case 160
            velCamp = 192
          Case 176
            velCamp = 224
          Case 192
            velCamp = 256
          Case 208
            velCamp = 320
          Case 224
            velCamp = 384
        End Select
      Case "III"
        Select Case bitB
          Case 16
            velCamp = 32
          Case 32
            velCamp = 40
          Case 48
            velCamp = 48
          Case 64
            velCamp = 56
          Case 80
            velCamp = 64
          Case 96
            velCamp = 80
          Case 112
            velCamp = 96
          Case 128
            velCamp = 112
          Case 144
            velCamp = 128
          Case 160
            velCamp = 160
          Case 176
            velCamp = 192
          Case 192
            velCamp = 224
          Case 208
            velCamp = 256
          Case 224
            velCamp = 320
        End Select
    End Select
    
  Else
    
    Select Case layB     ' Check the Layer
      Case "I"
        Select Case bitB
          Case 16
            velCamp = 32
          Case 32
            velCamp = 48
          Case 48
            velCamp = 56
          Case 64
            velCamp = 64
          Case 80
            velCamp = 80
          Case 96
            velCamp = 96
          Case 112
            velCamp = 112
          Case 128
            velCamp = 128
          Case 144
            velCamp = 144
          Case 160
            velCamp = 160
          Case 176
            velCamp = 176
          Case 192
            velCamp = 192
          Case 208
            velCamp = 224
          Case 224
            velCamp = 256
        End Select
      Case "II" To "III"
        Select Case bitB
          Case 16
            velCamp = 8
          Case 32
            velCamp = 16
          Case 48
            velCamp = 24
          Case 64
            velCamp = 32
          Case 80
            velCamp = 40
          Case 96
            velCamp = 48
          Case 112
            velCamp = 56
          Case 128
            velCamp = 64
          Case 144
            velCamp = 80
          Case 160
            velCamp = 96
          Case 176
            velCamp = 112
          Case 192
            velCamp = 128
          Case 208
            velCamp = 144
          Case 224
            velCamp = 320
        End Select
    End Select
    
  Endif
  
  Return velCamp
  
End


Private Function EstraeFrequenza(Vmpeg As String, fre As Byte) As Integer
 
 Dim frq As Integer
 
   Select Case Vmpeg
     Case "1"          ' In case of Mpeg vers. 1
      Select Case fre
        Case 0
          frq = 44100
        Case 4
          frq = 48000
        Case 8
          frq = 32000
      End Select
    Case "2"           ' In case of Mpeg vers. 2
      Select Case fre
        Case 0
          frq = 22050
        Case 4
          frq = 24000
        Case 8
          frq = 16000
      End Select
    Case "2.5"         ' In case of Mpeg vers. 2.5
      Select Case fre
        Case 0
          frq = 11025
        Case 4
          frq = 12000
        Case 8
          frq = 8000
      End Select
  End Select
  
  Return frq
  
End

Antworten

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast