Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

Error Reading Audio Bitrates

Thread view: 
jackroofman@gmail.com - 28 Oct 2005 02:55 GMT
I've been trying to put together an MP3 Player, primarily for my
personal use, and perhaps a few friends; nothing major. The one thing
I've noticed is that, at least when using a Player, the time returned
with Player.getDuration().getSeconds() assumes a bitrate of 128kbps.
I'm sure of this because the time is only right on files of that
bitrate, and it is proportionally off for the others. Thus, I need the
bitrate of the file (which I was planning to get anyway) to do some
converting.

However, I can't seem to find a good way to get the bitrate. I've
followed various leads on getting it from the Player, which failed
miserably. Using a Processor seemed hopeful, but that seems to require
that you TELL it what bitrate to re-record/re-encode it at, which I
don't want to do. Finally, the light at the end of the tunnel seems to
be in reading the first few bytes of data from the file. The bitrate is
supposed to be in there. Sometimes, it is.

If, and only if, the first byte is 255 (11111111), then the second byte
contains the right bitrate. However, in my experience, most of my MP3s
do NOT start with 255. In fact, the first few bytes seem to
consistantly be 73, 68, 51.

So, is there something wrong with the file's encoding, or is there
something in my code that I'm not accounting for? Should I be scanning
the whole file until I find a 255? I've already checked the first 500
bytes of some files and never found a 255 at all. Is there another,
EASIER way to get the bitrate of the file? Or, is there a more accurate
way to get the Time, that doesn't assume 128kbps?

PS: The code that I'm using to check the first few bytes of the file
was found here on a Google Group, posted by someone who said the code
worked with everything except one certain bitrate. I, however, can't
seem to get it to work with SEVERAL bitrates. I'm at a total loss.
Roedy Green - 28 Oct 2005 08:09 GMT
>I've been trying to put together an MP3 Player

I thought Java had no MP3 support.  Have you a third party codec?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

jackroofman@gmail.com - 28 Oct 2005 17:14 GMT
I've downloaded the Java Media Framework. Sorry I forgot to meantion
that. The playback works through javax.media.Player, but I can't find
anything in the media package that would appear to get the bitrate. At
least, not that I've been able to make work.

Thanks in advance

> >I've been trying to put together an MP3 Player
>
> I thought Java had no MP3 support.  Have you a third party codec?
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.
Knute Johnson - 28 Oct 2005 17:16 GMT
>>I've been trying to put together an MP3 Player
>
> I thought Java had no MP3 support.  Have you a third party codec?

http://java.sun.com/products/java-media/jmf/mp3/download.html

I've never gotten it to work however.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 28 Oct 2005 20:45 GMT
On Fri, 28 Oct 2005 09:16:25 -0700, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>http://java.sun.com/products/java-media/jmf/mp3/download.html

In it not part of JMF 2.1.1e. You have to download it separately as an
add-on.

I think we need to invent a Greco/latin word to describe the fear of
incrementing version numbers that leads to hard to remember numbers
like version 2.1.1e, as if there have been thousands of releases.
There are surely under 10 all told.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 28 Oct 2005 22:46 GMT
> I've been trying to put together an MP3 Player, primarily for my
> personal use, and perhaps a few friends; nothing major. The one thing
[quoted text clipped - 29 lines]
> worked with everything except one certain bitrate. I, however, can't
> seem to get it to work with SEVERAL bitrates. I'm at a total loss.

   You can read the specifications for the MP3 file format at
http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html

   The bitrate information is stored at bits 12-15, but to actually decode
them, you need to know the layer description, which is stored in bits 18 and
17.

   If your player is half decent, can you make it open source? I'm looking
for a Java based mp3 players, and I wouldn't mind contributing some code.
Specifically I'm looking for a replacement to iTunes with better Unicode
support, and support for the Ogg Vorbis file format.

   - Oliver
jackroofman@gmail.com - 31 Oct 2005 18:25 GMT
Everything I've done so far continues to fail. I know where the bitrate
values are stored in the bytes, but the data that my program is getting
is not what Windows and Winamp are. I have no idea why it's this way. I
found some code through one of the links here, but it doesn't seem to
be working. In fact, it gets one value the first time, and the
consistantly returns 128 every time after that. I don't get it.

   protected int sync(FileInputStream in) throws IOException {
       int store = 0;
       while (!finished) {
           int skip = in.read();
           while (skip != 255 && skip != -1)
               skip = in.read();
           if (skip == -1)
               throw new IOException();
           store = in.read();
           if (store >= 224)
               finished = true;
           else if (store == -1)
               throw new IOException();
           else {}
       }
       return store;
   }

   private int getBit(int input, int bit) {
       if ((input & (1 << bit)) > 0)
           return 1;
       else
           return 0;
   }

   protected int convertMPEGLevel(int in) {
       if (in == 1)
           return 1;
       else
           return 2;
   }

   protected int convertLayer(int in1, int in2) {
       if (in1 == 0 && in2 == 0)
           return 1;
       else
           return(4 - ((in1 << 1) + in2));
   }

   protected int convertBitRate(int in1, int in2, int in3, int in4) {
       int[][] convert = {
           { 0, 0, 0, 0, 0, 0 },
           { 32, 32, 32, 32, 32, 8 },
           { 64, 48, 40, 64, 48, 16 },
           { 96, 56, 48, 96, 56, 24 },
           { 128, 64, 56, 128, 64, 32 },
           { 160, 80, 64, 160, 80, 64 },
           { 192, 96, 80, 192, 96, 80 },
           { 224, 112, 96, 224, 112, 56 },
           { 256, 128, 112, 256, 128, 64 },
           { 288, 160, 128, 288, 160, 128 },
           { 320, 192, 160, 320, 192, 160 },
           { 352, 224, 192, 352, 224, 112 },
           { 384, 256, 224, 384, 256, 128 },
           { 416, 320, 256, 416, 320, 256 },
           { 448, 384, 320, 448, 384, 320 },
           { 0, 0, 0, 0, 0, 0 }
       };
       //System.out.println(in1 + ", " + in2 + ", " + in3 + ", " +
in4);
       //System.out.println(layer + ", " + level);
       int index1 = (in1 << 3) | (in2 << 2) | (in3 << 1) | in4;
       int index2 = (level - 1) * 3 + layer - 1;
       //System.out.println(index1 + ", " + index2);
       return convert[index1][index2];
   }

And then actually calling those...

protected int readProperties(File file) throws IOException {
       FileInputStream in = new FileInputStream(file);
       in.mark(15);
       int second = sync(in);
       int third = in.read();
       in.close();

       level = convertMPEGLevel(getBit(second, 3));
       layer = convertLayer(getBit(second, 2), getBit(second, 1));
       int bits = convertBitRate(getBit(third, 7), getBit(third, 6),
getBit(third, 5), getBit(third, 4));

       return bits;
   }

I just don't understand why it won't work with most of my files.
There's no way that 90% of them aren't encoded right. Any ideas?
Andrew Thompson - 31 Oct 2005 18:35 GMT
> be working. In fact, it gets one value the first time, and the
> consistantly returns 128 every time after that. I don't get it.
...
> I just don't understand why it won't work with most of my files.
> There's no way that 90% of them aren't encoded right. Any ideas?

Are most of your files encoded using VBR*?

* Variable Bit Rate.
jackroofman@gmail.com - 01 Nov 2005 18:31 GMT
No, the vast majority of the files are of fixed bitrate. The error
seems to be in reading the MPEG Version and/or layer, as it often gets
the bitrate from either the correct row or column of the array, but not
both. I don't know of any working programs that would allow me to check
the info mine gets against their own, though. Does WinAmp have a
feature that tells you that? I've never seen one.
Andrew Thompson - 01 Nov 2005 18:42 GMT
> No, the vast majority of the files are of fixed bitrate.
..
> ...Does WinAmp have a
> feature that tells you that?

..umm.  Yeah.

> I've never seen one.

Version 2.80 (an old version) has a running bit rate display
on the main UI.  If you 'right click|File Info' it has
a bunch of information on the lower left in 'MPEG Info' -
that includes something like '128 bkit' or '201 kbit VBR'.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.