实时麦克风音频播放

时间:2014-07-01 21:45:59

标签: java audio

好的,我一直在寻找,但还没有找到解决方案。我设法以byte[]格式从麦克风获得AudioFormat(44100.0f, 32, 2, true, true);输出。好的,但是现在我想通过耳机播放它进行测试。

我已经在StackOverflow上阅读了很多东西,谷歌搜索,并在这里查看,但我找不到一种方法来让我已存储的数据在耳机(或实际的音频输出设备)中播放。 / p>

我已经读过,常见的方法是从剪辑中播放它(剪辑读取音频文件的文件流),或者使用SourceDataLine。但我想将byte[]放回到OutputStream或缓冲区并实时播放。

我的代码每秒被称为arround 60次(因为它可以嵌套到3D应用程序中,也许我稍后会对其进行处理),并输出byte[]以进行图形表示。

我的实际(非工作)方法是这样的(在下面发布完整代码用于completition):

/* DATA
AudioFormat format;      //Audio format (already initialiced)
TargetDataLine line;     //Microphone line (working)
DataLine.Info info;
byte soundArray[];       //The last retrieved data
*/

private void playOnRealtime() throws LineUnavailableException {

    Clip clip = AudioSystem.getClip();

    TargetDataLine d_line;
    AudioInputStream inputStream;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            format); // format is an AudioFormat object
    if (!AudioSystem.isLineSupported(info)) {
        System.err.print("Dataline OUT not supported!");
    }

    try {
        d_line = AudioSystem.getTargetDataLine(format);
        d_line.open();
        d_line.start();
        inputStream = AudioSystem.getAudioInputStream(format, new AudioInputStream(d_line));

        clip.open(inputStream);
        clip.start();
        d_line.stop();
        clip.stop();

    } catch (LineUnavailableException ex) {
        // Handle the error.
        // ...
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这里的完整代码(如果有人发现它相关):

package main;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class Monitor implements Runnable {

    AudioFormat format;
    TargetDataLine line;
    DataLine.Info info;
    byte soundArray[];

    public Monitor() {
        format = new AudioFormat(44100.0f, 32, 2, true, true);
        // format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
        // 44100.0F, 16, 2, 4, 44100.0F, false);

        try {
            line = AudioSystem.getTargetDataLine(format);
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                format); // format is an AudioFormat object
        if (!AudioSystem.isLineSupported(info)) {
            // Handle the error ...
        }
        // Obtain and open the line.
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
        } catch (LineUnavailableException ex) {
            // Handle the error ...
        }

        soundArray = new byte[8000];

    }

    boolean started;

    @Override
    public void run() {
        try {
            line.open();
            line.start();
            line.read(soundArray, 0, 8000);
            System.out.println('A');
            printFloatArray(fragment(16, soundArray));
            line.stop();

        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            playOnRealtime();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static byte[] fragment(int n, byte[] array) {

        int length = array.length;

        byte[] result = new byte[n];

        for (int k = 0; k < n; k++) {
            result[k] = array[k * (length / n)];
        }

        return result;

    }

    private static void printFloatArray(byte[] array) {

        for (byte var : array) {
            System.out.print(String.format("%3d", var) + "\t");
        }
    }

    public byte[] getData(int n_div) {

        return fragment(n_div, soundArray);
    }

    private void playOnRealtime() throws LineUnavailableException {

        Clip clip = AudioSystem.getClip();

        TargetDataLine d_line;
        AudioInputStream inputStream;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                format); // format is an AudioFormat object
        if (!AudioSystem.isLineSupported(info)) {
            System.err.print("Dataline OUT not supported!");
        }

        try {
            d_line = AudioSystem.getTargetDataLine(format);
            d_line.open();
            d_line.start();
            inputStream = AudioSystem.getAudioInputStream(format, new AudioInputStream(d_line));

            clip.open(inputStream);
            clip.start();
            d_line.stop();
            clip.stop();

        } catch (LineUnavailableException ex) {
            // Handle the error.
            // ...
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

提前致谢!

0 个答案:

没有答案