如何将声音打印为字节数组?还是int数组?

时间:2014-11-05 08:17:56

标签: java audio audio-streaming audio-recording javasound

我用java做反相声音。(反相是反射波.x坐标没有改变,但y坐标是颠倒的。)

在反射声波之前,我必须从声音中获取字节数组(或int数组)。

我从笔记本电脑的麦克风听到声音。

这是我的 CODE (我有原始代码,在网络中将声音录制为文件。我修改了一点)

public class NoiseController extends Thread{
private TargetDataLine line;
private AudioInputStream audioInputStream;

public NoiseController(TargetDataLine line) {
    this.line = line;
    this.audioInputStream = new AudioInputStream(line);
}

public void start() {
    line.start();
    super.start();
}

public void stopRecording() {
    line.stop();
    line.close();
}

public void run() {
    try {
        int packet;
        while((packet = audioInputStream.read()) != -1)
            System.out.println(packet);
    }
    catch(IOException ioe) {
        ioe.getStackTrace();
    }
}

public static void main(String[] args) {
    AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
    TargetDataLine targetDataLine = null;

    try {
        targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
        targetDataLine.open(audioFormat);
    }
    catch(LineUnavailableException lue) {
        out("unable to get a recording line");
        lue.printStackTrace();
        System.exit(-1);
    }

    AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
    NoiseController recorder = new NoiseController(targetDataLine);
    System.out.println(targetDataLine);
    System.out.println(targetType);

    out("Press ENTER to start the recording.");

    try {
        System.in.read();
    }
    catch(IOException ioe) {
        ioe.printStackTrace();
    }

    recorder.start();
    out("Recording...");
    out("Press ENTER to stop the recording.");

    try {
        System.in.read();
        System.in.read();
    }
    catch(IOException ioe) {
        ioe.getStackTrace();
    }

    recorder.stopRecording();
    out("Recording stopped.");
}

private static void out(String msg) {
    System.out.println(msg);
}

}

但是,录制时控制台不会打印任何内容... 它只显示

com.sun.media.sound.DirectAudioDevice$DirectTDL@25154f_ WAVE Press ENTER to start the recording. Recording... Press ENTER to stop the recording. Recording stopped.

如果我像run()

那样修改AudioSystem.write(stream, fileType, out);

而不是

int packet;
        while((packet = audioInputStream.read()) != -1)
            System.out.println(packet);

程序保存wav文件。

我的课程有什么问题?

1 个答案:

答案 0 :(得分:0)

你没有像Uwe Allner所说的那样打印Exception。

我也尝试纠正它,我认为结果必须是这样的:

 public class NoiseController extends Thread {
    private final TargetDataLine        line;
    private final AudioInputStream  audioInputStream;

    public NoiseController(final TargetDataLine line) {
        this.line = line;
        this.audioInputStream = new AudioInputStream(line);
    }

    @Override
    public void start() {
        line.start();
        super.start();
    }

    public void stopRecording() {
        line.stop();
        line.close();
        try {
            audioInputStream.close();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            final int bufferSize = 1024;
            int read = 0;
            final byte[] frame = new byte[bufferSize];
            while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) {
                // only the first read bytes are valid
                System.out.println(Arrays.toString(frame));
            }
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public static void main(final String[] args) {
        final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
        final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
        TargetDataLine targetDataLine = null;

        try {
            targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
            targetDataLine.open(audioFormat);
        } catch (final LineUnavailableException lue) {
            out("unable to get a recording line");
            lue.printStackTrace();
            System.exit(-1);
        }

        final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
        final NoiseController recorder = new NoiseController(targetDataLine);
        System.out.println(targetDataLine);
        System.out.println(targetType);

        out("Press ENTER to start the recording.");

        try {
            System.in.read();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }

        recorder.start();
        out("Recording...");
        out("Press ENTER to stop the recording.");

        try {
            System.in.read();
            System.in.read();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }

        recorder.stopRecording();
        out("Recording stopped.");
    }

    private static void out(final String msg) {
        System.out.println(msg);
    }
 }