Java: Converting Audioinputstream BACK to InputStream?

177 views Asked by At

Ok, this is going to sound odd, but hear me out. I've been using Mendix to deal with an app that manipulates wav files. I love how easy Mendix makes the front end. I have a FileDocument with a WAV file in it. I use the custom java below to read the wav file as an InputStream, then convert to an AudioInput stream. I then concatenate the sound to itself. Basically a drum sound gets duplicated and repeated so it sounds like two drum sounds, one after another.

Then I save it two ways: (1) Back to a save file on my computer. This actual wav file is just fine. I can open it and play the wav file and I hear the back to back drum sounds. When I save it back to the FileDocument in mendix (which requires an inputstream), I feed it the audioinput stream instead. The size of the file appears to be exactly the same, but if I try to play this file I get an error about the file not being formatted properly. Does anyone know if there is a way to convert a file from an audioinputstream BACK to an inputstream? If I load then wav file from the first method into an inputstream and save it to the FileDocument, it works fine, but this is an extra step that eats up resources and forces me to save the file elsewhere, which I don't want to do.

In otherwords... If an AudioInputStream is an InputStream... then I'm not sure why feeing Mendix an Inputstream works, but not an AudioInputStream

         InputStream is;
         InputStream is2;
         InputStream iscopy;
         InputStream is2copy;

        TestFile myobject = this.Parameter.get(a);
        is = Core.getFileDocumentContent(getContext(), myobject.getMendixObject());
        is2 = Core.getFileDocumentContent(getContext(), myobject.getMendixObject());
        
        iscopy = Core.getFileDocumentContent(getContext(), myobject.getMendixObject());
        is2copy = Core.getFileDocumentContent(getContext(), myobject.getMendixObject());
        
        AudioInputStream clip1 = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
        AudioInputStream clip2 = AudioSystem.getAudioInputStream(new BufferedInputStream(is2));
        
        AudioInputStream clip1copy = AudioSystem.getAudioInputStream(new BufferedInputStream(iscopy));
        AudioInputStream clip2copy = AudioSystem.getAudioInputStream(new BufferedInputStream(is2copy));

        AudioInputStream appendedFiles = new AudioInputStream(new SequenceInputStream(clip1, clip2), clip1.getFormat(), clip1.getFrameLength() + clip2.getFrameLength());
        AudioInputStream appendedFiles2 = new AudioInputStream(new SequenceInputStream(clip1copy, clip2copy), clip1copy.getFormat(), clip1copy.getFrameLength() + clip2copy.getFrameLength());

        
    
        AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, new File("C:\\test\\wavAppended.wav"));
        
        IMendixObject newobject = Core.instantiate(getContext(), "MusicBack.TestFile");
        Core.storeFileDocumentContent(getContext(), newobject2, "TEST.WAV", appendedFiles2);
0

There are 0 answers