#Sending audio to VB-Audio Virtual Cable Microphone in Java

1 messages · Page 1 of 1 (latest)

hot marten
#

I am trying to send an .MP3 audio file I have represented as a byte[] to the VB-Audio Virtual Cable microphone.

This is my code so far:

    public void sendAudioToMicrophone(byte[] buffer) throws LineUnavailableException {
        AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
        DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);

        Mixer virtualCableMixer = getVirtualCableMixer();
        if (virtualCableMixer == null) {
            LOGGER.warn("VB-Audio Virtual Cable not found, playback to microphone is not possible");
            return;
        }

        SourceDataLine sourceDataLine = (SourceDataLine) virtualCableMixer.getLine(sourceInfo);
        sourceDataLine.open(format);
        sourceDataLine.start();

        int frameSize = format.getFrameSize();
        int bufferSize = buffer.length;
        int adjustedBufferSize = bufferSize - (bufferSize % frameSize); // Ensure buffer size is a multiple of frame size

        sourceDataLine.write(buffer, 0, adjustedBufferSize);
        sourceDataLine.drain();
        sourceDataLine.stop();
        sourceDataLine.close();
        virtualCableMixer.close();
    }

I am getting the following error message on line SourceDataLine sourceDataLine = (SourceDataLine) virtualCableMixer.getLine(sourceInfo);:

java.lang.IllegalArgumentException: Line unsupported: interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    at java.desktop/com.sun.media.sound.PortMixer.getLine(PortMixer.java:125) ~[?:?]

Does anybody know what I am doing wrong and if my code is even correct to begin with? I am certain the AudioFormat is correct as it shows up on the Sound settings on Windows. See attachment.

fierce eagleBOT
#

<@&987246399047479336> please have a look, thanks.

#

You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.