Albert Renshaw
Albert Renshaw

Reputation: 17902

Objective-C play sound

I know how to play mp3 files and whatnot in Xcode iOS. But how do I play a certain frequency, like if I just wanted to emit a C# note for 25 seconds; how might I do that? (The synth isn't as important to me as just the pitch of the note.)

Upvotes: 4

Views: 2196

Answers (2)

hotpaw2
hotpaw2

Reputation: 70733

You can synthesize waveforms of your desired frequency and feed them to the callbacks of either the Audio Queue or the RemoteIO Audio Unit API.

Here is a short tutorial on some of the code needed to create sine wave tones for iOS in C.

Upvotes: 0

Miguel Grinberg
Miguel Grinberg

Reputation: 67492

You need to generate the PCM audio waveform that corresponds to the note you want to play and store that into a sample buffer in memory. Then you send that buffer to the audio hardware.

Here is a tutorial on generating waveforms of several types. The article goes into some details on the many aspects to a note you need to consider, including the frequency, volume, waveform shape, sampling rate, etc. The article comes with Flash source code, I think you should have no problem taking the concepts and adapting them to iOS.

If you also need a library that you can use to play the generated buffers on iOS, then I recommend the open source Finch.

I hope this helps!

Upvotes: 1

Related Questions