Monday, 9 September 2013

Why does getting a magic cookie from an audio file fail sometime?

Why does getting a magic cookie from an audio file fail sometime?

I'm recording sound with AVAudioRecorder. I then need to get the magic
cookie from the recorded sound since I'm cutting and appending some other
sound files. The problem is that about 50% of the times I try this it
fails and the other 50% works fine.
Inintializing the recorder:
-(void)initRecorder
{
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *activeFilePath = [docsDir
stringByAppendingPathComponent:@"Input.caf"];
NSURL *activeFileURL = [NSURL fileURLWithPath:activeFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:
kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithFloat:16000.0],
AVSampleRateKey,
[NSNumber numberWithInt: 1],
AVNumberOfChannelsKey,
nil];
NSError *error = nil;
// Initialize the audiorecorder
_audioRecorder = [[AVAudioRecorder alloc]
initWithURL:activeFileURL
settings:recordSettings
error:&error];
_audioRecorder.meteringEnabled = YES;
_audioRecorder.delegate = self;
[_audioRecorder prepareToRecord];
recorderOption = 0;
}
This is the file I'm later trying to get the Magic Cookie from.
And here is the part where I open the recorded file and try to get the
Magic Cookie:
// Open inputFile -------->
AudioFileID inputAudioFile;
theErr = AudioFileOpenURL((__bridge
CFURLRef)_audioRecorder.url,kAudioFileReadPermission, 0, &inputAudioFile);
if(theErr != noErr){
NSLog(@"Error with open inputAudioFile in addTogetherREcordings: %@",
[self OSStatusToStr: theErr]);
}
// Check how many packets is already in the inputfile and store in
packetCountOutput ------->
UInt64 packetCountInput = 0;
thePropertySize = sizeof(packetCountInput);
theErr = AudioFileGetProperty(inputAudioFile,
kAudioFilePropertyAudioDataPacketCount, &thePropertySize,
&packetCountInput);
if(theErr != noErr)
NSLog(@"Error when getting packetcount of inputfile: %ld", theErr);
NSLog(@"The No. of packets in inputfile is: %llu",packetCountInput);
// Getting Magic Cookie data ----------------->
thePropertySize = sizeof (UInt32);
theErr = AudioFileGetPropertyInfo
(inputAudioFile,kAudioFilePropertyMagicCookieData,&thePropertySize,NULL);
if(theErr != noErr)
NSLog(@"Error when getting magic cookie info: %@", [self
OSStatusToStr: theErr]);
NSLog(@"What is the propertySize of Magic Cookie: %d",(unsigned
int)thePropertySize);
char *cookie = (char *) malloc (thePropertySize);
theErr = AudioFileGetProperty
(inputAudioFile,kAudioFilePropertyMagicCookieData,&thePropertySize,cookie);
if(theErr != noErr){
NSLog(@"Error when getting magic cookie: %@", [self OSStatusToStr:
theErr]);
thePropertySize = 39;
}
else{
magicCookie = cookie;
}
When I'm getting errors I get kAudioFileInvalidChunkError when trying to
get the cookie however already when getting the thePropertySize it is 0,
this however dosen't give an error but just sets it to 0, when it suceeds
thePropertySize is 39.
Anyone has any idea how to proceed?

No comments:

Post a Comment