# Creating 2D waveform for Cocos2d game

I'd like to have a Waveform in my iPhone game that I am developing using Cocos2d v2.0.

That's the ideal wave I'd like to achieve. A simpler wave which I would still be happy with is this one.

Some people suggested me to use ParticleEffects ands ParticleDesigners like 71squared but I am not sure that this is the right approach.

Brainstorming, I think that at the end a wave is a plot of some function, and at each frame translate the wave points that where from the origin apart (-x and +x) and computes new wave points for the origin.

Something like this:

for (duration of effect)
compute wave point intensity at origin and display it
and
for each wave point before the center translate to -x and display it
and after the center translate to +x and display it


Let me know what you think and if you have any better solution. I was thinking to achieve the display point by colouring a particular pixel but I need to figure out how :)..

## 2 Answers

One way would be to read as binary and parse from that.

for example: (Assuming you have a 8Bit wav file)

//Get your file
Load wav into buffer
for each 8 bit chunk convert to unsigned Integer
add to array.

//Generate image
for each Integer scale it down to half your image size
draw pixel wide line from center to top from scaled result.
Draw a mirror image of that line directly below.
X coord + 1

//Add prettiness.
Create gradient starting from center out like your image
Use generated image as mask to only expose the pixels you want to see.


That should give you the result you wanted, Unfortunatly I can't post code as I have little understanding of Objective C. Maybe pseudo helps tho :)

• thanks for ur reply. I won't be using any wave files, I just wanted a wave effect for some animation purposes. Although your answer is still useful so +1. I'll leave the question open as I was hoping for some Cocos2d specific answer. Someone in a different forum pointed me out this tutorial developer.apple.com/library/ios/#samplecode/aurioTouch/… and looks like I will have the next week busy learning that unless someone comes up with some additional useful learning material :)
– mm24
Commented Aug 2, 2012 at 14:17
• Ah ok I think i misinterpreted "sound wave" what i believe you are actually looking for is a "Waveform" and this could be used minus the loading of the actual wav file and by randomly generating the numbers (Mayb add a thresh hold of change to ensure it doesn't vary too quickly) Commented Aug 2, 2012 at 14:28

I asked the question also on the cocos2d-iphone forum and someone kindly replied. I thought I would share his answer to avoid duplicate effort:

Answer

I haven't yet tried them because I am struggling deciding what to learn first ES 1.0 vs ES 2.0 as I have just migrated my code to Cocos2d v2.0 and I don't fully understand all the implications, but judging Apple's AurioTouch example posted in the answer it looks promising.