此方法过滤音频并更改音高但未保存在任何位置
-(void)Pitch:(NSNumber*)pitch
{
audioPlayerNode = [AVAudioPlayerNode new];
[self.audioEngine attachNode:audioPlayerNode];
AVAudioUnitTimePitch *timePitchEffect = [AVAudioUnitTimePitch new];
[self.audioEngine attachNode:timePitchEffect];
timePitchEffect.pitch = [pitch floatValue];
[self.audioEngine connect:audioPlayerNode
to:timePitchEffect
format:nil];
[self.audioEngine connect:timePitchEffect
to:self.audioEngine.outputNode
format:nil];
[audioPlayerNode scheduleFile:self.audioFile
atTime:nil
completionHandler:nil];
[self.audioEngine startAndReturnError:&audioEngineError];
}
在这里我能够创建音频文件,但音频中没有声音可以任何人帮助我摆脱这个名为 outputfile 的新文件,但是当我播放该音频时,没有声音只是静音,创建了完整的音频但是无声
-(void)saveEffectedAudioToFolder
{
AVAudioUnitTimePitch *pitchEffect = [AVAudioUnitTimePitch new];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
[recordSetting setValue:[NSNumber numberWithFloat:pitch] forKey:AVAudioTimePitchAlgorithmSpectral];
AVAudioFormat *commonFormat = [[AVAudioFormat alloc] initWithSettings:recordSetting];
pitchEffect.pitch =pitch;
[self setupEQ];
NSError *error12;
[_audioEngine startAndReturnError:&error12];
if (!error12)
{
NSLog(@"Engine = %@",_audioEngine);
[audioPlayerNode scheduleFile:_audioFile atTime:nil completionHandler:nil];
NSError *error;
_outputFile = [[AVAudioFile alloc] initForWriting:[self testFilePathURL] settings:commonFormat.settings error:&error];
NSLog(@"outputfile = %@",_outputFile);
if (error)
{
NSLog(@"outputFile error = %@",error);
}
else
{
[SVProgressHUD dismiss];
[audioPlayerNode installTapOnBus:0 bufferSize:8192 format:_audioFile.processingFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
NSLog(@"Buffer Size = %@",buffer);
NSLog(@"when = %lld",when.sampleTime);
NSLog(@"outputfile length = %lli",_outputFile.length);
NSLog(@"input file length = %lld",_audioFile.length);
if (_outputFile.length<=_audioFile.length)
{
NSError *error;
[_outputFile writeFromBuffer:buffer error:&error];
if (error)
{
NSLog(@"writebuffererror =%@",error);
}
}
else
{
NSError *error2;
[audioPlayerNode removeTapOnBus:0];
// player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[self testFilePathURL] error:&error2];
//player2.delegate = self;
NSLog(@"Pathththt = %@",[self testFilePathURL]);
NSLog(@"error = %@",error2);
[audioPlayerNode scheduleFile:_outputFile atTime:nil completionHandler:nil];
[audioPlayerNode play];
// [self toMp3];
}
}];
}
}
else
{
NSLog(@"error12 =%@",error12);
}
}
- (void)setupEQ
{
NSLog(@"setupEQ");
_unitEq = [[AVAudioUnitEQ alloc] initWithNumberOfBands:12];
_unitEq.globalGain = 3.0;
AVAudioUnitEQFilterParameters *filterParameters;
filterParameters = _unitEq.bands[0];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[1];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[2];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[3];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[4];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[5];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[6];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[7];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain =20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[8];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain = 20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[9];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = pitch;
filterParameters.bandwidth = 1.0;
filterParameters.gain =20.0f;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[10];
filterParameters.filterType = AVAudioUnitEQFilterTypeLowPass;
filterParameters.frequency = pitch;
filterParameters.bypass = FALSE;
filterParameters = _unitEq.bands[11];
filterParameters.filterType = AVAudioUnitEQFilterTypeHighPass;
filterParameters.frequency = pitch;
filterParameters.bypass = FALSE;
[_audioEngine attachNode:_unitEq];
}