开发者

How to generate sine wave w.r.t a song in iphone?

i am building an music application in iphone which i have made using AVAudioPlayer,and it is playing music successfully.

But now my requirement is to generate a wave(sine/square/digital/analog) on screen as the music begins to play and stop the wa开发者_JAVA百科ve after music file ends.

How can i do this ?


Perhaps you can use the core-plot project to draw your curves.


This is a very good blog post that shows how to draw waveforms: Drawing Waveforms
It also contains hints on how to deals with data reduction for large buffers.

EDIT:
The article is written for Mac OS X but as it uses Quartz to display the waveform, it should work fine on the iPhone.


Just generating sine wave is easier. Well, I implemented sine wave into the UIView drawrect method as follows :

float x=75;
float yc=50;
float w=0; 
 while (w<=rect.frame.size.width) {
CGPathMoveToPoint(path, nil, w,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+x/4, -yc,w+ x/2, y/2);
CGPathMoveToPoint(path, nil, w+x/2,y/2);
CGPathAddQuadCurveToPoint(path, nil, w+3*x/4, y+yc, w+x, y/2);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathStroke);
w+=x;
}

Here x would be the width of each sine wave, while y is the height of the frame. This would draw number of sine waves to fit in the whole UIViewFrame. It would produce crisp looking sine wave and yc being control handle. Try it you might like it.

If the width ie. x is similar to the width of the frame then a single sine wave will be produced.

Number of complete sine wave = (width of frame) / ('x' width of each sine wave) Now, try to adjust the amplitude and frequency of the wave with respect to the power of audio.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜