Hi everyone,
I am working on a DSP tool, and to be honest, performance is not really
*that* important to me. It feels to me like working with buffers of
samples as plain C arrays is more trouble than it's worth--- So for
example, I just finished writing a hamming window class that looks like
this:
#import <Foundation/Foundation.h>
@interface HammingWindow : NSObject
+(NSArray *)process:(NSArray *)buffer;
@end
@implementation HammingWindow
+(NSArray *)process:(NSArray *)buffer {
NSUInteger numberOfSamples = [buffer count];
NSMutableArray *newBuffer = [NSMutableArray
arrayWithCapacity:numberOfSamples];
NSArray *window = [self windowForSize:numberOfSamples];
[buffer enumerateObjectsUsingBlock:^(NSNumber *sample, NSUInteger idx,
BOOL *stop) {
float windowedSample = [sample floatValue] * [[window
objectAtIndex:idx] floatValue];
[newBuffer addObject:[NSNumber numberWithFloat:windowedSample]];
}];
return [newBuffer copy];
}
+(NSArray *)windowForSize:(NSUInteger)size {
NSMutableArray *window = [NSMutableArray arrayWithCapacity:size];
for (int i=0; i < size; i++) {
float value = 0.54f - 0.46f * cos(2 * M_PI * i / (size - 1));
[window addObject:[NSNumber numberWithFloat:value]];
}
return [window copy];
}
@end
And after I wrote this, I wondered-- would other objective-c programmers
find anything here offensive?
The fact that core audio initially gave me a c buffer, and I copies all
those values into an NSArray, and then this class expects that array and
creates a new mutable array, fills it with data from the other one, and
then copies it to stop it from being mutable.. This code is clearly not
efficient-- but it feels "convenient".. so I just was philosophically
curious, what is the general consensus among you guys regarding code
like this?
Would you argue that I should in fact be using C arrays since they are
light-weight, fast, etc... ? After all, I am dealing with a buffer of
samples, which potnentially could be as many as 50k elements.
Patrick J. Collins
http://collinatorstudios.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com
This email sent to [email protected]