2 Replies - 281 Views - Last Post: 17 May 2014 - 05:26 PM Rate Topic: -----

#1 lisedaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 14

reading and writing wav files

Posted 17 May 2014 - 04:39 PM

I have copied a wav file as explained in https://ccrma.stanfo...cts/WaveFormat/

When trying to play my new file, it runs but no sound is hear. Can somebody help? My reading and writting class is as explained in another post in the site:

This is the function that reads the file:
// read a wav file into this class
public boolean read(DataInputStream inFile) {
    myData = null;
    byte[] tmpInt = new byte[4];
    byte[] tmpShort = new byte[2];
//        byte[] buffer= new byte[offset];
    try {
        System.out.print(inFile.available());
    } catch (IOException ex) {
        Logger.getLogger(Wav.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
       // if (offset!=0)
        //    inFile.read(buffer, 0,offset);

        //System.out.println("Reading wav file...\n"); // for debugging only

        String chunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpInt); // read the ChunkSize
        myChunkSize = byteArrayToIntLittle(tmpInt);

        String format = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        // print what we've read so far
        //System.out.println("chunkID:" + chunkID + " chunk1Size:" + myChunkSize + " format:" + format); // for debugging only

        String subChunk1ID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpInt); // read the SubChunk1Size
        mySubChunk1Size = byteArrayToIntLittle(tmpInt);

        inFile.read(tmpShort); // read the audio format.  This should be 1 for PCM
        myFormat = byteArrayToShortLittle(tmpShort);

        inFile.read(tmpShort); // read the # of channels (1 or 2)
        myChannels = byteArrayToShortLittle(tmpShort);

        inFile.read(tmpInt); // read the samplerate
        mySampleRate = byteArrayToIntLittle(tmpInt);

        inFile.read(tmpInt); // read the byterate
        myByteRate = byteArrayToIntLittle(tmpInt);

        inFile.read(tmpShort); // read the blockalign
        myBlockAlign = byteArrayToShortLittle(tmpShort);

        inFile.read(tmpShort); // read the bitspersample
        myBitsPerSample = byteArrayToShortLittle(tmpShort);

        // print what we've read so far
        System.out.println("SubChunk1ID:" + subChunk1ID + " SubChunk1Size:" + mySubChunk1Size + " AudioFormat:" + myFormat + " Channels:" + myChannels + " SampleRate:" + mySampleRate);

        inFile.read(tmpShort);
        extraParams= byteArrayToShortLittle(tmpShort);

        // read the data chunk header - reading this IS necessary, because not all wav files will have the data chunk here - for now, we're just assuming that the data chunk is here
        String dataChunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpInt); // read the size of the data
        myDataSize = byteArrayToIntLittle(tmpInt);
        // read the data chunk
        myData = new byte[(int) myDataSize];

        // close the input stream
        inFile.close();

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

    return true; 
}



then to save it as a new wav file i use this function:
public boolean save() {
    try {
        // Creating the  file
           File file = new File(myPath);
           boolean isCreated = false;
           try {
                   isCreated = file.createNewFile();
                   if (!isCreated) {
                           file.delete();
                           file.createNewFile();
                   }
           } catch (IOException e) {
                   e.printStackTrace();
                   return false;
           }

        DataOutputStream outFile = new DataOutputStream(new FileOutputStream(myPath));

        // write the wav file per the wav file format
        outFile.writeBytes("RIFF");                 // 00 - RIFF
        outFile.write(intToByteArray((int) myChunkSize), 0, 4);     // 04 - how big is the rest of this file?
        outFile.writeBytes("WAVE");                 // 08 - WAVE
        outFile.writeBytes("fmt ");                 // 12 - fmt
        outFile.write(intToByteArray((int) 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(intToByteArray((int) mySampleRate), 0, 4);        // 24 - samples per second (numbers per second)
        outFile.write(intToByteArray((int) 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.write(shortToByteArray((short) extraParams), 0, 2);
        outFile.writeBytes("data");                 // 36 - data
        outFile.write(intToByteArray((int) myDataSize), 0, 4);      // 40 - how big is this data chunk
        outFile.write(myData);                      // 44 - the actual data itself - just a long string of numbers

        System.out.print("size of wav "+ outFile.size()+ "\n");
        outFile.close();
    } catch (Exception e) {
        System.out.println(e);
        return false;
    }

    return true;
}



please help, cant hear the copied wav file, but still the wav file is correctly copied...

thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: reading and writing wav files

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: reading and writing wav files

Posted 17 May 2014 - 04:53 PM

I won't comment on your code, as I did not read it, but know that Java is very picky about wavs. I've found that certain ones will play and others won't. I am sure it has to do with the encoding of the wav, but I have never been able to pin down the exact bit rates, etc, supported. It's always a hit or miss. Stick with a third party api that reads ogg or something.
Was This Post Helpful? 0
  • +
  • -

#3 lisedaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 14

Re: reading and writing wav files

Posted 17 May 2014 - 05:26 PM

is not relevant anymore, I decided to treath the wav file as any other and finally things are working, dont know why i didnt taught about this from the beginning!!

thanks anyways!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1