0

I am able to read the amplitude of a wav file using the following code

AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(Encoding.PCM_FLOAT, 44100, 32, 1, 4, 44100, false);
AudioInputStream din = AudioSystem.getAudioInputStream(decodedFormat, in);

byte[] array = new byte[4];
int read = din.read(array);
while (read != -1) {
    ByteBuffer bb = ByteBuffer.wrap(array);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    float amplitude = bb.asFloatBuffer().get();

    System.out.println(amplitude);

    read = din.read(array);
}

How do I read the same date for the equivalent mp3 file. I tried including the MP3 SPI library

However, the amplitudes I receive are just bogus numbers that fluctuate randomly without any natural smoothness of correct amplitude array.

Is there anything I can do to read mp3 file amplitude?

InformedA
  • 179
  • 1
  • 9
  • you need to decode mp3 into PCM format to gain access to the equivalent raw audio – Scott Stensland Oct 16 '20 at 13:23
  • @ScottStensland he said he tried the MP3 SPI. I just can't see that attempt in his example. Therefore the question is incomplete. – Mark Jeronimus Nov 04 '20 at 12:27
  • to become self sufficient in digital audio I strongly suggest first writing some code to synthesize a simple sine wav which you then save as WAV file as well as save to MP3 file ... then write more code to read in a WAV file and another function to read in MP3 file ... do this for mono that is one channel audio ... this will give you the critical known good input from which you can work to produce a known good output ... also choose to do above using bit depth of 16 bits and a sample rate of 44100 Hertz give special attention to endianness of your two byte audio – Scott Stensland Nov 04 '20 at 13:06
  • in your above code be aware of the parms feed into AudioFormat especially where you are giving it value 32 and its implications ... typically folks use a bit depth of 16 bits not 32 – Scott Stensland Nov 04 '20 at 13:11

0 Answers0