Skip to content

Instantly share code, notes, and snippets.

@C4Code
Created March 18, 2013 19:01
Show Gist options
  • Save C4Code/5189835 to your computer and use it in GitHub Desktop.
Save C4Code/5189835 to your computer and use it in GitHub Desktop.
C4Sample audio metering example
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Sample *s;
C4Timer *meterUpdateTimer;
C4Shape *peak, *avg;
}
-(void)setup {
s = [C4Sample sampleNamed:@"C4Loop.aif"];
s.loops = YES;
s.meteringEnabled = YES;
[s prepareToPlay];
[s play];
CGPoint pts[2] = {
CGPointMake(self.canvas.width / 3, self.canvas.height),
CGPointMake(self.canvas.width / 3, 0)
};
avg = [C4Shape line:pts];
pts[0].x *= 2;
pts[1].x *= 2;
peak = [C4Shape line:pts];
[self.canvas addObjects:@[avg,peak]];
meterUpdateTimer = [C4Timer timerWithInterval:1/30.0f target:self method:@"updateMeters" repeats:YES];
[meterUpdateTimer start];
}
-(void)updateMeters {
[s updateMeters];
avg.strokeEnd = [C4Math pow:10 raisedTo:0.05 * [s averagePowerForChannel:0]];
peak.strokeEnd = [C4Math pow:10 raisedTo:0.05 * [s peakPowerForChannel:0]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment