iOS 画音频波形曲线 根据音频数据版
来源:互联网 发布:centos 桥接网络配置 编辑:程序博客网 时间:2024/06/05 16:47
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;@endDrawView.m[objc]#import "DrawView.h"@implementation DrawView#define KSIZE 20#define BIAS 10000static 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[n]/=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);}@endViewController.h[objc]#import <UIKit/UIKit.h>@interface ViewController : UIViewController@endViewController.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 ()@endvoid 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 sizefread(id, sizeof(char), 4, fp);//read waveid[4]='\0';if (!strcmp(id, "WAVE")){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
0 0
- iOS 画音频波形曲线 根据音频数据版
- iOS 画音频波形曲线 根据音频数据版
- iOS 画音频波形曲线 根据音频数据版
- iOS 画音频波形曲线 根据音频数据版
- Delphi根据音频数据画波形
- iOS 根据音频内容绘制波形图 绘图文件独立版
- iOS 画平滑曲线的方法及取音频数据的方法
- iOS 画平滑曲线的方法及取音频数据的方法
- 使用HDC画音频波形图
- 22,波形音频
- 22.2 波形音频 I
- 22.2 波形音频 II
- 可视化音频信号波形
- ffmpeg查看音频波形
- 获取音频的波形
- AVAssetReader 音频波形图
- Android 画曲线音频示意图
- java绘制音频波形图
- 搭建android开发环境(一)
- android之NDK C++图像处理
- iphone4越狱后恢复的几个问题
- 提高Axure设计效率的10条建议
- Web开发中遇到的数据传递问题(二) ——jsonp的用法
- iOS 画音频波形曲线 根据音频数据版
- 读TIJ -6 类再生
- wi7下面配置 git bash使用ssh公钥/私钥登陆centos
- 税率核算Java程序设计
- 异步调用实现对比 java
- Java Mail多人群发与多附件发送
- 32位PLSQL配置为64位的Oracle和64位系统
- 用 Graphviz 可视化函数调用
- Extjs4 下拉框清空已选数据插件