0

I'm trying to archive an array with floats by using NSKeyedArchiver. The data is a WAV file with 40 seconds of audio, which should result in a file size of approx. 10.5 MB.

First I was using NSMutableArray with [NSNumber numberWithFloat:], but the file size ended up being huge, approx 50 MB. Now I'm using NSData instead and the file size is significantly smaller, around 22 MB:

NSMutableDictionary *state = [[NSMutableDictionary alloc] initWithDictionary: super.fullState];
     
NSMutableDictionary *dict = [NSMutableDictionary new];

NSMutableData *data = [NSMutableData dataWithCapacity:0];

int _current_size = 44100 * 40; // 40 seconds of audio             

for(int e=0; e < _current_size; e++){
         
  float val = 0;
                 
  [data appendBytes:&val length:sizeof(float)];
                 
}
             
[dict setObject:data forKey:@"wav"];

state[@"fullStateParams"] = [NSKeyedArchiver archivedDataWithRootObject: dict];

I don't understand why it is still that much bigger than the original wav file. Seems like the NSData is creating a lot of overhead or is that coming from the NSKeyedArchiver or both? I'm using the NSKeyedArchiver for many other parameters that's why I want to keep using it. Is there a different way of serializing the audio data and storing it inside the dictionary?

Any help highly appreciated

HTron
  • 337
  • 3
  • 14
  • 1
    NSData is stored in plist as base64 encoded text. It should be 33% bigger. You have something more in that file or your assumption of 10.5 MB original data is not correct. See this https://stackoverflow.com/a/47074786/1041929 and this: https://stackoverflow.com/questions/33022858/how-is-nsdata-stored-in-plists – Nick Oct 07 '22 at 13:25
  • 1
    @Nick I didn't know that. Thank you for the explanation. I was using this link to calculate the file size: https://www.colincrawley.com/audio-file-size-calculator . I've just realized that the result seems to get encoded as NSData again by NSKeyedArchiver, so it becomes 33% bigger on top of that. That could be the reason.. – HTron Oct 08 '22 at 07:19

0 Answers0