简体   繁体   English

分析音频文件Java

[英]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. 我正在用Java开发音乐可视化程序,但是遇到了一个概念性问题,难以解决。 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): 这是我的代码片段,用于读取音频(创建一行后,并且使用getAudioInputStream在文件上调用了保存为ais的AudioInputSystem):

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. 我还希望也许将其放在line.write()之前可以,但是没有用。

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)? 如果没有,我将最后一次尝试:如何组织一个程序来读取音频文件,然后对该文件进行一堆算术运算,同时看起来两者同时发生(或者可能使用Thread可能同时发生)日程安排或我无法弄清楚的事情)? 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. (每块128、256、512等样本)每块更多的样本意味着音频播放器为下一块音频样本做好准备之前,每块需要更多的“可视化处理”时间。 You can experiment with chunk sizes to find a good balance between enough visualization processing time and glitch free playback. 您可以尝试使用块大小,以在足够的可视化处理时间和无故障播放之间找到良好的平衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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