iOS 画平滑曲线的方法及取音频数据的方法
源码:http://files.cnblogs.com/ios8/iOS%E5%BF%83%E7%94%B5%E5%9B%BEDemo.zip
取音频数据和画波形图的方法
ViewController.h
- //
- // ViewController.h
- // iOS心电图Demo
- //
- // Created by 杜甲 on 13-10-18.
- // Copyright (c) 2013年 杜甲. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "HLSonogramView.h"
- @interface ViewController : UIViewController
- @property (strong, nonatomic) HLSonogramView* hlSonogramView;
- @end
ViewController.m
- //
- // ViewController.m
- // iOS心电图Demo
- //
- // Created by 杜甲 on 13-10-18.
- // Copyright (c) 2013年 杜甲. All rights reserved.
- //
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- self.hlSonogramView = [[HLSonogramView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
- [self.view addSubview:self.hlSonogramView];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
HLSonogramView.h
- //
- // HLSonogramView.h
- // iOS心电图Demo
- //
- // Created by 杜甲 on 13-10-18.
- // Copyright (c) 2013年 杜甲. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #define kMaxKeyPoints 1000
- #define kHillSegmentWidth 10
- struct ret_value
- {
- unsigned charchar *data;//注意这里是unsigned char
- unsigned long int size;
- };
- @interface HLSonogramView : UIView
- {
- CGPoint m_pSonogramKeyPoint[kMaxKeyPoints];
- }
- @property (assign ,nonatomic) float m_pOffsetX;
- @property (assign ,nonatomic) int m_pSonogramKeyPointNum;
- //转换后的座标数据,用于绘制波形图
- @property (nonatomic, strong) NSMutableArray *m_pointWavArray;
- @end
HLSonogramView.m
- //
- // HLSonogramView.m
- // iOS心电图Demo
- //
- // Created by 杜甲 on 13-10-18.
- // Copyright (c) 2013年 杜甲. All rights reserved.
- //
- #import "HLSonogramView.h"
- #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
- #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
- @implementation HLSonogramView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- self.backgroundColor = [UIColor whiteColor];
- [self generatePoint];
- }
- return self;
- }
- -(void)drawRect:(CGRect)rect
- {
- [super drawRect:rect];
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetLineCap(context, kCGLineCapSquare);
- CGContextSetLineWidth(context, 1.0);
- CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
- CGContextBeginPath(context);
- CGContextMoveToPoint(context, 0, ScreenHeight / 2);
- for (int i = 1; i < kMaxKeyPoints; i++) {
- CGPoint p0 = m_pSonogramKeyPoint[i - 1];
- CGPoint p1 = m_pSonogramKeyPoint[i];
- int hSegments = floorf((p1.x - p0.x) / kHillSegmentWidth);
- float dx = (p1.x - p0.x) / hSegments;
- float da = M_PI / hSegments;
- float ymid = (p0.y + p1.y) / 2;
- float ampl = (p0.y - p1.y) / 2;
- CGPoint pt0,pt1;
- pt0 = p0;
- for (int j = 0; j < hSegments + 1; ++j) {
- pt1.x = p0.x + j * dx;
- pt1.y = ymid + ampl * cosf(da * j);
- CGContextAddLineToPoint(context, pt0.x, pt0.y);
- CGContextAddLineToPoint(context, pt1.x, pt1.y);
- pt0 = pt1;
- }
- }
- CGContextStrokePath(context);
- }
- -(void)generatePoint
- {
- float m_pWinHeight = ScreenHeight;
- float m_pWinWidth = ScreenWidth;
- float x = 0;
- float y = m_pWinHeight / 2;
- for (int i = 0; i < kMaxKeyPoints; ++i) {
- m_pSonogramKeyPoint[i] = CGPointMake(x, y);
- x += m_pWinWidth / 2;
- y = rand() % (int)m_pWinHeight;
- }
- }
- //取音频数据
- - (void)transformDateOFWavFromFile
- {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
- struct ret_value ret;
- load_wave_file([path UTF8String], &ret);
- // printf("data.size = %lu\n", ret.size);
- int d = ret.size / 2;
- short int wave_data[d];
- for(long i=0; i<d; i++)
- {
- short int w = (ret.data[i*2+1]<<8) | ret.data[i*2];
- //printf("%d\n", w);
- wave_data[i] = w;
- }
- int myLineSize = d;
- //--添加成员变量-方便外部调用----
- CGPoint myLines[myLineSize];
- self.m_pointWavArray = [NSMutableArray arrayWithCapacity:8];
- //for(int i=0;i<myLineSize;i++)
- //{
- // myLines[i]=CGPointMake(10+i, [self getRandomNumber:200 to:400]);
- //}
- // for (int i = 0; i < d; i++) {
- // NSLog(@"wave_data[i] = %hd",wave_data[i]);
- // }
- for (int i = 0; i < d ; i++) {
- float x = 11 * i;
- float y = 47.75 + wave_data[i] / 1000;
- if (y < 5) {
- y = 5;
- }
- if (y > 92.5) {
- y = 92.5;
- }
- myLines[i] = CGPointMake(x, y);
- NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];
- [self.m_pointWavArray addObject:pValue];
- }
- // for(int i=0;i<d;i++)
- // {
- // float x = 10.0 + i * 300.0 / d;
- // float y = 85+ 200.0 * wave_data[i] / 12767.0 ;
- // // printf("x=%f, y=%f\n", x, y);
- // myLines[i] = CGPointMake(x, y);
- //
- // //---存放到波形图的点数组中----------------
- // NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];
- // [self.m_pointWavArray addObject:pValue];
- // }
- NSLog(@"%@",self.m_pointWavArray);
- }
- void load_wave_file(const charchar *fname, struct ret_value *ret)
- {
- NSLog(@"%s: %d", __func__, __LINE__);
- FILEFILE *fp;
- fp = fopen(fname, "rb");
- if(fp)
- {
- char id[5];
- unsigned long size;
- short format_tag, channels, block_align, bits_per_sample;//16 bit data
- unsigned long format_length, sample_rate, avg_bytes_sec, data_size;//32 bit data
- 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, "WAVE"))
- {
- fread(id, sizeof(char), 4, fp);//读取4字节"fmt"
- fread(&format_length, sizeof(unsigned long), 1, fp);
- fread(&format_tag, sizeof(short), 1, fp);//读取文件tag
- fread(&channels, sizeof(short), 1, fp);//读取通道数目
- fread(&sample_rate, sizeof(unsigned long), 1, fp);//读取采样率大小
- fread(&avg_bytes_sec, sizeof(unsigned long), 1, fp);//读取每秒数据量
- fread(&block_align, sizeof(short), 1, fp);//读取块对齐
- fread(&bits_per_sample, sizeof(short), 1, fp);//读取每一样本大小
- fread(id, sizeof(char), 4, fp);//读取data
- fread(&data_size, sizeof(unsigned long), 1, fp);
- ret->size = data_size;
- ret->data = (unsigned charchar *)malloc(sizeof(char)*data_size);//申请内存空间
- fread(ret->data, sizeof(char), data_size, fp);//读取数据
- printf("bits_per_sample = %d\n", bits_per_sample);
- printf("channels = %d\n", channels);
- printf("sample_rate = %lu\n", sample_rate);
- }else{
- printf("Error: RIFF file but not a wave file\n");
- }
- }else{
- printf("Error: not a RIFF file\n");
- }
- fclose(fp);
- }
- }
- @end
iOS 画平滑曲线的方法及取音频数据的方法的更多相关文章
- 转:使用linq to sql 随机取一行数据的方法
原文地址:http://outofmemory.cn/code-snippet/1760/usage-linq-to-sql-suiji-take-yixing-data-method 虽然这看来已经 ...
- ios crash的原因与抓取crash日志的方法
首先我们经常会闪退的异常有哪些呢?crash的产生来源于两种问题:违反iOS策略被干掉,以及自身的代码bug. 1.IOS策略 1.1 低内存闪退 前面提到大多数crash日志都包含着执行线程的栈调用 ...
- Charles抓取HTTPS数据包方法
设置代理端口8888 ssl代理设置 允许所有地址连接 手机获取证书之前,先在电脑安装证书,需要信任.help-->ssl-proxying-->Install Charles Root ...
- Flask Response响应(flask中设置响应信息的方法,返回json数据的方法)
设置响应信息的方法 1. 返回自定义的响应头,有两种方式: (1) 第一种是:视图函数return的时候,使用元组,返回自定义的信息 返回的时候的状态码可以自定义信息:"状态码 自定 ...
- FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法。。
[原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法. 1.单个NAL包单元 12字节 ...
- Loadrunner Vugen参数列表中数据分配方法及更新值的时间9种组合说明及验证
作为刚开始学习Loadrunner的新人,Data Assignment Method以及Update Method在相互组合之后,LR如何进行取值让我很是头疼. 于是花了一个晚上的时间认真学习官方文 ...
- js去重复和取重复数据
js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...
- 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)
最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...
- Oracle优化器基础知识之访问数据的方法
目录 一.访问数据的方法 1.直接访问数据 2.访问索引 一.访问数据的方法 Oracle访问表中数据的方法有两种,一种是直接表中访问数据,另外一种是先访问索引,如果索引数据不符合目标SQL,就回表, ...
随机推荐
- node.js模块化写法入门
子模块的写法: function SVN(){ console.log('svn initialized'); return this; } function getInstance() { cons ...
- 将excel表导入到mysql中
//导入excel表 方法一: )打开Excel另存为CSV文件 )将文件编码转化为utf8,用NotePad++打开csv文件,选择格式—转为utf8编码格式—保存 )在MySQL建表,字段的顺序要 ...
- DHCP 服务测试
DHCP三个端口: 服务端:UDP 67 客户端:UDP 68 DHCPv6 客户端:UDP 546,这是需要特别开启的 DHCP failover 服务,用来做双机热备的. 实验一.DHCP服务器基 ...
- asp.net 如何配置authentication,完成基于表单的身份验证
步骤一: 在根目录下的web.config中加入: <system.web> <authentication mode="Forms"> ...
- Dockerfile 构建前端node应用cnpm命令启动nodejs服务
cat Dockerfile.node FROM centos MAINTAINER zha*****ch.cn ENV LANG en_US.UTF-8 RUN /bin/cp /usr/share ...
- Mac巧用AirDrop实现大文件传输
最近想更新一下Xcode8.3,无奈资源太大,不想一点点下载了,公司服务器上正好有官网下载好的,就想直接拿过来使用,本来想通过QQ传输的,无奈发现QQ传输提示资源过大. 于是就想到了这货:AirDro ...
- Eclipse中10个最有用的快捷键组合(转)
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- 树莓派进阶之路 (032) -字符问题(2) - 用c语言怎样得到一个汉字的GB2312编码(转)
C/C++支持的是ASCII,不过汉字编码中,GB2312与ASCII是兼容的,所以可以在C中获得汉字的GB2312编码 GB2312是两个字节的,第一字节是高八位,第二字节是低八位,比如下面的程序: ...
- Using 1-Wire device with Intel Galileo
Using 1-Wire device with Intel Galileo 3 Replies Many people have had trouble with getting 1-Wire de ...
- Java8 lambda表达式10个示例
Java 8 刚于几周前发布,日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Ja ...