gpt4 book ai didi

objective-c - 我正在尝试更改音频的音调,但无法保存受影响的音频。如何在背景中保存效果音频?

转载 作者:行者123 更新时间:2023-12-02 23:40:38 25 4
gpt4 key购买 nike

此方法过滤音频并更改音调,但不保存在任何地方

-(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];

}

最佳答案

你应该更换

[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);



}



}];



// });


}

关于objective-c - 我正在尝试更改音频的音调,但无法保存受影响的音频。如何在背景中保存效果音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com