0

I'm developing an ios audio app on xcode and I'm trying to use 2 audio files I have recorded - which are playing at the same time and export it to one audio file. All I have managed to do is merge 2 audio files to one, but the 2 audios are playing one after another and not in sync at the same time. Does anyone have a clue how I can sort it out? Thanks

Papito
  • 5
  • 2
  • 4

3 Answers3

1

Try Apple's MixerHost sample app.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • Thanks for your feedback.I did manage to play two audio files at the same time. I can't manage to export it to one file. Can't find anything about export on "Mixerhost" – Papito Jun 24 '12 at 14:25
  • See if this helps: http://stackoverflow.com/questions/7118429/how-to-record-sound-produced-by-mixer-unit-output-ios-core-audio-audio-graph – user523234 Jun 24 '12 at 16:16
1

You should take a look at this for AAC conversion (http://atastypixel.com/blog/easy-aac-compressed-audio-conversion-on-ios/). It's super useful.

Another thing you might want to consider... combining two audio signals is as easy as adding the samples together. So what you could do is:

Open both recordings and get an array for each of the recordings that holds the audio samples.

Make a for() loop that adds each sample and puts it in an output array

for(int i = 0; i<numberOfSamples; i++) {
   exportBuffer[i] = firstTrack[i] + secondTrack[i];
}

and then write the exportBuffer to an m4a file.

This code will only work if the two files are the same exact length, so adjust it to your needs. You'll need to add a conditional that fires if you've reached the end of one of the arrays. In that case, just add 0's.

Kpmurphy91
  • 554
  • 5
  • 15
  • No problem! One thing I should add... If both Audio Files are loud enough, you might get distortion on the resulting file, so you may want to do something like: exportBuffer[i] = firstTrack[i]*0.5 + secondTrack[i]*0.5 I think this is only a valid concern if you are using integers to represent the audio samples, but something to be thinking about. – Kpmurphy91 Jun 26 '12 at 02:59
  • Thanks mate we actually ended up using an API of Michael Tylor (that guy you offered us) he's great and we ended up successfully doing online export of 2 audio. Now we are trying to figure out how the hell we can make it offline. – Papito Jul 01 '12 at 19:27
  • Ok my output saved in the exportbuffer successfully but how I manage to convert this byte [] to audio stream? – Ahmad Arslan Mar 24 '16 at 04:42
-1

/* Implement this method if you have already saved your recorded audio file */

-(void)mixAudio{
AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack setPreferredVolume:0.8];
NSString *soundOne  =[[NSBundle mainBundle]pathForResource:@"RecordAudio1" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:soundOne];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack = [tracks objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack setPreferredVolume:0.8];
NSString *soundOne1  =[[NSBundle mainBundle]pathForResource:@"RecordAudio2" ofType:@"wav"];
NSURL *url1 = [NSURL fileURLWithPath:soundOne1];
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack1 = [tracks1 objectAtIndex:0];
[compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset1.duration) ofTrack:clipAudioTrack1 atTime: kCMTimeZero error:nil];

  AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:composition
                                           presetName:AVAssetExportPresetAppleM4A];
    if (nil == exportSession) return NO;

NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:@"combined10.m4a"];
//NSLog(@"Output file path - %@",soundOneNew);

// configure export session  output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type

// perform the export
[exportSession exportAsynchronouslyWithCompletionHandler:^{

    if (AVAssetExportSessionStatusCompleted == exportSession.status) {
        NSLog(@"AVAssetExportSessionStatusCompleted");
    } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
        // a failure may happen because of an event out of your control
        // for example, an interruption like a phone call comming in
        // make sure and handle this case appropriately
        NSLog(@"AVAssetExportSessionStatusFailed");
    } else {
        NSLog(@"Export Session Status: %d", exportSession.status);
    }
}];
}
Woodstock
  • 22,184
  • 15
  • 80
  • 118
Taranjit
  • 13
  • 3