将带有样本的数组转换为字节数组

发布于 2024-12-03 21:58:01 字数 1491 浏览 0 评论 0原文

我有二维整数数组。第一个索引表示通道数。第二个表示通道中的样本数。如何将该数组保存到音频文件中?我知道,我必须将其转换为字节数组,但我不知道该怎么做。

// 编辑

更多信息。我已经有一门用于绘制波形的课程。它在这里:

http://javafaq.nu/java-example-code-716.html

现在我想剪切该波形的一部分并将其保存到新文件中。所以我必须剪切int[][]samplesContainer的一部分,将其转换为字节数组(我不知道如何),然后将其保存到与audioInputStream格式相同的文件中。

// 编辑

确定。所以最大的问题是向这个函数写入反函数:

protected int[][] getSampleArray(byte[] eightBitByteArray) {
int[][] toReturn = new int[getNumberOfChannels()][eightBitByteArray.length / (2 * getNumberOfChannels())];
int index = 0;
    //loop through the byte[]
    for (int t = 0; t < eightBitByteArray.length;) {
        //for each iteration, loop through the channels
        for (int a = 0; a < getNumberOfChannels(); a++) {
            //do the byte to sample conversion
            //see AmplitudeEditor for more info
            int low = (int) eightBitByteArray[t];
            t++;
            int high = (int) eightBitByteArray[t];
            t++;
            int sample = (high << 8) + (low & 0x00ff);

            if (sample < sampleMin) {
                sampleMin = sample;
            } else if (sample > sampleMax) {
                sampleMax = sample;
            }
            //set the value.
        toReturn[a][index] = sample;
        }
        index++;
        }
    return toReturn;
}

我不明白为什么在 high 之后会有 t 的第二次增量。我也不知道如何从样本中获得高点和低点。

I have two-dimensional array of integers. First index indicates the number of channels. The second one indicates the number of sample in the channel. How can I save this array into the audio file? I know, I have to convert it to byte array, but I have no idea how to do that.

// edit

More info. I already have a class for drawing a waveform. It is here:

http://javafaq.nu/java-example-code-716.html

now I want to cut part of this wave and save it to the new file. So I have to cut part of int[][] samplesContainer, convert it to byte array (I don't know how) and then save it to file with the same format as audioInputStream.

// edit

OK. So the biggest problem is to write inverted function to this one:

protected int[][] getSampleArray(byte[] eightBitByteArray) {
int[][] toReturn = new int[getNumberOfChannels()][eightBitByteArray.length / (2 * getNumberOfChannels())];
int index = 0;
    //loop through the byte[]
    for (int t = 0; t < eightBitByteArray.length;) {
        //for each iteration, loop through the channels
        for (int a = 0; a < getNumberOfChannels(); a++) {
            //do the byte to sample conversion
            //see AmplitudeEditor for more info
            int low = (int) eightBitByteArray[t];
            t++;
            int high = (int) eightBitByteArray[t];
            t++;
            int sample = (high << 8) + (low & 0x00ff);

            if (sample < sampleMin) {
                sampleMin = sample;
            } else if (sample > sampleMax) {
                sampleMax = sample;
            }
            //set the value.
        toReturn[a][index] = sample;
        }
        index++;
        }
    return toReturn;
}

I don't understand why there is second incrementation of t, after high. I also have no idea how can i get high and low from sample.

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

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

发布评论

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

评论(1

梦里°也失望 2024-12-10 21:58:01

您发布的代码将示例流逐字节读取到示例数组中。该代码假设在流中,每两个 8 位字节形成一个 16 位样本,并且每个 NumOfChannels 通道都有一个样本。

因此,给定一个样本数组(例如该代码返回的

   int[][] samples; 

样本数组)和一个用于流式传输的字节数组,

   byte[] stream;

您可以通过这种方式构建相反的字节流

  for (int i=0; i<NumOfSamples; i++) {
    for (int j=0; j<NumOfChannels; j++) {
      int sample=samples[i][j];
      byte low = (byte) (sample & 0xff) ;
              byte high = (byte) ((sample & 0xff00 ) >> 8);
              stream[((i*NumOfChannels)+j)*2] = low;    
              stream[(((i*NumOfChannels)+j)*2)+1] = high;         
    }
  }

The code you posted reads a sample stream, byte by byte, into the samples array. The code assumes that, in the stream, every two 8-bit bytes form a 16-bit sample, and that there is one sample for each of the NumOfChannels channels.

So, given an array of samples like the one returned by that code,

   int[][] samples; 

and a byte array for streaming,

   byte[] stream;

you might build the converse stream of bytes this way

  for (int i=0; i<NumOfSamples; i++) {
    for (int j=0; j<NumOfChannels; j++) {
      int sample=samples[i][j];
      byte low = (byte) (sample & 0xff) ;
              byte high = (byte) ((sample & 0xff00 ) >> 8);
              stream[((i*NumOfChannels)+j)*2] = low;    
              stream[(((i*NumOfChannels)+j)*2)+1] = high;         
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文