Simulate microphone Input
Asked Answered
H

1

7

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.

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.

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();
}
 }

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();
}
}
Hipparch answered 17/1, 2014 at 18:23 Comment(1)
I'm confused; how do you mean "as if it would come from my microphone"?Snapdragon
P
2

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.

Parr answered 17/1, 2014 at 18:39 Comment(1)
Yes, that worked. I had to install VoiceMeeter. That's from the same guy as Virtual Audio Cable which you mentioned. I'll edit my question above with the final solution. I am not happy with how I obtain the Line Info. Is there a different way if you know how the mixer is called instead of iterating over all mixers?Hipparch

© 2022 - 2024 — McMap. All rights reserved.