• iOS 画音频波形曲线 根据音频数据版


    效果图

     
     
     
     
    DrawView.h
     
     
    [objc]  
    #import <UIKit/UIKit.h>  
      
    @interface DrawView : UIView  
      
    @property shortshort *drawBuffer;  
    @property int  dataLen;  
    @property floatfloat *outRel;  
    @property floatfloat *outImg;  
    @property int bias;  
    @property int wSize;  
    - (void)genKernel;  
      
      
    @end  
     
    DrawView.m
     
    [objc]  
    #import "DrawView.h"  
      
    @implementation DrawView  
      
    #define KSIZE 20  
    #define BIAS 10000  
      
    static double fk[KSIZE] = {0};  
    static double _filterData[2048];  
      
      
      
    - (void)genKernel  
    {  
        double fc = .05;  
        for (int i = 0; i < KSIZE; i++)  
        {  
            if ((i - KSIZE/2) == 0)fk[i] = 22 * M_PI *fc;  
            if ((i - KSIZE/2) != 0 )fk[i] = sin(22 * M_PI * fc * (i - KSIZE/2))/(i - KSIZE/2);  
            fk[i] = fk[i] * (0.54 - 0.46 * cos(22 * M_PI * i / KSIZE ));  
              
        }  
          
        double sum = 0;  
        for (int m = 0; m < KSIZE; m++)  
            sum+=fk[m];  
          
        for (int n = 0; n < KSIZE; n++)  
            fk[n]/=sum;  
    }  
      
    - (void)improveSpectrum  
    {  
        memset(_filterData, 0x0, sizeof(double) * 1024);  
        short transData[(int)self.wSize];  
        memcpy(transData, self.drawBuffer+_bias, _wSize * sizeof(short));  
        for (int i = 0; i < _wSize; i++)  
        {  
            for (int j = 0; j < KSIZE; j++)  
            {  
                _filterData[i] = _filterData[i] + transData[ i - j] * fk[j];  
            }  
        }  
          
    }  
      
      
    - (id)initWithFrame:(CGRect)frame  
    {  
        self = [super initWithFrame:frame];  
        if (self) {  
        }  
        return self;  
    }  
      
      
      
    - (void)drawRect:(CGRect)rect  
    {  
        float delta = 320. / _wSize;  
        [self improveSpectrum];  
      
        [[UIColor grayColor] set];  
        UIRectFill ([self bounds]);  
          
          
        CGContextRef currentContext = UIGraphicsGetCurrentContext();  
        CGContextBeginPath(currentContext);  
        CGContextMoveToPoint(currentContext, 0., 230.);  
        CGContextAddLineToPoint(currentContext, 320., 230.);  
        [[UIColor blueColor] setStroke];  
        CGContextStrokePath(currentContext);  
      
          
        CGContextBeginPath(currentContext);  
        CGContextMoveToPoint(currentContext, 0., 230.);  
        for (int i = 0; i < _wSize; i++)  
        {  
            CGFloat x = i * delta;  
            CGFloat y = _filterData[i] / 150.0 + 230.0;  
              
              
              
              
              
            CGContextAddLineToPoint(currentContext, x, y);  
              
        }  
        [[UIColor redColor] setStroke];  
        CGContextStrokePath(currentContext);  
      
    }  
      
      
    @end  
     
     
     
    ViewController.h
     
     
     
    [objc]  
    #import <UIKit/UIKit.h>  
      
    @interface ViewController : UIViewController  
      
    @end  
     
     
     
     
     
     
     
     
    ViewController.m
     
     
     
    [objc] 
    #import "ViewController.h"  
    #import "DrawView.h"  
      
      
    struct WavInfo  
    {  
        int   size;  
        char  *data;  
        short channels;  
        short block_align;  
        short bits_per_sample;  
        unsigned long sample_rate;  
        unsigned long format_length;  
        unsigned long format_tag;  
        unsigned long avg_bytes_sec;  
          
    };  
      
      
    @interface ViewController ()  
      
    @end  
      
    void decodeWaveInfo(const charchar *fname, struct WavInfo *info)  
    {  
          
        FILEFILE *fp;  
        fp = fopen(fname, "rb");  
      
        if(fp)  
        {  
            char id[5];  
            unsigned long dataSize,size;  
              
            fread(id, sizeof(char), 4, fp);  
            id[4]='';  
            if (!strcmp(id, "RIFF"))  
            {  
                fread(&size, sizeof(unsigned long), 1, fp);//read file size  
                fread(id, sizeof(char), 4, fp);//read wave  
                id[4]='';  
                  
                if (!strcmp(id, "WAVE"))  
                {  
                    fread(id, sizeof(char), 4, fp);  
                    fread(&info->format_length, sizeof(unsigned long), 1, fp);  
                    fread(&info->format_tag, sizeof(short), 1, fp);  
                    fread(&info->channels, sizeof(short), 1, fp);  
                    fread(&info->sample_rate, sizeof(unsigned long), 1, fp);  
                    fread(&info->avg_bytes_sec, sizeof(unsigned long), 1, fp);  
                    fread(&info->block_align, sizeof(short), 1, fp);  
                    fread(&info->bits_per_sample, sizeof(short), 1, fp);  
                    fread(id, sizeof(char), 4, fp);  
                    fread(&dataSize, sizeof(unsigned long), 1, fp);  
                    info->size = dataSize;  
                    info->data = ( charchar *)malloc(sizeof(char)*dataSize);  
                    fread(info->data, sizeof(char), dataSize, fp);  
                      
                }  
                else  
                {  
                    printf("Error ");  
                }  
            }  
            else  
            {  
                printf("Error ");  
            }  
            fclose(fp);  
        }  
    }  
      
    @implementation ViewController  
      
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
        NSString *path = [[NSBundle mainBundle] pathForResource:@"effect1" ofType:@"wav"];  
        struct WavInfo wavInfo;  
        decodeWaveInfo([path UTF8String], &wavInfo);  
          
          
        DrawView *d = (DrawView *)self.view;  
        d.drawBuffer = (shortshort *)malloc(sizeof(short) * wavInfo.size / 2 );  
        [d genKernel];  
        d.dataLen = wavInfo.size / 2;  
        d.wSize = 256;  
        d.bias = 0;  
          
        int n = 0;  
        for (int m = 0; m < wavInfo.size / 2; m++)  
        {  
            d.drawBuffer[n++] = wavInfo.data[m * 2 + 1] << 8 | wavInfo.data[m * 2];  
        }  
          
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragView:)];  
        [self.view addGestureRecognizer:pan];  
          
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];  
        [self.view addGestureRecognizer:pinch];  
      
          
    }  
      
    #pragma mark -  
    #pragma mark Gesture Recognizer callback  
      
    - (void)dragView:(UIPanGestureRecognizer *)recognizer  
    {  
        DrawView *d = (DrawView *)self.view;  
          
        CGPoint p = [recognizer translationInView:self.view];  
        NSLog(@"translate point is : %@",NSStringFromCGPoint(p));  
        d.bias -= p.x;  
          
        [self.view setNeedsDisplay];  
    }  
      
    - (void)pinchView:(UIPinchGestureRecognizer *)recognizer  
    {  
        DrawView *d = (DrawView *)self.view;  
          
        if (recognizer.scale > 1.0)  
        {  
            if (d.wSize > 128)  
            {  
                d.wSize *= 0.95;  
            }  
        }  
        else  
        {  
            if (d.wSize < 1024)  
            {  
                d.wSize *= 1.05;  
            }  
        }  
          
        [self.view setNeedsDisplay];  
    }  
      
      
    - (void)didReceiveMemoryWarning  
    {  
        [super didReceiveMemoryWarning];  
        // Dispose of any resources that can be recreated.  
    }  
      
    @end  
     
  • 相关阅读:
    maven打包时加入依赖包及加入本地依赖包
    is_file和file_exists效率比较
    window.open()详解及浏览器兼容性问题示例探讨
    Zend Studio汉化失败,如何给Zend Studio进行汉化
    HTML页面跳转的5种方法
    PHP中的符号 ->、=> 和 :: 分别表示什么意思?
    php中$this->是什么意思
    关于define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -7));的理解
    【精华】PHP网站验证码不显示的终结解决方案
    php提示undefined index的几种解决方法
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5357439.html
Copyright © 2020-2023  润新知