0

How does the AudioInputStream.read(byte[] b) function work?

I have posted the questeion also here [https://stackoverflow.com/questions/52129685/how-does-the-audioinputstream-readbyte-b-function-work] I have a problem with this [ "https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioInputStream.html#read(byte[])" ] 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? I haven't enough space here to post the full code but it is the following method that - called multiple times - results in different outcomes. System.out.println("read " + converted.read(audioBytes)); System.out.println(converted.available()); The first time the javax.sound.sampled.AudioInputStream called "converted" gets smaller the second time it doesn't. To see the full code I recommend watching out my post on stackoverflow https://stackoverflow.com/questions/52129685/how-does-the-audioinputstream-readbyte-b-function-work or https://stackoverflow.com/q/52129685/6307611

1st Sep 2018, 5:54 PM
Johann Leis
Johann Leis - avatar
3 ответов
+ 1
AudioInputStream read always a number of bytes multiple of "frameSize" and if in backed stream read a fraction multiple (at example 3/2) the fracional part going store inside an internal buffer (readed while it dont fill a number of bytes equals to "frameSize" count in internal buffer). Futhermore "avaible" method DONT return avaible bytes for backed stream BUT it return the maximum bytes avaible for read many (or one) FULL frames (then it not count fractional part of). I think that you will understand better if you see the code: http://www.docjar.com/html/api/javax/sound/sampled/AudioInputStream.java.html
1st Sep 2018, 6:53 PM
KrOW
KrOW - avatar
+ 1
@KrOW First of all thanks for the link. nice work. 258 // make sure we don't read fractions of a frame. 259 if( (len%frameSize) != 0 ) { 260 len -= (len%frameSize); 261 if (len == 0) { 262 return 0; 263 } 264 } @KrOW Is this the code part you are refering to? Wouldn't it "return 0;" if that was the case instead of returning 4069?
1st Sep 2018, 8:51 PM
Johann Leis
Johann Leis - avatar
0
I dont think because probably your frame size is 4 (2 channells for 2 byte per sample) and because byte buffer size is 4096 you get: 4096%4 = 0 then continue on next code... Futhermore i forget that you decoding mp3 then all is more complicated because coding/decoding.. See here for source of decoding input stream used by mp3plugin https://github.com/sahriartoufiq/com.sahriar.projects/blob/master/SMPlayer/com/sun/media/codec/audio/mp3/JS_MP3DecoderStream.java
2nd Sep 2018, 9:48 AM
KrOW
KrOW - avatar