Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get an audio session / apply audio units to playback from MPMusicPlayerController?

I'd like to take control of the audio coming from MPMusicPlayerController (i.e., playing from the iPod library). For example, I'd like to apply EQ to it or do DSP, reverb, that kind of thing.

Is this possible? Is there an audio session that I can grab a handle on? Or, perhaps is there some way to play back files from the iPod library using an AVAudioPlayer?

like image 222
Simon Woodside Avatar asked May 03 '10 18:05

Simon Woodside


1 Answers

MPMusicPLayerController does not work "nicely" with the AV Framework I managed to get some DSP Using the MPMusicPlayerController to get the media item then get url for that item. then use the AVURLAsset and AVAssetReader. something like this:

MPMediaItem *currentSong = [myMusicController nowPlayingItem];
NSURL *currentSongURL = [currentSong valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:currentSongURL options:nil];
NSError *error = nil;        
AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];

AVAssetTrack* track = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];
[audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
                     forKey:AVFormatIDKey];

AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:audioReadSettings];
[reader addOutput:readerOutput];
[reader startReading];
CMSampleBufferRef sample = [readerOutput copyNextSampleBuffer];
while( sample != NULL )
{
    sample = [readerOutput copyNextSampleBuffer];

    if( sample == NULL )
        continue;

    CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer( sample );
    CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples(sample);

    AudioBufferList audioBufferList;

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sample,
                                                            NULL,
                                                            &audioBufferList,
                                                            sizeof(audioBufferList),
                                                            NULL,
                                                            NULL,
                                                            kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
                                                            &buffer
                                                            );

    for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
        SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
        for (int i=0; i < numSamplesInBuffer; i++) {
            NSLog(@"%i", samples[i]);
        }
    }

    //Release the buffer when done with the samples 
    //(retained by CMSampleBufferGetAudioBufferListWithRetainedblockBuffer)
    CFRelease(buffer);             

    CFRelease( sample );
like image 169
ugiflezet Avatar answered Nov 16 '22 18:11

ugiflezet