您的位置:首页 > 移动开发 > IOS开发

iOS 画音频波形曲线 根据音频数据版

2014-07-06 22:11 387 查看
http://www.2cto.com/kf/201311/255886.html








DrawView.h


[objc]
#import <UIKit/UIKit.h>

@interface DrawView : UIView

@property shortshort *drawBuffer;
@property int dataLen;
@property floatfloat *outRel;
@property floatfloat *outImg;
@property int bias;
@property int wSize;
- (void)genKernel;


@end

DrawView.m

[objc]
#import "DrawView.h"

@implementation DrawView

#define KSIZE 20
#define BIAS 10000

static double fk[KSIZE] = {0};
static double _filterData[2048];



- (void)genKernel
{
double fc = .05;
for (int i = 0; i < KSIZE; i++)
{
if ((i - KSIZE/2) == 0)fk[i] = 22 * M_PI *fc;
if ((i - KSIZE/2) != 0 )fk[i] = sin(22 * M_PI * fc * (i - KSIZE/2))/(i - KSIZE/2);
fk[i] = fk[i] * (0.54 - 0.46 * cos(22 * M_PI * i / KSIZE ));

}

double sum = 0;
for (int m = 0; m < KSIZE; m++)
sum+=fk[m];

for (int n = 0; n < KSIZE; n++)
fk
/=sum;
}

- (void)improveSpectrum
{
memset(_filterData, 0x0, sizeof(double) * 1024);
short transData[(int)self.wSize];
memcpy(transData, self.drawBuffer+_bias, _wSize * sizeof(short));
for (int i = 0; i < _wSize; i++)
{
for (int j = 0; j < KSIZE; j++)
{
_filterData[i] = _filterData[i] + transData[ i - j] * fk[j];
}
}

}


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}



- (void)drawRect:(CGRect)rect
{
float delta = 320. / _wSize;
[self improveSpectrum];

[[UIColor grayColor] set];
UIRectFill ([self bounds]);


CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, 0., 230.);
CGContextAddLineToPoint(currentContext, 320., 230.);
[[UIColor blueColor] setStroke];
CGContextStrokePath(currentContext);


CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, 0., 230.);
for (int i = 0; i < _wSize; i++)
{
CGFloat x = i * delta;
CGFloat y = _filterData[i] / 150.0 + 230.0;





CGContextAddLineToPoint(currentContext, x, y);

}
[[UIColor redColor] setStroke];
CGContextStrokePath(currentContext);

}


@end



ViewController.h



[objc]
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end








ViewController.m



[objc]
#import "ViewController.h"
#import "DrawView.h"


struct WavInfo
{
int size;
char *data;
short channels;
short block_align;
short bits_per_sample;
unsigned long sample_rate;
unsigned long format_length;
unsigned long format_tag;
unsigned long avg_bytes_sec;

};


@interface ViewController ()

@end

void decodeWaveInfo(const charchar *fname, struct WavInfo *info)
{

FILEFILE *fp;
fp = fopen(fname, "rb");

if(fp)
{
char id[5];
unsigned long dataSize,size;

fread(id, sizeof(char), 4, fp);
id[4]='\0';
if (!strcmp(id, "RIFF"))
{
fread(&size, sizeof(unsigned long), 1, fp);//read file size
fread(id, sizeof(char), 4, fp);//read wave
id[4]='\0';

if (!strcmp(id, "W***E"))
{
fread(id, sizeof(char), 4, fp);
fread(&info->format_length, sizeof(unsigned long), 1, fp);
fread(&info->format_tag, sizeof(short), 1, fp);
fread(&info->channels, sizeof(short), 1, fp);
fread(&info->sample_rate, sizeof(unsigned long), 1, fp);
fread(&info->avg_bytes_sec, sizeof(unsigned long), 1, fp);
fread(&info->block_align, sizeof(short), 1, fp);
fread(&info->bits_per_sample, sizeof(short), 1, fp);
fread(id, sizeof(char), 4, fp);
fread(&dataSize, sizeof(unsigned long), 1, fp);
info->size = dataSize;
info->data = ( charchar *)malloc(sizeof(char)*dataSize);
fread(info->data, sizeof(char), dataSize, fp);

}
else
{
printf("Error\n");
}
}
else
{
printf("Error\n");
}
fclose(fp);
}
}

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"effect1" ofType:@"wav"];
struct WavInfo wavInfo;
decodeWaveInfo([path UTF8String], &wavInfo);


DrawView *d = (DrawView *)self.view;
d.drawBuffer = (shortshort *)malloc(sizeof(short) * wavInfo.size / 2 );
[d genKernel];
d.dataLen = wavInfo.size / 2;
d.wSize = 256;
d.bias = 0;

int n = 0;
for (int m = 0; m < wavInfo.size / 2; m++)
{
d.drawBuffer[n++] = wavInfo.data[m * 2 + 1] << 8 | wavInfo.data[m * 2];
}

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragView:)];
[self.view addGestureRecognizer:pan];

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[self.view addGestureRecognizer:pinch];


}

#pragma mark -
#pragma mark Gesture Recognizer callback

- (void)dragView:(UIPanGestureRecognizer *)recognizer
{
DrawView *d = (DrawView *)self.view;

CGPoint p = [recognizer translationInView:self.view];
NSLog(@"translate point is : %@",NSStringFromCGPoint(p));
d.bias -= p.x;

[self.view setNeedsDisplay];
}

- (void)pinchView:(UIPinchGestureRecognizer *)recognizer
{
DrawView *d = (DrawView *)self.view;

if (recognizer.scale > 1.0)
{
if (d.wSize > 128)
{
d.wSize *= 0.95;
}
}
else
{
if (d.wSize < 1024)
{
d.wSize *= 1.05;
}
}

[self.view setNeedsDisplay];
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: