change sound pitch (not in realtime) - iphone

i alway had this question in my mind, but wherever i asked, i could never get an answer or a suggestion that would be helpful:
How can i pitch a sound (not realtime) ?
Im using AVFoundation framework to play my sounds like so:
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:TempRecFile error:nil];
player.volume = 1;
[player play];
How can i set the pitch or the frequency of my sound without having to use some othere frameworks like OpenAL. Although, if you know a place where i could learn some OpenAl you're very welcome :D

There are a few iPhone time-pitch shifting libraries mentioned the this StackOverflow answer: Real-time Pitch Shifting on the iPhone,
which work offline as well as potentially in real-time.
Here's a wikipedia article on time-pitch technology, and a dspdimention overview on the topic.

Related

iPhone audio equalizer

I am playing video from the web with AVPlayerLayer :
AVAsset *newAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
AVPlayerItem *newPlayerItem = [[AVPlayerItem alloc]initWithAsset:newAsset];
audioPlayer = [[AVPlayer alloc]initWithPlayerItem:newPlayerItem];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:audioPlayer];
And i want to be able to add Audio Equalizer to the video. Something like :
Pop, Flat, Balled, Blues, Classical, Dance, Metal
I search for and documentation for this in Google and in apple developer program resources and didn't find nothing.
And i also noticed that some apps have this function, but it's for iOS 6.1 only.
Any help with this issue? Did apple have something build-in or it's a non-apple source?
This Stack Overflow question has answers offering several different solutions: How to make a simple EQ AudioUnit (bass, mid, treble) with iOS?

MPMusicPlayerController Speed Adjustment

HI,
Is there any way to adjust the speed of the song being played by the MPMusicPlayerController?
I've searched everywhere but didn't find anything useful. If there is no way to do it where can I find an example which does it with other components? Some say OpenAL, but I can't find any clear way to use the iPod library with this and change the speed of the song...
Mainly the thing I need is:
The user chooses a song from the iPod library trough MPMusicPlayerController
You have 2 buttons: Slow Down & Speed Up
If the user presses "Slow Down" the speed of the song is slowed down by lets say 5% or something. "Speed Up" visa-versa.
I really hope someone can help me with this!
Thanks in advance!
You could do this as of iOS 3.2 and later -
[musicPlayer play];
[musicPlayer setCurrentPlaybackRate:2.0];
Ref: https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMediaPlayback_protocol/Reference/Reference.html#//apple_ref/occ/intfp/MPMediaPlayback/currentPlaybackRate
You can use AVAudioPlayer and AVPlayer for the above purpose.
They have a rate property which can set the speed of the song.
[AVPlayer setRate:1.25]; // 25% faster
However, AVPlayer cannot change the speed by 5% but 25% and AVAudioPlayer works for iOS 5 and above.
If you wish to go for other alternative then Dirac is the best option. Try DIRAC 3 LE which is free.
Use this Link to get an idea on how to use Dirac. For more information, let me know.
#girish_vr
No, you can't do this using only the MPMusicPlayerController.
If you don't mind the pitch going up and down proportional to the speed changes, then you can use the OpenAL resampler after converting your sound file to raw PCM sample files or buffers.
If you don't want the pitch to change, what you are looking for is DSP time pitch stretching/modification/correction techniques. OpenAL can't do this, but there are commercial DSP libraries (DIRAC may be one) available that can do this on iOS. You could also try writing your own phase vocoder, but this is non-trivial digital signal processing code.

Audio/Voice Visualization

Hey you Objective-C bods.
Does anyone know how I would go about changing (transforming) an image based on the input from the Microphone on the iPhone?
i.e. When a user speaks into the Mic, the image will pulse or skew.
[edit] Anyone have any ideas, I have (what is basically) a voice recording app. I just wanted something to change as the voice input is provided. I've seen it in a sample project, but that wasn't with an UIImage. [/edit]
Thanking you!!
Apple put together some great frameworks for this! The AVFoundation framework and CoreAudio framework will be the most useful to you.
To get audio level information AVAudioRecorder is useful. Although it is made to be used for recording, it also provides levels data for the microphone. This would be useful for deforming your image base on how loud the user is shouting at his phone ;)
Here is the apple documentation for AVAudioRecorder: AVAudioRecorder Class Reference
A bit more detail:
// You will need an AVAudioRecorder object
AVAudioRecorder *myRecorderObject;
// To be able to get levels data from the microphone you need
// to enable metering for your recorder object
[myRecorderObject prepareToRecord];
myRecorderObject.meteringEnabled=YES;
// Now you can poll the microphone to get some levels data
float peakPower = [myRecorderObject peakPowerForChannel:0];
float averagePower = [myRecorderObject averagePowerForChannel:0];
If you want to see a great example of how an AVAudioRecorder object can be used to get levels data, check out this tutorial.
As far as deforming your image, that would be up to an image library. There are a lot to choose from and some great ones from apple. I am not familiar with anything though so that might be up for someone else to answer.
Best of luck!
You may try using gl-data-visualization-view extensible framework in order to visualize your sound levels.

What do you use to play sound in iPhone games?

I have a performance-intensive iPhone game I would like to add sounds to. There seem to be about three main choices: (1) AVAudioPlayer, (2) Audio Queues and (3) OpenAL. I’d hate to write pages of low-level code just to play a sample, so that I would like to use AVAudioPlayer. The problem is that it seems to kill the performace – I’ve done a simple measuring using CFAbsoluteTimeGetCurrent and the play message seems to take somewhere from 9 to 30 ms to finish. That’s quite miserable, considering that 25 ms == 40 fps.
Of course there is the prepareToPlay method that should speed things up. That’s why I wrote a simple class that keeps several AVAudioPlayers at its disposal, prepares them beforehand and then plays the sample using the prepared player. No cigar, still it takes the ~20 ms I mentioned above.
Such performance is unusable for games, so what do you use to play sounds with a decent performance on iPhone? Am I doing something wrong with the AVAudioPlayer? Do you play sounds with Audio Queues? (I’ve written something akin to AVAudioPlayer before 2.2 came out and I would love to spare that experience.) Do you use OpenAL? If yes, is there a simple way to play sounds with OpenAL, or do you have to write pages of code?
Update: Yes, playing sounds with OpenAL is fairly simple.
AVAudioPlayer is very marginal for game audio. Tackling AudioQueue or OpenAL by adapting one of the examples is definitely the way to go. latency is much more controllable that way.
If you're calling play on the main thread, try running it on a separate thread. What I ended up doing is:
#include <dispatch/dispatch.h>
dispatch_queue_t playQueue = dispatch_queue_create("com.example.playqueue", NULL);
AVAudioPlayer* player = ...
dispatch_async(playQueue, ^{
[player play];
});
which fixed the worst of the framerate stuttering I was experiencing.
I use OpenAL and the classes that came with the CrashLanding sample code. It's worked fine so far to play samples and play looped music all at the same time. I'm currently learning how to release the memory I've allocated for a sound (.wav file) when, for example, I want to play some intro music just once.
Use CocosDenshion – it’s free, easy, and works. It wraps AVAudioPlayer for background tracks and OpenAL for sounds.
Do you want to check the buffering with the implementation you're using? It might be somehow related to the 20ms delay you're experiencing. i.e., try to play around with the buffer size.

Pre-built audio player UI on iphone?

My boss seems to think there's a way to play audio files via a built-in QT interface that comes up with a modal UI, much like address book framework does.
Also, the mail app pops up a QT modal player to play attached audio files and then hides itself when done.
I've done a good 2 hours of research and I cannot find any pre-built UI for playing audio. I understand that I can do it via AVAudioPlayer, but that still requires I build a custom UI, which is fine, but we're looking for visual familiarity and consistency.
I just now found MPMusicPlayerController, but it appears to only play itunes media, not media from file paths.
So, is there a pre-built audio player UI?
MPMoviePlayerController can also be used to play audio files (although it's not recommended):
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"http://www.freeaudioclips.com/music1/Sound_Effects/austinmail.wav"]];
// Show Audio player just to test it out
if (mPlayer)
{
[mPlayer play];
}
Read Apple's Documentation and for more details.