AudioInputStream.read方法究竟返回什么?

我发现了一些问题,我实际上是用AudioInputStream read的。 下面的程序只打印我得到的字节数组,但我实际上甚至不知道,如果字节实际上是样本,那么字节数组就是音频波形

 File fileIn; AudioInputStream audio_in; byte[] audioBytes; int numBytesRead; int numFramesRead; int numBytes; int totalFramesRead; int bytesPerFrame; try { audio_in = AudioSystem.getAudioInputStream(fileIn); bytesPerFrame = audio_in.getFormat().getFrameSize(); if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) { bytesPerFrame = 1; } numBytes = 1024 * bytesPerFrame; audioBytes = new byte[numBytes]; try { numBytesRead = 0; numFramesRead = 0; } catch (Exception ex) { System.out.println("Something went completely wrong"); } } catch (Exception e) { System.out.println("Something went completely wrong"); } 

在其他一些部分,我用这个读了一些字节:

 try { if ((numBytesRead = audio_in.read(audioBytes)) != -1) { numFramesRead = numBytesRead / bytesPerFrame; totalFramesRead += numFramesRead; } } catch (Exception e) { System.out.println("Had problems reading new content"); } 

首先,这段代码不是来自我。 这是我第一次阅读音频文件,所以我从网络间获得了一些帮助。 (找到链接: Java – 读取,操作和编写WAV文件 stackoverflow,谁知道。

问题是,audioBytes中的字节代表什么? 由于光源是44kHz,立体声,在那里必须有2波隐藏,我是对的吗? 那么如何从这些字节中过滤掉重要的信息呢?

//编辑

所以我添加的是这个function:

 public short[] Get_Sample() { if(samplesRead == 1024) { Read_Buffer(); samplesRead = 4; } else { samplesRead = samplesRead + 4; } short sample[] = new short[2]; sample[0] = (short)(audioBytes[samplesRead-4] + 256*audioBytes[samplesRead-3]); sample[1] = (short)(audioBytes[samplesRead-2] + 256*audioBytes[samplesRead-1]); return sample; } 

其中Read_Buffer()读取下一个1024(或更少)字节并将它们加载到audioBytes中。 样品[0]用于左侧,样品[1]用于右侧。 但我仍然不确定,因为我从这看起来非常“嘈杂”。 (编辑:使用过的WAV实际上使用了little-endian字节顺序,所以我不得不改变计算。)

AudioInputStream read()方法返回原始音频数据。 在使用返回AudioFormat的getFormat()读取音频格式之前,您不知道数据的“结构”是什么。 从AudioFormat可以获得getChannels()和getSampleSizeInBits()以及更多……这是因为AudioInputStream是针对已知格式的。

如果计算样本值,则对数据的signes和endianness有不同的可能性(如果是16位样本)。 要创建更通用的代码,请使用从AudioInputStream返回的AudioFormat对象来获取有关数据缓冲区的更多信息:

  • encoding():PCM_SIGNED,PCM_UNSIGNED ……
  • bigEndian():true或false

正如您已经发现的那样,错误的样品构建可能会导致一些不安的声音。 如果您使用各种文件,将来可能会出现问题。 如果您不提供某些格式的支持,只需检查AudioFormat的内容并抛出exception(例如javax.sound.sampled.UnsupportedAudioFileException)。 它会节省您的时间。