December 09, 2019, 08:49:37 AM

Author Topic: [bb] Length of mp3 by Sonari Eclipsi Onimari [ 1+ years ago ]  (Read 3263 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Length of mp3
Author : Sonari Eclipsi Onimari
Posted : 1+ years ago

Description : Okay, this has been a butt for anyone trying to devolp a media player that shows time. This works perfect on any mp3 file. Thanks to Floppy, and Bram32 on www.codersworkshop.com and the Mp3 tags archive here in the codearcs, this function is now a reality!!!

Code :
Code: BlitzBasic
  1. ;def
  2.  
  3. Global looser
  4.  
  5. Dim mp3_bri$(16)
  6. mp3_bri$(0) = "free"
  7. mp3_bri$(1) = "32"
  8. mp3_bri$(2) = "40"
  9. mp3_bri$(3) = "48"
  10. mp3_bri$(4) = "56"
  11. mp3_bri$(5) = "64"
  12. mp3_bri$(6) = "80"
  13. mp3_bri$(7) = "96"
  14. mp3_bri$(8) = "112"
  15. mp3_bri$(9) = "128"
  16. mp3_bri$(10) = "160"
  17. mp3_bri$(11) = "192"
  18. mp3_bri$(12) = "224"
  19. mp3_bri$(13) = "256"
  20. mp3_bri$(14) = "320"
  21. mp3_bri$(15) = "bad"
  22.  
  23. Dim mp3_sri$(4)
  24. mp3_sri(0) = "44100"
  25. mp3_sri(1) = "48000"
  26. mp3_sri(2) = "32000"
  27. mp3_sri(3) = "reserved"
  28.  
  29. Dim mp3_chi$(4)
  30. mp3_chi(0) = "Stereo"
  31. mp3_chi(1) = "Joint Stereo"
  32. mp3_chi(2) = "Dual"
  33. mp3_chi(3) = "Mono"
  34.  
  35. Dim mp3_emi$(4)
  36. mp3_emi(0) = "None"
  37. mp3_emi(1) = "50/15"
  38. mp3_emi(2) = "reserved"
  39. mp3_emi(3) = "CCIT J.17"
  40.  
  41. Dim mp3_mvi$(4)
  42. mp3_mvi$(0) = "MPEG Version 2.5 (not an official standard)"
  43. mp3_mvi$(1) = "reserved"
  44. mp3_mvi$(2) = "MPEG Version 2"
  45. mp3_mvi$(3) = "MPEG Version 1"
  46.  
  47. Dim mp3_mli$(4)
  48. mp3_mli$(0) = "reserved"
  49. mp3_mli$(1) = "Layer III"
  50. mp3_mli$(2) = "Layer II"
  51. mp3_mli$(3) = "Layer I"
  52.  
  53. Dim mp3_genre$(128)
  54. mp3_genre( 0)= "Blues"
  55. mp3_genre( 1)= "Classic Rock"
  56. mp3_genre( 2)= "Country"
  57. mp3_genre( 3)= "Dance"
  58. mp3_genre( 4)= "Disco"
  59. mp3_genre( 5)= "Funk"
  60. mp3_genre( 6)= "Grunge"
  61. mp3_genre( 7)= "Hip-Hop"
  62. mp3_genre( 8)= "Jazz"
  63. mp3_genre( 9)= "Metal"
  64. mp3_genre(10)= "New Age"
  65. mp3_genre(11)= "Oldies"
  66. mp3_genre(12)= "Other"
  67. mp3_genre(13)= "Pop"
  68. mp3_genre(14)= "R&B"
  69. mp3_genre(15)= "Rap"
  70. mp3_genre(16)= "Reggae"
  71. mp3_genre(17)= "Rock"
  72. mp3_genre(18)= "Techno"
  73. mp3_genre(19)= "Industrial"
  74. mp3_genre(20)= "Alternative"
  75. mp3_genre(21)= "Ska"
  76. mp3_genre(22)= "Death Metal"
  77. mp3_genre(23)= "Pranks"
  78. mp3_genre(24)= "Soundtrack"
  79. mp3_genre(25)= "Euro-Techno"
  80. mp3_genre(26)= "Ambient"
  81. mp3_genre(27)= "Trip-Hop"
  82. mp3_genre(28)= "Vocal"
  83. mp3_genre(29)= "Jazz+Funk"
  84. mp3_genre(30)= "Fusion"
  85. mp3_genre(31)= "Trance"
  86. mp3_genre(32)= "Classical"
  87. mp3_genre(33)= "Instrumental"
  88. mp3_genre(34)= "Acid"
  89. mp3_genre(35)= "House"
  90. mp3_genre(36)= "Game"
  91. mp3_genre(37)= "Sound Clip"
  92. mp3_genre(38)= "Gospel"
  93. mp3_genre(39)= "Noise"
  94. mp3_genre(40)= "AlternRock"
  95. mp3_genre(41)= "Bass"
  96. mp3_genre(42)= "Soul"
  97. mp3_genre(43)= "Punk"
  98. mp3_genre(44)= "Space"
  99. mp3_genre(45)= "Meditative"
  100. mp3_genre(46)= "Instrumental Pop"
  101. mp3_genre(47)= "Instrumental Rock"
  102. mp3_genre(48)= "Ethnic"
  103. mp3_genre(49)= "Gothic"
  104. mp3_genre(50)= "Darkwave"
  105. mp3_genre(51)= "Techno-Industrial"
  106. mp3_genre(52)= "Electronic"
  107. mp3_genre(53)= "Pop-Folk"
  108. mp3_genre(54)= "Eurodance"
  109. mp3_genre(55)= "Dream"
  110. mp3_genre(56)= "Southern Rock"
  111. mp3_genre(57)= "Comedy"
  112. mp3_genre(58)= "Cult"
  113. mp3_genre(59)= "Gangsta"
  114. mp3_genre(60)= "Top 40"
  115. mp3_genre(61)= "Christian Rap"
  116. mp3_genre(62)= "Pop/Funk"
  117. mp3_genre(63)= "Jungle"
  118. mp3_genre(64)= "Native American"
  119. mp3_genre(65)= "Cabaret"
  120. mp3_genre(66)= "New Wave"
  121. mp3_genre(67)= "Psychadelic"
  122. mp3_genre(68)= "Rave"
  123. mp3_genre(69)= "Showtunes"
  124. mp3_genre(70)= "Trailer"
  125. mp3_genre(71)= "Lo-Fi"
  126. mp3_genre(72)= "Tribal"
  127. mp3_genre(73)= "Acid Punk"
  128. mp3_genre(74)= "Acid Jazz"
  129. mp3_genre(75)= "Polka"
  130. mp3_genre(76)= "Retro"
  131. mp3_genre(77)= "Musical"
  132. mp3_genre(78)= "Rock & Roll"
  133. mp3_genre(79)= "Hard Rock"
  134.  
  135. Global mp3_FileSize
  136.  
  137. Type id3v1
  138.         Field key$
  139.         Field Dat$
  140. End Type
  141.  
  142. Type id3v2
  143.         Field key$
  144.         Field flags
  145.         Field Dat$
  146.         Field size
  147.         Field start
  148. End Type
  149.  
  150. Type AudioFrame
  151.         Field mpegVersion$
  152.         Field mpegLayer$
  153.         Field CRCprotection
  154.         Field Bitrate
  155.         Field samplerate
  156.         Field padding
  157.         Field PrivateBit
  158.         Field Channel$
  159.         Field ModeExtension_intensity
  160.         Field ModeExtension_MS
  161.         Field copyright
  162.         Field Original
  163.         Field Emphasis$
  164.        
  165.         Field num
  166.         Field framelen
  167.         Field framestart
  168. End Type
  169.  
  170.  
  171. Global filename$ = RequestFile$("","mp3")
  172.  
  173. ; "Anlysing: "+filename
  174. ; "----"
  175. readTagv1(filename)
  176. file = mp3_openFile(filename)
  177. analyseMP3(file)
  178. file = mp3_closeFile(filename)
  179.  
  180. ; "----"
  181. mp3_printAudioFrame(Last AudioFrame)
  182.  
  183. ; "----"
  184. For t1.id3v1 = Each id3v1
  185.         mp3_printID3v1(t1)
  186. Next
  187.  
  188. ; "----"
  189. For t2.id3v2 = Each id3v2
  190.         mp3_printID3v2(t2)
  191. Next
  192.  
  193.  
  194. Function mp3_printID3v1(t.id3v1)
  195.         If tkey = "Genre"
  196.                 ; tkey+": "+mp3_genre(Int(tdat))
  197.         Else
  198.                 ; tkey+": "+tdat
  199.         EndIf
  200. End Function
  201.  
  202. Function mp3_printID3v2(t.id3v2)
  203.         ; tkey+": "+tdat
  204. End Function
  205.  
  206. Function mp3_openFile(filename$)
  207.         If Not FileType(filename) = 1 Then Return False
  208.  
  209.         file = ReadFile(filename)
  210.         mp3_FileSize = FileSize(filename)
  211.         Return file
  212. End Function
  213.  
  214. Function mp3_closeFile(filename$)
  215.         If Not FileType(filename) = 1 Then Return False
  216.  
  217.         file = ReadFile(filename)
  218.         Return file
  219. End Function
  220.  
  221. Function analyseMP3(file)
  222.         SeekFile(file,0)
  223.         readTagv2(file)
  224.  
  225.         While Not Eof(file)
  226.                 a.audioframe = mp3_readAudioFrame.AudioFrame(file,count)
  227.                 If a = Null
  228.                         ; "interupt by "+FilePos(file)
  229.                         Exit
  230.                 Else
  231.                         lastvalid.Audioframe = a
  232.                 EndIf
  233.                 count = count + 1
  234.         Wend
  235.        
  236.         ; "Analysed "+count+" Audioframes"
  237. End Function
  238.  
  239. Function mp3_readAudioFrame.AudioFrame(file,count)
  240.        
  241.         pos = FilePos(file)
  242.        
  243.         b1 = ReadByte(file)
  244.         b2 = ReadByte(file)
  245.         b3 = ReadByte(file)
  246.         b4 = ReadByte(file)
  247.        
  248.         If b1 <> $ff And (b2 And 224) <> 224
  249.                 ; "Frame syncronizer not found"
  250.                 SeekFile(file,FilePos(file)-4)
  251.                 Return Null
  252.         EndIf
  253.        
  254.         a.AudioFrame = New AudioFrame
  255.         aframestart = pos
  256.         a
  257. um = count
  258.        
  259.         MPEGversionID = (b2 And 24) Shr 3
  260.         ampegVersion = mp3_mvi(MPEGversionID)
  261.  
  262.         MPEGlayerID = (b2 And 6) Shr 1
  263.         ampegLayer = mp3_mli(MPEGlayerID)
  264.        
  265.         aCRCprotection = (b2 And $1)
  266.        
  267.         BitrateID = (b3 And $f0) Shr 4
  268.         aBitRate = Int(mp3_bri(BitrateID)) * 1000
  269.        
  270.         SamplerateID = (b3 And $c) Shr 2
  271.         aSampleRate = Int(mp3_sri(SamplerateID))
  272.        
  273.         aPadding = (b3 And $2) Shr 1
  274.  
  275.         aprivateBit = (b3 And 1)
  276.  
  277.         channelID = (b4 And $C0) Shr 6
  278.         achannel = mp3_chi(channelID)
  279.  
  280.         aModeExtension_intensity = (b4 And $10) Shr 5
  281.         aModeExtension_MS = (b4 And $20) Shr 6
  282.        
  283.         acopyright = (b4 And $8)
  284.  
  285.         aoriginal = (b4 And $4)
  286.        
  287.         emphasisID = (b4 And $3)
  288.         aemphasis = mp3_emi(emphasisID)
  289.        
  290.         aFrameLen = Floor((144.0 * Float(aBitRate) / Float(aSampleRate) ) + aPadding)
  291.        
  292.         SeekFile(file,FilePos(file)+aframelen-4)
  293.         Return a
  294. End Function
  295.  
  296. Function mp3_printAudioFrame(a.audioframe)
  297.         ; "Audioframe "+a
  298. um
  299.         ; "MPEG Version: " + ampegVersion
  300.         ; "MPEG Layer: " + ampegLayer
  301.         If aCRCprotection
  302.                 ; "NOT Protected by CRC"
  303.         Else
  304.                 ; "Protected by CRC"
  305.         EndIf
  306.        
  307.         looser= + aBitRate + " bps"    
  308.         ; "Samplig rate frequency: " + aSampleRate+" hz"
  309.        
  310.         If aPadding
  311.                 ; "Frame is padded"
  312.         Else
  313.                 ; "Frame is not padded"
  314.         EndIf
  315.  
  316.         If aprivateBit
  317.                 ; "private bit is set"
  318.         Else
  319.                 ; "private bit is not set"
  320.         EndIf
  321.  
  322.         ; "Channel: " + achannel+" mode"
  323.  
  324.         ; "Mode Extension: "
  325.         If aModeExtension_intensity
  326.                 ; "Intensity Stereo on"
  327.         Else
  328.                 ; "Intensity Stereo off"
  329.         EndIf
  330.         If aModeExtension_MS
  331.                 ; "MS Stereo on"
  332.         Else
  333.                 ; "MS Stereo off"
  334.         EndIf
  335.  
  336.         If acopyright
  337.                 ; "Audio is copyrighted"
  338.         Else
  339.                 ; "Audio is not copyrighted"
  340.         EndIf
  341.  
  342.         If aoriginal
  343.                 ; "Original media"
  344.         Else
  345.                 ; "Copy of original media"
  346.         EndIf
  347.        
  348.         ; "Emphasis: " + aemphasis
  349.        
  350.         ; "Framelength: "+aFrameLen
  351. End Function
  352.  
  353. Function readTagv1(filename$)
  354.         If Not FileType(filename) = 1 Then Return False
  355.  
  356.         file = ReadFile(filename)
  357.         SeekFile(file,FileSize(filename)-128)
  358.        
  359.         For i=0 To 2
  360.                 txt$ = txt$ + Chr(ReadByte(file))
  361.         Next
  362.        
  363.         If txt = "TAG"
  364.                 ; "ID3 v1 Tag present"
  365.                                
  366.                 txt = ""
  367.                 For i=0 To 29
  368.                         songname$ = songname$ + Chr(ReadByte(file))
  369.                 Next
  370.                 t.id3v1 = New id3v1
  371.                 tkey = "Songname"
  372.                 tdat = Trim(songname)
  373.                
  374.                 For i=0 To 29
  375.                         Artist$ = Artist$ + Chr(ReadByte(file))
  376.                 Next
  377.                 t.id3v1 = New id3v1
  378.                 tkey = "Artist"
  379.                 tdat = Trim(artist)
  380.  
  381.                 For i=0 To 29
  382.                         Album$ = Album$ + Chr(ReadByte(file))
  383.                 Next
  384.                 t.id3v1 = New id3v1
  385.                 tkey = "Album"
  386.                 tdat = Trim(album)
  387.  
  388.                 txt = ""
  389.                 For i=0 To 3
  390.                         year$ = year$ + Chr(ReadByte(file))
  391.                 Next
  392.                 t.id3v1 = New id3v1
  393.                 tkey = "Year"
  394.                 tdat = Trim(year)
  395.  
  396.                 For i=0 To 29
  397.                         Comment$ = Comment$ + Chr(ReadByte(file))
  398.                 Next
  399.                 t.id3v1 = New id3v1
  400.                 tkey = "Comment"
  401.                 tdat = Trim(comment)
  402.  
  403.                 For i=0 To 0
  404.                         genre = ReadByte(file)
  405.                 Next
  406.                 t.id3v1 = New id3v1
  407.                 tkey = "Genre"
  408.                 tdat = Trim(genre)
  409.                
  410.         EndIf
  411. End Function
  412.  
  413. Function readTagv2(file)               
  414.         For i=0 To 2
  415.                 txt$ = txt$ + Chr(ReadByte(file))
  416.         Next
  417.        
  418.         If txt = "ID3"
  419.                
  420.                 ;read TAG version
  421.                 hiVersion = ReadByte(file)
  422.                 lowVersion = ReadByte(file)
  423.                 ; "ID3 v2 Tag present (version: "+hiVersion+"/"+lowVersion+")"
  424.                
  425.                 ;read flags
  426.                 flags = ReadByte(file)
  427.  
  428.                
  429.                 ;read size of tag
  430.                 b = ReadInt(file)
  431.                
  432.                 size = syncsafeInt(b)
  433.  
  434.                 While tagpos < size
  435.                         ;read frame TAG
  436.                         txt = ""
  437.  
  438.                         b1 = ReadByte(file)
  439.                         tagpos = tagpos + 1
  440.                         If b1
  441.                                 t.id3v2 = New id3v2
  442.                                 tstart = FilePos(file)-1
  443.                                
  444.                                 b2 = ReadByte(file)
  445.                                 b3 = ReadByte(file)
  446.                                 b4 = ReadByte(file)
  447.                                 tagpos = tagpos + 3
  448.  
  449.                                 txt$ = Chr(b1) + Chr(b2) + Chr(b3) + Chr(b4)
  450.                                 tkey = txt
  451.                                
  452.                                 framesize = syncsafeInt(ReadInt(file))
  453.                                 tagpos = tagpos + 4
  454.                                 tsize = framesize
  455.                                
  456.                                 flags = ReadShort(file)
  457.                                 tagpos = tagpos + 2
  458.                                 tflags = flags
  459.                                                                
  460.                                 txt$ = ""
  461.                                 For i= 1 To framesize
  462.                                         txt = txt + Chr(ReadByte(file))
  463.                                         tagpos = tagpos + 1
  464.                                 Next
  465.                                 tdat$ = Trim(txt)
  466.                         EndIf
  467.                         ;WaitKey
  468.                 Wend
  469.         Else
  470.                 SeekFile(file,0)
  471.         EndIf
  472. End Function
  473.  
  474. Function syncsafeInt(b)
  475.        
  476.         b1 = b And $ff000000
  477.         b2 = b And $00ff0000
  478.         b3 = b And $0000ff00
  479.         b4 = b And $000000ff
  480.        
  481.         b1 = (b And $ff000000) Shr 24
  482.         b2 = (b And $00ff0000) Shr 8
  483.         b3 = (b And $0000ff00) Shr 8
  484.         b4 = (b And $000000ff) Shr 24
  485.  
  486.         b4 = b4 Shr 3
  487.         b3 = b3 Shr 2
  488.         b2 = b2 Shr 1
  489.        
  490.         size = b4 Or b3 Or b2 Or b1
  491.         Return size
  492. End Function
  493.  
  494.  
  495.  
  496.  
  497. y=looser/1000
  498. Print "Bitrate: "+y
  499.  
  500.  
  501.  
  502. z#=FileSize(filename$)/1024.0
  503. bits  = z*1024*8
  504. time# = (bits/(y*1000)) ;this is in seconds
  505.  
  506. ;calculate min/secs
  507. min% = Floor(time/60)
  508. sec% = time Mod 60
  509.  
  510. Print min + " minutes " + sec + " seconds"
  511.  
  512. WaitKey()
  513.  
  514. End


Comments :


Sonari Eclipsi Onimari(Posted 1+ years ago)

 I had to take out all the other stuff I didn't need...


_PJ_(Posted 1+ years ago)

 Need to change:
Code: [Select]
Global filename$ = RequestFile$("","mp3")

For this to run without extraneous files.Substituting in a valid path&filename, however works fine.


_PJ_(Posted 1+ years ago)

 Calculations with the floating poitns are somewhat inaccurate.Here I have devised a method to read the duration (And much more) from the ID V2 TAGs.<a href="../logs/userloga501.html?log=1719&user=2370" target="_blank">http://www.blitzbasic.com/logs/userlog.php?log=1719&user=2370[/url] [/i]