应用过滤器(AVAudioUnitEQ)后保存音频文件,最后保存为mp3? [复制]
Posted
技术标签:
【中文标题】应用过滤器(AVAudioUnitEQ)后保存音频文件,最后保存为mp3? [复制]【英文标题】:Save Audio File after apply Filter(AVAudioUnitEQ) and finally save it as mp3? [duplicate] 【发布时间】:2016-03-01 05:26:59 【问题描述】:应用过滤器后保存音频文件让我非常沮丧。该过滤器仅适用于 AVAudioPlayerNode,这意味着在播放器中播放期间过滤器非常适合应用,但我该如何保存呢?过去 3 天我很挣扎,请任何帮助将不胜感激。 我使用了以下这些代码,
engine = [[AVAudioEngine alloc] init];
- (void)setupEQ
NSLog(@"setupEQ");
unitEq = [[AVAudioUnitEQ alloc] initWithNumberOfBands:12];
unitEq.globalGain = 3.0;
AVAudioUnitEQFilterParameters *filterParameters;
filterParameters = unitEq.bands[0];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 31;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_31.5hz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[1];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 63;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_63hz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[2];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 125;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_125hz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[3];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 250;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_250hz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[4];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 500;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_500hz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[5];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 1000;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_1khz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[6];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 2000;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_2khz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[7];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 4000;
filterParameters.bandwidth = 1.0;
filterParameters.gain =[[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_4khz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[8];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 8000;
filterParameters.bandwidth = 1.0;
filterParameters.gain = [[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_8khz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[9];
filterParameters.filterType = AVAudioUnitEQFilterTypeParametric;
filterParameters.frequency = 16000;
filterParameters.bandwidth = 1.0;
filterParameters.gain =[[[filter_arr objectAtIndex:index_filter] objectForKey:@"filter_freq_16khz"] floatValue];
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[10];
filterParameters.filterType = AVAudioUnitEQFilterTypeLowPass;
filterParameters.frequency = 16857;
filterParameters.bypass = FALSE;
filterParameters = unitEq.bands[11];
filterParameters.filterType = AVAudioUnitEQFilterTypeHighPass;
filterParameters.frequency = 205;
filterParameters.bypass = FALSE;
[engine attachNode:unitEq];
audioFile = [[AVAudioFile alloc] initForReading:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",[self audio_file_path:mp3FileName]]] error:&playerError];
[self setupPlayer];
[self setupEQ];
AVAudioMixerNode *mixerNode = [engine mainMixerNode];
[engine connect:audio_player_node to:unitEq format:audio_file.processingFormat];
[engine connect:unitEq to:mixerNode format:audio_file.processingFormat];
这些是可以正常工作的示例。但是我如何存储具有相同频率的音频文件?音频文件应为 mp3 格式。在苹果中,他们只将保存文件定位为 m4a。
【问题讨论】:
我从这个链接得到了答案,我分享了答案***.com/questions/35660501/… 【参考方案1】:试试这个,当你播放文件时,你必须再次播放整个文件才能保存效果。
AVAudioFormat * commonFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:44100 channels:2 interleaved:NO];
AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:[NSURL URLWithString:[self filePath1]] settings:commonFormat.settings error:&error];
[unitEq installTapOnBus:0 bufferSize:4096 format:commonFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when)
if (outputFile.length < file.length)
//Let us know when to stop saving the file, otherwise saving infinitely
NSError *error;
NSAssert([outputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]);
else
audioEngine = nil;
[unitEq removeTapOnBus:0];//if we dont remove it, will keep on tapping infinitely
];
【讨论】:
我之前尝试过这个,但是在 installTapOnBus 之后没有执行任何操作,没有响应。让我再试一次。 代替mixerNode
试试unitEq
K朋友让我看看..
不。它不起作用。我会更新问题请检查并让我知道问题..以上是关于应用过滤器(AVAudioUnitEQ)后保存音频文件,最后保存为mp3? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
xcode:如何在使用 AVFoundation 录制音频后保存音频文件
录制音频并保存到文档目录后无法使用 AVAudioPlayer 播放音频文件