I am facing an issue in getting 2 .wav files mixed together.
I'm using the code from this site: http://eigenclass.blogspot.in/2010/12/merging-wav-files-in-objective-c.html
Now the problem I'm facing is that, the code generally works for wav files lesser than 10 secs and anything more than 10 secs causes the app to crash.
For some strange reason, even if I'm properly releasing the objects at the end of the for loop (for loop consists of more than 10 lakh iterations and increases as size of recording increases), the objects dont seem to get released and keep hogging memory.
Here's my for loop:
short iValueWav1Sample;
short iValueWav2Sample;
short iValueWavSampleAverage;
for (int i=0; i<(outputWavDataSize/2); i++)
{
//simulate little endian by flipping the bytes
wav1DataBuffer1 = [[NSData alloc]initWithData:[wav1Data subdataWithRange:NSMakeRange(44 + (i*2), 1)]];
wav2DataBuffer1 = [[NSData alloc]initWithData:[wav2Data subdataWithRange:NSMakeRange(44 + (i*2), 1)]];
wav1DataBuffer2 = [[NSData alloc]initWithData:[wav1Data subdataWithRange:NSMakeRange(44 + (i*2) + 1, 1)]];
wav2DataBuffer2 = [[NSData alloc]initWithData:[wav2Data subdataWithRange:NSMakeRange(44 + (i*2) + 1, 1)]];
littleEndianHexWav1Sample = [[NSMutableData alloc] initWithData:wav1DataBuffer1];
[littleEndianHexWav1Sample appendData:[NSMutableData dataWithData:wav1DataBuffer2]];
littleEndianHexWav2Sample = [[NSMutableData alloc]initWithData:wav2DataBuffer1];
[littleEndianHexWav2Sample appendData:wav2DataBuffer2];
NSString* wav1HexString = [[littleEndianHexWav1Sample description] substringWithRange:NSMakeRange(1, 4)];
NSString* wav2HexString = [[littleEndianHexWav2Sample description] substringWithRange:NSMakeRange(1, 4)];
unsigned wav1Hexint;
unsigned wav2Hexint;
[[NSScanner scannerWithString:wav1HexString] scanHexInt:&wav1Hexint];
[[NSScanner scannerWithString:wav2HexString] scanHexInt:&wav2Hexint];
unsigned wavAverage;
wavAverage = (wav1Hexint+wav2Hexint);
iValueWav1Sample = wav1Hexint;
iValueWav2Sample = wav2Hexint;
iValueWavSampleAverage = wavAverage;
bigEndian = [[NSData alloc ]initWithBytes:&iValueWavSampleAverage length:2];
littleEndian = [[NSMutableData alloc] initWithData:[bigEndian subdataWithRange:NSMakeRange(1, 1)]];
[littleEndian appendData:[bigEndian subdataWithRange:NSMakeRange(0, 1)]];
[headerBuffer appendData:littleEndian];
//Release stuff
[wav1HexString release];
[wav2HexString release];
[wav1DataBuffer1 release];
[wav2DataBuffer1 release];
[wav1DataBuffer2 release];
[wav2DataBuffer2 release];
[littleEndianHexWav1Sample release];
[littleEndianHexWav2Sample release];
[bigEndian release];
[littleEndian release];
wav1DataBuffer1 = nil;
wav2DataBuffer1= nil;
wav1DataBuffer2 = nil;
wav2DataBuffer2 = nil;
littleEndianHexWav1Sample= nil;
littleEndianHexWav2Sample= nil;
bigEndian= nil;
littleEndian= nil;
}
Edit:
@Usman:
I am already running the mixing function in background using the performselectorinbackground method.
Ive narrowed down the leaks to the objects wav1Data and wav2Data, both NSData which were set as autorelease objects in the starting.
NSData * wav1Data = [[NSData alloc ]initWithContentsOfFile:soundFile1Path];
NSData * wav2Data = [[NSData alloc]initWithContentsOfFile:soundFile2Path];
Now what I'm doing is, creating wav1Data and wav2Data inside the for loop and then releasing them at the end.
I've solved the memory leak but now another issue has risen wherein the execution of the for loop has slowed down tremendously. This must be due to the fact that Im initializing contents of a file on every iteration.
Any suggestion or ideas?