简体   繁体   English

如何在iPhone SDK中设置音频文件或录制的音频文件的音高?

[英]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. 如何在使用Objective-C的iPhone程序中设置音高。

Please help me out of this. 请帮助我。

Thank you, Madan Mohan. 谢谢,马丹·莫汉(Madan Mohan)。

you can play sound with changed pitch using AVAudioEngine, AVAudioPlayerNode.The sample tested code is given bellow . 您可以使用AVAudioEngine,AVAudioPlayerNode以变化的音高播放声音。经过测试的示例代码如下。

@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. 注意::此代码已在实际设备上经过测试,并且在我的项目中可以正常工作。不要忘记添加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. 例如,如果文件以Fs = 44100Hz录制,则以Fs = 22050Hz播放时,将给您原始音高的一半。

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. 如果您需要一些天真的东西,则必须自己实现音高转换算法,这是一个很大的话题---我建议您首先在Google中搜索pitch shift

You can use the soundtouch open source project to change pitch 您可以使用soundtouch开源项目来更改音高

Here is the link : http://www.surina.net/soundtouch/ 这是链接: 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. 将soundtouch添加到项目后,必须提供输入声音文件路径,输出声音文件路径和音高变化作为输入。

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 iPhone上的实时音高转换

Create pitch changing code? 创建音高变化代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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