My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// EchoNestTestClass.m
// soundscapes 3D
//
// Created by Kamel Makhloufi on 17/06/09.
// Copyright 2009 melka. All rights reserved.
//

#import "EchoNestTestClass.h"
#import "MyView.h";

@implementation EchoNestTestClass

- (void)applicationDidFinishLaunching:(NSNotification *)not {
NSLog(@"APPLICATION LAUNCHED");
/*
Here I used the Long Way
Each action (get_tatums, get_metadata, etc...) sends a notification.
You can either group all the notification to be sent to only one method.
--> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackNotifications:) name:nil object:nil];
--> then use :
--> -(void)ENTrackNotifications:(NSNotification*)not {
--> if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
--> if ([[not name] isEqual:@"ENTrackBarsLoaded"]) {
--> NSLog(@"ENTrackBarsLoaded");
--> ACTION GOES HERE
--> }
--> if ([[not name] isEqual:@"ENTrackBeatsLoaded"]) {
--> NSLog(@"ENTrackBeatsLoaded");
--> }
--> etc...

Or as with the following notifications.
You should always keep the NEST notifications in separate methods, it will
be easier to deal with the HTTP data feeds for key validation and track upload.
This way you can be sure you're dealing with working data.
*/

// NEST NOTIFICATIONS
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENKeyValidated:) name:@"ENApiKeyIsValid" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENArtistFound:) name:@"ENArtistSearchFinished" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackUploadValidationFinished:) name:@"ENUploadValidationFinished" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackUploadStarted:) name:@"ENTrackUploadStarted" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackUploadFinished:) name:@"ENTrackUploadFinished" object:nil];
// TRACK NOTIFICATIONS
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackBarsLoaded:) name:@"ENTrackBarsLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackBeatsLoaded:) name:@"ENTrackBeatsLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackDurationLoaded:) name:@"ENTrackDurationLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackEndOfFadeInLoaded:) name:@"ENTrackEndOfFadeInLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackKeyLoaded:) name:@"ENTrackKeyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackLoudnessLoaded:) name:@"ENTrackLoudnessLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackMetadataLoaded:) name:@"ENTrackMetadataLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackModeLoaded:) name:@"ENTrackModeLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackSectionsLoaded:) name:@"ENTrackSectionsLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackSegmentsLoaded:) name:@"ENTrackSegmentsLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackStartOfFadeOutLoaded:) name:@"ENTrackStartOfFadeOutLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackTatumsLoaded:) name:@"ENTrackTatumsLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackTempoLoaded:) name:@"ENTrackTempoLoaded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ENTrackTimeSignatureLoaded:) name:@"ENTrackTimeSignatureLoaded" object:nil];

bars = NO;
beats = NO;
fades = NO;
tatums = NO;
sections = NO;

#pragma mark Enter Your EchoNest API Key here
APIKey = [NSString stringWithString:@"yourApiKey"];
nest = [[EchoNest alloc] initWithAPIKey:APIKey];
[loader startAnimation:nil];
}

#pragma mark Actions for Interface Builder buttons
// Action for the toolbar buttons
-(IBAction)loadBars:(id)sender {
[loader startAnimation:nil];
bars = YES;
[track getBars];
}
-(IBAction)loadBeats:(id)sender {
[loader startAnimation:nil];
beats = YES;
[track getBeats];
}
-(IBAction)loadDuration:(id)sender {
[loader startAnimation:nil];
[track getDuration];
}
-(IBAction)loadEndOfFadeIn:(id)sender {
[loader startAnimation:nil];
fades = YES;
[track getEndOfFadeIn];
[track getStartOfFadeOut];
}
-(IBAction)loadKey:(id)sender {
[loader startAnimation:nil];
[track getKey];
}
-(IBAction)loadLoudness:(id)sender {
[loader startAnimation:nil];
[track getLoudness];
}
-(IBAction)loadMetadata:(id)sender {
[loader startAnimation:nil];
[track getMetadata];
}
-(IBAction)loadMode:(id)sender {
[loader startAnimation:nil];
[track getMode];
}
-(IBAction)loadSections:(id)sender {
[loader startAnimation:nil];
sections = YES;
[track getSections];
}
-(IBAction)loadSegments:(id)sender {
[loader startAnimation:nil];
[track getSegments];
}
-(IBAction)loadTatums:(id)sender {
[loader startAnimation:nil];
tatums = YES;
[track getTatums];
}
-(IBAction)loadTempo:(id)sender {
[loader startAnimation:nil];
[track getTempo];
}
-(IBAction)loadTimeSignature:(id)sender {
[loader startAnimation:nil];
[track getTimeSignature];
}

// The bottom right button open a file dialog to
// upload a mp3 file
-(IBAction)uploadTrack:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
NSArray* fileTypes = [NSArray arrayWithObjects:@"mp3",nil];
[openDlg setPrompt:@"Load"];
[openDlg setTitle:@"Select mp3 file"];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:NO];
if ( [openDlg runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton )
{
NSArray* files = [openDlg filenames];
int i;
for( i = 0; i < [files count]; i++ )
{
fileName = [files objectAtIndex:i];
// Sends the filename to the EchoNest object
// it will upload the file to the server
// and sends a notification when done
[nest uploadFile:fileName];
// Sends the file to the "drawing view"
// That's were the loading and playing is done
[drawingView initSound:fileName];
}
}
}

#pragma mark EchoNest notifications

// ECHONEST GENERAL NOTIFICATIONS
-(void)ENKeyValidated:(NSNotification*)not {
// Test if your api key is valid
if ([[not object] boolValue]) {
[labelInfos setTitleWithMnemonic:@"EchoNest API Key valid"];
[loader startAnimation:nil];
} else {
[labelInfos setTitleWithMnemonic:@"Invalid API Key"];
}
}
-(void)ENArtistFound:(NSNotification*)not {
if ([[not object] boolValue]) {

}
}
-(void)ENTrackUploadValidationFinished:(NSNotification*)not {
// Sent if the file's MD5 hash was found on the
// EchoNest server. Basically the same thing as
// ENTrackUploadFinished if the file was already
// analyzed at some time.

if ([[not object] boolValue]) {
// RETRIEVE THE TRACK OBJECT
track = [nest track];
// RETRIEVE INFOS FROM TRACK
//[track getMetadata];
//[track getSections];
if (bars) {
[track getBars];
}
if (beats) {
[track getBeats];
}
if (fades) {
[track getEndOfFadeIn];
[track getStartOfFadeOut];
}
if (tatums) {
[track getTatums];
}
}
}
-(void)ENTrackUploadStarted:(NSNotification*)not {
[loader startAnimation:nil];
[labelInfos setTextColor:[NSColor blackColor]];
[labelInfos setTitleWithMnemonic:@"MP3 UPLOAD STARTED"];
}

-(void)ENTrackUploadFinished:(NSNotification*)not {
if ([[not object] boolValue]) {
[labelInfos setTextColor:[NSColor blackColor]];
[labelInfos setTitleWithMnemonic:@"MP3 UPLOAD FINISHED"];
// RETRIEVE THE TRACK OBJECT
track = [nest track];
// RETRIEVE INFOS FROM TRACK
//[track getMetadata];
//[track getSections];
if (bars) {
[track getBars];
}
if (beats) {
[track getBeats];
}
if (fades) {
[track getEndOfFadeIn];
[track getStartOfFadeOut];
}
if (tatums) {
[track getTatums];
}
}
[loader stopAnimation:nil];
}
// ECHONEST TRACK NOTIFICATIONS
-(void)ENTrackBarsLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setXmlData:[not object] forType:@"bars"];
}
[loader stopAnimation:nil];
}
-(void)ENTrackBeatsLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setXmlData:[not object] forType:@"beats"];
}
[loader stopAnimation:nil];
}
-(void)ENTrackDurationLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[labelInfos setTextColor:[NSColor blackColor]];
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Duration : %f seconds",[[not object] time]]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackEndOfFadeInLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setFadeInData:[not object]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackStartOfFadeOutLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setFadeOutData:[not object]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackKeyLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
float confidence = [[not object] confidence];
NSColor* c = [NSColor colorWithCalibratedHue:confidence/4 saturation:.8 brightness:1 alpha:1];
[labelInfos setTextColor:c];
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Key : %i",[[not object] key]]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackLoudnessLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[labelInfos setTextColor:[NSColor blackColor]];
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Loudness : %fdB",[[not object] loudness]]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackMetadataLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
NSString* m = [NSString stringWithFormat:@"%@ - %@ : %@ : %@",[[not object] status], [[not object] artist],[[not object] album],[[not object] title]];
[labelInfos setTextColor:[NSColor blackColor]];
[labelInfos setTitleWithMnemonic:m];
}
[loader stopAnimation:nil];
}
-(void)ENTrackModeLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
float confidence = [[not object] confidence];
NSColor* c = [NSColor colorWithCalibratedHue:confidence/4 saturation:.8 brightness:1 alpha:1];
[labelInfos setTextColor:c];
NSString* mode;
if ([[not object] mode] == 1) {
mode = @"Major";
} else {
mode = @"Minor";
}
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Mode : %@",mode] ];
}
[loader stopAnimation:nil];
}
-(void)ENTrackSectionsLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setXmlData:[not object] forType:@"sections"];
}
[loader stopAnimation:nil];
}
-(void)ENTrackSegmentsLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setXmlData:[not object] forType:@"segments"];
}
[loader stopAnimation:nil];
}
-(void)ENTrackTatumsLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
[drawingView setXmlData:[not object] forType:@"tatums"];
}
[loader stopAnimation:nil];
}
-(void)ENTrackTempoLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
float confidence = [[not object] confidence];
NSColor* c = [NSColor colorWithCalibratedHue:confidence/4 saturation:.8 brightness:1 alpha:1];
[labelInfos setTextColor:c];
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Tempo : %f BPM",[[not object] tempo]]];
}
[loader stopAnimation:nil];
}
-(void)ENTrackTimeSignatureLoaded:(NSNotification*)not {
if ([[[not userInfo] valueForKey:@"success"] boolValue]) {
float confidence = [[not object] confidence];
NSColor* c = [NSColor colorWithCalibratedHue:confidence/4 saturation:.8 brightness:1 alpha:1];
[labelInfos setTextColor:c];
[labelInfos setTitleWithMnemonic:[NSString stringWithFormat:@"Time Signature : %f",[[not object] signature]]];
}
[loader stopAnimation:nil];
}

@end

Change log

r13 by me...@melkaone.net on Jul 10, 2009   Diff
- Added support for iPhone + iPhone test
App. Works on simulator and device for
iPhone OS 3. Other OSes and platforms
(iPod touch) not tested.
Go to: 
Project members, sign in to write a code review

Older revisions

r11 by me...@melkaone.net on Jul 10, 2009   Diff
- Changed NSXMLDocument XML parsing by
NSXMLParser to allow iPhone to use the
library
r4 by me...@melkaone.net on Jun 30, 2009   Diff
Initial Import
All revisions of this file

File info

Size: 12528 bytes, 344 lines
Powered by Google Project Hosting