模拟麦克风输入 [英] Simulate microphone Input

查看:209
本文介绍了模拟麦克风输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个读取wav文件的小程序,并将输出发送,就像它来自我的麦克风一样。不幸的是,我对声音API没有多少经验。

I am trying to write a little program that reads a wav file and sends the output as if it would come from my microphone. Unfortunately I do not have much experience with the sound API.

背景:我基本上想要实现的是一个在我语音聊天时播放声音的程序(即Teamspeak,Ventrilo)。为了让它现在起作用,我必须将录音设备切换到你听到了什么,播放声音,然后切换回麦克风。该程序应该模拟来自麦克风的输入。

Background: What I am basically trying to realize is a program that plays a sound while I am in a voicechat (i.e Teamspeak, Ventrilo). To get that to work now I would have to switch the recording device to "What you hear", play the sound and then switch back to microphone. The program should simulate input from the microphone.

到目前为止,除了播放声音之外,我无法进一步。我想我只需要一个不同的SourceLine?

So far I could not get any further than just playing the sound. I guess I just need a different SourceLine?

public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

public void playSound(File soundFile) {
    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    audioFormat = audioStream.getFormat();
    DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
        sourceLine = (SourceDataLine) AudioSystem.getLine(infoIn);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    sourceLine.start();

    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
    }
    sourceLine.drain();
    sourceLine.close();
}
 }

解决方案:
安装VB音频线( http://vb-audio.pagesperso-orange.fr/Cable / )。这是捐赠软件。制作VB输出标准录音设备。在麦克风属性中选择VB-Input作为回放。

Solution: Install VB Audio Cable (http://vb-audio.pagesperso-orange.fr/Cable/). It's donationware. Make VB-Output standard recording device. In the microphone properties choose VB-Input as playback.

public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

public void playSound(File soundFile) {
    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    audioFormat = audioStream.getFormat();
    DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
        Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
        Mixer mixer = null;
        for (int i = 0; i < mixerInfos.length; i++) {
            System.out.println(mixerInfos[i].getName());
            if (mixerInfos[i].getName().equals(
                    "CABLE Input (VB-Audio Virtual Cable)")) {
                mixer = AudioSystem.getMixer(mixerInfos[i]);
                break;
            }
        }
        sourceLine = (SourceDataLine) mixer.getLine(infoIn);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    }
    sourceLine.start();
    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
    }
    sourceLine.drain();
    sourceLine.close();
}
}


推荐答案

你可以安装一些程序,如虚拟音频线,并从您的音乐播放器重定向播放声音到虚拟输入。在您的程序中,您必须侦听该虚拟输入源,您可以将测试重定向到普通耳机或扬声器接收信号或将其保存到文件中。

You can install some program like Virtual Audio Cable and from your music player redirect playing sound to virtual input. In your program you must listen that virtual input source and you can for test redirect to normal headphones or speaker received signal or saved it to file.

这篇关于模拟麦克风输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆