源码: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. 【Linux】常见Linux默认的shell

    常见的操作系统下的shell: Linux下默认的shell是Bourne Again shell(bash) Solaris和FreeBSD下默认的是Bourne shell(sh) AIX系统下默 ...

  2. django之创建第7-2个项目-url配置分离

    1.urls.PY分离 # -*- coding: UTF-8 -*- from django.conf.urls import patterns, include, url # Uncomment ...

  3. 【php页面表单提交】form校验后再提交,非ajax提交

    form表单校验后,在执行提交动作: <form method="post" action="{:U('Custom/addmsg')}" id=&quo ...

  4. JavaScript Window Screen 用户屏幕的信息

    window.screen 对象包含有关用户屏幕的信息. Window Screen window.screen 对象在编写时可以不使用 window 这个前缀. 一些属性: screen.avail ...

  5. Weex开发之路(1):开发环境搭建

    一.Weex介绍 Weex是阿里巴巴在2016年6月份对外开源的一款移动端跨平台的移动开发工具,Weex的出现让我们的应用既有了Native的性能和H5的动态性,只要通过前端JS语法就能写出同时兼容i ...

  6. window 10 企业版激活

    一. 用管理员权限打开CMD.EXE 接着输入以下命令: slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 弹出窗口提示:“成功的安装了产品密钥”. 继续输入以下命令: ...

  7. 根据友盟统计错误分析线上的崩溃-b

    登陆友盟官网找到友盟统计,找到你iOS平台下你所属的APP(图1) 图1 点击进去会出现当日错误列表,选择你发生错误的日期(图2) 图2 我们可以看到,这一天中出现了两个错误,每个错误出现在不同的时间 ...

  8. SQL SERVER 2008自动发送邮件(完整版)

    这两天都在搞这个东西,从开始的一点不懂,到现在自己可以独立的完成这个功能!在这个过程中,CSDN的好多牛人都给了我很大的帮助,在此表示十二分的感谢!写这篇文章,一是为了巩固一下,二嘛我也很希望我写的这 ...

  9. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  10. sublime Text 些许使用配置

    在安装numpy等库函数时,通过“命令提示符”操作显示库函数已经安装完毕,在pycharm中可是依然显示引用失败,尝试使用sublime,显示可用,遂好好使用sublime,现配置成想用的模式. 1 ...