I have copied a wav file as explained here
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;
}