vendredi 30 janvier 2015

Android - Cut audio file for 2 seconds in beginning and 1 second in end

I require to cut my 1 audio sample to another which have no data of staring of 2 seconds and 1 second of last. Currently I am trying following code , but its not get cut properly.


Header is of 44 bytes , 2 second is of 176400 and 1 sec 88200



private void copyWaveFileForAlgo(String inFilename, String outFilename) {
Log.v("copyWaveFile ", "---------copyWaveFile---------");
FileInputStream in = null;
FileOutputStream out = null;
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = RECORDER_SAMPLERATE;
int channels = 1;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;

byte[] tmpdata = new byte[88200];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;



int lenth = 0;
Log.v("data ", "----totalDataLen-----"+totalDataLen+"---------"+tmpdata.length);
long dataToCopy = totalDataLen - 88200;
long dataLenthTransfered = 0;
WriteWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);
long skippedB = 0;
lenth = in.read(tmpdata);

while (lenth != -1)
{

if (skippedB <176400)
{
skippedB+= in.skip(88200);
Log.v("skippedB ", "----skippedB-----"+skippedB+"---------");
}else
{
if(dataLenthTransfered < dataToCopy)
{Log.v("dataLenth ", "----elseee-----"+dataLenthTransfered+"-----dataToCopy----"+dataToCopy);
out.write(tmpdata);

}
else
{
Log.v("dataLenthTransfered ", "----dataLenthTransfered-----"+dataLenthTransfered+"----dataToCopy-----"+dataToCopy);
}

}
dataLenthTransfered+=lenth;
lenth = in.read(tmpdata);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}`


Any help is appreciated. Thanks, Vyoma


Aucun commentaire:

Enregistrer un commentaire