1
votes

I am looking into making a drawing app with Sprite Kit in iOS, with either swift or objective C.

There is a tutorial here that shows how to draw a line with sprite kit. This is great, but for my app I want more. I want the app to play sound effects while the line is being drawn. The tone of the sound will depend on the speed at which the user is drawing the line. The faster the user moves their finger, the higher pitch the sound is. All i have found regarding playing sounds in sprite kit is background music and playing a single sound. Can someone point me in the right direction to accomplish my goal?

1
Right direction - Use touchesMoved method to track speed of touch coordinates changes and run sound code accordingly. BTW, AVAudioPlayer has no pitch function so you will have to come up with your own solution for that. - sangony
As @sangony said such functionality is not supported by AVAudioPlayer. You should check out other audio API. For example you can try to use AUSampler audio unit. There is a free app called AU Lab which you can use to create a aupreset file that is then loaded by AUSampler. To find out more about Apple's audio API's and Audio Units in particular, I would recomend you this book amazon.com/Learning-Core-Audio-Hands-On-Programming/dp/… - Dmitry Klochkov

1 Answers

0
votes

You'll probably want to have a chromatic set of samples, ranging from the lowest to the highest possible tone you want to play, at a very short duration (maybe 0.25 seconds? You'll need to experiment, but they will all have to be the same duration).

Let's say you have 30 different samples, line-0.wav .. line-29.wav. Then all you need to do is calculate the velocity of the user's drag appropriately, and use some function to map the possible range of drag velocity to the integer range of sample indices. As long as the drag is in progress, play the appropriate sound repeatedly.