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

白昼怎懂夜的黑 提交于 2019-11-29 00:43:25

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.

visakh7

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!