源码: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. 解决java网络编程IPv6问题

    如果系统中开启了IPV6协议(比如window7),java网络编程经常会获取到IPv6的地址,这明显不是我们想要的结果,搜索发现很多蹩脚的做法是:禁止IPv6协议.其实查看官方文档有详细的说明: j ...

  2. java 八种基本数据类型之初始值、取值范围、对应的封装类

      CreateTime--2017年12月6日10:03:53 Author:Marydon 一.java数据类型之基本数据类型 (二)八种基本数据类型的特征 import java.math.Bi ...

  3. Eureka客户端网卡和网段选择

    当机器上有多个网卡或者机器上配置了回环地址的时候,Eureka客户端呈报给服务端的IP将不可预见,为了指定IP我们需要增加以下配置: 在bootstrap.yml中增加配置内容: spring: cl ...

  4. 编译Boost库

    VS版本: 虽然网络上有,但是还是记录下,找到VS的command prompt,然后切换到boost的根目录: 1.运行 bootstrap.bat 它在当前目录下会生成b2.exe2. 2.运行b ...

  5. <转>lua解释执行脚本流程

    本文转自:http://www.cnblogs.com/zxh1210603696/p/4458473.html #include "lua.hpp" #include <i ...

  6. 【基础】centos 6.X 下修改图形界面为命令行界面(单用户救援模式)

    1. Linux开机引导的时候,按键盘上的e 就可以进入进入GRUB菜单界面. 2.在出现GRUB引导画面时(CentOS(2.6.18-274**)),按字母e键,进入GRUB编辑状态: 3.把光标 ...

  7. Sqlserver大数据量分区表创建

    /* 逆向删除对象 DROP PARTITION SCHEME [PS_BasicPolicy2014]; DROP PARTITION FUNCTION [PF_BasicPolicy2014]; ...

  8. TextView中显示链接 定义颜色

    <TextView android:id="@+id/textView" android:layout_width="match_parent" andr ...

  9. Access数据库中日期时间类型的时间段查询

    例: select  ID,预设点,备注  from 预设点派车预警 where ( 到达时间>=#2013-01-01 12:12:12# and 到达时间<=#2016-01-24 2 ...

  10. Oracle 12C -- 清空audit记录

    1.使用job清空 SQL> dbms_audit_mgmt.create_purge_job ( audit_trail_type=> DBMS_AUDIT_MGMT.AUDIT_TRA ...