MP3 Bitrate/Track Length Algorithm Improvement

Problems compiling? Don't understand the source code? Don't know how to code your feature? Post here.

Moderator: Moderators

Gratch06
Posts: 141
Joined: 2003-05-25 06:48
Location: USA

MP3 Bitrate/Track Length Algorithm Improvement

Post by Gratch06 » 2003-07-27 02:02

OK, I currently have a function that will read the bitrate and track length from an MP3 file, but it, unfortunately takes a significant amount of time to work. I am using it to create a modified file list with bitrate and track length included (for use on private hubs with access from only this client...). Difference in boot time: a list of 100 GB of MP3 files, 1 minute without bitrate turned on. a list of 100 GB of MP3 files, 7 minutes, with bitrate and track length turned on. I don't expect anywhere near equal boot times between grabbing bitrates and not; that would be very unrealistic. What I am looking for is a shorter load time. If anyone can improve on this algorithm (or submit a totally different one), it would be much appreciated!

Code: Select all

// added by Zc
// *** start of modifications ***
int Util::GetMp3Informations(string FileName, int* BitRate, int* TrackLength)
{
   unsigned char szBuffer[4096];
   int i =0;
   long FileSize;
   unsigned char mp3Informations[2];
   
   FILE* mFile;
   
   if ((mFile = fopen(FileName.c_str(),"rb")) != NULL)
   {
      FileSize = filelength(fileno(mFile));
      fread(szBuffer,sizeof(char),4096,mFile);
      fclose(mFile);

      do
      {
         if (((((szBuffer[i] & 0xFF) & (szBuffer[i+1] & 0xF0)) == 0xF0)
            && (szBuffer[i+2] & 0xF0) != 0xF0) && (szBuffer[i+2] & 0xD) != 0x0D)
         {
            mp3Informations[0] = (szBuffer[i+1]<<4 | szBuffer[i+2]>>4) & 0xFF;
            mp3Informations[1] = (szBuffer[i+2]<<4 | szBuffer[i+3]>>4) & 0xFF;
            break;
         }
         i++;
      } while (i < 4055);

      if (i != 4055)
      {
         int mp3_id = (0x80 & mp3Informations[0]) / 128;
         int mp3_layer = (0x60 & mp3Informations[0]) / 32;
         int mp3_bitrate = 0xf & mp3Informations[0];
         int actual_bitrate = iBitRateLookup[(mp3_id * 4) | mp3_layer][mp3_bitrate];
         int mp3_freq = 0xc0 & mp3Informations[1];
         // :?
         if (mp3_freq == 64)
            mp3_freq = 1;
         //
         int mp3_pad = (0x20 & mp3Informations[1]) / 2;

         
         int sample_rate = 0;
         switch ((mp3_id * 4) | mp3_freq)
         {
            case 0:sample_rate = 22050;break;
            case 1:sample_rate = 24000;break;
            case 2:sample_rate = 16000;break;
            case 4:sample_rate = 44100;break;
            case 5:sample_rate = 48000;break;
            case 6:sample_rate = 32000;break;
         }

         double track_length = 0;
         if (sample_rate != 0)
         {
            double framesize = ((144 * 1000 * actual_bitrate) / sample_rate) + mp3_pad;
            double total_frames = FileSize / framesize;
            track_length = total_frames / 38.5;
         }
         *BitRate = actual_bitrate;
         *TrackLength = (int)track_length;
         return 0;
      }
      *BitRate = 0;
      *TrackLength = 0;
      return -1;
   }
   return -1;
}
// *** end of modifications ***


-Gratch06

ivulfusbar
Posts: 506
Joined: 2003-01-03 12:33

Post by ivulfusbar » 2003-07-27 10:27

i recomend you save it, so that only has to be run once. And next time you run the app, it will use the old calculated bitrates. Regarding the algorithm you can always take a sneak at id3lib, http://id3lib.sourceforge.net/
Everyone is supposed to download from the hubs, - I don´t know why, but I never do anymore.

GargoyleMT
DC++ Contributor
Posts: 3212
Joined: 2003-01-08 02:46
Location: .pa.us

Post by GargoyleMT » 2003-07-27 15:50

ivulfusbar wrote:i recomend you save it, so that only has to be run once. And next time you run the app, it will use the old calculated bitrates. Regarding the algorithm you can always take a sneak at id3lib, http://id3lib.sourceforge.net/


ID3Lib is a bit... bloated. For any serious ID3 tag reading/writing, I think the tagging library included as part of MAD is good. It's what JT/dieselmachine uses in his mod (source is linked in the features forum).

If you're trying to deal with VBR MP3s, parse the Xing header instead.

Note that there's another format for VBR headers: VBRI - used by the FhG encoder - though possibly not currently. I think this is more common than un-tagged VBR mp3s (ie. mp3s where the frames just vary, with no XING header).

Gratch06
Posts: 141
Joined: 2003-05-25 06:48
Location: USA

Post by Gratch06 » 2003-07-27 17:05

ivulfusbar wrote:i recomend you save it, so that only has to be run once. And next time you run the app, it will use the old calculated bitrates.

Will look into this solution! thanks for the idea....

GargoyleMT wrote:It's what JT/dieselmachine uses in his mod (source is linked in the features forum).


err....where exactly? I found a link to two screenshots, but that appears to be it...and it appears to be a mod based on DC++k, rather than a mod which would include MP3 support.

-Gratch06

GargoyleMT
DC++ Contributor
Posts: 3212
Joined: 2003-01-08 02:46
Location: .pa.us

Post by GargoyleMT » 2003-07-27 19:05

Gratch06 wrote:err....where exactly? I found a link to two screenshots, but that appears to be it...and it appears to be a mod based on DC++k, rather than a mod which would include MP3 support.


Exact link? Well, his mention is in this thread and code is here. If it's gone, I have it in my share - drop by the dev hub.

Who is online

Users browsing this forum: Google [Bot] and 0 guests