Skip to content
Browse files

Added Audio Player and Level Meter

  • Loading branch information...
1 parent c8606e7 commit d82569b7521078093540dd4952a67ceb8e4449ae @rin-23 committed
View
8 AudioInfo.xcodeproj/project.pbxproj
@@ -13,6 +13,8 @@
150E57CE15703F4000ED9C34 /* NSDataAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 150E57CB15703F4000ED9C34 /* NSDataAdditions.m */; };
150E57CF15703F4000ED9C34 /* TBXML.m in Sources */ = {isa = PBXBuildFile; fileRef = 150E57CD15703F4000ED9C34 /* TBXML.m */; };
153ECF2D15717EFC00751F1D /* no_image.gif in Resources */ = {isa = PBXBuildFile; fileRef = 153ECF2C15717EFC00751F1D /* no_image.gif */; };
+ 153ECF621571FD8F00751F1D /* play1-150x150.png in Resources */ = {isa = PBXBuildFile; fileRef = 153ECF611571FD8E00751F1D /* play1-150x150.png */; };
+ 153ECF641571FD9900751F1D /* pause1-150x150.png in Resources */ = {isa = PBXBuildFile; fileRef = 153ECF631571FD9900751F1D /* pause1-150x150.png */; };
157264AE156D90EE0012A1D6 /* ASIAuthenticationDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = 1572649B156D90EE0012A1D6 /* ASIAuthenticationDialog.m */; };
157264AF156D90EE0012A1D6 /* ASIDataCompressor.m in Sources */ = {isa = PBXBuildFile; fileRef = 1572649E156D90EE0012A1D6 /* ASIDataCompressor.m */; };
157264B0156D90EE0012A1D6 /* ASIDataDecompressor.m in Sources */ = {isa = PBXBuildFile; fileRef = 157264A0156D90EE0012A1D6 /* ASIDataDecompressor.m */; };
@@ -60,6 +62,8 @@
150E57CC15703F4000ED9C34 /* TBXML.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TBXML.h; sourceTree = "<group>"; };
150E57CD15703F4000ED9C34 /* TBXML.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TBXML.m; sourceTree = "<group>"; };
153ECF2C15717EFC00751F1D /* no_image.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = no_image.gif; sourceTree = "<group>"; };
+ 153ECF611571FD8E00751F1D /* play1-150x150.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "play1-150x150.png"; sourceTree = "<group>"; };
+ 153ECF631571FD9900751F1D /* pause1-150x150.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "pause1-150x150.png"; sourceTree = "<group>"; };
1572649A156D90EE0012A1D6 /* ASIAuthenticationDialog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIAuthenticationDialog.h; sourceTree = "<group>"; };
1572649B156D90EE0012A1D6 /* ASIAuthenticationDialog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIAuthenticationDialog.m; sourceTree = "<group>"; };
1572649C156D90EE0012A1D6 /* ASICacheDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASICacheDelegate.h; sourceTree = "<group>"; };
@@ -164,6 +168,8 @@
153ECF2B15717EF000751F1D /* Images */ = {
isa = PBXGroup;
children = (
+ 153ECF631571FD9900751F1D /* pause1-150x150.png */,
+ 153ECF611571FD8E00751F1D /* play1-150x150.png */,
153ECF2C15717EFC00751F1D /* no_image.gif */,
);
name = Images;
@@ -381,6 +387,8 @@
157264D1156D95CE0012A1D6 /* ASIWebPageRequest in Resources */,
157264E0156D96C20012A1D6 /* README-GHUnit in Resources */,
153ECF2D15717EFC00751F1D /* no_image.gif in Resources */,
+ 153ECF621571FD8F00751F1D /* play1-150x150.png in Resources */,
+ 153ECF641571FD9900751F1D /* pause1-150x150.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
BIN ...xcodeproj/project.xcworkspace/xcuserdata/Rinat.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
View
11 AudioInfo/CoverArtView.m
@@ -185,8 +185,15 @@ - (void)requestFinished:(ASIHTTPRequest *)request {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageURL]];
[request setTag:REQUEST_COVER];
- [request setDelegate:self];
- [request startAsynchronous];
+ //[request setDelegate:self];
+ //[request startSynchronous];
+ //ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
+ [request startSynchronous];
+ NSError *error = [request error];
+ if (!error) {
+ //NSString *response = [request responseString];
+ imageView_.image = [[UIImage alloc] initWithData:[request responseData]];
+ }
} else if ([request tag] == REQUEST_COVER) {
View
6 AudioInfo/LyricsView.m
@@ -63,7 +63,7 @@ - (void) getLyricsForArtist:(NSString*)artist song:(NSString*)song {
artist = [artist stringByReplacingOccurrencesOfString:@" " withString:@"-"];
song = [song stringByReplacingOccurrencesOfString:@" " withString:@"-"];
NSString* urlstring = [NSString stringWithFormat:@"http://www.songlyrics.com/%@/%@-lyrics/", artist, song];
- NSLog(@"Lyrics URL:%@", urlstring);
+ //NSLog(@"Lyrics URL:%@", urlstring);
NSURL *url = [NSURL URLWithString:urlstring];
//Send http request to fethc lyrics
@@ -94,7 +94,7 @@ - (void)requestFinished:(ASIHTTPRequest *)request
scanner = [NSScanner scannerWithString:lyrics];
[scanner scanUpToString:@"&#" intoString:nil];
[scanner scanUpToString:@"</p>" intoString:&lyrics2];
- NSLog(@"%@", lyrics2);
+ //NSLog(@"%@", lyrics2);
lyrics2 = [lyrics2 stringByReplacingOccurrencesOfString:@"<br />" withString:@""];
lyrics2 = [lyrics2 stringByReplacingOccurrencesOfString:@"&#" withString:@""];
@@ -113,7 +113,7 @@ - (void)requestFinished:(ASIHTTPRequest *)request
[FINAL_LYRICS appendFormat:@"%@\n", actual_sentence];
[actual_sentence release];
}
- NSLog(@"%@", FINAL_LYRICS);
+ //NSLog(@"%@", FINAL_LYRICS);
lyricsView_.text = FINAL_LYRICS;
CGRect frame = lyricsView_.frame;
frame.size.height = lyricsView_.contentSize.height;
View
14 AudioInfo/MainViewController.h
@@ -13,7 +13,7 @@
#import "LyricsView.h"
#import "CoverArtView.h"
-@interface MainViewController : UIViewController <MPMediaPickerControllerDelegate, ASIHTTPRequestDelegate> {
+@interface MainViewController : UIViewController <AVAudioPlayerDelegate, MPMediaPickerControllerDelegate, ASIHTTPRequestDelegate> {
UIScrollView* mainScrollView_;
UIButton* songChooseButton_;
UILabel* title_;
@@ -27,10 +27,20 @@
UILabel* coverArtLabel_;
UIImageView* coverArtView_;
UILabel* lyricsLabel_;
+ UILabel* audioPLayerLabel_;
LyricsView* lyricsView_;
CoverArtView* amazonCovertArt_;
+ AVAudioPlayer* avplayer_;
+
+ UIProgressView* audioLevel_;
-
+ UIView* audioPlayerView_;
+ UIButton* playButton_;
+ UISlider* seekSlider_;
+ UISlider* speedSlider_;
+ UISlider* volumeSlider_;
+ UISlider* panSlider_;
+ UIActivityIndicatorView* convertingAudioSpinner_;
UIImageView* waveform_;
UIActivityIndicatorView* waveformSpinner_;
View
367 AudioInfo/MainViewController.m
@@ -10,6 +10,10 @@
#import "MainViewController.h"
+#define EXPORT_NAME @"exported.caf"
+#define PAUSE 0
+#define PLAY 1
+
@interface MainViewController()
- (NSData *) renderPNGAudioPictogramForAssett:(AVURLAsset *)songAsset;
@@ -17,6 +21,8 @@ - (UIImage *) audioImageGraph:(SInt16*)samples normalizeMax:(SInt16)normalizeMax
channelCount:(NSInteger)channelCount imageHeight:(float)imageHeight;
-(CGFloat) getBottomY: (UIView*) view;
-(void) createWaveForm:(AVURLAsset*) songAsset;
+-(void) showAudioPlayer;
+-(void) convertToCAF:(AVURLAsset*) songAsset;
@end
@@ -27,6 +33,8 @@ - (id)init {
if (self) {
// Custom initialization
self.view.backgroundColor = [UIColor whiteColor];
+ [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateLevel) userInfo:nil repeats:YES];
+
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(lyricsReceived)
name:@"LyricsReceived"
@@ -109,11 +117,100 @@ - (void)viewDidLoad {
[mainScrollView_ addSubview:sampleRate_];
[sampleRate_ release];
+
amazonCovertArt_ = [[CoverArtView alloc] initWithFrame:CGRectMake(320/2-250/2, [self getBottomY: sampleRate_] + 10, 250, 250)];
[mainScrollView_ addSubview:amazonCovertArt_];
[amazonCovertArt_ release];
-
- waveFormLabel = [[UILabel alloc] initWithFrame:CGRectMake(320/2 - 100/2, [self getBottomY:amazonCovertArt_] + 10, 100, 30)];
+
+ //Set up audioplayer view
+
+
+ audioPLayerLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(320/2 - 100/2, [self getBottomY:amazonCovertArt_] + 10, 100, 30)];
+ audioPLayerLabel_.backgroundColor = [UIColor clearColor];
+ audioPLayerLabel_.text = @"Audio Player";
+ audioPLayerLabel_.hidden = YES;
+ [mainScrollView_ addSubview:audioPLayerLabel_];
+ [audioPLayerLabel_ release];
+
+ audioPlayerView_ = [[UIView alloc] initWithFrame:CGRectMake(20, [self getBottomY:audioPLayerLabel_],280, 220)];
+ audioPlayerView_.backgroundColor = [UIColor clearColor];
+ convertingAudioSpinner_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
+ convertingAudioSpinner_.frame = CGRectMake(320/2 - 15, audioPlayerView_.frame.origin.y + audioPlayerView_.frame.size.height/2 - 15, 30, 30);
+ [mainScrollView_ addSubview:convertingAudioSpinner_];
+ [convertingAudioSpinner_ release];
+
+ playButton_= [UIButton buttonWithType:UIButtonTypeCustom];
+ playButton_.frame = CGRectMake(5, 25, 50, 50);
+ playButton_.selected = NO;
+ [playButton_ setImage:[UIImage imageNamed:@"play1-150x150.png"] forState:UIControlStateNormal];
+ [playButton_ setImage:[UIImage imageNamed:@"pause1-150x150"] forState:UIControlStateSelected];
+ [playButton_ setImage:[UIImage imageNamed:@"play1-150x150.png"] forState:UIControlStateHighlighted];
+ [playButton_ addTarget:self action:@selector(playButtonClicked) forControlEvents:UIControlEventTouchUpInside];
+ playButton_.tag = PAUSE;
+ [audioPlayerView_ addSubview:playButton_];
+
+ UILabel* audioLevelLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 25, 220, 25)];
+ audioLevelLabel.backgroundColor = [UIColor clearColor];
+ audioLevelLabel.text = @"Level Meter";
+ [audioPlayerView_ addSubview:audioLevelLabel];
+ [audioLevelLabel release];
+
+ audioLevel_ = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
+ audioLevel_.frame = CGRectMake(65, 25+30, 220, 30);
+ [audioPlayerView_ addSubview:audioLevel_];
+ [audioLevel_ release];
+
+ UILabel* volumeSliderText = [[UILabel alloc] initWithFrame:CGRectMake(0, [self getBottomY:playButton_]+10, 80, 20)];
+ volumeSliderText.backgroundColor = [UIColor clearColor];
+ volumeSliderText.userInteractionEnabled = NO;
+ volumeSliderText.text = @"Volume";
+ [audioPlayerView_ addSubview:volumeSliderText];
+ [volumeSliderText release];
+ volumeSlider_ = [[UISlider alloc] initWithFrame:CGRectMake(80, [self getBottomY:playButton_]+10, 280-80, 10)];
+ [volumeSlider_ setMaximumValue:1.0f];
+ [volumeSlider_ setMinimumValue:0.0f];
+ [volumeSlider_ addTarget:self action:@selector(volumeChanged) forControlEvents:UIControlEventTouchDragInside];
+ [audioPlayerView_ addSubview:volumeSlider_];
+ [volumeSlider_ release];
+
+ UILabel* speedSliderText = [[UILabel alloc] initWithFrame:CGRectMake(0, [self getBottomY:volumeSlider_]+10, 50, 20)];
+ speedSliderText.backgroundColor = [UIColor clearColor];
+ speedSliderText.userInteractionEnabled = NO;
+ speedSliderText.text = @"Rate";
+ [audioPlayerView_ addSubview:speedSliderText];
+ [speedSliderText release];
+ speedSlider_ = [[UISlider alloc] initWithFrame:CGRectMake(80, [self getBottomY:volumeSlider_]+10, 280-80-60, 10)];
+ [speedSlider_ setMaximumValue:2.0f];
+ [speedSlider_ setMinimumValue:0.5f];
+ [speedSlider_ addTarget:self action:@selector(speedChanged) forControlEvents:UIControlEventTouchDragInside];
+ [audioPlayerView_ addSubview:speedSlider_];
+ [speedSlider_ release];
+ UIButton* setToNormalRate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ setToNormalRate.frame = CGRectMake(80+speedSlider_.frame.size.width+5, [self getBottomY:volumeSlider_]+10, 60, 20);
+ [setToNormalRate setTitle:@"normal" forState:UIControlStateNormal];
+ [setToNormalRate addTarget:self action:@selector(setToNormalRateClicked) forControlEvents:UIControlEventTouchUpInside];
+ [audioPlayerView_ addSubview:setToNormalRate];
+
+
+ UILabel* panSliderText = [[UILabel alloc] initWithFrame:CGRectMake(0, [self getBottomY:speedSlider_]+10, 200, 20)];
+ panSliderText.backgroundColor = [UIColor clearColor];
+ panSliderText.userInteractionEnabled = NO;
+ panSliderText.text = @"Pan (need headphones)";
+ [audioPlayerView_ addSubview:panSliderText];
+ [panSliderText release];
+ panSlider_ = [[UISlider alloc] initWithFrame:CGRectMake(80, [self getBottomY:panSliderText]+10, 280-80, 10)];
+ [panSlider_ setMaximumValue:1.0f];
+ [panSlider_ setMinimumValue:-1.0f];
+ [panSlider_ addTarget:self action:@selector(panChanged) forControlEvents:UIControlEventTouchDragInside];
+ [audioPlayerView_ addSubview:panSlider_];
+ [panSlider_ release];
+
+
+ [mainScrollView_ addSubview:audioPlayerView_];
+ [audioPlayerView_ release];
+
+
+ waveFormLabel = [[UILabel alloc] initWithFrame:CGRectMake(320/2 - 100/2, [self getBottomY:audioPlayerView_] + 20, 100, 30)];
waveFormLabel.backgroundColor = [UIColor clearColor];
waveFormLabel.text = @"Wave form";
waveFormLabel.hidden = YES;
@@ -145,14 +242,66 @@ - (void)viewDidLoad {
[mainScrollView_ addSubview:lyricsView_];
[lyricsView_ release];
+
+
mainScrollView_.contentSize = CGSizeMake(320, [self getBottomY:lyricsView_] + 50);
}
+
+-(void)updateLevel{
+ if (!audioPlayerView_.hidden) {
+
+ [avplayer_ updateMeters];
+
+ float power = [avplayer_ averagePowerForChannel:0];
+ power = 100 + power;
+ audioLevel_.progress = power/100;
+ //power = -1*power;
+
+ // NSLog(@"%f", [avplayer_ averagePowerForChannel:0]);
+ // NSLog(@"%f", [avplayer_ peakPowerForChannel:0]);
+ }
+}
+
+-(void) setToNormalRateClicked{
+
+
+ avplayer_.rate = 1.0;
+ speedSlider_.value = 1.0;
+}
+
+-(void)volumeChanged{
+ avplayer_.volume = volumeSlider_.value;
+}
+
+-(void) panChanged{
+ avplayer_.pan = panSlider_.value;
+
+}
+-(void) speedChanged{
+ avplayer_.rate = speedSlider_.value;
+}
+
+
+
+-(void)playButtonClicked{
+ if (playButton_.tag == PAUSE) {
+ playButton_.selected = YES;
+ playButton_.tag = PLAY;
+ [avplayer_ play];
+ } else {
+ playButton_.selected = NO;
+ playButton_.tag = PAUSE;
+ [avplayer_ pause];
+ }
+}
+
//Called upon receiving LyricsReceived notification
-(void)lyricsReceived{
mainScrollView_.contentSize = CGSizeMake(320, [self getBottomY:lyricsView_] + 50);
lyricsLabel_.hidden = NO;
+
}
-(void)lyricsFailed {
mainScrollView_.contentSize = CGSizeMake(320, [self getBottomY:lyricsView_] + 50);
@@ -205,7 +354,10 @@ - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MP
albumTitle_.text = [NSString stringWithFormat:@"Album: %@", [song valueForProperty:MPMediaItemPropertyAlbumTitle]];
genre_.text = [NSString stringWithFormat:@"Genre: %@", [song valueForProperty:MPMediaItemPropertyGenre]];
duration_.text = [NSString stringWithFormat:@"Duration: %@ %@", [song valueForProperty:MPMediaItemPropertyPlaybackDuration], @"ms"];
+ numberOfChannels_.text = @"Number Of Channels: Loading...";
+ sampleRate_.text = @"Samplong Rate: Loading...";
waveFormLabel.hidden = NO;
+ audioPLayerLabel_.hidden = NO;
UIImage* nativeimage = [[song valueForProperty:MPMediaItemPropertyArtwork] imageWithSize: amazonCovertArt_.bounds.size];
if (nativeimage == nil) {
@@ -214,32 +366,36 @@ - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MP
} else {
[amazonCovertArt_ setCoverArtImage:nativeimage];
}
-
// Create wave form
waveform_.image = nil;
NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
- AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0];
- UInt32 sampleRate,channelCount;
-
- NSArray* formatDesc = songTrack.formatDescriptions;
- for(unsigned int i = 0; i < [formatDesc count]; ++i) {
- CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
- const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item);
- if(fmtDesc ) {
- sampleRate = fmtDesc->mSampleRate;
- channelCount = fmtDesc->mChannelsPerFrame;
- // NSLog(@"channels:%u, bytes/packet: %u, sampleRate %f",fmtDesc->mChannelsPerFrame, fmtDesc->mBytesPerPacket,fmtDesc->mSampleRate);
- }
- }
+// AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0];
+// UInt32 sampleRate,channelCount;
+
+// NSArray* formatDesc = songTrack.formatDescriptions;
+// for(unsigned int i = 0; i < [formatDesc count]; ++i) {
+// CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
+// const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item);
+// if(fmtDesc ) {
+// sampleRate = fmtDesc->mSampleRate;
+// channelCount = fmtDesc->mChannelsPerFrame;
+// // NSLog(@"channels:%u, bytes/packet: %u, sampleRate %f",fmtDesc->mChannelsPerFrame, fmtDesc->mBytesPerPacket,fmtDesc->mSampleRate);
+// }
+// }
- numberOfChannels_.text = [NSString stringWithFormat:@"Number of Channels: %i", channelCount];
- sampleRate_.text = [NSString stringWithFormat:@"Sampling Rate: %i Hz", sampleRate];
+ //numberOfChannels_.text = [NSString stringWithFormat:@"Number of Channels: %i", channelCount];
+ //sampleRate_.text = [NSString stringWithFormat:@"Sampling Rate: %i Hz", sampleRate];
[waveformSpinner_ startAnimating];
+ [convertingAudioSpinner_ startAnimating];
+ [avplayer_ stop];
+ audioPlayerView_.hidden = YES;
+ playButton_.selected = NO;
[self performSelectorInBackground:@selector(createWaveForm:) withObject:songAsset];
-
+ [self performSelectorInBackground:@selector(convertToCAF:) withObject:songAsset];
+ [self convertToCAF:songAsset];
//Get lyrics. Will update the lyricsView automatically.
@@ -247,7 +403,6 @@ - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MP
// [lyricsView_ getLyricsForArtist:@"jay-z kanye west" song:@"gotta have it"];
-
}
@@ -271,8 +426,163 @@ - (void)requestFailed:(ASIHTTPRequest *)request
#pragma mark - Draw Waveform Methods
+-(void) convertToCAF:(AVURLAsset*) songAsset {
+ 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 (@"can't add reader output... die!");
+ return;
+ }
+ [assetReader addOutput: assetReaderOutput];
+
+ NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
+ NSString *exportPath = [[documentsDirectoryPath stringByAppendingPathComponent:EXPORT_NAME] retain];
+ if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
+ [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
+ }
+ NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
+ AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL
+ fileType:AVFileTypeCoreAudioFormat
+ error:&assetError]
+ retain];
+ if (assetError) {
+ NSLog (@"error: %@", assetError);
+ return;
+ }
+ AudioChannelLayout channelLayout;
+ memset(&channelLayout, 0, sizeof(AudioChannelLayout));
+ channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
+ NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
+ [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
+ [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
+ [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
+ [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
+ [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
+ [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
+ [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
+ nil];
+ AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
+ outputSettings:outputSettings]
+ retain];
+ if ([assetWriter canAddInput:assetWriterInput]) {
+ [assetWriter addInput:assetWriterInput];
+ } else {
+ NSLog (@"can't add asset writer input... die!");
+ return;
+ }
+
+ assetWriterInput.expectsMediaDataInRealTime = NO;
+
+ [assetWriter startWriting];
+ [assetReader startReading];
+
+ AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
+ CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
+ [assetWriter startSessionAtSourceTime: startTime];
+
+ __block UInt64 convertedByteCount = 0;
+
+ dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
+ [assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
+ usingBlock: ^
+ {
+ // NSLog (@"top of block");
+ while (assetWriterInput.readyForMoreMediaData) {
+ CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
+ if (nextBuffer) {
+ // append buffer
+ [assetWriterInput appendSampleBuffer: nextBuffer];
+ // NSLog (@"appended a buffer (%d bytes)",
+ // CMSampleBufferGetTotalSampleSize (nextBuffer));
+ convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
+ // oops, no
+ // sizeLabel.text = [NSString stringWithFormat: @"%ld bytes converted", convertedByteCount];
+
+ NSNumber *convertedByteCountNumber = [NSNumber numberWithLong:convertedByteCount];
+ CFRelease(nextBuffer);
+ } else {
+ // done!
+ [assetWriterInput markAsFinished];
+ [assetWriter finishWriting];
+ [assetReader cancelReading];
+ NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
+ attributesOfItemAtPath:exportPath
+ error:nil];
+ NSLog (@"done. file size is %ld", [outputFileAttributes fileSize]);
+ NSNumber *doneFileSize = [NSNumber numberWithLong:[outputFileAttributes fileSize]];
+
+ [self performSelectorOnMainThread:@selector(showAudioPlayer) withObject:nil waitUntilDone:NO];
+
+ [assetReader release];
+ [assetReaderOutput release];
+ [assetWriter release];
+ [assetWriterInput release];
+ [exportPath release];
+ break;
+ }
+ }
+
+ }];
+ NSLog (@"bottom of convertTapped:");
+
+
+}
+-(void) showAudioPlayer{
+ if (avplayer_ !=nil) {
+ [avplayer_ release];
+ }
+ NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
+ NSString *exportPath = [[documentsDirectoryPath stringByAppendingPathComponent:EXPORT_NAME] retain];
+ NSURL *url = [NSURL fileURLWithPath:exportPath];
+ NSError *error;
+ avplayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
+ if (error)
+ {
+ NSLog(@"Error in audioPlayer: %@",
+ [error localizedDescription]);
+ } else {
+ avplayer_.delegate = self;
+ avplayer_.volume = 0.3;
+ avplayer_.enableRate = YES;
+ [avplayer_ setMeteringEnabled:YES];
+
+ speedSlider_.value = 1.0;
+ volumeSlider_.value = 0.3;
+ panSlider_.value = 0.0;
+
+ [avplayer_ prepareToPlay];
+ NSLog(@"%@", [avplayer_.settings valueForKey:AVChannelLayoutKey]);
+ NSLog(@"%@", [avplayer_.settings valueForKey:AVEncoderBitRateKey]);
+ NSLog(@"%@", [avplayer_.settings valueForKey:AVFormatIDKey]);
+
+
+ numberOfChannels_.text = [NSString stringWithFormat:@"Number Of Channels: %@ Hz", [avplayer_.settings valueForKey:AVNumberOfChannelsKey]];
+ sampleRate_.text = [NSString stringWithFormat:@"Sampling Rate: %@" , [avplayer_.settings valueForKey:AVSampleRateKey]];
+ NSLog(@"%@", [avplayer_.settings valueForKey:AVLinearPCMBitDepthKey]);
+ NSLog(@"%@", [avplayer_.settings valueForKey:AVLinearPCMIsBigEndianKey]);
+
+ }
+ [convertingAudioSpinner_ stopAnimating];
+ audioPlayerView_.hidden = NO;
+
+}
+
-(void) createWaveForm:(AVURLAsset*) songAsset {
+
+
NSData* imageData = [self renderPNGAudioPictogramForAssett:songAsset];
UIImage* wave = [UIImage imageWithData:imageData];
waveform_.frame = CGRectMake(0, 0, 310, 200);
@@ -432,7 +742,20 @@ - (NSData *) renderPNGAudioPictogramForAssett:(AVURLAsset *)songAsset {
return nil;
}
+
+
if (reader.status == AVAssetReaderStatusCompleted){
+
+ // NSURL *url = [NSURL fileURLWithPath:@"exported.caf"];
+
+
+ NSError *error;
+ AVAudioPlayer* avplayer = [[AVAudioPlayer alloc] initWithData:fullSongData error:&error];
+ avplayer.delegate = self;
+ avplayer.volume = 1.0;
+
+ [avplayer prepareToPlay];
+ [avplayer play];
NSLog(@"rendering output graphics using normalizeMax %d",normalizeMax);
@@ -455,6 +778,10 @@ - (NSData *) renderPNGAudioPictogramForAssett:(AVURLAsset *)songAsset {
return finalData;
}
+-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
+{
+ NSLog(@"Shit happened" );
+}
-(UIImage *) audioImageGraph:(SInt16 *) samples
normalizeMax:(SInt16) normalizeMax
sampleCount:(NSInteger) sampleCount
View
BIN AudioInfo/pause1-150x150.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN AudioInfo/play1-150x150.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d82569b

Please sign in to comment.
Something went wrong with that request. Please try again.