3

I'm trying to apply filters to sound taken from apple music library by using AVAudioEngine:

First of all, I get AVAudioFile:

NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
NSError *error;
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:assetURL error:&error];

Then I try to convert it to AVAudioPCMBuffer:

AVAudioFormat *format = audioFile.processingFormat;
AVAudioFrameCount capacity = (AVAudioFrameCount)audioFile.length;
AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:capacity];
[audioFile readIntoBuffer:buffer error:nil];

And finally I try to connect everything using AVAudioEngine and schedule the buffer:

[engine attachNode:audioEQ];
[engine attachNode:avHighPassFilter];
[engine attachNode:audioPlayerNode];
[engine connect:audioPlayerNode to:mainMixer format:[mainMixer outputFormatForBus:0]];
[engine connect: audioPlayerNode to:audioEQ format:buffer.format];
[engine connect: audioEQ to:avHighPassFilter format:buffer.format];
[engine connect:avHighPassFilter to:mainMixer format:buffer.format];
[audioPlayerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];

It plays the song, but in terrible quality. When I try to export the MPMediaItem to my documents directory first, the quality of sound is better a little but still not good enough.

Can anyone help me to fix it? Is "com.apple.m4a-audio" format the problem?

Mateusz
  • 233
  • 3
  • 8

0 Answers0