Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
1,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include <libspotify/api.h>
  7.  
  8. #include <allegro5/allegro.h>
  9. #include <allegro5/allegro_audio.h>
  10.  
  11. #define DEBUG 0 // change to 1 for debugging information
  12.  
  13. extern const uint8_t g_appkey[];
  14. extern const size_t g_appkey_size;
  15. extern const char *username;
  16. extern const char *password;
  17.  
  18. static bool g_logged_in;
  19. static bool g_playing;
  20. static bool g_running;
  21.  
  22. static ALLEGRO_AUDIO_STREAM *stream;
  23.  
  24. static ALLEGRO_EVENT_SOURCE sp_notify_event_source;
  25. ALLEGRO_EVENT sp_notify_event;
  26.  
  27. void debug(const char *format, ...)
  28. {
  29. if (!DEBUG)
  30. return;
  31.  
  32. va_list argptr;
  33. va_start(argptr, format);
  34. vprintf(format, argptr);
  35. printf("\n");
  36. }
  37.  
  38. void play(sp_session *session, sp_track *track)
  39. {
  40. sp_error error = sp_session_player_load(session, track);
  41. if (error != SP_ERROR_OK) {
  42. fprintf(stderr, "Error: %s\n", sp_error_message(error));
  43. exit(1);
  44. }
  45.  
  46. sp_session_player_play(session, 1);
  47. al_emit_user_event(&sp_notify_event_source, &sp_notify_event, NULL);
  48. }
  49.  
  50. static void on_search_complete(sp_search *search, void *userdata)
  51. {
  52. debug("callback: on_search_complete");
  53. sp_error error = sp_search_error(search);
  54. if (error != SP_ERROR_OK) {
  55. fprintf(stderr, "Error: %s\n", sp_error_message(error));
  56. exit(1);
  57. }
  58.  
  59. int num_tracks = sp_search_num_tracks(search);
  60. if (num_tracks == 0) {
  61. fprintf(stderr, "Sorry, couldn't find that track. =/\n\n");
  62. sp_search_release(search);
  63. g_playing = 0;
  64. return;
  65. }
  66.  
  67. sp_track *track = sp_search_track(search, 0);
  68. play((sp_session*)userdata, track);
  69. sp_search_release(search);
  70. }
  71.  
  72. static void on_login(sp_session *session, sp_error error)
  73. {
  74. debug("callback: on_login");
  75. if (error != SP_ERROR_OK) {
  76. fprintf(stderr, "login failed: %s\n", sp_error_message(error));
  77. exit(2);
  78. }
  79.  
  80. g_logged_in = 1;
  81. }
  82.  
  83. static void on_notify_main_thread(sp_session *session)
  84. {
  85. debug("callback: on_notify_main_thread");
  86. al_emit_user_event(&sp_notify_event_source, &sp_notify_event, NULL);
  87. }
  88.  
  89. static int on_music_delivery(sp_session *session, const sp_audioformat *format, const void *frames, int num_frames)
  90. {
  91. debug("callback: on_music_delivery");
  92. debug(" sample type: %d", format->sample_type);
  93. debug(" sample rate: %d", format->sample_rate);
  94. debug(" channels: %d", format->channels);
  95.  
  96. void *buffer = al_get_audio_stream_fragment(stream);
  97. if (buffer == NULL) {
  98. return 0;
  99. }
  100.  
  101. int max = (num_frames < 2048 ? num_frames : 2048);
  102. memcpy(buffer, frames, max);
  103.  
  104. al_set_audio_stream_fragment(stream, buffer);
  105. return max;
  106. }
  107.  
  108. static void on_end_of_track(sp_session *session)
  109. {
  110. debug("callback: on_end_of_track");
  111. }
  112.  
  113. static void on_log(sp_session *session, const char *data)
  114. {
  115. debug("spotify: %s\n", data);
  116. }
  117.  
  118. static sp_session_callbacks session_callbacks = {
  119. .logged_in = &on_login,
  120. .notify_main_thread = &on_notify_main_thread,
  121. .music_delivery = &on_music_delivery,
  122. .log_message = &on_log,
  123. .end_of_track = &on_end_of_track
  124. };
  125.  
  126. static sp_session_config spconfig = {
  127. .api_version = SPOTIFY_API_VERSION,
  128. .cache_location = "/tmp/spot",
  129. .settings_location = "/tmp/spot",
  130. .application_key = g_appkey,
  131. .application_key_size = 0, // set in main()
  132. .user_agent = "spot",
  133. .callbacks = &session_callbacks,
  134. NULL
  135. };
  136.  
  137. // Initialize the Spotify session.
  138. sp_session *init_spotify()
  139. {
  140. sp_error error;
  141. sp_session *session;
  142.  
  143. spconfig.application_key_size = g_appkey_size;
  144. if ((error = sp_session_create(&spconfig, &session)) != SP_ERROR_OK) {
  145. fprintf(stderr, "unable to create session: %s\n", sp_error_message(error));
  146. exit(1);
  147. }
  148.  
  149. g_logged_in = 0;
  150. sp_session_login(session, username, password, 0, NULL);
  151.  
  152. return session;
  153. }
  154.  
  155. // Initialize Allegro.
  156. void init_allegro()
  157. {
  158. if (!al_init()) {
  159. printf("failed to start allegro!\n");
  160. exit(1);
  161. }
  162.  
  163. if (!al_install_audio()) {
  164. printf("failed to install audio subsystem!\n");
  165. exit(1);
  166. }
  167.  
  168. if (!al_reserve_samples(1)) {
  169. printf("failed to reserve an audio sample!\n");
  170. exit(1);
  171. }
  172.  
  173. al_init_user_event_source(&sp_notify_event_source);
  174. sp_notify_event.user.type = ALLEGRO_GET_EVENT_TYPE('S', 'P', 'O', 'T');
  175.  
  176. ALLEGRO_CHANNEL_CONF chan_conf = ALLEGRO_CHANNEL_CONF_2;
  177. ALLEGRO_AUDIO_DEPTH depth = ALLEGRO_AUDIO_DEPTH_INT16;
  178. int bytes_per_fragment = 2048; // no idea what I'm doing here
  179. int fragment_count = 2;
  180. int freq = 2048; // again, no idea
  181.  
  182. int sample_size = al_get_channel_count(chan_conf) * al_get_audio_depth_size(depth);
  183. int samples = bytes_per_fragment / sample_size;
  184. //int buffer_size = bytes_per_fragment * fragment_count;
  185. stream = al_create_audio_stream(fragment_count, samples, freq, depth, chan_conf);
  186.  
  187. al_attach_audio_stream_to_mixer(stream, al_get_default_mixer());
  188. }
  189.  
  190. void play_song(sp_session *session, char *artist, char *track)
  191. {
  192. char q[4096];
  193. sprintf(q, "artist:\"%s\" track:\"%s\"", artist, track);
  194. sp_search_create(session, q, 0, 1, 0, 0, 0, 0, 0, 0, SP_SEARCH_STANDARD, &on_search_complete, session);
  195. }
  196.  
  197. int main(void)
  198. {
  199. sp_session *session = init_spotify();
  200. play_song(session, "Green Day", "Welcome to Paradise");
  201. init_allegro();
  202.  
  203. ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
  204. al_register_event_source(event_queue, &sp_notify_event_source);
  205.  
  206. // main loop
  207. g_running = 1;
  208. int next_timeout = 0;
  209.  
  210. // start the ball rolling
  211. sp_session_process_events(session, &next_timeout);
  212.  
  213. ALLEGRO_EVENT event;
  214.  
  215. while (g_running) {
  216. al_wait_for_event(event_queue, &event);
  217. switch (event.type) {
  218. case ALLEGRO_GET_EVENT_TYPE('S', 'P', 'O', 'T'):
  219. // process Spotify events
  220. do {
  221. sp_session_process_events(session, &next_timeout);
  222. } while (next_timeout == 0);
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228.  
  229. return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement