Remove initial silence from audio file of wave type.

Can anyone help me out removing the initial silence in recorded audio file?


- (double)processAudio:(float)totalFileDuration withFilePathURL:(NSURL *)filePathURL{

NSMutableData *data = [NSMutableData dataWithContentsOfURL:filePathURL];

NSMutableData *Wave1= [NSMutableData dataWithData:[data subdataWithRange:NSMakeRange(44, [data length] - 44)]];

uint8_t * bytePtr = (uint8_t * )[Wave1 bytes] ;

NSInteger totalData = [Wave1 length] / sizeof(uint8_t);

int endRange = 0;

for (int i = 0 ; i < totalData; i ++){

/

if (bytePtr[i] == 0) {

endRange = i;

}else

break;

}

double silentAudioDuration =(((float)endRange/(float)totalData)*totalFileDuration);

return silentAudioDuration;

}

- (void)trimAudioFileWithInputFilePath :(NSString *)inputPath toOutputFilePath : (NSString *)outputPath{

/

NSString *strInputFilePath = inputPath;

NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];

/

NSString *strOutputFilePath = [outputPath stringByDeletingPathExtension];

strOutputFilePath = [strOutputFilePath stringByAppendingString:@".m4a"];

NSURL *audioFileOutput = [NSURL fileURLWithPath:strOutputFilePath];

newPath = strOutputFilePath;

if (!audioFileInput || !audioFileOutput){

/

}

[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];

AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

CMTime audioDuration = asset.duration;

float audioDurationSeconds = CMTimeGetSeconds(audioDuration);

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];

if (exportSession == nil){

/

}

/

float startTrimTime = [self processAudio:audioDurationSeconds withFilePathURL:audioFileInput];

/

/

float endTrimTime = audioDurationSeconds;

recordingDuration = audioDurationSeconds - startTrimTime;

CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100);

CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100);

CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

exportSession.outputURL = audioFileOutput;

exportSession.outputFileType = AVFileTypeAppleM4A;

exportSession.timeRange = exportTimeRange;

[exportSession exportAsynchronouslyWithCompletionHandler:^{

if (AVAssetExportSessionStatusCompleted == exportSession.status){

}

else if (AVAssetExportSessionStatusFailed == exportSession.status){

}

}];

}



What am I doing wrong here?

You are assuming that all the data past the first 44 bytes of header are audio samples. But Apple WAVE files can have longer headers, perhaps up to 4Kbytes including added 'FLLR' padding.

So how many bytes I have to ignore as header bytes?


How do I identify this?

If the bytes at byte offset 60 into a .wav file are 'FLLR' followed by the 4-byte little-endian value 0x4020, it is possible that the .wav file header before the 1st chunk of raw audio samples is 4096 bytes (4kB) long instead of 44 bytes long.

Remove initial silence from audio file of wave type.
 
 
Q