9 Replies Latest reply: Dec 8, 2010 1:37 PM by 804900 RSS

    Trouble splitting .WAV file

    804900
      Hello,

      I am trying to split a .WAV file into 3 smaller .WAV files. The problem I am currently having is that the file is merely being replicated 3 times, and there is no actual splitting being done. There is also the added problem of the produced files giving an EOFException when run through DataInputStream.readFully(Byte[]).

      Here is the code I am currently using for the splitting
         public File[] split() {
              File[] output = new File[3];
              FileOutputStream out = null;
              File outputFile = null;
              AudioFormat audioFormatNew;
              AudioFormat audioFormatOld;
              AudioInputStream audioInputStream = null;
              File tempOutputFile = new File(System.getProperty("user.dir") + "/tmp_" + file.getName());
              tempOutputFile.deleteOnExit();
              try {
                  audioInputStream = AudioSystem.getAudioInputStream(file);
      
                  out = new FileOutputStream(tempOutputFile);
                  audioFormatOld = audioInputStream.getFormat();
      
                  FileInputStream fis = new FileInputStream(file);
      
                  int totalBytes = (int) (audioInputStream.getFrameLength() * audioInputStream.getFormat().getFrameSize());
      
                  System.out.println("total bytes: "+totalBytes + " total bytes/3: " + totalBytes/3);
      
                  int nBytesRead = 0;
                  byte[] sampledData = new byte[(totalBytes / 3)];
                  int offset = 0;
                  int element = 0;
                  System.out.println(offset);
                  while (nBytesRead != -1) {
                      for(int a = 0;a<3;a++){
                      audioInputStream.skip(offset);
                      nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
      
                      audioInputStream = new AudioInputStream(new ByteArrayInputStream(sampledData), audioFormatOld, (long) (sampledData.length));
                      ByteArrayOutputStream bos = new ByteArrayOutputStream();
                      outputFile = new File("element" + element + ".wav");
                      AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, outputFile);
                      System.out.println("wrote to file:audio.write: "+ element);
                      out.write(bos.toByteArray());
      
                      output[element] = outputFile;
                      element++;
                      offset = totalBytes/3;
                      }
                  }
              } catch (UnsupportedAudioFileException ex) {
                  Logger.getLogger(AudioSplit.class.getName()).log(Level.SEVERE, null, ex);
              } catch (IOException ex) {
                  Logger.getLogger(AudioSplit.class.getName()).log(Level.SEVERE, null, ex);
              }
              return output;
      
          }
      As you can see I use system output to check the offset, which as far as i can see produces the correct numbers.

      also, the section of code which gives the EOFException is as follows
      int length = (int) (audioStream.getFrameLength() * audioFormat.getFrameSize());
      
              byte[] data = new byte[length];
              DataInputStream is = new DataInputStream(audioStream);
              try {
                  is.readFully(data);
              } catch (IOException ex) {
                  ex.printStackTrace();
              }
              return data;
      where audioStream is an AudioInputStream with the argument of an element from the file [] produced by the splitting

      As previously mentioned, the 'split' files play fine in sound players, but produce and EOFException and are the same file 3 times, rather than 3 different files.
        • 1. Re: Trouble splitting .WAV file
          804900
          After a bit of playing around, the error is thrown whenever an altered .wav file is passed to the method. a hack to solve this is to pass the original file into this method first, then to split the file afterwards.

          The problem has now evolved, and the 1st file contains all the sound data, whereas the other 2 from the split contain nothing at all.
          file output[] contains the original files which shall be split into 3. the 1st chunk from the 1st split will be chosen, and the 2nd from the 2nd, ect.

          The new code is as follows
           File[] output1 = new File[3];
                  FileOutputStream out = null;
                  File outputFile = null;
                  AudioFormat audioFormatNew;
                  AudioFormat audioFormatOld;
                  AudioInputStream audioInputStream = null;
                  int c = 0;
                  int offset = 0;
                  int element = 0;
                  try {
                      for (int b = 0; b < 3; b++) {
                          File tempOutputFile = new File(System.getProperty("user.dir") + "/tmp_" + output[c].getName());
                          tempOutputFile.deleteOnExit();
          
                          audioInputStream = AudioSystem.getAudioInputStream(output[c]);
                          AudioFormat audioFormat = audioInputStream.getFormat(); 
                          out = new FileOutputStream(tempOutputFile);
                          audioFormatOld = audioInputStream.getFormat();
          
                          FileInputStream fis = new FileInputStream(output[c]);
          
                          int totalBytes = (int) (audioInputStream.getFrameLength() * audioInputStream.getFormat().getFrameSize());
                          int totalB = audioInputStream.available();
          
                          int nBytesRead = 0;
                          byte[] sampledData = new byte[(totalBytes / 3)];
          
                          audioInputStream.skip(offset);
                          nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
          
                          audioInputStream = new AudioInputStream(new ByteArrayInputStream(sampledData), audioFormatOld, (long) (sampledData.length));
                          ByteArrayOutputStream bos = new ByteArrayOutputStream();
                          outputFile = new File("element" + c + ".wav");
                          AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, outputFile);
                          System.out.println("nBytesRead: " + nBytesRead);
                          System.out.println("wrote to file:audio.write: " + c);
                          out.write(bos.toByteArray());
          
                          output1[b] = outputFile;
                          offset = offset + totalBytes / 3;
                          c++;
                      }
                  } catch (UnsupportedAudioFileException ex) {
                      Logger.getLogger(AudioSplit.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (IOException ex) {
                      Logger.getLogger(AudioSplit.class.getName()).log(Level.SEVERE, null, ex);
                  }
          
          
          
                  return output1;
              }
          the problem shown is similar to that of the original post, and i am relatively sure that they stem from the same cause.

          error messages shown when trying to read in from chunks 2 or 3 are as follows
          java.io.IOException: cannot read a single byte if frame size > 1
          thanks again
          • 2. Re: Trouble splitting .WAV file
            user9022924
            try this link it may hep u
            http://java.itags.org/java-essentials/27824/

            and maybe there is something wrong with ur equation to figure out the size of the .wav file
            http://en.wikipedia.org/wiki/WAV

            i hope i helped u a little :)
            • 3. Re: Trouble splitting .WAV file
              DarrylBurke
              user9022924 wrote:
              try this link it may hep u
              Please use full words and an uppercase letter at the start of a sentence.

              db
              • 4. Re: Trouble splitting .WAV file
                captfoss
                There are all sorts of mistakes in your code...

                Rather than go into all of them, try the following code (fix the syntax errors as necessary)...
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
                
                long frames = audioInputStream.getFrameLength();
                
                int[] splits = {frames/3,frames/3, frames}
                splits[2] -= (splits[0]+splits[1]);
                
                for (int i = 0; i < splits; i++) {
                    byte[] data = new byte[splits*audioInputStream.getFormat().getFrameSize())]; 

                int nBytesRead = 0;
                int counter = 0;
                while (nBytesRead != -1) {
                     nBytesRead = audioInputStream.read(data, counter, data.length-counter);
                     if (nBytesRead != -1) {
                     counter += nBytesRead;
                     }
                }

                File outputFile = new File("element" + i+ ".wav");

                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                AudioInputStream ais = new AudioInputStream(bais);

                AudioSystem.write(ais, AudioFileFormat.Type.WAVE, outputFile);
                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: Trouble splitting .WAV file
                  804900
                  have implemented your suggestion, but seem to get stuck in a infinite loop in this section of code
                                      while (nBytesRead != -1) {
                                          System.out.println("loop");
                                          nBytesRead = audioInputStream.read(data, counter, data.length - counter);
                                          if (nBytesRead != -1) {
                                              System.out.println("loopy");
                                              counter += nBytesRead;
                                          }
                                      }
                  both if the system out's are getting printed, so i would guess that nBytesRead is never getting to -1. nBytesRead is always 0.

                  Edited by: Fillis52 on 03-Dec-2010 10:18
                  • 6. Re: Trouble splitting .WAV file
                    captfoss
                    both if the system out's are getting printed, so i would guess that nBytesRead is never getting to -1. nBytesRead is always 0.
                    What're the values for all of the important variables in there? Namely i, split. data.length, and counter?
                    • 7. Re: Trouble splitting .WAV file
                      804900
                      i = 0, counter =7170, data.length = 7170, nBytesRead = 0,
                      • 8. Re: Trouble splitting .WAV file
                        captfoss
                        Fillis52 wrote:
                        counter =7170
                        Ah, yeah. Oops...
                        for (int i = 0; i < splits; i++) {
                            byte[] data = new byte[splits*audioInputStream.getFormat().getFrameSize())]; 

                        int nBytesRead = 0;
                        int counter = 0;
                        while ((nBytesRead != -1)&&(counter < data.length)) {
                             nBytesRead = audioInputStream.read(data, counter, data.length-counter);
                             if (nBytesRead != -1) {
                             counter += nBytesRead;
                             }
                        }

                        File outputFile = new File("element" + i+ ".wav");

                        ByteArrayInputStream bais = new ByteArrayInputStream(data);
                        AudioInputStream ais = new AudioInputStream(bais);

                        AudioSystem.write(ais, AudioFileFormat.Type.WAVE, outputFile);
                        }
                        Wasn't handling the case where the full length of data had been read...so essentially the code was telling it to read 0 bytes, so it was infininately reading zero bytes.
                        
                        Edited by: captfoss on Dec 6, 2010 10:09 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                        • 9. Re: Trouble splitting .WAV file
                          804900
                          you're a complete star!

                          The method now works completely fine with no errors or anything.

                          now to research how to concatenate them again! (dont ask :p)