Skip to content

Instantly share code, notes, and snippets.

@VincentDondain
Created January 25, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentDondain/a6a4c8914a51c9726c40 to your computer and use it in GitHub Desktop.
Save VincentDondain/a6a4c8914a51c9726c40 to your computer and use it in GitHub Desktop.
- (void)startRecordingMixerOutput
{
// install a tap on the main mixer output bus and write output buffers to file
/* The method installTapOnBus:bufferSize:format:block: will create a "tap" to record/monitor/observe the output of the node.
@param bus
the node output bus to which to attach the tap
@param bufferSize
the requested size of the incoming buffers. The implementation may choose another size.
@param format
If non-nil, attempts to apply this as the format of the specified output bus. This should
only be done when attaching to an output bus which is not connected to another node; an
error will result otherwise.
The tap and connection formats (if non-nil) on the specified bus should be identical.
Otherwise, the latter operation will override any previously set format.
Note that for AVAudioOutputNode, tap format must be specified as nil.
@param tapBlock
a block to be called with audio buffers
Only one tap may be installed on any bus. Taps may be safely installed and removed while
the engine is running. */
NSError *error;
if (!_mixerOutputFileURL) _mixerOutputFileURL = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"mixerOutput.caf"]];
AVAudioMixerNode *mainMixer = [_engine mainMixerNode];
AVAudioFile *mixerOutputFile = [[AVAudioFile alloc] initForWriting:_mixerOutputFileURL settings:[[mainMixer outputFormatForBus:0] settings] error:&error];
NSAssert(mixerOutputFile != nil, @"mixerOutputFile is nil, %@", [error localizedDescription]);
[self startEngine];
[mainMixer installTapOnBus:0 bufferSize:4096 format:[mainMixer outputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
NSError *error;
BOOL success = NO;
// as AVAudioPCMBuffer's are delivered this will write sequentially. The buffer's frameLength signifies how much of the buffer is to be written
// IMPORTANT: The buffer format MUST match the file's processing format which is why outputFormatForBus: was used when creating the AVAudioFile object above
success = [mixerOutputFile writeFromBuffer:buffer error:&error];
NSAssert(success, @"error writing buffer data to file, %@", [error localizedDescription]);
}];
_isRecording = YES;
}
- (void)stopRecordingMixerOutput
{
if (_isRecording) {
[[_engine mainMixerNode] removeTapOnBus:0];
_isRecording = NO;
if (self.recordingIsAvailable) {
//Post a notificaiton that the record is complete
//Other nodes/objects can listen to this update accordingly
[[NSNotificationCenter defaultCenter] postNotificationName:kRecordingCompletedNotification object:nil];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment