源码:http://files.cnblogs.com/ios8/iOS%E5%BF%83%E7%94%B5%E5%9B%BEDemo.zip

取音频数据和画波形图的方法

ViewController.h

  1. //
  2. //  ViewController.h
  3. //  iOS心电图Demo
  4. //
  5. //  Created by 杜甲 on 13-10-18.
  6. //  Copyright (c) 2013年 杜甲. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "HLSonogramView.h"
  10. @interface ViewController : UIViewController
  11. @property (strong, nonatomic) HLSonogramView* hlSonogramView;
  12. @end

ViewController.m

  1. //
  2. //  ViewController.m
  3. //  iOS心电图Demo
  4. //
  5. //  Created by 杜甲 on 13-10-18.
  6. //  Copyright (c) 2013年 杜甲. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. @interface ViewController ()
  10. @end
  11. @implementation ViewController
  12. - (void)viewDidLoad
  13. {
  14. [super viewDidLoad];
  15. // Do any additional setup after loading the view, typically from a nib.
  16. self.hlSonogramView = [[HLSonogramView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
  17. [self.view addSubview:self.hlSonogramView];
  18. }
  19. - (void)didReceiveMemoryWarning
  20. {
  21. [super didReceiveMemoryWarning];
  22. // Dispose of any resources that can be recreated.
  23. }
  24. @end

HLSonogramView.h

  1. //
  2. //  HLSonogramView.h
  3. //  iOS心电图Demo
  4. //
  5. //  Created by 杜甲 on 13-10-18.
  6. //  Copyright (c) 2013年 杜甲. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #define kMaxKeyPoints  1000
  10. #define kHillSegmentWidth 10
  11. struct ret_value
  12. {
  13. unsigned charchar *data;//注意这里是unsigned char
  14. unsigned long int size;
  15. };
  16. @interface HLSonogramView : UIView
  17. {
  18. CGPoint m_pSonogramKeyPoint[kMaxKeyPoints];
  19. }
  20. @property (assign ,nonatomic) float m_pOffsetX;
  21. @property (assign ,nonatomic) int m_pSonogramKeyPointNum;
  22. //转换后的座标数据,用于绘制波形图
  23. @property (nonatomic, strong) NSMutableArray *m_pointWavArray;
  24. @end

HLSonogramView.m

    1. //
    2. //  HLSonogramView.m
    3. //  iOS心电图Demo
    4. //
    5. //  Created by 杜甲 on 13-10-18.
    6. //  Copyright (c) 2013年 杜甲. All rights reserved.
    7. //
    8. #import "HLSonogramView.h"
    9. #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
    10. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
    11. @implementation HLSonogramView
    12. - (id)initWithFrame:(CGRect)frame
    13. {
    14. self = [super initWithFrame:frame];
    15. if (self) {
    16. // Initialization code
    17. self.backgroundColor = [UIColor whiteColor];
    18. [self generatePoint];
    19. }
    20. return self;
    21. }
    22. -(void)drawRect:(CGRect)rect
    23. {
    24. [super drawRect:rect];
    25. CGContextRef context = UIGraphicsGetCurrentContext();
    26. CGContextSetLineCap(context, kCGLineCapSquare);
    27. CGContextSetLineWidth(context, 1.0);
    28. CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    29. CGContextBeginPath(context);
    30. CGContextMoveToPoint(context, 0, ScreenHeight / 2);
    31. for (int i = 1; i < kMaxKeyPoints; i++) {
    32. CGPoint p0 = m_pSonogramKeyPoint[i - 1];
    33. CGPoint p1 = m_pSonogramKeyPoint[i];
    34. int hSegments = floorf((p1.x - p0.x) / kHillSegmentWidth);
    35. float dx = (p1.x - p0.x) / hSegments;
    36. float da = M_PI / hSegments;
    37. float ymid = (p0.y + p1.y) / 2;
    38. float ampl = (p0.y - p1.y) / 2;
    39. CGPoint pt0,pt1;
    40. pt0 = p0;
    41. for (int j = 0; j < hSegments + 1; ++j) {
    42. pt1.x = p0.x + j * dx;
    43. pt1.y = ymid + ampl * cosf(da * j);
    44. CGContextAddLineToPoint(context, pt0.x, pt0.y);
    45. CGContextAddLineToPoint(context, pt1.x, pt1.y);
    46. pt0 = pt1;
    47. }
    48. }
    49. CGContextStrokePath(context);
    50. }
    51. -(void)generatePoint
    52. {
    53. float m_pWinHeight = ScreenHeight;
    54. float m_pWinWidth  = ScreenWidth;
    55. float x  = 0;
    56. float y = m_pWinHeight / 2;
    57. for (int i = 0; i < kMaxKeyPoints; ++i) {
    58. m_pSonogramKeyPoint[i] = CGPointMake(x, y);
    59. x += m_pWinWidth / 2;
    60. y = rand() % (int)m_pWinHeight;
    61. }
    62. }
    63. //取音频数据
    64. - (void)transformDateOFWavFromFile
    65. {
    66. NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
    67. struct ret_value ret;
    68. load_wave_file([path UTF8String], &ret);
    69. //  printf("data.size = %lu\n", ret.size);
    70. int d = ret.size / 2;
    71. short int wave_data[d];
    72. for(long i=0; i<d; i++)
    73. {
    74. short int w = (ret.data[i*2+1]<<8) | ret.data[i*2];
    75. //printf("%d\n", w);
    76. wave_data[i] = w;
    77. }
    78. int myLineSize = d;
    79. //--添加成员变量-方便外部调用----
    80. CGPoint myLines[myLineSize];
    81. self.m_pointWavArray = [NSMutableArray arrayWithCapacity:8];
    82. //for(int i=0;i<myLineSize;i++)
    83. //{
    84. //    myLines[i]=CGPointMake(10+i, [self getRandomNumber:200 to:400]);
    85. //}
    86. //    for (int i = 0; i < d; i++) {
    87. //        NSLog(@"wave_data[i] = %hd",wave_data[i]);
    88. //    }
    89. for (int i = 0; i < d ; i++) {
    90. float x = 11 * i;
    91. float y = 47.75 + wave_data[i] / 1000;
    92. if (y < 5) {
    93. y = 5;
    94. }
    95. if (y > 92.5) {
    96. y = 92.5;
    97. }
    98. myLines[i] = CGPointMake(x, y);
    99. NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];
    100. [self.m_pointWavArray addObject:pValue];
    101. }
    102. //    for(int i=0;i<d;i++)
    103. //    {
    104. //        float x = 10.0 + i * 300.0 / d;
    105. //        float y = 85+ 200.0 * wave_data[i] / 12767.0 ;
    106. //       // printf("x=%f, y=%f\n", x, y);
    107. //        myLines[i] = CGPointMake(x, y);
    108. //
    109. //        //---存放到波形图的点数组中----------------
    110. //        NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];
    111. //        [self.m_pointWavArray addObject:pValue];
    112. //    }
    113. NSLog(@"%@",self.m_pointWavArray);
    114. }
    115. void load_wave_file(const charchar *fname, struct ret_value *ret)
    116. {
    117. NSLog(@"%s: %d", __func__, __LINE__);
    118. FILEFILE *fp;
    119. fp = fopen(fname, "rb");
    120. if(fp)
    121. {
    122. char id[5];
    123. unsigned long size;
    124. short format_tag, channels, block_align, bits_per_sample;//16 bit data
    125. unsigned long format_length, sample_rate, avg_bytes_sec, data_size;//32 bit data
    126. fread(id, sizeof(char), 4, fp);
    127. id[4]='\0';
    128. if (!strcmp(id, "RIFF"))
    129. {
    130. fread(&size, sizeof(unsigned long), 1, fp);//read file size
    131. fread(id, sizeof(char), 4, fp);//read wave
    132. id[4]='\0';
    133. if (!strcmp(id, "WAVE"))
    134. {
    135. fread(id, sizeof(char), 4, fp);//读取4字节"fmt"
    136. fread(&format_length, sizeof(unsigned long), 1, fp);
    137. fread(&format_tag, sizeof(short), 1, fp);//读取文件tag
    138. fread(&channels, sizeof(short), 1, fp);//读取通道数目
    139. fread(&sample_rate, sizeof(unsigned long), 1, fp);//读取采样率大小
    140. fread(&avg_bytes_sec, sizeof(unsigned long), 1, fp);//读取每秒数据量
    141. fread(&block_align, sizeof(short), 1, fp);//读取块对齐
    142. fread(&bits_per_sample, sizeof(short), 1, fp);//读取每一样本大小
    143. fread(id, sizeof(char), 4, fp);//读取data
    144. fread(&data_size, sizeof(unsigned long), 1, fp);
    145. ret->size = data_size;
    146. ret->data = (unsigned charchar *)malloc(sizeof(char)*data_size);//申请内存空间
    147. fread(ret->data, sizeof(char), data_size, fp);//读取数据
    148. printf("bits_per_sample = %d\n", bits_per_sample);
    149. printf("channels = %d\n", channels);
    150. printf("sample_rate = %lu\n", sample_rate);
    151. }else{
    152. printf("Error: RIFF file but not a wave file\n");
    153. }
    154. }else{
    155. printf("Error: not a RIFF file\n");
    156. }
    157. fclose(fp);
    158. }
    159. }
    160. @end

iOS 画平滑曲线的方法及取音频数据的方法的更多相关文章

  1. 转:使用linq to sql 随机取一行数据的方法

    原文地址:http://outofmemory.cn/code-snippet/1760/usage-linq-to-sql-suiji-take-yixing-data-method 虽然这看来已经 ...

  2. ios crash的原因与抓取crash日志的方法

    首先我们经常会闪退的异常有哪些呢?crash的产生来源于两种问题:违反iOS策略被干掉,以及自身的代码bug. 1.IOS策略 1.1 低内存闪退 前面提到大多数crash日志都包含着执行线程的栈调用 ...

  3. Charles抓取HTTPS数据包方法

    设置代理端口8888 ssl代理设置 允许所有地址连接 手机获取证书之前,先在电脑安装证书,需要信任.help-->ssl-proxying-->Install Charles Root ...

  4. Flask Response响应(flask中设置响应信息的方法,返回json数据的方法)

    设置响应信息的方法 1.  返回自定义的响应头,有两种方式: (1)  第一种是:视图函数return的时候,使用元组,返回自定义的信息 返回的时候的状态码可以自定义信息:"状态码   自定 ...

  5. FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法。。

    [原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法. 1.单个NAL包单元 12字节 ...

  6. Loadrunner Vugen参数列表中数据分配方法及更新值的时间9种组合说明及验证

    作为刚开始学习Loadrunner的新人,Data Assignment Method以及Update Method在相互组合之后,LR如何进行取值让我很是头疼. 于是花了一个晚上的时间认真学习官方文 ...

  7. js去重复和取重复数据

    js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...

  8. 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)

    最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...

  9. Oracle优化器基础知识之访问数据的方法

    目录 一.访问数据的方法 1.直接访问数据 2.访问索引 一.访问数据的方法 Oracle访问表中数据的方法有两种,一种是直接表中访问数据,另外一种是先访问索引,如果索引数据不符合目标SQL,就回表, ...

随机推荐

  1. log4j的详细配置(最省心完美配置)

    先说下我的需求 1,可以记录日记在我们的java开发项目周期中: 2,很简单即可输出日志: 3,每天按照时间将不同的日志输出到不同的文件中,每天输出日志到一个带有当前时间戳的文件中: 4,可以修改当前 ...

  2. springMVC加载远程freemarker模板文件

    在一个大网站里,有很多子域名,也就是有很多子系统,这些子系统由不同的团队负责,对整个网站的风格的风格至少得要是一致的(最基本的页头.页尾必须一致),这个时候得提供一份统一的页头.页尾以及公共的JS.c ...

  3. in文件

    in文件: 被调用的配置文件

  4. 【转】TestNG 与 Junit的比较

    转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1.         JDK 5 Annotations (JDK ...

  5. Inno Setup入门(九)——修改安装过程中的文字显示

    前面说到过可以使用不用的语言文件实现不同的显示方式,方便与国际接轨,事实上即使没有语言文件也可以实现修改.[Messages] 段用于定义安装程序和卸载程序中显示的消息.一般不需要创建 [Messag ...

  6. 转:Ogre内部渲染流程

    以下是 Ogre 的代码中的详细说明: Renderable是OGRE中所有可渲染对象的抽象接口 这个接口抽象出了在渲染管线中的被分组的离散的可渲染对象基本的方法. 此接口的实现类必须是基于单一的材质 ...

  7. 如何查看 EBS 环境上的 INV RUP 版本号

    select 'Application Environment: '|| i.instance_name || ', Host: '|| i.host_name ||', Application Re ...

  8. Qt5中表格处理大数据量

    在Qt中如果是普通项目,GUI处理展现的数据量不大,一般用QTableWidget,QTreeWidget这样的控件就满足了,但是如果数据量行数达到了几万行,那么Widget的展示性能就偏差了. Qt ...

  9. Nginx自定义404页面

    指定一个错误页面: error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } 指定一个url地址: ...

  10. 树莓派进阶之路 (025) - ubuntu下使用VNC连接树莓派raspberry(转)

    转载:http://www.embbnux.com/2014/03/24/on_ubuntu_use_vnc_connect_raspberry/ ‎ 参考:  http://elinux.org/R ...