views:

50

answers:

1

Hello all,

I am trying to compare two .caf files on the basis of some parameter, say maximum decibels or duration of files. I have recorded these two files using the AVAudioRecorder class using the following settings:

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

Thanks for the help!

+1  A: 

To get duration of files you following code:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError* error;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error&error];

NSInteger duration = fileAttributes.fileAttributes / sampleRare / numberOfChannels / bytesPerSample;

To get maximum decibels you'll have to analyze content of file.

eviltrue
Thank you for the response. I will try this way out to determine the duration of the audio. But how do I go about analyzing the content of the file ? Should I use NSData ? There isn't enough functionality available for that in NSData. Is it possible to use averagePowerForChannel method while recording or playing the audio ?
Viraj
averagePowerForChannel method returns current average power, in decibels, for the sound being recorded/played, so it should be used only while recording/plying. You can use NSData to get content of exiting file. Once you got the content use your favorite method/library to analyze it. To get C-style array from NSData object use bytes method.
eviltrue
How about I use averagePowerForChannel method in a loop for every 3 seconds while I am recording or playing and take the highest value in that ? Also after I get the content, I am quite not sure what to do with it ? Or how to analyze it with my favorite method/library ? I am a newbie for audio development. Thanks.
Viraj
Could you please describe a little what for are you going to do with data you get from sound? Knowing some details i could answer you questions with move confidence.=)
eviltrue
Well I just want to compare two sound files that I recorded, on basis of a parameter. If that audio's highest decibel value is more than the other audio then I choose the one with the higher decibel. Does that clear it ?
Viraj
OK, if you want to analyze file while recording just use averagePowerForChannel, I think you can even use it once - when recording is stopped, this way you should get average power for entire record. Or, if you want to analyze already recorded files, you can calculate average power by yourself, google will definitely help you to find some good code =).
eviltrue
I tried to analyze the file after the recording has stopped. It still outputs 0 as the value.
Viraj