0

I'm trying to write a .wav file from bytearray[] by adding the header of wave file and playing the written .wav file later. Null value is returned while saving it to .wav file? Can some one please point out the mistake i am committing? The below mentioned function generates a wave file but writes a null value in the file?

Can arraylist` be used for writing .wav file instead of bytearray[]?

         // write out the wav file
         public boolean save() {
        try {
            DataOutputStream outFile = new DataOutputStream(new 
     FileOutputStream("cartoon-birds_Result.wav"));

            // write the wav file per the wav file format
            outFile.writeBytes("RIFF");                 // 00 - RIFF
            outFile.write(shortToByteArray((short) myChunkSize), 0, 4);     
     // 04 - how big is the rest of this file?
            outFile.writeBytes("WAVE");                 // 08 - WAVE
            outFile.writeBytes("fmt ");                 // 12 - fmt
            outFile.write(shortToByteArray((short) mySubChunk1Size), 0, 4); 
       // 16 - size of this chunk
            outFile.write(shortToByteArray((short) myFormat), 0, 2);        
       // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
            outFile.write(shortToByteArray((short) myChannels), 0, 2);  // 22 
      - mono or stereo? 1 or 2?  (or 5 or ???)
            outFile.write(shortToByteArray((short) mySampleRate), 0, 4);        
       // 24 - samples per second (numbers per second)
            outFile.write(shortToByteArray((short) myByteRate), 0, 4);      
       // 28 - bytes per second
            outFile.write(shortToByteArray((short) myBlockAlign), 0, 2);    
       // 32 - # of bytes in one sample, for all channels
            outFile.write(shortToByteArray((short) myBitsPerSample), 0, 2); 
       // 34 - how many bits in a sample(number)?  usually 16 or 24
            outFile.writeBytes("data");                 // 36 - data
            outFile.write(shortToByteArray((short) myDataSize), 0, 4);      
       // 40 - how big is this data chunk
            outFile.write(myData);                      // 44 - the actual 
       data itself - just a long string of numbers

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }

        return true;
         }

I am basically looking how to write bytearray back to .wav file which can be played

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 3
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 14 '18 at 15:10
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question that is probably not a duplicate and worth answering!"* –  Jun 15 '18 at 17:00
  • also `ByteBuffer` exists for a reason ... –  Jun 15 '18 at 17:01

1 Answers1

-1

You don't have to build your own header. The AudioSystem.write method can handle this for you.

Are you aware of the Oracle "Sound Trail" tutorial. The page Using Files and Format Converters has a section on saving to files. See the section heading Writing to Sound Files just above the halfway point.

When I did this, I took the following steps:

  • made a class that extends InputStream, where the read method takes PCM data and converts it to integer values that correspond to the byte values needed by the audio format;
  • use that InputStream class as an argument to an AudioInputStream (along with the audio format and frame length);
  • use that AudioInputStream as an argument to the AudioSystem.write method, along with the target file type and file.

While your starting point is a byte array, not PCM, you should still be able to craft a similar sort of plan.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • Would appreciate learning why this answer was marked down. I put in this answer prior to the "duplicate" flags being raised. I think this answer is better in some ways than the answer I gave in 2011 in one of the links. When I first was trying to write, I also attempted to create a wav header manually, before learning that Java had built-in code to handle this. I assume OP had the same experience. – Phil Freihofner Jun 16 '18 at 03:35