如何查找2D字节数组的长度以及如何在ByteArrayInputStream

时间:2016-11-24 07:11:27

标签: java audio javasound java-api javax.sound.sampled

首先,在下面的代码中,我想要做的是使用'byteBuffer [0] .length'找到2D字节数组的长度,但它实际上不起作用。当我打印'byteBuffer [0] .length'时,它输出为4而不是882000,根据我传递的参数,后者应该是正确的输出。那么如何在循环中迭代它?

其次,我想在'ByteArrayInputStream'中传递'byteBuffer',但在'ByteArrayInputStream'中我们无法传递2D数组。那么有没有一种方法可以附加值并在那里使用它?而且我还需要传递'Frequency1'和'Frequency2'的值并以.wav格式保存它们,以便我可以在我的媒体播放器中相应地播放它们。例如:救护车的警报器。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class AudioPlay {

public static void main(String[] args) throws IOException {

    Scanner in = new Scanner(System.in);
    final double SAMPLING_RATE = 44100;             // Audio sampling rate
    int time = in.nextInt();                        //Time specified by user in seconds
    int frequency1 = in.nextInt();                  //Frequency specified by the user in hz
    int frequency2 = in.nextInt();

    //Size of buffer, in case time is 10 seconds it will be [2][441000]
    float buffer[][] = new float[2][(int) (time * SAMPLING_RATE)];   

    for (int sample = 0; sample < buffer[0].length; sample++) {
        double cycle = sample / SAMPLING_RATE;                  //Fraction of cycle between samples
        buffer[0][sample] = (float) (Math.sin(2 * Math.PI * frequency1 * cycle));   //Storing value at every index of 1st row
        buffer[1][sample] = (float) (Math.sin(2 * Math.PI * frequency2 * cycle));   //Storing value at every index of 2nd row
    }

    //Size of byteBuffer, in case time is 10sec it will be [2][882000]
    byte byteBuffer[][] = new byte[2][(int)(buffer.length * 2)];
    System.out.println(byteBuffer[0].length);      // Giving wrong output

    int count = 0;

    for (int j = 0; j < byteBuffer.length; j++) {
        for (int i = 0; i < byteBuffer[0].length; i++) {
            final int x = (int) ((buffer[j][count++]) * Short.MAX_VALUE);
            byteBuffer[j][i++] = (byte) x;
            byteBuffer[j][i] = (byte) (x / 256);       //Total Value of Byte
        }
    }
    File out = new File("E:/RecordAudio7.wav"); //The path where user want the file data to be written

    //Construct an audio format, using 44100hz sampling rate, 16 bit samples, mono, and big 
    // endian byte ordering
    AudioFormat format = new AudioFormat((float) SAMPLING_RATE, 16, 1, true, false);

    // It uses bytebuffer as its buffer array that contains bytes that may be read from the stream.
    ByteArrayInputStream bais = new ByteArrayInputStream(byteBuffer[0]);

    //Constructs an audio input stream that has the requested format and length in sample frames, using audio data 
    //from the specified input stream.
    AudioInputStream audioInputStream = new AudioInputStream(bais, format, buffer.length);

    //Writes a stream of bytes representing an audio file of the specified file type to the external file provided.
    AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, out);

    audioInputStream.close();       //Closes this audio input stream
}

}

2 个答案:

答案 0 :(得分:2)

我找到了问题第一部分的答案。 代码中只有一个小小的变化:

byte byteBuffer[][] = new byte[2][(int)(buffer[0].length * 2)];

而不是

 byte byteBuffer[][] = new byte[2][(int)(buffer.length * 2)];

答案 1 :(得分:1)

声明byteBuffer不正确 您正在使用buffer.length,这就是输出为4

的原因

使用buffer[0].length * 2代替buffer.length * 2,如下所示:

byte byteBuffer[][] = new byte[2][(int)(buffer[0].length * 2)];

表示第二部分,(将2D数组传递给ByteArrayInputStream) 你可以把2D元素放到一个更长的数组中 它的长度等于byteBuffer[0].length*byteBuffer.length

您可以使用System.arraycopy()这样的内容:

int newArrayLength = byteBuffer.length*byteBuffer[0].length;
byte oneArray[] = new byte[newArrayLength];

//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
for(int b=0;b<byteBuffer.length;b++){
    System.arraycopy(byteBuffer[b], 0, oneArray, (b*byteBuffer[b].length), byteBuffer[b].length)
}

此代码的作用是转换此

byteBuffer(2D)

0- [] [] [] [] [] [] [] ......

1- [] [] [] [] [] [] [] ......

2- [] [] [] [] [] [] [] ......

进入这个:

oneArray(1D)

[][][][][][][]...[][][][][][][]...[][][][][][][]...

oneArray

使用ByteArrayInputStream

PS:如果你的应用程序在此步骤之后继续运行,最好释放为byteBuffer分配的内存,因为它不再需要,因为你将使用oneArray,你可以释放内存 - 引用它。

byteBuffer=null;