IOS Draw audio waveform based on audio data version

Source: Internet
Author: User

&NBSP;&NBSP;&NBSP;&NBSP;DRAWVIEW.H&NBSP;&NBSP;[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 &NBSP;&NBSP;DRAWVIEW.M&NBSP;[OBJC]   #import "DrawView.h"      @implementation Drawview     #define KSIZE   #define BIAS 10000    static Double Fk[ksize] = {0};  static Double _filterdata[2048];        -(void) GenKernel  {     Double fc =.,    & nbsp for (int i = 0; i < ksize; i++)      {         if ((i-ksize/2) = = 0) Fk[i] = * M_PI *FC;          if ((I-KSIZE/2)! = 0) Fk[i] = sin (* m_PI * FC * (I-KSIZE/2))/(I-KSIZE/2);          fk[i] = fk[i] * (0.54-0.46 * COS (* 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];         }     }        }     &nbsp ;-(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;                              &NBSP;&N bsp;                            CGCONTEXTA Ddlinetopoint (CurrentContext, x, y);               }      [[Uicolor Redcolor] setstroke];      Cgcontextstrokepath (CurrentContext); &NBSP;&NBSP;&NBSP,}       @end     viewcontrolleR.H&NBSP;&NBSP;&NBSP;[OBJC]   #import <UIKit/UIKit.h>     @interface Viewcontroller: Uiviewcontroller     @end &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VIEWCONTROLLER.M    [objc]  #import "ViewController.h"   #import "DrawView.h"       struct Wavinfo  {     int   size      char  *data;      Shor t 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];    &N Bsp     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          &NB Sp   Fread (ID, sizeof (char), 4, FP);//read Wave              id[4]= '.                            if (!strcmp (ID, "WAV E "))              {                 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, F p);                  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);                               } &nbs p;            Else              {     &NB Sp           printf ("error\n");             }         }          El Se          {             printf ("error\n");    &NB Sp              fclose (FP);       }     @implementation Viewcontroller    -(void) Viewdidload & nbsp {     [super Viewdidload];      NSString *path = [[NSBundle mainbundle] pathforresource:@ "E Ffect1 "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];  &nbsp  }            Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] Init Withtarget:self Action: @selector (Dragview:)];      [Self.view Addgesturerecognizer:pan];            Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] Initwithtarget:self Action: @selector (Pinchview:)];      [Self.view addgesturerecognizer:pinch];         &nbsp,}     #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)      {     &NB Sp   if (D.wsize > +)          {             D.wsize * = 0.95;         }     }      Else      {   &nbs P     if (D.wsize < 1024x768)          {             D . wsize *= 1.05;         }     }            [Self.view setneed Sdisplay];  }      -(void) didreceivememorywarning  {     [Super Didreceivememorywarning];     /Dispose of any resources the can be reCreated. &NBSP,}     @end   

IOS picture Audio waveform curve based on audio data version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.