zoukankan      html  css  js  c++  java
  • iOS 画平滑曲线的方法及取音频数据的方法

    源码: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.   
    9. #import <UIKit/UIKit.h>  
    10. #import "HLSonogramView.h"  
    11. @interface ViewController : UIViewController  
    12. @property (strong, nonatomic) HLSonogramView* hlSonogramView;  
    13.   
    14. @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.   
    9. #import "ViewController.h"  
    10.   
    11. @interface ViewController ()  
    12.   
    13. @end  
    14.   
    15. @implementation ViewController  
    16.   
    17. - (void)viewDidLoad  
    18. {  
    19.     [super viewDidLoad];  
    20.     // Do any additional setup after loading the view, typically from a nib.  
    21.       
    22.     self.hlSonogramView = [[HLSonogramView alloc] initWithFrame:CGRectMake(00320480)];  
    23.       
    24.       
    25.     [self.view addSubview:self.hlSonogramView];  
    26. }  
    27.   
    28. - (void)didReceiveMemoryWarning  
    29. {  
    30.     [super didReceiveMemoryWarning];  
    31.     // Dispose of any resources that can be recreated.  
    32. }  
    33.   
    34. @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.   
    9. #import <UIKit/UIKit.h>  
    10. #define kMaxKeyPoints  1000  
    11. #define kHillSegmentWidth 10  
    12.   
    13. struct ret_value  
    14. {  
    15.     unsigned charchar *data;//注意这里是unsigned char  
    16.     unsigned long int size;  
    17. };  
    18.   
    19. @interface HLSonogramView : UIView  
    20. {  
    21.      CGPoint m_pSonogramKeyPoint[kMaxKeyPoints];  
    22. }  
    23. @property (assign ,nonatomicfloat m_pOffsetX;  
    24. @property (assign ,nonatomicint m_pSonogramKeyPointNum;  
    25. //转换后的座标数据,用于绘制波形图  
    26. @property (nonatomicstrongNSMutableArray *m_pointWavArray;  
    27. @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.   
      9. #import "HLSonogramView.h"  
      10. #define ScreenHeight [[UIScreen mainScreen] bounds].size.height  
      11. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width  
      12. @implementation HLSonogramView  
      13.   
      14. - (id)initWithFrame:(CGRect)frame  
      15. {  
      16.     self = [super initWithFrame:frame];  
      17.     if (self) {  
      18.         // Initialization code  
      19.         self.backgroundColor = [UIColor whiteColor];  
      20.         [self generatePoint];  
      21.     }  
      22.     return self;  
      23. }  
      24.   
      25. -(void)drawRect:(CGRect)rect  
      26. {  
      27.     [super drawRect:rect];  
      28.     CGContextRef context = UIGraphicsGetCurrentContext();  
      29.     CGContextSetLineCap(context, kCGLineCapSquare);  
      30.     CGContextSetLineWidth(context, 1.0);  
      31.     CGContextSetRGBStrokeColor(context, 1.00.00.01.0);  
      32.     CGContextBeginPath(context);  
      33.     CGContextMoveToPoint(context, 0, ScreenHeight / 2);  
      34.     for (int i = 1; i < kMaxKeyPoints; i++) {  
      35.           
      36.         CGPoint p0 = m_pSonogramKeyPoint[i - 1];  
      37.         CGPoint p1 = m_pSonogramKeyPoint[i];  
      38.           
      39.         int hSegments = floorf((p1.x - p0.x) / kHillSegmentWidth);  
      40.         float dx = (p1.x - p0.x) / hSegments;  
      41.         float da = M_PI / hSegments;  
      42.         float ymid = (p0.y + p1.y) / 2;  
      43.         float ampl = (p0.y - p1.y) / 2;  
      44.           
      45.           
      46.         CGPoint pt0,pt1;  
      47.         pt0 = p0;  
      48.         for (int j = 0; j < hSegments + 1; ++j) {  
      49.             pt1.x = p0.x + j * dx;  
      50.             pt1.y = ymid + ampl * cosf(da * j);  
      51.             CGContextAddLineToPoint(context, pt0.x, pt0.y);  
      52.             CGContextAddLineToPoint(context, pt1.x, pt1.y);  
      53.             pt0 = pt1;  
      54.         }     
      55.     }  
      56.     CGContextStrokePath(context);  
      57. }  
      58.   
      59. -(void)generatePoint  
      60. {  
      61.     float m_pWinHeight = ScreenHeight;  
      62.     float m_pWinWidth  = ScreenWidth;  
      63.       
      64.       
      65.     float x  = 0;  
      66.     float y = m_pWinHeight / 2;  
      67.          
      68.     for (int i = 0; i < kMaxKeyPoints; ++i) {  
      69.         m_pSonogramKeyPoint[i] = CGPointMake(x, y);  
      70.         x += m_pWinWidth / 2;  
      71.         y = rand() % (int)m_pWinHeight;  
      72.     }  
      73. }  
      74. //取音频数据  
      75. - (void)transformDateOFWavFromFile  
      76. {  
      77.     NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];  
      78.     struct ret_value ret;  
      79.     load_wave_file([path UTF8String], &ret);  
      80.     //  printf("data.size = %lu ", ret.size);  
      81.       
      82.     int d = ret.size / 2;  
      83.     short int wave_data[d];  
      84.     for(long i=0; i<d; i++)  
      85.     {  
      86.         short int w = (ret.data[i*2+1]<<8) | ret.data[i*2];  
      87.         //printf("%d ", w);  
      88.         wave_data[i] = w;  
      89.     }  
      90.       
      91.     int myLineSize = d;  
      92.       
      93.     //--添加成员变量-方便外部调用----  
      94.     CGPoint myLines[myLineSize];  
      95.     self.m_pointWavArray = [NSMutableArray arrayWithCapacity:8];  
      96.       
      97.     //for(int i=0;i<myLineSize;i++)  
      98.     //{  
      99.     //    myLines[i]=CGPointMake(10+i, [self getRandomNumber:200 to:400]);  
      100.     //}  
      101.     //    for (int i = 0; i < d; i++) {  
      102.     //        NSLog(@"wave_data[i] = %hd",wave_data[i]);  
      103.     //    }  
      104.       
      105.     for (int i = 0; i < d ; i++) {  
      106.         float x = 11 * i;  
      107.         float y = 47.75 + wave_data[i] / 1000;  
      108.         if (y < 5) {  
      109.             y = 5;  
      110.         }  
      111.         if (y > 92.5) {  
      112.             y = 92.5;  
      113.         }  
      114.         myLines[i] = CGPointMake(x, y);  
      115.         NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];  
      116.         [self.m_pointWavArray addObject:pValue];  
      117.     }  
      118.       
      119.     //    for(int i=0;i<d;i++)  
      120.     //    {  
      121.     //        float x = 10.0 + i * 300.0 / d;  
      122.     //        float y = 85+ 200.0 * wave_data[i] / 12767.0 ;  
      123.     //       // printf("x=%f, y=%f ", x, y);  
      124.     //        myLines[i] = CGPointMake(x, y);  
      125.     //  
      126.     //        //---存放到波形图的点数组中----------------  
      127.     //        NSValue *pValue = [NSValue valueWithCGPoint:myLines[i]];  
      128.     //        [self.m_pointWavArray addObject:pValue];  
      129.     //    }  
      130.     NSLog(@"%@",self.m_pointWavArray);  
      131.       
      132. }  
      133.   
      134. void load_wave_file(const charchar *fname, struct ret_value *ret)  
      135. {  
      136.     NSLog(@"%s: %d", __func__, __LINE__);  
      137.       
      138.     FILEFILE *fp;  
      139.     fp = fopen(fname, "rb");  
      140.     if(fp)  
      141.     {  
      142.         char id[5];  
      143.         unsigned long size;  
      144.         short format_tag, channels, block_align, bits_per_sample;//16 bit data  
      145.         unsigned long format_length, sample_rate, avg_bytes_sec, data_size;//32 bit data  
      146.           
      147.         fread(idsizeof(char), 4, fp);  
      148.         id[4]='';  
      149.         if (!strcmp(id"RIFF"))  
      150.         {  
      151.             fread(&size, sizeof(unsigned long), 1, fp);//read file size  
      152.             fread(idsizeof(char), 4, fp);//read wave  
      153.             id[4]='';  
      154.               
      155.             if (!strcmp(id"WAVE"))  
      156.             {  
      157.                 fread(idsizeof(char), 4, fp);//读取4字节"fmt"  
      158.                 fread(&format_length, sizeof(unsigned long), 1, fp);  
      159.                 fread(&format_tag, sizeof(short), 1, fp);//读取文件tag  
      160.                 fread(&channels, sizeof(short), 1, fp);//读取通道数目  
      161.                 fread(&sample_rate, sizeof(unsigned long), 1, fp);//读取采样率大小  
      162.                 fread(&avg_bytes_sec, sizeof(unsigned long), 1, fp);//读取每秒数据量  
      163.                 fread(&block_align, sizeof(short), 1, fp);//读取块对齐  
      164.                 fread(&bits_per_sample, sizeof(short), 1, fp);//读取每一样本大小  
      165.                 fread(idsizeof(char), 4, fp);//读取data  
      166.                 fread(&data_size, sizeof(unsigned long), 1, fp);  
      167.                 ret->size = data_size;  
      168.                 ret->data = (unsigned charchar *)malloc(sizeof(char)*data_size);//申请内存空间  
      169.                 fread(ret->data, sizeof(char), data_size, fp);//读取数据  
      170.                   
      171.                 printf("bits_per_sample = %d ", bits_per_sample);  
      172.                 printf("channels = %d ", channels);  
      173.                 printf("sample_rate = %lu ", sample_rate);  
      174.             }else{  
      175.                 printf("Error: RIFF file but not a wave file ");  
      176.             }  
      177.         }else{  
      178.             printf("Error: not a RIFF file ");  
      179.         }  
      180.         fclose(fp);  
      181.     }  
      182. }  
      183.   
      184. @end  
  • 相关阅读:
    Python学习笔记013_正则表达式
    Python学习笔记012_网络_异常
    Python学习笔记011_模块_标准库_第三方库的安装
    Python学习笔记010_迭代器_生成器
    PHP extract() 函数
    php 获取客户端IP
    php array_walk
    PHP array_map()
    PHP call_user_func
    类的更新----MVC设计模式
  • 原文地址:https://www.cnblogs.com/ios8/p/ios-line.html
Copyright © 2011-2022 走看看