save the audio file in the background

后端 未结 1 615
谎友^
谎友^ 2021-01-13 03:23

I have an app that changes the pitch of the audio when the pitch button is selected, now i am using installTapOnBus to save the file but this method gets invoca

1条回答
  •  野的像风
    2021-01-13 03:55

    Actually we made mistake in the settings of Output Audio File. The output audio file processing format should be same like as the input file(which u put effect or pitch).

    And the Output file format should be in the wav or caf format. This format only save to the output audio file.

     - (IBAction)save_it_after_changes:(id)sender
        {
    
                 engine = [[AVAudioEngine alloc] init];
                 audio_player_node= [[AVAudioPlayerNode alloc] init];
                [engine attachNode:audio_player_node];
                [self setupEQ];
    
                AVAudioMixerNode *mixerNode = [engine mainMixerNode];
                [engine connect:audio_player_node to:unitEq format:audioFile.processingFormat];
                [engine connect:unitEq to:mixerNode format:audioFile.processingFormat];
    
                NSError *error12;
                [engine startAndReturnError:&error12];
                if (!error12)
                {
                      NSLog(@"Engine = %@",engine);
                     [audio_player_node scheduleFile:audioFile atTime:nil completionHandler:nil];
                     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];
    
    
                      NSError *error;
                     outputFile = [[AVAudioFile alloc] initForWriting:[self testFilePathURL] settings:recordSetting error:&error];
                      NSLog(@"outputfile = %@",outputFile);
                      if (error)
                      {
                             NSLog(@"outputFile error = %@",error);
                      }
                      else
                      { //(AVAudioFrameCount)audioFile.length
                            [audio_player_node 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 0) ? [paths objectAtIndex:0] : nil;
          return basePath;
    }
    
    //------------------------------------------------------------------------------
    
    - (NSURL *)testFilePathURL
    {
          return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/test.caf",
                                         [self applicationDocumentsDirectory]]];
    }
    

    Please play the file after successfully saved. It works for me. check it out.

    Please refer below link, I get more from here, Can I use AVAudioEngine to read from a file, process with an audio unit and write to a file, faster than real-time?

    refer this sample project. It is what we are looking for https://github.com/VladimirKravchenko/AVAudioEngineOfflineRender

    0 讨论(0)
提交回复
热议问题