Playlist Generator
1.0
|
00001 // 00002 // AnalyseViewController.m 00003 // Playlist2 00004 // 00005 // Created by Max Woolf on 25/11/2011. 00006 // Copyright (c) 2011 __MyCompanyName__. All rights reserved. 00007 // 00008 00009 #import "AnalyseViewController.h" 00010 #import "MBProgressHUD.h" 00011 #import "SBJson.h" 00012 #import "EchonestPlaylistParameterViewController.h" 00013 #import "EchonestAnalyseConnection.h" 00014 00015 @implementation AnalyseViewController 00016 @synthesize recordingURL, artistField, trackField; 00017 extern const char * GetPCMFromFile(char * filename); 00018 00019 #pragma mark - HUD Methods 00020 -(void)showHUD 00021 { 00022 HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 00023 [self.navigationController.view addSubview:HUD]; 00024 HUD.mode = MBProgressHUDModeIndeterminate; 00025 HUD.delegate = self; 00026 HUD.dimBackground = YES; 00027 HUD.labelText = @"Identifying"; 00028 HUD.animationType = MBProgressHUDAnimationFade; 00029 [HUD show:YES]; 00030 } 00031 00032 00033 -(NSString *)getEchoprintCode 00034 { 00035 const char * fpCode = GetPCMFromFile((char*) [[[recordingURL.absoluteString substringFromIndex:16]stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding] cStringUsingEncoding:NSASCIIStringEncoding]); 00036 NSString *cCode = [NSString stringWithCString:fpCode]; 00037 //Set the objects code variable 00038 //NSLog(@"%@", cCode); 00039 return cCode; 00040 } 00041 00042 -(void)connectToEchonest 00043 { 00044 analysisConnection = [[AnalysisConnection alloc] initWithRequest:nil delegate:self echoprintCode:[self getEchoprintCode]]; 00045 [analysisConnection start]; 00046 } 00047 00048 -(void)getTrackID 00049 { 00050 SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 00051 NSError *error = nil; 00052 NSDictionary *jsonObjects = [jsonParser objectWithString:jsonData error:&error]; 00053 NSLog(@"%@", jsonObjects); 00054 NSDictionary *response = [jsonObjects objectForKey:@"response"]; 00055 NSArray *songs = [response objectForKey:@"songs"]; 00056 00057 //Find out whether songs were matched 00058 if([songs count] == 0) 00059 { 00060 [analyseView setAlpha:1]; 00061 [goButton setAlpha:1]; 00062 [goButton setTitle:@"Generate Playlist" forState:UIControlStateNormal]; 00063 [HUD setLabelText:@"Analysing"]; 00064 [HUD setDetailsLabelText:@"This might take a while..."]; 00065 trackID = @"No-Match"; 00066 echonestUpload = [[EchonestAnalyseConnection alloc] initWithFileURL:recordingURL.absoluteString delegate:self]; 00067 [echonestUpload start]; 00068 }else{ 00069 [goButton setAlpha:1]; 00070 [goButton setTitle:@"Choose Options" forState:UIControlStateNormal]; 00071 [identifyView setAlpha:1]; 00072 NSDictionary *track = [songs objectAtIndex:0]; 00073 NSString *localTrackID = [track objectForKey:@"id"]; 00074 trackID = localTrackID; 00075 00076 00077 songProfileConnection = [[SongProfileConnection alloc] initWithRequest:nil delegate:self trackID:trackID]; 00078 [songProfileConnection start]; 00079 HUD.labelText = @"Match Found"; 00080 [HUD hide:YES afterDelay:1]; 00081 } 00082 00083 } 00084 00085 -(void)getAnalysisDataOf:(NSString *)data 00086 { 00087 SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 00088 NSError *error = nil; 00089 NSDictionary *jsonObjects = [jsonParser objectWithString:data error:&error]; 00090 NSDictionary *response = [jsonObjects valueForKey:@"response"]; 00091 00092 //Check all is ok, then go ahead or show an error. 00093 if([[[response valueForKey:@"status"]valueForKey:@"code"]isEqualToNumber:[NSNumber numberWithInt:0]]) 00094 { 00095 NSDictionary *track = [response valueForKey:@"track"]; 00096 NSDictionary *audio_summary = [track valueForKey:@"audio_summary"]; 00097 tempo = [audio_summary valueForKey:@"tempo"]; 00098 danceability = [audio_summary valueForKey:@"danceability"]; 00099 key = [NSString stringWithFormat:@"%@", [audio_summary valueForKey:@"key"]]; 00100 mode = [NSString stringWithFormat:@"%@", [audio_summary valueForKey:@"mode"]]; 00101 energy = [audio_summary valueForKey:@"energy"]; 00102 timeSignature = [NSString stringWithFormat:@"%@", [audio_summary valueForKey:@"time_signature"]]; 00103 00104 [tempoSlider setValue:[tempo floatValue]]; 00105 [danceSlider setValue:[danceability floatValue]]; 00106 [energySlider setValue:[energy floatValue]]; 00107 [keyButton setTitle:[NSString stringWithFormat:@"%@ %@", [self convertNumberToKey:key], [self convertnumberToMode:mode]] forState:UIControlStateNormal]; 00108 [timeButton setTitle:timeSignature forState:UIControlStateNormal]; 00109 00110 00111 }else{ 00112 UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"That didn't work..." message:@"Echonest is having a bad day, come back later, sorry." delegate:self cancelButtonTitle:@"Alright, bro." otherButtonTitles: nil]; 00113 [fail show]; 00114 NSLog(@"Status Code: %@", [[response valueForKey:@"status"]valueForKey:@"code"]); 00115 } 00116 } 00117 00118 -(NSString *)convertnumberToMode:(NSString *)inputMode 00119 { 00120 if([inputMode isEqualToString:@"0"]) 00121 { 00122 return @"Minor"; 00123 } 00124 return @"Major"; 00125 } 00126 00127 -(NSString *)convertNumberToKey:(NSString *)inputKey 00128 { 00129 NSArray *keys = [[NSArray alloc] initWithObjects:@"C", @"C#", @"D", @"D#", @"E", @"F", @"F#", @"G", @"G#", @"A", @"A#", @"B", nil]; 00130 00131 return [keys objectAtIndex:[inputKey intValue]]; 00132 } 00133 00134 -(void)getTrackData 00135 { 00136 SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 00137 NSError *error = nil; 00138 NSDictionary *jsonObjects = [jsonParser objectWithString:jsonData error:&error]; 00139 NSDictionary *response = [jsonObjects objectForKey:@"response"]; 00140 NSArray *songs = [response objectForKey:@"songs"]; 00141 NSDictionary *track = [songs objectAtIndex:0]; 00142 trackArtist = [track objectForKey:@"artist_name"]; 00143 trackTitle = [track objectForKey:@"title"]; 00144 artistField.text = trackArtist; 00145 trackField.text = trackTitle; 00146 } 00147 00148 -(void)goButtonPressed:(id)sender 00149 { 00150 EchonestPlaylistParameterViewController *nextVC = [[EchonestPlaylistParameterViewController alloc] init]; 00151 [nextVC setTrackID:trackID]; 00152 [nextVC setTitle:@"Generate Playlist"]; 00153 if([trackID isEqualToString:@"No-Match"]) 00154 { 00155 nextVC.trackWasIdentified = NO; 00156 NSMutableDictionary *analyseDict = [[NSMutableDictionary alloc] init]; 00157 [analyseDict setValue:tempo forKey:@"Tempo"]; 00158 [analyseDict setValue:danceability forKey:@"Danceability"]; 00159 [analyseDict setValue:key forKey:@"Key"]; 00160 [analyseDict setValue:mode forKey:@"Mode"]; 00161 [analyseDict setValue:energy forKey:@"Energy"]; 00162 [analyseDict setValue:timeSignature forKey:@"Time"]; 00163 [nextVC setDictionaryData:analyseDict]; 00164 }else{ 00165 nextVC.trackWasIdentified = YES; 00166 } 00167 [self.navigationController pushViewController:nextVC animated:YES]; 00168 } 00169 00170 #pragma mark - Response methods 00171 //Wait for response! 00172 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 00173 //Just initate the NSData regardless of who made the request. 00174 [receivedData setLength:0]; 00175 } 00176 00177 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 00178 [receivedData appendData:d]; 00179 jsonData = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]; 00180 if(connection == analysisConnection) 00181 { 00182 //Deal with identifications 00183 [self getTrackID]; 00184 }else if(connection == songProfileConnection) 00185 { 00186 [self getTrackData]; 00187 }else if(connection == echonestUpload) 00188 { 00189 [HUD hide:YES afterDelay:1]; 00190 NSLog(@"Getting Data"); 00191 NSLog(@"%@", [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]); 00192 [self getAnalysisDataOf:[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]]; 00193 } 00194 00195 } 00196 00197 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 00198 { 00199 00200 } 00201 00202 #pragma mark - View lifecycle 00203 - (void)didReceiveMemoryWarning 00204 { 00205 // Releases the view if it doesn't have a superview. 00206 [super didReceiveMemoryWarning]; 00207 00208 // Release any cached data, images, etc that aren't in use. 00209 } 00210 00211 -(void)viewDidAppear:(BOOL)animated 00212 { 00213 00214 [self connectToEchonest]; 00215 } 00216 00217 - (void)viewDidLoad 00218 { 00219 [self.navigationController setTitle:@"Identify"]; 00220 [self showHUD]; 00221 [goButton setAlpha:0]; 00222 [identifyView setAlpha:0]; 00223 [analyseView setAlpha:0]; 00224 [super viewDidLoad]; 00225 // Do any additional setup after loading the view from its nib. 00226 } 00227 00228 - (void)viewDidUnload 00229 { 00230 [super viewDidUnload]; 00231 // Release any retained subviews of the main view. 00232 // e.g. self.myOutlet = nil; 00233 } 00234 00235 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 00236 { 00237 // Return YES for supported orientations 00238 return (interfaceOrientation == UIInterfaceOrientationPortrait); 00239 } 00240 00241 @end