Java:在 XY 网格上绘制 .flac 音频文件的左声道和右声道。 (示波器)

发布于 2024-10-24 01:40:08 字数 794 浏览 3 评论 0原文

我正在尝试用 Java 创建 XY 示波器,但我陷入了困境。我已通读 jFlac 的所有文档、我用来解码 .flac 音频文件的库以及免费的无损音频编解码器文档

public void processPCM(ByteData pcm) {
    byte[] temp = pcm.getData();
    line.write(temp, 0, pcm.getLen());
    leftAndRight = new int[pcm.getData().length];
    for(int i = 0; i < temp.length; i+=2){
        leftAndRight[i/2] = (temp[i + 1]<<8) + (temp[i] & 0xff);
    }
}

这是我的文件中的示例数据被放入 SourceDataLine 并播放的地方。将数据写入该行后,我希望将左音频和右音频分别转换为单独的整数数组变量 x[] 和 y[]。

这是我正在使用的文件的 StreamInfo:

PCM_SIGNED 44100.0 Hz,16 位,立体声,4 字节/帧,小尾数

从上面的代码来看,pcm.getLen() 返回值 16384,pcm.getData() 返回长度为 90112 的字节数组。这个字节数组,如果我没记错的话,我需要将左通道和右通道分开,然后转换为整数数组。这是我不明白如何完成的部分。

编辑:我将上面的代码更改为我遇到的东西,我认为可能是如何将字节数组转换为整数数组,但我仍然不知道左右通道是如何排列的。

I am trying to create an X-Y Oscilloscope in Java and I have gotten stuck. I have read through all the documentation for jFlac, the library i am using to decode my .flac audio file and the free lossless audio codec documentation here. I cannot seem to find the answer to the problem i am having. Please consider the following.

public void processPCM(ByteData pcm) {
    byte[] temp = pcm.getData();
    line.write(temp, 0, pcm.getLen());
    leftAndRight = new int[pcm.getData().length];
    for(int i = 0; i < temp.length; i+=2){
        leftAndRight[i/2] = (temp[i + 1]<<8) + (temp[i] & 0xff);
    }
}

This is where the sample data from my file is put into the SourceDataLine and is played. After i write the data to the line, i wish to convert the left and right audio into separate integer array variables x[] and y[], respectively.

This is the StreamInfo for the file i am using:

PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian

from the code above, pcm.getLen() returns a value of 16384 and pcm.getData() returns a byte array of length 90112. It's the data in this byte array, if I'm not mistaken, that i need to separate the left and right channels from, and then convert into integer arrays. This is the part that I do not understand how to accomplish.

Edit: I changed the above code to something i came across that i think might be how to get the byte array into an integer array, but i am still clueless to how the left and right channels are arranged.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-31 01:40:08

查看

因此,您需要类似的东西:(

byte[] data = pcm.getData();
int n_bytes = pcm.getLen();
int n_samples = n_bytes / (n_channels*bytes_per_sample);
int k=0;
for (int i=0; i<n_samples; ++i) {
  for (int j=0; j<n_channels; ++j) {
    output[j][i] = data[k] + (data[k+1)<<8);
    k += 2;
  }
}

警告:代码尚未经过测试并且可能完全由语法错误和错误组成,但希望这个想法足够清晰)。

Looking at the code in http://jflac.cvs.sourceforge.net/viewvc/jflac/jFLAC/src/java/org/kc7bfi/jflac/FLACDecoder.java?revision=1.13&view=markup it appears that the contents of the byte array are: a series of samples, each of which is: a series of channels, each of which is: a series of bytes representing one sample on one channel, least significant byte first. The value of getLen() is the number of valid bytes in it; there may be more data than that because the same ByteData object is reused, it may be overallocated to avoid repeated reallocation, etc.

So, you'll want something like:

byte[] data = pcm.getData();
int n_bytes = pcm.getLen();
int n_samples = n_bytes / (n_channels*bytes_per_sample);
int k=0;
for (int i=0; i<n_samples; ++i) {
  for (int j=0; j<n_channels; ++j) {
    output[j][i] = data[k] + (data[k+1)<<8);
    k += 2;
  }
}

(warning: code has not been tested and may consist entirely of syntax errors and bugs, but hopefully the idea is clear enough).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文