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());
}
}
}