iphone - Extract iPod Library raw PCM samples and play with sound effects -
i trying extract raw pcm samples mp3 in ipod library can play song , manipulate pitch, tempo, , apply sound effects (such filters). have gone down route of avplayer , avaudioplayer both not allow control on playback @ all.
the code below far have gotten this. @ point not know cmsamplebufferref's in while loop because not know framework use in order playback audio , apply such effects.
any idea best approach achieve this? have looked @ cases file converted using avassetwriter not going cut me because process time consuming. surely can read pcm samples memory playback without having write them disk first?
nb: know code below references mp3 within project aware approach work same if pulling nsurl mpmediapropertyasseturl
-(ibaction)loadtrack:(id)sender { nsstring *songpath = [[nsbundle mainbundle] pathforresource:@"smooth_sub focus_192" oftype:@"mp3"]; nsurl *asseturl = [[nsurl alloc] initfileurlwithpath:songpath]; avurlasset *songasset = [avurlasset urlassetwithurl:asseturl options:nil]; nserror *asseterror = nil; avassetreader *assetreader = [[avassetreader assetreaderwithasset:songasset error:&asseterror] retain]; if (asseterror) { nslog (@"error: %@", asseterror); return; } avassetreaderoutput *assetreaderoutput = [[avassetreaderaudiomixoutput assetreaderaudiomixoutputwithaudiotracks:songasset.tracks audiosettings: nil] retain]; if (![assetreader canaddoutput:assetreaderoutput]) { nslog (@"incompatible asser reader output"); return; } [assetreader addoutput: assetreaderoutput]; [assetreader startreading]; cmsamplebufferref nextbuffer; while (nextbuffer = [assetreaderoutput copynextsamplebuffer]) { /* do here? */ } [assetreader release]; [assetreaderoutput release]; }
i'm doing similar in own code. following method returns nsdata avurlasset:
- (nsdata *)extractdataforasset:(avurlasset *)songasset { nserror * error = nil; avassetreader * reader = [[avassetreader alloc] initwithasset:songasset error:&error]; avassettrack * songtrack = [songasset.tracks objectatindex:0]; avassetreadertrackoutput * output = [[avassetreadertrackoutput alloc] initwithtrack:songtrack outputsettings:nil]; [reader addoutput:output]; [output release]; nsmutabledata * fullsongdata = [[nsmutabledata alloc] init]; [reader startreading]; while (reader.status == avassetreaderstatusreading){ avassetreadertrackoutput * trackoutput = (avassetreadertrackoutput *)[reader.outputs objectatindex:0]; cmsamplebufferref samplebufferref = [trackoutput copynextsamplebuffer]; if (samplebufferref){ cmblockbufferref blockbufferref = cmsamplebuffergetdatabuffer(samplebufferref); size_t length = cmblockbuffergetdatalength(blockbufferref); uint8 buffer[length]; cmblockbuffercopydatabytes(blockbufferref, 0, length, buffer); nsdata * data = [[nsdata alloc] initwithbytes:buffer length:length]; [fullsongdata appenddata:data]; [data release]; cmsamplebufferinvalidate(samplebufferref); cfrelease(samplebufferref); } } if (reader.status == avassetreaderstatusfailed || reader.status == avassetreaderstatusunknown){ // went wrong. handle it. } if (reader.status == avassetreaderstatuscompleted){ // you're done. worked. } [reader release]; return [fullsongdata autorelease]; }
i recommend doing on background thread because it's time consuming.
a drawback method whole song loaded memory, of course limited.
Comments
Post a Comment