2016-03-01 66 views
2

我非常沮喪,在應用過濾器後保存音頻文件。該過濾器只適用於AVAudioPlayerNode,這意味着在播放器播放過濾器是完美的應用,但我怎麼能保存這個?我最近3天很掙扎,請任何幫助表示感謝。 這些代碼是我使用的,應用濾波器(AVAudioUnitEQ)後保存音頻文件,最後將其保存爲mp3?

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。

+5

我從這個鏈接的回答,我分享我試過這個答案http://stackoverflow.com/questions/35660501/save-the-audio-file-in-the-background/35739362#35739362 –

回答

0

嘗試此操作,當您播放該文件時,您必須再次播放整個文件才能保存該效果。

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 

    } 

} 
]; 
+0

之前,但沒有任何執行installTapOnBus後沒有響應。讓我再嘗試一次。 –

+0

而不是'mixerNode'嘗試'unitEq' –

+0

K朋友讓我檢查.. –

相關問題