Java(声音)-将2D int数组转换为字节数组

Java(声音)-将2D int数组转换为字节数组,java,bytearray,wav,javasound,mixing,Java,Bytearray,Wav,Javasound,Mixing,我正在尝试混合2个音频剪辑(相同格式,相同长度)。对于每个片段,我将它们转换为字节数组,将这些字节数组转换为2D int数组,添加它们,然后将新的2D int数组转换为字节数组,然后将其写入磁盘。 这很有效。。。只有一个频道。我创建的文件,在本例中是test.wav,只在左耳中有声音。 在我的程序中,我有一个方法,允许我将2D int制表符存储到txt文件中。我尝试分析test.wav,将其转换为字节数组,然后转换为2D int数组,与转换原始剪辑的方式相同。我得到两个向量,每个声道一个。在我的

我正在尝试混合2个音频剪辑(相同格式,相同长度)。对于每个片段,我将它们转换为字节数组,将这些字节数组转换为2D int数组,添加它们,然后将新的2D int数组转换为字节数组,然后将其写入磁盘。 这很有效。。。只有一个频道。我创建的文件,在本例中是test.wav,只在左耳中有声音。 在我的程序中,我有一个方法,允许我将2D int制表符存储到txt文件中。我尝试分析test.wav,将其转换为字节数组,然后转换为2D int数组,与转换原始剪辑的方式相同。我得到两个向量,每个声道一个。在我的第一个向量中,我得到了我应该得到的,两个剪辑的左声道的加法,但在我的第二个向量(右声道)中,我得到了-1和0的序列。我猜我的TabToByte方法有问题,但我无法确定

任何帮助都将不胜感激! 提前谢谢你

守则:

    public class Main {
    public static void main (String[] args) {

        //My audio clips
        String wavFile1 = "C:\\Users\\Alban.Alban-PC\\Documents\\Java\\Generateur-de-musiques-commerciales\\Samples\\Fa.wav";
        String wavFile2 = "C:\\Users\\Alban.Alban-PC\\Documents\\Java\\Generateur-de-musiques-commerciales\\Samples\\Drum beat.wav";

        try {

            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
            AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));
            //frameLength of each clip
            int frameLength1 = (int) clip1.getFrameLength();        
            int frameLength2 = (int) clip2.getFrameLength();
            //Frame size = 4 because I'm using 2-channels with 16-bits encoded sound
            int frameSize1 = (int) clip1.getFormat().getFrameSize();
            int frameSize2 = (int) clip2.getFormat().getFrameSize();
            //Number of channels = 2
            int numChannels = clip1.getFormat().getChannels();

            //byte array to store my entier clips
            byte[] eightBitByteArray1 = new byte[frameLength1 * frameSize1];
            byte[] eightBitByteArray2 = new byte[frameLength2 * frameSize2];

            //Converts my clips into chosen byte arrays
            clip1.read(eightBitByteArray1);
            clip2.read(eightBitByteArray2);

            //I want to store my clip samples in 2D int arrays
            int[][] toReturn1 = new int[numChannels][frameLength1];
            int[][] toReturn2 = new int[numChannels][frameLength2];
            int[][] toReturn = new int[numChannels][frameLength2];

            //I convert each byte array into 2D int arrays
            toReturn1 = ByteToTab(eightBitByteArray1);
            toReturn2 = ByteToTab(eightBitByteArray2);

            //I add my 2 int arrays 
            //This is equivalent to mixing my clips
            toReturn = addTab(toReturn1, toReturn2);

            //I convert my new int array into a new byte array
            byte[] mix = TabToByte(toReturn);


            //I store my 2D int arrays in txt files to see if I get proper results
            fichierTxt(toReturn1, "do.txt");
            fichierTxt(toReturn2, "drum.txt");
            fichierTxt(toReturn, "mix.txt");

            //I create an inputStream with my new byte array
            InputStream byteArray = new ByteArrayInputStream(mix);
            //I create a new clip 
            AudioInputStream ais = new AudioInputStream(byteArray, 
                    clip1.getFormat(), clip1.getFrameLength());
            //I write it on the disk
            AudioSystem.write(ais, 
                    AudioFileFormat.Type.WAVE, 
                    new File("C:\\Users\\Alban.Alban-PC\\Documents\\Java\\test.wav"));

        } catch (UnsupportedAudioFileException e) {
        } catch (IOException e) {e.printStackTrace(); }


    }

    //Transforms 2 bytes into a single int
    public static int getSixteenBitSample(int high, int low) {
        return (high << 8) + (low & 0x00ff);
    }   

    //Creates a byte array from a 2D int array
    public static byte[] TabToByte (int[][] tab) {
        byte[] b = new byte[tab[0].length*4];
        int count = 0;
        for (int i = 0; i < tab[0].length; i ++){
            for (int j = 0; j <tab.length; j++){
                for (int k = 0; k < 2; k++){
                    b[count] = (byte)(tab[j][i] >>> (count * 8));
                    count++;
                }
            }
        }
        return b;
    }

    //Creates a 2D int array from a byte array
    public static int[][] ByteToTab (byte[] array) {
        int sampleIndex = 0;
        int[][] toReturn = new int [2][array.length/4];

        for (int t = 0; t < array.length;) {
            for (int channel = 0; channel < 2; channel++) {
                int low = (int) array[t];
                t++;
                int high = (int) array[t];
                t++;
                int sample = getSixteenBitSample(high, low);
                toReturn[channel][sampleIndex] = sample;
            }
            sampleIndex++;
        }
        return toReturn;
    }


    //Ajouter 2 tableaux de même dimension entre eux
    public static int[][] addTab(int[][] tab1, int[][] tab2) {
        int[][] tab = new int[tab1.length][tab1[0].length]; 
        for (int i = 0; i < tab1.length; i ++) {
            for (int j = 0; j < tab1[0].length; j++) {
                tab [i][j] = tab1[i][j]+tab2[i][j];
            }
        }
        return tab;
    }


    //To write a 2D tab into a txt file
    public static void fichierTxt(int[][] tab, String s) {
        try {
            String s1 = "C:\\Users\\Alban.Alban-PC\\Documents\\Java\\";
            String st = s1 +s;
            File fichier = new File(st);
            fichier.createNewFile();
            FileWriter fichierWrite = new FileWriter(fichier);
            for (int i = 0; i < tab.length; i++){
                fichierWrite.write("[ ");
                for (int j = 0; j < tab[i].length; j ++){
                    fichierWrite.write(tab[i][j]+" ");
                }
                fichierWrite.write("]");
                fichierWrite.write(System.lineSeparator());
            }
            fichierWrite.close();
        } catch (Exception e) {}
    }
}
公共类主{
公共静态void main(字符串[]args){
//我的音频剪辑
String wavFile1=“C:\\Users\\Alban.Alban PC\\Documents\\Java\\Generateur de musiques commercials\\Samples\\Fa.wav”;
String waffile2=“C:\\Users\\Alban.Alban PC\\Documents\\Java\\Generateur de musiques commercials\\Samples\\Drum beat.wav”;
试一试{
AudioInputStream clip1=AudioSystem.getAudioInputStream(新文件(wavFile1));
AudioInputStream clip2=AudioSystem.getAudioInputStream(新文件(wavFile2));
//每个剪辑的帧长度
int frameLength1=(int)clip1.getFrameLength();
int frameLength2=(int)clip2.getFrameLength();
//帧大小=4,因为我使用的是带有16位编码声音的2个通道
int frameSize1=(int)clip1.getFormat().getFrameSize();
int frameSize2=(int)clip2.getFormat().getFrameSize();
//通道数=2
int numChannels=clip1.getFormat().getChannels();
//用于存储我的entier片段的字节数组
字节[]eightBitByteArray1=新字节[frameLength1*frameSize1];
字节[]eightBitByteArray2=新字节[frameLength2*frameSize2];
//将剪辑转换为选定的字节数组
clip1.read(八位字节1);
clip2.read(八位字节2);
//我想将剪辑样本存储在2D int数组中
int[]toReturn1=新的int[numChannels][frameLength1];
int[]toReturn2=新的int[numChannels][frameLength2];
int[]toReturn=新的int[numChannels][frameLength2];
//我将每个字节数组转换为2D int数组
toReturn1=ByteToTab(八位ByteArray1);
toReturn2=ByteToTab(八位ByteArray2);
//我添加2个int数组
//这相当于混合我的剪辑
toReturn=addTab(toReturn1、toReturn2);
//我将新的int数组转换为新的字节数组
字节[]混合=表字节(返回);
//我将2D int数组存储在txt文件中,以查看是否得到正确的结果
fichierText(toReturn1,“do.txt”);
fichierText(toReturn2,“drum.txt”);
fichierText(toReturn,“mix.txt”);
//我用我的新字节数组创建了一个inputStream
InputStream byteArray=新的ByteArrayInputStream(混合);
//我创建了一个新剪辑
AudioInputStream ais=新的AudioInputStream(byteArray,
clip1.getFormat(),clip1.getFrameLength();
//我把它写在磁盘上
音频系统写入(ais,
AudioFileFormat.Type.WAVE,
新文件(“C:\\Users\\Alban.Alban PC\\Documents\\Java\\test.wav”);
}捕获(不支持的数据文件异常e){
}catch(IOE异常){e.printStackTrace();}
}
//将2个字节转换为单个整数
公共静态整型GetSixteenbitsSample(整型高,整型低){
返回(高>(计数*8));
计数++;
}
}
}
返回b;
}
//从字节数组创建二维整数数组
公共静态int[]ByteToTab(字节[]数组){
int-sampleIndex=0;
int[]toReturn=newint[2][array.length/4];
for(int t=0;tb[count] = (byte)(tab[j][i] >>> (count * 8));
count++;
b[count] = (byte)(tab[j][i] >>> (k * 8));
count++;