Last active
December 2, 2022 16:52
-
-
Save WedgeSparda/d337a1a10d52646e86dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <AVFoundation/AVFoundation.h> | |
@interface VisualizerView : UIView | |
@property (strong, nonatomic) AVAudioPlayer *audioPlayer; | |
@property (nonatomic, assign) NSInteger numberOfBars; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "VisualizerView.h" | |
#import "MeterTable.h" | |
@implementation VisualizerView { | |
MeterTable meterTable; | |
NSMutableArray *_barsArray; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
_numberOfBars = 8; | |
_barsArray = [NSMutableArray array]; | |
float halfContainerWidth = self.frame.size.width / 2; | |
float barContainerWidth = halfContainerWidth / _numberOfBars; | |
float barWidth = barContainerWidth / 2; | |
if (self) { | |
[self setBackgroundColor:[UIColor blackColor]]; | |
for (int i = 0; i < _numberOfBars; i++) { | |
CAShapeLayer *barLayer = [CAShapeLayer layer]; | |
float barOriginX = (i * barContainerWidth) + barWidth; | |
[barLayer setPath:[[UIBezierPath bezierPathWithRect:CGRectMake(barOriginX, | |
0, | |
barWidth, | |
self.frame.size.height / 3)] CGPath]]; | |
[barLayer setFillColor:[[UIColor whiteColor] CGColor]]; | |
[self.layer addSublayer:barLayer]; | |
[_barsArray addObject:barLayer]; | |
CAShapeLayer *oppositeBarLayer = [CAShapeLayer layer]; | |
float oppositeBarOriginX = self.bounds.size.width - barOriginX; | |
[oppositeBarLayer setPath:[[UIBezierPath bezierPathWithRect:CGRectMake(oppositeBarOriginX, | |
0, | |
barWidth, | |
self.frame.size.height / 3)] CGPath]]; | |
[oppositeBarLayer setFillColor:[[UIColor whiteColor] CGColor]]; | |
[self.layer addSublayer:oppositeBarLayer]; | |
[_barsArray addObject:oppositeBarLayer]; | |
} | |
self.layer.transform = CATransform3DMakeRotation(M_PI, 1.0f, 0.0f, 0.0f); | |
CADisplayLink *dpLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; | |
[dpLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; | |
} | |
return self; | |
} | |
- (void)update | |
{ | |
float scale = 0.5; | |
if (_audioPlayer.playing ) | |
{ | |
self.hidden = NO; | |
[_audioPlayer updateMeters]; | |
float power = 0.0f; | |
for (int i = 0; i < [_audioPlayer numberOfChannels]; i++) { | |
power += [_audioPlayer averagePowerForChannel:i]; | |
} | |
power /= [_audioPlayer numberOfChannels]; | |
float level = meterTable.ValueAt(power); | |
scale = level * 5; | |
} else { | |
self.hidden = YES; | |
} | |
for (int i = 0; i < _barsArray.count; i = i+2) { | |
CGFloat transformScale = i * scale; | |
transformScale = fmod(transformScale, 4); | |
CAShapeLayer *barLayer = _barsArray[i]; | |
barLayer.transform = CATransform3DMakeScale(1, transformScale, 1); | |
CAShapeLayer *oppositeBarLayer = _barsArray[i+1]; | |
oppositeBarLayer.transform = CATransform3DMakeScale(1, transformScale, 1); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment