Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: C++ | Size: 5.18 KB | Hits: 11 | Expires: Never
Copy text to clipboard
  1. #include <iostream>
  2.  
  3. #include <iomanip>
  4.  
  5. #include <fstream>
  6.  
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. struct id3Tag {
  14.  
  15.         string title;
  16.  
  17.         string artist;
  18.  
  19.         string album;
  20.  
  21.         int year;
  22.  
  23.         string comment;
  24.  
  25.         int genre;
  26.  
  27. };
  28.  
  29.  
  30.  
  31. string g_GenreTable[] = {
  32.  
  33.         "Blues",
  34.  
  35.         "Classic Rock",
  36.  
  37.         "Country",
  38.  
  39.         "Dance",
  40.  
  41.         "Disco ",
  42.  
  43.         "Funk ",
  44.  
  45.         "Grunge ",
  46.  
  47.         "Hip-Hop ",
  48.  
  49.         "Jazz ",
  50.  
  51.         "Metal  ",
  52.  
  53.         "New Age  ",
  54.  
  55.         "Oldies  ",
  56.  
  57.         "Other  ",
  58.  
  59.         "Pop  ",
  60.  
  61.         "R&B  ",
  62.  
  63.         "Rap  ",
  64.  
  65.         "Reggae ",
  66.  
  67.         "Rock  ",
  68.  
  69.         "Techno  ",
  70.  
  71.         "Industrial",  
  72.  
  73.         "Alternative ",
  74.  
  75.         "Ska  ",
  76.  
  77.         "Death Metal  ",
  78.  
  79.         "Pranks  ",
  80.  
  81.         "Soundtrack",  
  82.  
  83.         "Euro-Techno ",
  84.  
  85.         "Ambient  ",
  86.  
  87.         "Trip-Hop  ",
  88.  
  89.         "Vocal  ",
  90.  
  91.         "Jazz+Funk",  
  92.  
  93.         "Fusion  ",
  94.  
  95.         "Trance  ",
  96.  
  97.         "Classical ",
  98.  
  99.         "Instrumental",  
  100.  
  101.         "Acid  ",
  102.  
  103.         "House  ",
  104.  
  105.         "Game  ",
  106.  
  107.         "Sound Clip  ",
  108.  
  109.         "Gospel  ",
  110.  
  111.         "Noise  ",
  112.  
  113.         "Alternative Rock  ",
  114.  
  115.         "Bass  ",
  116.  
  117.         "Soul  ",
  118.  
  119.         "Punk  ",
  120.  
  121.         "Space  ",
  122.  
  123.         "Meditative  ",
  124.  
  125.         "Instrumental Pop  ",
  126.  
  127.         "Instrumental Rock  ",
  128.  
  129.         "Ethnic  ",
  130.  
  131.         "Gothic  ",
  132.  
  133.         "Darkwave  ",
  134.  
  135.         "Techno-Industrial  ",
  136.  
  137.         "Electronic  ",
  138.  
  139.         "Pop-Folk  ",
  140.  
  141.         "Eurodance",  
  142.  
  143.         "Dream  ",
  144.  
  145.         "Southern Rock  ",
  146.  
  147.         "Comedy  ",
  148.  
  149.         "Cult  ",
  150.  
  151.         "Gangsta  ",
  152.  
  153.         "Top 40  ",
  154.  
  155.         "Christian Rap  ",
  156.  
  157.         "Pop/Funk  ",
  158.  
  159.         "Jungle  ",
  160.  
  161.         "Native US  ",
  162.  
  163.         "Cabaret  ",
  164.  
  165.         "New Wave  ",
  166.  
  167.         "Psychadelic  ",
  168.  
  169.         "Rave  ",
  170.  
  171.         "Showtunes",  
  172.  
  173.         "Trailer  ",
  174.  
  175.         "Lo-Fi  ",
  176.  
  177.         "Tribal  ",
  178.  
  179.         "Acid Punk ",
  180.  
  181.         "Acid Jazz  ",
  182.  
  183.         "Polka  ",
  184.  
  185.         "Retro  ",
  186.  
  187.         "Musical  ",
  188.  
  189.         "Rock & Roll  ",
  190.  
  191.         "Hard Rock  ",
  192.  
  193.         "Folk  ",
  194.  
  195.         "Folk-Rock  ",
  196.  
  197.         "National Folk  ",
  198.  
  199.         "Swing  ",
  200.  
  201.         "Fast Fusion  ",
  202.  
  203.         "Bebob  ",
  204.  
  205.         "Latin  ",
  206.  
  207.         "Revival  ",
  208.  
  209.         "Celtic  ",
  210.  
  211.         "Bluegrass  ",
  212.  
  213.         "Avantgarde  ",
  214.  
  215.         "Gothic Rock  ",
  216.  
  217.         "Progressive Rock  ",
  218.  
  219.         "Psychedelic Rock  ",
  220.  
  221.         "Symphonic Rock  ",
  222.  
  223.         "Slow Rock  ",
  224.  
  225.         "Big Band  ",
  226.  
  227.         "Chorus  ",
  228.  
  229.         "Easy Listening  ",
  230.  
  231.         "Acoustic  ",
  232.  
  233.         "Humour  ",
  234.  
  235.         "Speech  ",
  236.  
  237.         "Chanson  ",
  238.  
  239.         "Opera  ",
  240.  
  241.         "Chamber Music  ",
  242.  
  243.         "Sonata  ",
  244.  
  245.         "Symphony  ",
  246.  
  247.         "Booty Bass  ",
  248.  
  249.         "Primus  ",
  250.  
  251.         "Porn Groove  ",
  252.  
  253.         "Satire ",
  254.  
  255.         "Slow Jam ",
  256.  
  257.         "Club ",
  258.  
  259.         "Tango ",
  260.  
  261.         "Samba ",
  262.  
  263.         "Folklore",
  264.  
  265.         "Ballad ",
  266.  
  267.         "Power Ballad ",
  268.  
  269.         "Rhythmic Soul ",
  270.  
  271.         "Freestyle ",
  272.  
  273.         "Duet ",
  274.  
  275.         "Punk Rock ",
  276.  
  277.         "Drum Solo ",
  278.  
  279.         "Acapella ",
  280.  
  281.         "Euro-House ",
  282.  
  283.         "Dance Hall ",
  284.  
  285.         "Goa ",
  286.  
  287.         "Drum & Bass ",
  288.  
  289.         "Club - House ",
  290.  
  291.         "Hardcore ",
  292.  
  293.         "Terror ",
  294.  
  295.         "Indie ",
  296.  
  297.         "BritPop ",
  298.  
  299.         "Negerpunk ",
  300.  
  301.         "Polsk Punk ",
  302.  
  303.         "Beat ",
  304.  
  305.         "Christian Gangsta Rap ",
  306.  
  307.         "Heavy Metal ",
  308.  
  309.         "Black Metal ",
  310.  
  311.         "Crossover ",
  312.  
  313.         "Contemporary Christian ",
  314.  
  315.         "Christian Rock ",
  316.  
  317.         "Merengue ",
  318.  
  319.         "Salsa ",
  320.  
  321.         "Thrash Metal ",
  322.  
  323.         "Anime ",
  324.  
  325.         "JPop ",
  326.  
  327.         "Synthpop "
  328.  
  329. };
  330.  
  331.  
  332.  
  333. string LoadFileName() {
  334.  
  335.         string name;
  336.  
  337.         cout << "Please, enter mp3 file name: ";
  338.  
  339.         getline(cin, name);
  340.  
  341.  
  342.  
  343.         return name;
  344.  
  345. }
  346.  
  347.  
  348.  
  349. bool ParseMP3(const string& name, id3Tag& tag) {
  350.  
  351.         ifstream mp3file( name.c_str(), ios::binary );
  352.  
  353.  
  354.  
  355.         if ( !mp3file ) {
  356.  
  357.                 return false;
  358.  
  359.         }
  360.  
  361.  
  362.  
  363.         tag.genre = 0;
  364.  
  365.         tag.year = 0;
  366.  
  367.  
  368.  
  369.         //max needed buffer
  370.  
  371.         const int maxTagLength = 128;
  372.  
  373.         char buff[maxTagLength] = {0};
  374.  
  375.         mp3file.seekg(-maxTagLength, ios_base::end);
  376.  
  377.  
  378.  
  379.         mp3file.read( buff, maxTagLength - 1);
  380.  
  381.         if ( mp3file.bad() ) {
  382.  
  383.                 return false;
  384.  
  385.         }
  386.  
  387.         //genre requires special handling
  388.  
  389.         char genre = 0;
  390.  
  391.         mp3file.read( &genre, 1 );
  392.  
  393.         if ( mp3file.bad() ) {
  394.  
  395.                 return false;
  396.  
  397.         }
  398.  
  399.  
  400.  
  401.         //u prva tri bajta mora pisati "TAG", inače file nema v1 tag
  402.  
  403.         if ( buff[0] != 'T' || buff[1] != 'A' || buff[2] != 'G' ) {
  404.  
  405.                 return false;
  406.  
  407.         }
  408.  
  409.  
  410.  
  411.         //sljedećih 30 bajtova čini title, pa 30 artist, itd...
  412.  
  413.         tag.title = string(buff + 3, buff + 33);
  414.  
  415.         tag.artist = string(buff + 33, buff + 63);
  416.  
  417.         tag.album = string(buff + 63, buff + 93);
  418.  
  419.  
  420.  
  421.         //godina je spremljena kao 4 char simbola, pa treba pretvoriti u broj
  422.  
  423.         string temp = string( buff + 93, buff + 97);
  424.  
  425.         tag.year = atoi(temp.c_str());
  426.  
  427.  
  428.  
  429.         tag.comment = string(buff + 97, buff + 127 );
  430.  
  431.  
  432.  
  433.         tag.genre = genre;
  434.  
  435.  
  436.  
  437.         return true;
  438.  
  439. }
  440.  
  441.  
  442.  
  443. void DisplayTag(const id3Tag& tag) {
  444.  
  445.         const unsigned int width = 20;
  446.  
  447.         cout << setw(width) << "Artist: " << tag.artist << endl;
  448.  
  449.         cout << setw(width) << "Album: " << tag.album << endl;
  450.  
  451.         cout << setw(width) << "Title: " << tag.title << endl;
  452.  
  453.         cout << setw(width) << "Year: " << tag.year << endl;
  454.  
  455.         cout << setw(width) << "Comment: " << tag.comment << endl;
  456.  
  457.         cout << setw(width) << "Genre: ";
  458.  
  459.         if ( tag.genre >= 0 && tag.genre <= 148 ) {
  460.  
  461.                 cout << g_GenreTable[tag.genre] << endl;
  462.  
  463.         }
  464.  
  465.         else {
  466.  
  467.                 cout << "Unknown" << endl;
  468.  
  469.         }
  470.  
  471. }
  472.  
  473.  
  474.  
  475. int main() {
  476.  
  477.  
  478.  
  479.         while ( true ) {
  480.  
  481.                 string mp3ime = LoadFileName();
  482.  
  483.  
  484.  
  485.                 if ( mp3ime.length() < 1 ) {
  486.  
  487.                         break;
  488.  
  489.                 }
  490.  
  491.  
  492.  
  493.                 id3Tag tag;
  494.  
  495.                 if ( ParseMP3(mp3ime, tag) ) {
  496.  
  497.                         cout << endl << "id3tag for " << mp3ime << endl;
  498.  
  499.                         cout << string(42, '=') << endl;
  500.  
  501.                         DisplayTag(tag);
  502.  
  503.                 }
  504.  
  505.                 else {
  506.  
  507.                         cerr << "Error while loading file \"" << mp3ime << "\"" << endl;
  508.  
  509.                 }
  510.  
  511.         }
  512.  
  513.         cout << endl << "Bye, bye!" << endl;
  514.  
  515.  
  516.  
  517.         return 0;
  518.  
  519. }