Storing WAV using NSData and NSKeyedArchiver in Objective-C

44 Views Asked by At

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

0

There are 0 best solutions below