How do I save Java Discord API user input stream byte[] to a human recognizable .wav file

Viewed 43

New programmer here, I have been going through documentation (Oracle, Java Discord API, CMU Sphinx 4) for what feels like forever, stuck on this issue.

There is a userAudio object containing a User(holds unique identifiers to the specific speaker such as name, id, etc) and a byte[]("provides 20 Milliseconds of user audio data in 48KHz 16bit stereo signed BigEndian PCM"). I am wanting to save the byte[] as a .wav file by using my VoiceInputConverter class, but I don't fully understand the 20 millisecond part, and there's not much explanation?

The VoiceInputConverter will take in the audio data, save the file, convert it to Sphinx format, run voice recognition on the file and then return a string of the words spoken. CMU Sphinx 4's criteria for voice recognition is 16KHz 16bit mono little-endian.

I've tried a few different things, but cannot figure out how save the audio stream byte[] coming in from discord to a human recognizable .wav file. How do you go about doing this? Any help would be much appreciated!

(Edit)At this point I suspect that the input being streamed is providing several 20ms byte arrays (packets maybe) that can be tied together to create fully recognizable audio. I'm still not sure how to do this, but at least it gives me something to research. Still if anyone knows how the Java Discord API (JDA) AudioReceiveHandler works, please let me know as the documentation does not explain this very well!


public class VoiceInputConverter {

    byte[] audio;
    User user;

    public VoiceInputConverter(UserAudio userAudio) {
        this.audio = userAudio.getAudioData(1);  // 20 milliseconds of user audio data in 48KHz 16bit stereo signed BigEndian PCM, volume (1) meaning 100%
        this.user = userAudio.getUser();
    }

    public void saveAudioFile() {
        AudioFormat sphinxTargetFormat = new AudioFormat(16000f, 16, 1, true, false);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(audio);
        AudioInputStream audioInputStream = new AudioInputStream(byteArrayInputStream, AudioReceiveHandler.OUTPUT_FORMAT, audio.length);

        try {
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File("src/main/resources/tmp" + user.getIdLong() + ".wav"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

0 Answers
Related