Ooops
March 05, 2021, 07:22:18 AM

Author Topic: [bmx] mp3 tag reader by BlitzSupport [ 1+ years ago ]  (Read 535 times)

Offline BlitzBot

[bmx] mp3 tag reader by BlitzSupport [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : mp3 tag reader
Author : BlitzSupport
Posted : 1+ years ago

Description : I needed to obtain tag info from mp3 files... so I wrote this.

The function here just returns song and artist name, but it should be easy to see how to return the other information.

This is the quick format summary I used -- you just read the last 128 bytes of the file and check for "TAG", then read the characters:

ID3v1 identification -> 3 characters (the word "TAG")
Song Title -> 30 characters
Artist -> 30 characters
Album -> 30 characters
Year -> 4 characters
Comment -> 30 characters
Genre -> 1 byte


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. ' ID3v1 tag reader
  4.  
  5. Global MP3_GENRE:String [192]
  6.  
  7. ' Standard tags:
  8.  
  9. MP3_GENRE[0] = "Blues"
  10. MP3_GENRE[1] = "Classic Rock"
  11. MP3_GENRE[2] = "Country"
  12. MP3_GENRE[3] = "Dance"
  13. MP3_GENRE[4] = "Disco"
  14. MP3_GENRE[5] = "Funk"
  15. MP3_GENRE[6] = "Grunge"
  16. MP3_GENRE[7] = "Hip-Hop"
  17. MP3_GENRE[8] = "Jazz"
  18. MP3_GENRE[9] = "Metal"
  19. MP3_GENRE[10] = "New Age"
  20. MP3_GENRE[11] = "Oldies"
  21. MP3_GENRE[12] = "Other"
  22. MP3_GENRE[13] = "Pop"
  23. MP3_GENRE[14] = "R&B"
  24. MP3_GENRE[15] = "Rap"
  25. MP3_GENRE[16] = "Reggae"
  26. MP3_GENRE[17] = "Rock"
  27. MP3_GENRE[18] = "Techno"
  28. MP3_GENRE[19] = "Industrial"
  29. MP3_GENRE[20] = "Alternative"
  30. MP3_GENRE[21] = "Ska"
  31. MP3_GENRE[22] = "Death Metal"
  32. MP3_GENRE[23] = "Pranks"
  33. MP3_GENRE[24] = "Soundtrack"
  34. MP3_GENRE[25] = "Euro-Techno"
  35. MP3_GENRE[26] = "Ambient"
  36. MP3_GENRE[27] = "Trip-Hop"
  37. MP3_GENRE[28] = "Vocal"
  38. MP3_GENRE[29] = "Jazz+Funk"
  39. MP3_GENRE[30] = "Fusion"
  40. MP3_GENRE[31] = "Trance"
  41. MP3_GENRE[32] = "Classical"
  42. MP3_GENRE[33] = "Instrumental"
  43. MP3_GENRE[34] = "Acid"
  44. MP3_GENRE[35] = "House"
  45. MP3_GENRE[36] = "Game"
  46. MP3_GENRE[37] = "Sound Clip"
  47. MP3_GENRE[38] = "Gospel"
  48. MP3_GENRE[39] = "Noise"
  49. MP3_GENRE[40] = "AlternRock"
  50. MP3_GENRE[41] = "Bass"
  51. MP3_GENRE[42] = "Soul"
  52. MP3_GENRE[43] = "Punk"
  53. MP3_GENRE[44] = "Space"
  54. MP3_GENRE[45] = "Meditative"
  55. MP3_GENRE[46] = "Instrumental Pop"
  56. MP3_GENRE[47] = "Instrumental Rock"
  57. MP3_GENRE[48] = "Ethnic"
  58. MP3_GENRE[49] = "Gothic"
  59. MP3_GENRE[50] = "Darkwave"
  60. MP3_GENRE[51] = "Techno-Industrial"
  61. MP3_GENRE[52] = "Electronic"
  62. MP3_GENRE[53] = "Pop-Folk"
  63. MP3_GENRE[54] = "Eurodance"
  64. MP3_GENRE[55] = "Dream"
  65. MP3_GENRE[56] = "Southern Rock"
  66. MP3_GENRE[57] = "Comedy"
  67. MP3_GENRE[58] = "Cult"
  68. MP3_GENRE[59] = "Gangsta"
  69. MP3_GENRE[60] = "Top 40"
  70. MP3_GENRE[61] = "Christian Rap"
  71. MP3_GENRE[62] = "Pop/Funk"
  72. MP3_GENRE[63] = "Jungle"
  73. MP3_GENRE[64] = "Native American"
  74. MP3_GENRE[65] = "Cabaret"
  75. MP3_GENRE[66] = "New Wave"
  76. MP3_GENRE[67] = "Psychadelic"
  77. MP3_GENRE[68] = "Rave"
  78. MP3_GENRE[69] = "Showtunes"
  79. MP3_GENRE[70] = "Trailer"
  80. MP3_GENRE[71] = "Lo-Fi"
  81. MP3_GENRE[72] = "Tribal"
  82. MP3_GENRE[73] = "Acid Punk"
  83. MP3_GENRE[74] = "Acid Jazz"
  84. MP3_GENRE[75] = "Polka"
  85. MP3_GENRE[76] = "Retro"
  86. MP3_GENRE[77] = "Musical"
  87. MP3_GENRE[78] = "Rock & Roll"
  88. MP3_GENRE[79] = "Hard Rock"
  89.  
  90. ' Winamp extended tags:
  91.  
  92. MP3_GENRE[80] = "Folk"
  93. MP3_GENRE[81] = "Folk-Rock"
  94. MP3_GENRE[82] = "National Folk"
  95. MP3_GENRE[83] = "Swing"
  96. MP3_GENRE[84] = "Fast Fusion"
  97. MP3_GENRE[85] = "Bebop"
  98. MP3_GENRE[86] = "Latin"
  99. MP3_GENRE[87] = "Revival"
  100. MP3_GENRE[88] = "Celtic"
  101. MP3_GENRE[89] = "Bluegrass"
  102. MP3_GENRE[90] = "Avantgarde"
  103. MP3_GENRE[91] = "Gothic Rock"
  104. MP3_GENRE[92] = "Progressive Rock"
  105. MP3_GENRE[93] = "Psychedelic Rock"
  106. MP3_GENRE[94] = "Symphonic Rock"
  107. MP3_GENRE[95] = "Slow Rock"
  108. MP3_GENRE[96] = "Big Band"
  109. MP3_GENRE[97] = "Chorus"
  110. MP3_GENRE[98] = "Easy Listening"
  111. MP3_GENRE[99] = "Acoustic"
  112. MP3_GENRE[100] = "Humour"
  113. MP3_GENRE[101] = "Speech"
  114. MP3_GENRE[102] = "Chanson"
  115. MP3_GENRE[103] = "Opera"
  116. MP3_GENRE[104] = "Chamber Music"
  117. MP3_GENRE[105] = "Sonata"
  118. MP3_GENRE[106] = "Symphony"
  119. MP3_GENRE[107] = "Booty Bass"
  120. MP3_GENRE[108] = "Primus"
  121. MP3_GENRE[109] = "Porn groove"
  122. MP3_GENRE[110] = "Satire"
  123. MP3_GENRE[111] = "Slow Jam"
  124. MP3_GENRE[112] = "Club"
  125. MP3_GENRE[113] = "Tango"
  126. MP3_GENRE[114] = "Samba"
  127. MP3_GENRE[115] = "Folklore"
  128. MP3_GENRE[116] = "Ballad"
  129. MP3_GENRE[117] = "Power Ballad"
  130. MP3_GENRE[118] = "Rhythmic Soul"
  131. MP3_GENRE[119] = "Freestyle"
  132. MP3_GENRE[120] = "Duet"
  133. MP3_GENRE[121] = "Punk rock"
  134. MP3_GENRE[122] = "Drum Solo"
  135. MP3_GENRE[123] = "A capella"
  136. MP3_GENRE[124] = "Euro-House"
  137. MP3_GENRE[125] = "Dance Hall"
  138. MP3_GENRE[126] = "Goa Trance"
  139. MP3_GENRE[127] = "Drum & Bass"
  140. MP3_GENRE[128] = "Club-House"
  141. MP3_GENRE[129] = "Hardcore Techno"
  142. MP3_GENRE[130] = "Terror"
  143. MP3_GENRE[131] = "Indie"
  144. MP3_GENRE[132] = "BritPop"
  145. MP3_GENRE[133] = "Afro-punk"
  146. MP3_GENRE[134] = "Polsk Punk"
  147. MP3_GENRE[135] = "Beat"
  148. MP3_GENRE[136] = "Christian Gangsta Rap"
  149. MP3_GENRE[137] = "Heavy Metal"
  150. MP3_GENRE[138] = "Black Metal"
  151. MP3_GENRE[139] = "Crossover"
  152. MP3_GENRE[140] = "Contemporary Christian"
  153. MP3_GENRE[141] = "Christian Rock"
  154. MP3_GENRE[142] = "Merengue"
  155. MP3_GENRE[143] = "Salsa"
  156. MP3_GENRE[144] = "Thrash Metal"
  157. MP3_GENRE[145] = "Anime"
  158. MP3_GENRE[146] = "Jpop"
  159. MP3_GENRE[147] = "Synthpop"
  160. MP3_GENRE[148] = "Abstract"
  161. MP3_GENRE[149] = "Art Rock"
  162. MP3_GENRE[150] = "Baroque"
  163. MP3_GENRE[151] = "Bhangra"
  164. MP3_GENRE[152] = "Big Beat"
  165. MP3_GENRE[153] = "Breakbeat"
  166. MP3_GENRE[154] = "Chillout"
  167. MP3_GENRE[155] = "Downtempo"
  168. MP3_GENRE[156] = "Dub"
  169. MP3_GENRE[157] = "EBM"
  170. MP3_GENRE[158] = "Eclectic"
  171. MP3_GENRE[159] = "Electro"
  172. MP3_GENRE[160] = "Electroclash"
  173. MP3_GENRE[161] = "Emo"
  174. MP3_GENRE[162] = "Experimental"
  175. MP3_GENRE[163] = "Garage"
  176. MP3_GENRE[164] = "Global"
  177. MP3_GENRE[165] = "IDM"
  178. MP3_GENRE[166] = "Illbient"
  179. MP3_GENRE[167] = "Industro-Goth"
  180. MP3_GENRE[168] = "Jam Band"
  181. MP3_GENRE[169] = "Krautrock"
  182. MP3_GENRE[170] = "Leftfield"
  183. MP3_GENRE[171] = "Lounge"
  184. MP3_GENRE[172] = "Math Rock"
  185. MP3_GENRE[173] = "New Romantic"
  186. MP3_GENRE[174] = "Nu-Breakz"
  187. MP3_GENRE[175] = "Post-Punk"
  188. MP3_GENRE[176] = "Post-Rock"
  189. MP3_GENRE[177] = "Psytrance"
  190. MP3_GENRE[178] = "Shoegaze"
  191. MP3_GENRE[179] = "Space Rock"
  192. MP3_GENRE[180] = "Trop Rock"
  193. MP3_GENRE[181] = "World Music"
  194. MP3_GENRE[182] = "Neoclassical"
  195. MP3_GENRE[183] = "Audiobook"
  196. MP3_GENRE[184] = "Audio Theatre"
  197. MP3_GENRE[185] = "Neue Deutsche Welle"
  198. MP3_GENRE[186] = "Podcast"
  199. MP3_GENRE[187] = "Indie Rock"
  200. MP3_GENRE[188] = "G-Funk"
  201. MP3_GENRE[189] = "Dubstep"
  202. MP3_GENRE[190] = "Garage Rock"
  203. MP3_GENRE[191] = "Psybient"
  204.  
  205. Function GetTags:String (f:String)
  206.  
  207.         Local tag:String
  208.        
  209.         Local mp3:TStream = ReadStream (f)
  210.        
  211.         If mp3
  212.        
  213.                 SeekStream mp3, StreamSize (mp3) - 128
  214.                
  215.                 If ReadString (mp3, 3) = "TAG"
  216.                
  217.                         Local song:String               = Trim (ReadString (mp3, 30))
  218.                         Local artist:String             = Trim (ReadString (mp3, 30))
  219.                         Local album:String              = Trim (ReadString (mp3, 30))
  220.                         Local year:String               = Trim (ReadString (mp3, 4))
  221.                         Local comment:String    = Trim (ReadString (mp3, 30))
  222.  
  223.                         Local genre:String
  224.                        
  225.                         Local readgenre:Int = ReadByte (mp3)
  226.  
  227.                         ' Safety check...
  228.                        
  229.                         If readgenre > -1 And readgenre < 192
  230.                                 genre = MP3_GENRE [readgenre]
  231.                         Else
  232.                                 genre = "Unknown"
  233.                         EndIf
  234.                
  235.                         ' Returning song and artist, if available (just add the
  236.                         ' other strings above if you want more). Strings available:
  237.                        
  238.                         ' song
  239.                         ' artist
  240.                         ' album
  241.                         ' year
  242.                         ' comment
  243.                         ' genre
  244.                        
  245.                         If song
  246.                                 tag = song
  247.                                 If artist Then tag = tag + " by " + artist
  248.                         EndIf
  249.                        
  250.                 EndIf
  251.                
  252.                 CloseStream mp3
  253.                
  254.         EndIf
  255.  
  256.         ' No tag info? Return filename without extension...
  257.                
  258.         If Not tag Then tag = Trim (StripExt (f))
  259.  
  260.         Return tag
  261.        
  262. End Function
  263.  
  264. ' -------------------------------------------------------
  265. ' Demo - stick some mp3s in same folder as source/exe...
  266. ' -------------------------------------------------------
  267.  
  268. Local files:String[]
  269.  
  270. files = LoadDir (CurrentDir ())
  271.  
  272. For Local t:String = EachIn files
  273.         If Lower (ExtractExt (t)) = "mp3"
  274.                 Print GetTags (t)
  275.         EndIf
  276. Next


Comments :


Blitzplotter(Posted 1+ years ago)

 Nice work, have been toying with the idea of an MP3 browser/player and this will help, cheers.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal