Я пытаюсь изменить высоту звука, но не могу сохранить полученное аудио. Как сохранить эффект аудио в фоновом режиме?
Этот метод фильтрует аудио и изменяет высоту тона, но не сохраняется где-либо
-(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];
}
1 ответ
Вы должны заменить
[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];
}
}];
}
С
Здесь ввод означает audioEngine.inputnode
AVAudioInputNode *input = [audioEngine inputNode];
AVAudioFormat *format = [input outputFormatForBus: 0];
outputFile = [[AVAudioFile alloc] initForWriting:[self testFilePathURL] settings:recordSetting error:&error];
if (error)
{
// NSLog(@"outputFile error = %@",error);
}
else
{
// dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
// {
[input installTapOnBus:0 bufferSize:8091 format:format block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
if (outputFile.length<audioFile.length)
{
NSError *error;
[outputFile writeFromBuffer:buffer error:&error];
if (error)
{
NSLog(@"writebuffererror =%@",error);
}
}
else
{
activityMainView.hidden=YES;
[input removeTapOnBus:0];
// [self savingAudioFile];
//_doneButtonImageView.image=[UIImage imageNamed:@"save"];
[self doneButtonClick:nil];
NSError *error2;
NSLog(@"Pathththt = %@",[self testFilePathURL]);
NSLog(@"error = %@",error2);
}
}];
// });
}