简体   繁体   中英

How to set pitch of an audio file or recorded audio file in iphone sdk?

I am recoding a file or I have audio file I want to change the pitch and play the audio file. How can I set the pitch in a iphone program that is using objective-c.

Please help me out of this.

Thank you, Madan Mohan.

you can play sound with changed pitch using AVAudioEngine, AVAudioPlayerNode.The sample tested code is given bellow .

@interface ViewController (){

    AVAudioEngine *engine;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self playAudio];
}

-(void)playAudio{

    engine = [[AVAudioEngine alloc] init];
    NSString* path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
    NSURL *soundUrl = [NSURL fileURLWithPath:path];
    AVAudioFile* File = [[AVAudioFile alloc] initForReading: soundUrl error: nil];
    AVAudioPlayerNode* node = [AVAudioPlayerNode new];

    [node stop];
    [engine stop];
    [engine reset];
    [engine attachNode: node];
    AVAudioUnitTimePitch* changeAudioUnitTime = [AVAudioUnitTimePitch new];


    //change this rate variable to play fast or slow .         
    changeAudioUnitTime.rate = 1;

    //change pitch variable to according your requirement.
    changeAudioUnitTime.pitch = 100;
    [engine attachNode: changeAudioUnitTime];
    [engine connect:node to: changeAudioUnitTime format: nil];
    [engine connect:changeAudioUnitTime to: engine.outputNode format:nil];
    [node scheduleFile:File atTime: nil completionHandler: nil];
    [engine startAndReturnError: nil];
    [node play];


}

Make sure path variable have valid file path

Note :: this code is tested on actual device and working fine in my project.don't forget to add CoreAudio.framework , AVFoundation.framework.

The naive approach is to play it using a different sampling rate as compared to the sampling rate used to record the file. For example, if the file was recorded with Fs=44100Hz, then playing it with Fs=22050Hz, will give you half the original pitch.

Of course this naive approach involves changing the duration of the file, and other sound-related artifacts. If you need something less naive, you will have to implement a pitch-shifting algorithm yourself, and that's a huge topic --- I suggest you start by searching pitch shift in google.

You can use the soundtouch open source project to change pitch

Here is the link : http://www.surina.net/soundtouch/

Once you add soundtouch to your project, you have to give the input sound file path, output sound file path and pitch change as the input.

Since it takes more time to process your sound its better to modify soundtouch so that when you record the voice, directly give the data for processing. It will make your application better.

References

Real-time Pitch Shifting on the iPhone

Create pitch changing code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM