Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Error In WavFile.....

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error In WavFile.....

    import java.io.*;
    import java.lang.Object;
    import java.io.File;
    import java.util.*;
    import javax.sound.sampled.*;

    public class lmn
    {
    public FileOutputStream newFile;
    public byte[] audioData;
    public byte[] totalFileData;
    Vector v;
    WavFile wavFile;
    public byte[] newData;

    public static void main(String[] args) throws IOException
    {
    int i;
    Vector v=new Vector();
    WavFile wavFile = WavFile.openWavFile(new File(args[0]));
    System.out.println("AUDIO FILE INTO BITS");
    new lmn();
    }
    public lmn()
    {
    encode1LSB();
    }


    public static byte[] intToByteArray(int i)
    {
    byte[] bt = new byte[4];
    bt[0] = (byte) (i & 0x00FF);
    bt[1] = (byte) ((i >> 8) & 0x000000FF);
    bt[2] = (byte) ((i >> 16) & 0x000000FF);
    bt[3] = (byte) ((i >> 24) & 0x000000FF);
    return bt;
    }


    // convert a short to a byte array
    public static byte[] shortToByteArray(short data)
    {
    return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};
    }


    public static BitSet fromByte(byte b)
    {
    BitSet bits = new BitSet(8);
    for (int i = 0; i < 8; i++)
    {
    bits.set(i, (b & 1) == 1);
    b >>= 1;
    }
    return bits;
    }


    public static byte[] toByteArray(BitSet bits) {
    byte[] bytes = new byte[bits.length()/8+1];
    for (int i=0; i<bits.length(); i++) {
    if (bits.get(i)) {
    bytes[bytes.length-i/8-1] |= 1<<(i%8);
    }
    }
    return bytes;
    }








    public void encode1LSB()
    {
    System.out.println("encode()");

    try
    {
    System.out.println("writeWaveFile()");
    String temp = "C:\\Program Files\\Java\\jdk1.6.0_21\\bin\\Project.wav";
    newFile = new FileOutputStream(temp);
    //newFile.write(wavFile.getHeader());
    byte[] b = new byte[4];
    b = "RIFF".getBytes();
    newFile.write(b, 0, 4);
    newFile.write(intToByteArray((int)wavFile.getChunk Size()), 0, 4);
    b = "WAVE".getBytes();
    newFile.write(b, 0, 4);
    b = "fmt ".getBytes();
    newFile.write(b, 0, 4);
    newFile.write(intToByteArray((int)wavFile.getSubCh unkSize()), 0, 4);
    newFile.write(shortToByteArray((short)wavFile.getA udioFormat()), 0, 2);
    newFile.write(shortToByteArray((short)wavFile.getC hannels()), 0, 2);
    newFile.write(intToByteArray((int)wavFile.getSampl eRate()), 0, 4);
    newFile.write(intToByteArray((int)wavFile.getByteR ate()), 0, 4);
    newFile.write(shortToByteArray((short)wavFile.getB lockAlign()), 0, 2);
    newFile.write(shortToByteArray((short)wavFile.getB itsPerSample()), 0, 2);
    b = "data".getBytes();
    newFile.write(b, 0, 4);
    newFile.write(intToByteArray((int)wavFile.getDataC hunkSize()), 0, 4);
    //newFile.write(newData);
    //newFile.close();
    //System.out.println("done");

    //instanciate the new dataChunk array
    //creates new byte array for
    audioData = new byte[(int)wavFile.getDataChunkSize()];

    //reads the dataChunk into the audio data array
    audioData = wavFile.readData();
    totalFileData = new byte[(int)v.elementAt(0).getFile().length()];
    totalFileData = v.elementAt(0).readData();
    int aCount = 0;

    //loop through total file bytes
    for(int i = 0; i<v.elementAt(0).getFile().length();i++)
    {
    //new temp bitset
    BitSet tempBits = new BitSet(8);

    //convert the first byte of the array to bits
    tempBits = fromByte(totalFileData[i]);
    //System.out.println("encode: "+(char)totalFileData[i]);

    //for each bit
    for(int j = 0;j<8;j++)
    {
    BitSet tempBits2 = new BitSet(8);

    //convert the current audio data byte to bits
    tempBits2 = fromByte(audioData[aCount]);

    //set the lsb of the current audio byte to the j'th bit of tempBitSet
    //tempBits2.set(0, tempBits.get(j));
    if(tempBits.get(j) == true)
    tempBits2.set(0);

    //convert back to byte and copy into newData array
    byte bb[] = new byte[1];
    bb = toByteArray(tempBits2);
    newFile.write(bb);
    //System.arraycopy(bb, 0, newData, aCount, 1);
    aCount++;
    }
    }
    if(aCount < (int)wavFile.getDataChunkSize())
    {
    newData = new byte[(int)wavFile.getDataChunkSize() - aCount];
    System.arraycopy(audioData, aCount, newData, 0, (int)wavFile.getDataChunkSize() - aCount);
    newFile.write(newData);
    }
    newFile.close();
    System.out.println("done");
    }
    catch(Exception e)
    {
    System.out.println("writeWaveFile(): "+e.toString());
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error In WavFile.....

    Do you have a question?

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.