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

Java 将带样本的数组转换为字节数组,java,file,scala,audio,bytearray,Java,File,Scala,Audio,Bytearray,我有二维整数数组。第一个索引表示通道数。第二个表示通道中的采样数。如何将此阵列保存到音频文件中?我知道,我必须把它转换成字节数组,但我不知道怎么做 //编辑 更多信息。我已经有了绘制波形的课程。在这里: 现在,我想剪切这个波形的一部分,并将其保存到新文件中。因此,我必须剪切int[][]samplesContainer的一部分,将其转换为字节数组(我不知道如何转换),然后将其保存到与audioInputStream格式相同的文件中 //编辑 嗯。因此,最大的问题是将反向函数写入到这个函数: p

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

//编辑

更多信息。我已经有了绘制波形的课程。在这里:

现在,我想剪切这个波形的一部分,并将其保存到新文件中。因此,我必须剪切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;
}
protectedint[]getSampleArray(字节[]eightBitByteArray){
int[]toReturn=new int[getNumberOfChannels()][eightBitByteArray.length/(2*getNumberOfChannels())];
int指数=0;
//循环遍历字节[]
对于(int t=0;t

我不明白为什么在高之后,t会第二次增加。我也不知道如何从样本中获取高和低。

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

因此,给定一个类似于该代码返回的样本数组

   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;         
    }
  }
for(inti=0;i8);
溪流[((i*NumOfChannels)+j)*2]=低;
溪流[((i*NumOfChannels)+j)*2)+1]=高;
}
}

能否更具体地说明您要编写的音频文件的类型?如果您询问如何将一组整数写入二进制文件,您可能希望查看and类,更一般地说,我不知道是否有不清楚的地方,请告诉我。