[英]How to set pitch of an audio file or recorded audio file in iphone sdk?
我正在編碼文件,或者我有音頻文件,我想更改音高並播放音頻文件。 如何在使用Objective-C的iPhone程序中設置音高。
請幫助我。
謝謝,馬丹·莫漢(Madan Mohan)。
您可以使用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];
}
確保路徑變量具有有效的文件路徑
注意::此代碼已在實際設備上經過測試,並且在我的項目中可以正常工作。不要忘記添加CoreAudio.framework,AVFoundation.framework。
天真的方法是使用與記錄文件的采樣率不同的采樣率播放它。 例如,如果文件以Fs = 44100Hz錄制,則以Fs = 22050Hz播放時,將給您原始音高的一半。
當然,這種幼稚的方法涉及更改文件的持續時間以及其他與聲音有關的工件。 如果您需要一些天真的東西,則必須自己實現音高轉換算法,這是一個很大的話題---我建議您首先在Google中搜索pitch shift
。
您可以使用soundtouch開源項目來更改音高
這是鏈接: http : //www.surina.net/soundtouch/
將soundtouch添加到項目后,必須提供輸入聲音文件路徑,輸出聲音文件路徑和音高變化作為輸入。
由於處理聲音會花費更多時間,因此最好修改聲音觸摸,以便在錄制聲音時直接提供數據進行處理。 它將使您的應用程序更好。
參考文獻
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.