I have few AVAudioPlayerNode
s for each sound user can play. Each player node is connected to its own AVAudioMixerNode
to change volume. All of these mixers are connected to one AVAudioMixerNode
(I named it soundsMixerNode
) which is connected to the engine's mainMixerNode
. User can record these sounds and then play the result record via AVAudioPlayer
. The problem is that this record has a shifted tonality. Like if I used a pitch effect. And I get this result only on devices, simulators works good. Maybe the problem is in AVAudioSession
categories or modes? Or maybe I have problems with AVAudioFormat
or setting? I don't understand.
Here's my audio graph:
PlayerNode1 – MixerNode1
\
... soundsMixerNode – self.engine.mainMixerNode
/
PlayerNodeN – MixerNodeN
Here's my session setup:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setPreferredIOBufferDuration:128.0/audioSession.sampleRate error:NULL];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:&error];
[audioSession setActive:YES error:&error];
Here's my recording AVAudioFile
setup:
NSDictionary* recordSettings = [self.engine.outputNode inputFormatForBus:0].settings;
NSError* error = nil;
self.fileForRecording = [[AVAudioFile alloc] initForWriting:url
settings:recordSettings
error:&error];
And recording code:
AVAudioFormat* recordingFormat = [self.soundsMixerNode outputFormatForBus:0];
if (recordingFormat.sampleRate > 0) {
typeof(self) weakSelf = self;
[self.soundsMixerNode installTapOnBus:0
bufferSize:1024
format:recordingFormat
block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
NSError* error;
[weakSelf.fileForRecording writeFromBuffer:buffer error:&error];
}];
}
The error was in the record format. In AVAudioFile
setup I used [self.engine.outputNode inputFormatForBus:0].settings
, so I took an input format from my engine outputNode. But when installing tap on bus I used [self.soundsMixerNode outputFormatForBus:0]
. These two formats are not equal. They have different sample rates. So I got a pitch in a result file.
I need to use one format both in record file setup and in installTapOnBus:bufferSize:format:block:
. In my case I need to use outputFormat of my self.soundsMixerNode
.