forked from dasevilla/rdio-podmapper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
executable file
·1148 lines (1023 loc) · 29.3 KB
/
main.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000 Gracenote.
*
* This software may not be used in any way or distributed without
* permission. All rights reserved.
*
* Some code herein may be covered by US and international patents.
*/
/*
* Name: musicid_stream
* Description:
* This example uses MusicID-Stream to fingerprint and identify a music track.
*
* Command-line Syntax:
* sample <client_id> <client_id_tag> <license> [local|online] <file>
*/
/*
* Modifications made by Zero One <[email protected]>
*
* Add ability to pass file_url as an argument
*
* Add _display_track_info_json function that returns track info as JSON
*
*/
/* GNSDK headers
*
* Define the modules your application needs.
* These constants enable inclusion of headers and symbols in gnsdk.h.
* Define GNSDK_LOOKUP_LOCAL because this program has the potential to do local queries.
* For local queries, a Gracenote local database must be present.
*/
#define GNSDK_MUSICID_STREAM 1
#define GNSDK_STORAGE_SQLITE 1
#define GNSDK_LOOKUP_LOCAL 1
#define GNSDK_LOOKUP_LOCALSTREAM 1
#define GNSDK_DSP 1
#include "gnsdk.h"
/* Standard C headers - used by the sample app, but not required for GNSDK */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**********************************************
* Local Function Declarations
**********************************************/
static int
_init_gnsdk(
const char* client_id,
const char* client_id_tag,
const char* client_app_version,
const char* license_path,
int use_local,
const char* file_path,
gnsdk_user_handle_t* p_user_handle
);
static void
_shutdown_gnsdk(
gnsdk_user_handle_t user_handle
);
static void
_do_sample_musicid_stream(
gnsdk_user_handle_t user_handle,
gnsdk_cstr_t s_audio_file
);
/* callbacks */
gnsdk_void_t GNSDK_CALLBACK_API
_musicidstream_identifying_status_callback(
gnsdk_void_t* callback_data,
gnsdk_musicidstream_identifying_status_t status,
gnsdk_bool_t* pb_abort
);
gnsdk_void_t GNSDK_CALLBACK_API
_musicidstream_result_available_callback(
gnsdk_void_t* callback_data,
gnsdk_musicidstream_channel_handle_t channel_handle,
gnsdk_gdo_handle_t response_gdo,
gnsdk_bool_t* pb_abort
);
gnsdk_void_t GNSDK_CALLBACK_API
_musicidstream_completed_with_error_callback(
gnsdk_void_t* callback_data,
gnsdk_musicidstream_channel_handle_t channel_handle,
const gnsdk_error_info_t* p_error_info
);
/* Local data */
// static gnsdk_cstr_t s_audio_file = "../../sample_data/teen_spirit_14s.wav";
static gnsdk_cstr_t s_gdb_location = "../../sample_data/sample_db";
/******************************************************************
*
* MAIN
*
******************************************************************/
int
main(int argc, char* argv[])
{
gnsdk_user_handle_t user_handle = GNSDK_NULL;
const char* client_id = NULL;
const char* client_id_tag = NULL;
const char* client_app_version = "1.0.0.0"; /* Version of your application */
const char* license_path = NULL;
const char* file_path = NULL;
int use_local = -1;
int rc = 0;
if (argc == 6)
{
client_id = argv[1];
client_id_tag = argv[2];
license_path = argv[3];
file_path = argv[5];
if (!strcmp(argv[4], "online"))
{
use_local = 0;
}
else if (!strcmp(argv[4], "local"))
{
use_local = 1;
}
/* GNSDK initialization */
if (use_local != -1)
{
rc = _init_gnsdk(
client_id,
client_id_tag,
client_app_version,
license_path,
use_local,
file_path,
&user_handle
);
if (0 == rc)
{
/* Perform a sample audio stream query */
_do_sample_musicid_stream(user_handle, file_path);
/* Clean up and shutdown */
_shutdown_gnsdk(user_handle);
}
}
}
if (argc != 6 || use_local == -1)
{
printf("\nUsage:\n%s clientid clientidtag license [local|online] file\n", argv[0]);
rc = -1;
}
return rc;
} /* main() */
/******************************************************************
*
* _DISPLAY_LAST_ERROR
*
* Echo the error and information.
*
*****************************************************************/
static void
_display_last_error(
int line_num
)
{
/* Get the last error information from the SDK */
const gnsdk_error_info_t* error_info = gnsdk_manager_error_info();
/* Error_info will never be GNSDK_NULL.
* The SDK will always return a pointer to a populated error info structure.
*/
printf(
"\nerror from: %s() [on line %d]\n\t0x%08x %s\n",
error_info->error_api,
line_num,
error_info->error_code,
error_info->error_description
);
} /* _display_last_error() */
/******************************************************************
*
* _GET_USER_HANDLE
*
* Load existing user handle, or register new one.
*
* GNSDK requires a user handle instance to perform queries.
* User handles encapsulate your Gracenote provided Client ID
* which is unique for your application. User handles are
* registered once with Gracenote then must be saved by
* your application and reused on future invocations.
*
*****************************************************************/
static int
_get_user_handle(
const char* client_id,
const char* client_id_tag,
const char* client_app_version,
int use_local,
gnsdk_user_handle_t* p_user_handle
)
{
gnsdk_user_handle_t user_handle = GNSDK_NULL;
gnsdk_cstr_t user_reg_mode = GNSDK_NULL;
gnsdk_str_t serialized_user = GNSDK_NULL;
gnsdk_char_t serialized_user_buf[1024] = {0};
gnsdk_bool_t b_localonly = GNSDK_FALSE;
gnsdk_error_t error = GNSDK_SUCCESS;
FILE* file = NULL;
int rc = 0;
/* Creating a GnUser is required before performing any queries to Gracenote services,
* and such APIs in the SDK require a GnUser to be provided. GnUsers can be created
* 'Online' which means they are created by the Gracenote backend and fully vetted.
* Or they can be create 'Local Only' which means they are created locally by the
* SDK but then can only be used locally by the SDK.
*/
/* If the application cannot go online at time of user-regstration it should
* create a 'local only' user. If connectivity is available, an Online user should
* be created. An Online user can do both Local and Online queries.
*/
if (use_local)
{
user_reg_mode = GNSDK_USER_REGISTER_MODE_LOCALONLY;
}
else
{
user_reg_mode = GNSDK_USER_REGISTER_MODE_ONLINE;
}
/* Do we have a user saved locally? */
file = fopen("user.txt", "r");
if (file)
{
fgets(serialized_user_buf, sizeof(serialized_user_buf), file);
fclose(file);
/* Create the user handle from the saved user */
error = gnsdk_manager_user_create(serialized_user_buf, client_id, &user_handle);
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_user_is_localonly(user_handle, &b_localonly);
if (!b_localonly || (strcmp(user_reg_mode, GNSDK_USER_REGISTER_MODE_LOCALONLY) == 0))
{
*p_user_handle = user_handle;
return 0;
}
/* else desired regmode is online, but user is localonly - discard and register new online user */
gnsdk_manager_user_release(user_handle);
}
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
}
}
else
{
printf("\nInfo: No stored user - this must be the app's first run.\n");
}
/*
* Register new user
*/
error = gnsdk_manager_user_register(
user_reg_mode,
client_id,
client_id_tag,
client_app_version,
&serialized_user
);
if (GNSDK_SUCCESS == error)
{
/* Create the user handle from the newly registered user */
error = gnsdk_manager_user_create(serialized_user, client_id, &user_handle);
if (GNSDK_SUCCESS == error)
{
/* save newly registered user for use next time */
file = fopen("user.txt", "w");
if (file)
{
fputs(serialized_user, file);
fclose(file);
}
}
gnsdk_manager_string_free(serialized_user);
}
if (GNSDK_SUCCESS == error)
{
*p_user_handle = user_handle;
rc = 0;
}
else
{
_display_last_error(__LINE__);
rc = -1;
}
return rc;
} /* _get_user_handle() */
/******************************************************************
*
* _DISPLAY_EMBEDDED_DB_INFO
*
* Display local Gracenote DB information.
*
******************************************************************/
static void
_display_embedded_db_info(void)
{
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_cstr_t gdb_version = GNSDK_NULL;
gnsdk_uint32_t ordinal = 0;
error = gnsdk_lookup_local_storage_info_count(
GNSDK_LOOKUP_LOCAL_STORAGE_METADATA,
GNSDK_LOOKUP_LOCAL_STORAGE_GDB_VERSION,
&ordinal
);
if (!error)
{
error = gnsdk_lookup_local_storage_info_get(
GNSDK_LOOKUP_LOCAL_STORAGE_METADATA,
GNSDK_LOOKUP_LOCAL_STORAGE_GDB_VERSION,
ordinal,
&gdb_version
);
if (!error)
{
printf("Gracenote DB Version : %s\n", gdb_version);
}
else
{
_display_last_error(__LINE__);
}
}
else
{
_display_last_error(__LINE__);
}
} /* _display_embedded_db_info() */
/******************************************************************
*
* _DISPLAY_GNSDK_PRODUCT_INFO
*
* Display product version information
*
******************************************************************/
// static void
// _display_gnsdk_product_info(void)
// {
// /* Display GNSDK Version infomation */
// printf(
// "\nGNSDK Product Version : %s \t(built %s)\n",
// gnsdk_manager_get_product_version(),
// gnsdk_manager_get_build_date()
// );
// } /* _display_gnsdk_product_info() */
/******************************************************************
*
* _ENABLE_LOGGING
*
* Enable logging for the SDK. Not used by Sample App. This helps
* Gracenote debug your app, if necessary.
*
******************************************************************/
static int
_enable_logging(void)
{
gnsdk_error_t error = GNSDK_SUCCESS;
int rc = 0;
error = gnsdk_manager_logging_enable(
"sample.log", /* Log file path */
GNSDK_LOG_PKG_ALL, /* Include entries for all packages and subsystems */
GNSDK_LOG_LEVEL_ERROR, /* Include only error entries */
GNSDK_LOG_OPTION_ALL, /* All logging options: timestamps, thread IDs, etc */
0, /* Max size of log: 0 means a new log file will be created each run */
GNSDK_FALSE /* GNSDK_TRUE = old logs will be renamed and saved */
);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
return rc;
} /* _enable_logging() */
/*****************************************************************************
*
* _SET_LOCALE
*
* Set application locale. Note that this is only necessary if you are using
* locale-dependant fields such as genre, mood, origin, era, etc. Your app
* may or may not be accessing locale_dependent fields, but it does not hurt
* to do this initialization as a matter of course .
*
****************************************************************************/
static int
_set_locale(
gnsdk_user_handle_t user_handle
)
{
gnsdk_locale_handle_t locale_handle = GNSDK_NULL;
gnsdk_error_t error = GNSDK_SUCCESS;
int rc = 0;
error = gnsdk_manager_locale_load(
GNSDK_LOCALE_GROUP_MUSIC, /* Locale group */
GNSDK_LANG_ENGLISH, /* Language */
GNSDK_REGION_DEFAULT, /* Region */
GNSDK_DESCRIPTOR_SIMPLIFIED, /* Descriptor */
user_handle, /* User handle */
GNSDK_NULL, /* User callback function */
0, /* Optional data for user callback function */
&locale_handle /* Return handle */
);
if (GNSDK_SUCCESS == error)
{
/* Setting the 'locale' as default
* If default not set, no locale-specific results would be available
*/
error = gnsdk_manager_locale_set_group_default(locale_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
/* The manager will hold onto the locale when set as default
* so it's ok to release our reference to it here
*/
gnsdk_manager_locale_release(locale_handle);
}
else
{
_display_last_error(__LINE__);
rc = -1;
}
return rc;
} /* _set_locale() */
/****************************************************************************************
*
* _INIT_GNSDK
*
* Initializing the GNSDK is required before any other APIs can be called.
* First step is to always initialize the Manager module, then use the returned
* handle to initialize any modules to be used by the application.
*
* For this sample, we also load a locale which is used by GNSDK to provide
* appropriate locale-sensitive metadata for certain metadata values. Loading of the
* locale is done here for sample convenience but can be done at anytime in your
* application.
*
****************************************************************************************/
static int
_init_gnsdk(
const char* client_id,
const char* client_id_tag,
const char* client_app_version,
const char* license_path,
int use_local,
const char* file_path,
gnsdk_user_handle_t* p_user_handle
)
{
gnsdk_manager_handle_t sdkmgr_handle = GNSDK_NULL;
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_user_handle_t user_handle = GNSDK_NULL;
int rc = 0;
/* Display GNSDK Product Version Info */
// _display_gnsdk_product_info();
/* Initialize the GNSDK Manager */
error = gnsdk_manager_initialize(
&sdkmgr_handle,
license_path,
GNSDK_MANAGER_LICENSEDATA_FILENAME
);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
return -1;
}
/* Enable logging */
if (0 == rc)
{
rc = _enable_logging();
}
/* Initialize the Storage SQLite Library */
if (0 == rc)
{
error = gnsdk_storage_sqlite_initialize(sdkmgr_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
if (use_local)
{
/* Set folder location of local database */
if (0 == rc)
{
error = gnsdk_storage_sqlite_option_set(
GNSDK_STORAGE_SQLITE_OPTION_STORAGE_FOLDER,
s_gdb_location
);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
/* Initialize the Lookup Local Library */
if (0 == rc)
{
error = gnsdk_lookup_local_initialize(sdkmgr_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
else
{
/* Display information about our local EDB */
_display_embedded_db_info();
}
}
/* Initialize the Lookup LocalStream Library */
if (0 == rc)
{
error = gnsdk_lookup_localstream_initialize(sdkmgr_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
if (0 == rc)
{
error = gnsdk_lookup_localstream_storage_location_set(s_gdb_location);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
}
/* Initialize the DSP Library - used for generating fingerprints */
if (0 == rc)
{
error = gnsdk_dsp_initialize(sdkmgr_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
/* Initialize the MusicID-Stream Library */
if (0 == rc)
{
error = gnsdk_musicidstream_initialize(sdkmgr_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
/* Get a user handle for our client ID. This will be passed in for all queries */
if (0 == rc)
{
rc = _get_user_handle(
client_id,
client_id_tag,
client_app_version,
use_local,
&user_handle
);
}
/* Set the user option to use our local Gracenote DB unless overridden. */
if (use_local)
{
if (0 == rc)
{
error = gnsdk_manager_user_option_set(
user_handle,
GNSDK_USER_OPTION_LOOKUP_MODE,
GNSDK_LOOKUP_MODE_LOCAL
);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
rc = -1;
}
}
}
/* Set the 'locale' to return locale-specifc results values. This examples loads an English locale. */
if (0 == rc)
{
rc = _set_locale(user_handle);
}
if (0 != rc)
{
/* Clean up on failure. */
_shutdown_gnsdk(user_handle);
}
else
{
/* return the User handle for use at query time */
*p_user_handle = user_handle;
}
return rc;
} /* _init_gnsdk() */
/***************************************************************************
*
* _SHUTDOWN_GNSDK
*
* When your program is terminating, or you no longer need GNSDK, you should
* call gnsdk_manager_shutdown(). No other shutdown operations are required.
* gnsdk_manager_shutdown() also shuts down all other modules, regardless
* of the number of times they have been initialized.
* You can shut down individual modules while your program is running with
* their dedicated shutdown functions in order to free up resources.
*
***************************************************************************/
static void
_shutdown_gnsdk(
gnsdk_user_handle_t user_handle
)
{
gnsdk_error_t error = GNSDK_SUCCESS;
error = gnsdk_manager_user_release(user_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
}
/* Shutdown the Manager to shutdown all libraries */
gnsdk_manager_shutdown();
} /* _shutdown_gnsdk() */
/***************************************************************************
*
* _DISPLAY_TRACK_GDO
*
***************************************************************************/
static void
_display_track_gdo(
gnsdk_gdo_handle_t track_gdo
)
{
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_gdo_handle_t title_gdo = GNSDK_NULL;
gnsdk_cstr_t value = GNSDK_NULL;
// gnsdk_cstr_t value_2 = GNSDK_NULL;
/* Track Title */
error = gnsdk_manager_gdo_child_get( track_gdo, GNSDK_GDO_CHILD_TITLE_OFFICIAL, 1, &title_gdo );
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_value_get( title_gdo, GNSDK_GDO_VALUE_DISPLAY, 1, &value );
if (GNSDK_SUCCESS == error)
{
printf( "\"%s\": \"%s\"", "track", value );
}
else
{
_display_last_error(__LINE__);
}
gnsdk_manager_gdo_release(title_gdo);
}
else
{
_display_last_error(__LINE__);
}
/* Track number on album. */
// error = gnsdk_manager_gdo_value_get( track_gdo, GNSDK_GDO_VALUE_TRACK_NUMBER, 1, &value_1 );
// if (GNSDK_SUCCESS == error)
// {
// printf( "%26s %s\n", "Track number:", value_1 );
// }
// else
// {
// _display_last_error(__LINE__);
// }
/* Track duration. */
// error = gnsdk_manager_gdo_value_get( track_gdo, GNSDK_GDO_VALUE_DURATION, 1, &value_1 );
// if (GNSDK_SUCCESS == error)
// {
// error = gnsdk_manager_gdo_value_get( track_gdo, GNSDK_GDO_VALUE_DURATION_UNITS, 1, &value_2 );
// if (GNSDK_SUCCESS == error)
// {
// printf( "%26s %s %s\n", "Track duration:", value_1, value_2 );
// }
// else
// {
// _display_last_error(__LINE__);
// }
// }
// else
// {
// _display_last_error(__LINE__);
// }
/* Position in track where the fingerprint matched. */
// error = gnsdk_manager_gdo_value_get( track_gdo, GNSDK_GDO_VALUE_MATCH_POSITION_MS, 1, &value_1 );
// if (GNSDK_SUCCESS == error)
// {
// printf( "%26s %s ms\n", "Match position:", value_1 );
// }
// else
// {
// _display_last_error(__LINE__);
// }
/* Duration of the matched fingerprint. */
// error = gnsdk_manager_gdo_value_get( track_gdo, GNSDK_GDO_VALUE_MATCH_DURATION_MS, 1, &value_1 );
// if (GNSDK_SUCCESS == error)
// {
// printf( "%26s %s ms\n", "Match duration:", value_1 );
// }
} /* _display_track_gdo() */
/***************************************************************************
*
* _DISPLAY_ALBUM_GDO
*
***************************************************************************/
static void
_display_album_gdo(
gnsdk_gdo_handle_t album_gdo
)
{
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_gdo_handle_t title_gdo = GNSDK_NULL;
gnsdk_gdo_handle_t track_gdo = GNSDK_NULL;
gnsdk_gdo_handle_t artist_gdo = GNSDK_NULL;
gnsdk_cstr_t value = GNSDK_NULL;
printf("{");
/* Artist Title */
error = gnsdk_manager_gdo_child_get( album_gdo, GNSDK_GDO_CHILD_ARTIST, 1, &artist_gdo );
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_child_get( artist_gdo, GNSDK_GDO_CHILD_NAME_OFFICIAL, 1, &title_gdo );
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_value_get( title_gdo, GNSDK_GDO_VALUE_DISPLAY, 1, &value );
if (GNSDK_SUCCESS == error)
{
printf( "\"%s\": \"%s\", ", "artist", value );
}
else
{
_display_last_error(__LINE__);
}
gnsdk_manager_gdo_release(title_gdo);
}
else
{
_display_last_error(__LINE__);
}
gnsdk_manager_gdo_release(artist_gdo);
}
else
{
_display_last_error(__LINE__);
}
/* Album Title */
error = gnsdk_manager_gdo_child_get( album_gdo, GNSDK_GDO_CHILD_TITLE_OFFICIAL, 1, &title_gdo );
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_value_get( title_gdo, GNSDK_GDO_VALUE_DISPLAY, 1, &value );
if (GNSDK_SUCCESS == error)
{
printf( "\"%s\": \"%s\", ", "album", value );
/* Matched track number. */
error = gnsdk_manager_gdo_value_get( album_gdo, GNSDK_GDO_VALUE_TRACK_MATCHED_NUM, 1, &value );
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_child_get( album_gdo, GNSDK_GDO_CHILD_TRACK_MATCHED, 1, &track_gdo );
if (GNSDK_SUCCESS == error)
{
_display_track_gdo(track_gdo);
gnsdk_manager_gdo_release(track_gdo);
}
else
{
_display_last_error(__LINE__);
}
}
else
{
_display_last_error(__LINE__);
}
}
else
{
_display_last_error(__LINE__);
}
gnsdk_manager_gdo_release(title_gdo);
}
else
{
_display_last_error(__LINE__);
}
printf("}");
} /* _display_album_gdo() */
/***************************************************************************
*
* _PROCESS_AUDIO
*
* This function simulates streaming audio into the Channel handle to give
* MusicId-Stream audio to identify
*
***************************************************************************/
static int
_process_audio(
gnsdk_musicidstream_channel_handle_t channel_handle,
gnsdk_cstr_t s_audio_file
)
{
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_size_t read_size = 0;
gnsdk_byte_t pcm_audio[2048] = {0};
FILE* p_file = NULL;
int rc = 0;
/* check file for existence */
p_file = fopen(s_audio_file, "rb");
if (p_file == NULL)
{
printf("\n\n!!!!Failed to open input file: %s!!!\n\n", s_audio_file);
return -1;
}
/* skip the wave header (first 44 bytes). we know the format of our sample files */
if (0 != fseek(p_file, 44, SEEK_SET))
{
fclose(p_file);
return -1;
}
/* initialize the fingerprinter
Note: The sample file shipped is a 44100Hz 16-bit stereo (2 channel) wav file */
error = gnsdk_musicidstream_channel_audio_begin(
channel_handle,
44100, 16, 2
);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
fclose(p_file);
return -1;
}
/* To keep this sample single-threaded, we launch the identification request
** immediately then do the audio processing. Generally we expect this
** call to occur on a separate thread from audio processing thread.
**
** MusicId-Stream will actually perform the identification when it
** receives enough audio.
**
** With the asynchronous nature of MusicID-Stream this call is non-blocking so it is ok to
** call on the UI thread.
*/
error = gnsdk_musicidstream_channel_identify(channel_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
fclose(p_file);
return -1;
}
read_size = fread(pcm_audio, sizeof(char), 2048, p_file);
while (read_size > 0)
{
/* write audio to the fingerprinter */
error = gnsdk_musicidstream_channel_audio_write(
channel_handle,
pcm_audio,
read_size
);
if (GNSDK_SUCCESS != error)
{
if (GNSDKERR_SEVERE(error)) /* 'aborted' warnings could come back from write which should be expected */
{
_display_last_error(__LINE__);
}
rc = -1;
break;
}
read_size = fread(pcm_audio, sizeof(char), 2048, p_file);
}
fclose(p_file);
/*signal that we are done*/
if (GNSDK_SUCCESS == error)
{
error = gnsdk_musicidstream_channel_audio_end(channel_handle);
if (GNSDK_SUCCESS != error)
{
_display_last_error(__LINE__);
}
}
return rc;
} /* _process_audio() */
/***************************************************************************
*
* _DO_SAMPLE_MUSICID_STREAM
*
***************************************************************************/
static void
_do_sample_musicid_stream(
gnsdk_user_handle_t user_handle,
gnsdk_cstr_t s_audio_file
)
{
gnsdk_musicidstream_channel_handle_t channel_handle = GNSDK_NULL;
gnsdk_musicidstream_callbacks_t callbacks = {0};
gnsdk_error_t error = GNSDK_SUCCESS;
int rc = 0;
// printf("\n*****Sample MID-Stream Query*****\n");
/* MusicId-Stream requires callbacks to receive identification results.
** He we set the various callbacks for results ands status.
*/