0

I have a problem with this method. In the following i have written a test code that reads an audio stream into a byte array (and does a convertion [i have mp3plugin.jar as external jar, if you should wonder why this works] and some other stuff, but i have only let that in to make the code executable). The problem is that the method seams not allways to do the same thing. Sometimes the AudioInputStream is getting smaller and sometimes it is not. My question is why?

package doej.test;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class Test2 {

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        int totalFramesRead = 0;
        File fileIn = new File("D:/User/Music/eclipseTestfiles/test.mp3");
        try {
            AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(fileIn);
            AudioFormat sourceFormat = mp3Stream.getFormat();
            AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
            AudioInputStream converted = AudioSystem.getAudioInputStream(convertFormat, mp3Stream);
            int bytesPerFrame = converted.getFormat().getFrameSize();
            int numBytes = 1024 * bytesPerFrame;
            byte[] audioBytes = new byte[numBytes];
            try {
                int numBytesRead = 0;

                //My simplified Test part
                System.out.println(converted.available());

Ok, so this is the original size of the audio stream. (Until here everything is fine)

                System.out.println("read " + converted.read(audioBytes));
                System.out.println(converted.available());

Why is my Stream getting smaller? (in the documentation they say it is reading into the array so i kinda understand that part)

                System.out.println("read " + converted.read(audioBytes));
                System.out.println(converted.available());

Why is my Stream not getting smaller, as it did the first time? (for this is the part i have no clue)

                while ((numBytesRead = converted.read(audioBytes)) != -1) {
                    System.out.println(converted.available());

Why is the Stream now getting smaller every eleven times? (here I don't understand the world anymore)

                }
                //End of my test part

            } catch (Exception ex) {
            }
            System.out.println("finished");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Finally this is the output i get:

6043688
read 4096
6035592
read 4096
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6029324
6029324
6029324
[...]
4444
4444
4444
0
0
0
0
0
0
0
0
0
0
0
0
finished

Thanks for your help.

John

doej1367
  • 311
  • 2
  • 12
  • what is it that you want to do? read the available() method - and just go ahead and do it – gpasch Sep 02 '18 at 08:47
  • Actually I want to write a program that cuts the dynamic range of songs. the read()-method did what I wanted, because when I looped through "audioBytes" I got the binary output of a frame that i expected. – doej1367 Sep 02 '18 at 08:54
  • After that I couldn't really export the AudioInputStream because it had only 44KB instead of 42,3MB when I export it before using the read()-method. I realized that the method rips the frames out of the AudioInputStream while reading. That is the point I started to experiment to see what behavior the read()-method has. The code above is the result of those experiments. Furthermore, I realized that this method rips the frames out only sometimes. That is so irrational that I posted it over here. – doej1367 Sep 02 '18 at 09:06

0 Answers0