简体   繁体   中英

Analyzing audio files java

I'm working on a music visualizer in Java but I'm running into a conceptual issue that I'm having trouble getting around. My program successfully reads audio, and I've figured out the Mathematical operations/Drawing functions, but my issue is figuring out how to set up my Methods such that my programs draws the visuals while the audio plays.

This is a snippet of my code for reading in the audio (after a line has been created, and AudioInputSystem, saved as ais, has been called on a File with getAudioInputStream):

byte[] data = new byte[EXT_BUFF_SIZE];
int bytesRead =0;
while(bytesRead!= -1){
   try{
      bytesRead = ais.read(data,0,data.length);
   }catch(IOException e){e.printStackTrace();}
   if(bytesRead>0){
      int bytesWritten = line.write(data,0,bytesRead);
   }
}
line.drain();
line.close();

I've tried putting calls for other functions at different places inside the while or the try to no avail. I was also hoping that perhaps putting it before line.write() would work but it doesn't.

It seems like the only solution would be to call the read and write functions on increments of the file instead of the whole thing, but I'm afraid this will create discontinuities in playback. This is my first time posting on here, after a lot of lurking, so hopefully I was detailed enough in explaining my question. If not I will try one last time: How would one organize a program to read an audio file, then do a bunch of arithmetic operations on the file, while it appears that both are happening simultaneously (or perhaps they could both happen simultaneously using Thread Scheduling or something I can't quite figure out)? Thanks in advance for any and all help!

You should be able to process and play-back the audio file in "chunks" of a specific size the same way your audio drivers process audio streams. (128, 256, 512, etc. samples per chunk) More samples per chunk means more "visualization processing" time per chunk before the audio player is ready for the next chunk of audio samples. You can experiment with chunk sizes to find a good balance between enough visualization processing time and glitch free playback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM