static svn_error_t *token_compare(void *baton, void *ltoken, void *rtoken, int *result) { sp_track *ltrack = (sp_track *) ltoken, *rtrack = (sp_track *) rtoken; sp_link *llink = sp_link_create_from_track(ltrack, 0), *rlink = sp_link_create_from_track(rtrack, 0); char lbuf[kTrackLinkLength], rbuf[kTrackLinkLength]; sp_link_as_string(llink, lbuf, kTrackLinkLength); sp_link_as_string(rlink, rbuf, kTrackLinkLength); *result = strcmp(lbuf, rbuf); return SVN_NO_ERROR; }
/** * Print the given track title together with some trivial metadata * * @param track The track object */ void print_track(sp_track *track) { int duration = sp_track_duration(track); char url[256]; sp_link *l; #if WIN32 printf(" %s ", sp_track_is_starred(track) ? "*" : " "); #else printf(" %s ", sp_track_is_starred(track) ? "★" : "☆"); #endif printf("Track %s [%d:%02d] has %d artist(s), %d%% popularity", sp_track_name(track), duration / 60000, (duration / 1000) / 60, sp_track_num_artists(track), sp_track_popularity(track)); if(sp_track_disc(track)) printf(", %d on disc %d", sp_track_index(track), sp_track_disc(track)); printf("\n"); l = sp_link_create_from_track(track, 0); sp_link_as_string(l, url, sizeof(url)); printf("\t\t%s\n", url); sp_link_release(l); }
// --------------------------------------------------------------------------- // static size_t getTrackLinks( TrackArray& tracks, LPSTR buffer, size_t buffer_length ) { UINT link_counter = 0; UINT index = 0; buffer[0] = '\0'; buffer_length--; CString spotifyLink; for ( sp_track* track : tracks ) { sp_link * link = sp_link_create_from_track( track, 0 ); UINT size = sp_link_as_string ( link, spotifyLink.GetBuffer(MAX_LINK_SIZE), MAX_LINK_SIZE ); sp_link_release( link ); spotifyLink.ReleaseBuffer(); if ( size >= buffer_length ) break; strcpy_s( buffer, buffer_length, spotifyLink ); buffer = &buffer[size+1]; *buffer = '\0'; buffer_length -= (size+1); link_counter++; } return link_counter; }
void append_track(sp_track *track, svn_stringbuf_t *buf) { sp_link *link = sp_link_create_from_track(track, 0); char link_buf[kTrackLinkLength]; sp_link_as_string(link, link_buf, kTrackLinkLength); svn_stringbuf_appendcstr(buf, link_buf); svn_stringbuf_appendcstr(buf, APR_EOL_STR); }
QString Spotify::uriFromTrack(sp_track *track) { sp_link * link = sp_link_create_from_track(track, 0); if (!link) { fprintf(stderr, "Spotify: failed to create URI for track"); return QString(); } static int bufSize = 128; char * buf = new char[bufSize]; int uriSize = sp_link_as_string(link, buf, bufSize); while (uriSize >= bufSize) { // String truncated, increase buf size and try again bufSize = uriSize + 1; delete [] buf; buf = new char[bufSize]; uriSize = sp_link_as_string(link, buf, bufSize); } bufSize = uriSize + 1; QString uri = QString::fromLocal8Bit(buf, uriSize); delete [] buf; sp_link_release(link); return uri; }
/** * A track has ended. Remove it from the playlist. * * Called from the main loop when the music_delivery() callback has set g_playback_done. */ static void track_ended(void) { fprintf(stderr,"jahspotify::track_ended: called\n"); int tracks = 0; if (g_currenttrack) { sp_link *link = sp_link_create_from_track(g_currenttrack,0); char *trackLinkStr = NULL; if (link) { trackLinkStr = malloc ( sizeof ( char ) * ( 100 ) ); sp_link_as_string(link,trackLinkStr,100); sp_link_release(link); } sp_session_player_unload(g_sess); sp_track_release(g_currenttrack); g_currenttrack = NULL; signalTrackEnded(trackLinkStr,JNI_FALSE); if (trackLinkStr) { free(trackLinkStr); } } }
QString Track::uri() const { if(!isValid()) return QString(); char buffer[512]; sp_link *link = sp_link_create_from_track(m_spTrack, 0); int size = sp_link_as_string(link, buffer, 512); return QString::fromLocal8Bit(buffer, size); }
SxTrack::SxTrack(sp_track *spTrack) { // Logger::printOut("creating track"); // Logger::printOut(sp_track_name(spSxTrack)); while (!sp_track_is_loaded(spTrack)) ; //Logger::printOut("creating track loaded"); m_references = 1; m_spTrack = spTrack; m_name = sp_track_name(spTrack); m_rating = ceil((float)sp_track_popularity(spTrack) / 10); m_duration = 0.001 * sp_track_duration(spTrack); m_trackNumber = sp_track_index(spTrack); m_albumName = ""; m_albumArtistName = ""; m_year = 0; m_thumb = NULL; m_hasTHumb = false; //load the album and release it when we have harvested all data we need sp_album * album = sp_track_album(spTrack); if (sp_album_is_loaded(album)) { SxAlbum* sAlbum = AlbumStore::getInstance()->getAlbum(sp_track_album(spTrack), false); m_thumb = sAlbum->getThumb(); m_albumName = sAlbum->getAlbumName(); m_albumArtistName = sAlbum->getAlbumArtistName(); m_year = sAlbum->getAlbumYear(); //release it again AlbumStore::getInstance()->removeAlbum(sAlbum); if (m_thumb != NULL) { m_thumb->addRef(); m_hasTHumb = true; } } else Logger::printOut("no album loaded for track"); m_artistName = sp_artist_name(sp_track_artist(spTrack, 0)); sp_link *link = sp_link_create_from_track(spTrack, 0); m_uri = new char[256]; sp_link_as_string(link, m_uri, 256); sp_link_release(link); }
json_t *track_to_json(sp_track *track, json_t *object) { char uri[kTrackLinkLength]; sp_link *link = sp_link_create_from_track(track, 0); sp_link_as_string(link, uri, kTrackLinkLength); sp_link_release(link); json_object_set_new(object, "uri", json_string_nocheck(uri)); if (!sp_track_is_loaded(track)) return object; const char *name = sp_track_name(track); json_object_set_new(object, "title", json_string_nocheck(name)); return object; }
// ---------------------------------------------------------------------------- // sp_linktype SpotifyEngine::getTrackLink( sp_track* track, CString& spotify_link ) { spotify_link.Empty(); sp_link* link = sp_link_create_from_track( track, 0 ); if ( link == NULL ) return SP_LINKTYPE_INVALID; LPSTR spotify_link_ptr = spotify_link.GetBufferSetLength( 512 ); sp_link_as_string ( link, spotify_link_ptr, 512 ); sp_linktype link_type = sp_link_type( link ); sp_link_release( link ); return link_type; }
static PyObject * Link_from_track(Link * self, PyObject *args) { Track *track; int offset = 0; PyObject *plink; if (!PyArg_ParseTuple(args, "O!|i", &TrackType, &track, &offset)) { return NULL; } sp_link *link = sp_link_create_from_track(track->_track, offset); if (!link) { PyErr_SetString(SpotifyError, "Failed to get track from a Link"); return NULL; } plink = Link_FromSpotify(link); return plink; }
jobject createJPlaylist(JNIEnv *env, sp_playlist *playlist) { jclass jClass; jobject playlistInstance; jmethodID jMethod; jClass = (*env)->FindClass(env, "jahspotify/media/Playlist"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJPlaylist: could not load jahnotify.media.Playlist\n"); return NULL; } playlistInstance = (*env)->AllocObject(env,jClass); if (!playlistInstance) { fprintf(stderr,"jahspotify::createJPlaylist: could not create instance of jahspotify.media.Playlistt\n"); return NULL; } sp_link *playlistLink = sp_link_create_from_playlist(playlist); if (playlistLink) { jobject playlistJLink = createJLinkInstance(env,playlistLink); setObjectObjectField(env,playlistInstance,"id","Ljahspotify/media/Link;",playlistJLink); sp_link_release(playlistLink); } setObjectStringField(env,playlistInstance,"name",sp_playlist_name(playlist)); sp_user *owner = sp_playlist_owner(playlist); if (owner) { setObjectStringField(env,playlistInstance,"author",sp_user_display_name(owner)); sp_user_release(owner); } // Lookup the method now - saves us looking it up for each iteration of the loop jMethod = (*env)->GetMethodID(env,jClass,"addTrack","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJPlaylist: could not load method addTrack(track) on class Playlist\n"); return NULL; } int numTracks = sp_playlist_num_tracks(playlist); int trackCounter = 0; for (trackCounter = 0 ; trackCounter < numTracks; trackCounter++) { sp_track *track = sp_playlist_track(playlist,trackCounter); if (track) { sp_track_add_ref(track); sp_link *trackLink = sp_link_create_from_track(track, 0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); // Add it to the playlist (*env)->CallVoidMethod(env,playlistInstance,jMethod,trackJLink); sp_link_release(trackLink); } sp_track_release(track); } } return playlistInstance; }
json_t *playlist_to_json(sp_playlist *playlist, json_t *object) { assert(sp_playlist_is_loaded(playlist)); // Owner sp_user *owner = sp_playlist_owner(playlist); const char *username = sp_user_display_name(owner); sp_user_release(owner); json_object_set_new_nocheck(object, "creator", json_string_nocheck(username)); // URI size_t playlist_uri_len = strlen("spotify:user:"******":playlist:") + strlen("284on3DVWeAxWkgVuzZKGt"); char *playlist_uri = malloc(playlist_uri_len); if (playlist_uri == NULL) { return NULL; } sp_link *playlist_link = sp_link_create_from_playlist(playlist); sp_link_as_string(playlist_link, playlist_uri, playlist_uri_len); sp_link_release(playlist_link); json_object_set_new(object, "uri", json_string_nocheck(playlist_uri)); free(playlist_uri); // Title const char *title = sp_playlist_name(playlist); json_object_set_new(object, "title", json_string_nocheck(title)); // Collaborative playlist_to_json_set_collaborative(playlist, object); // Description const char *description = sp_playlist_get_description(playlist); if (description != NULL) { json_object_set_new(object, "description", json_string_nocheck(description)); } // Number of subscribers int num_subscribers = sp_playlist_num_subscribers(playlist); json_object_set_new(object, "subscriberCount", json_integer(num_subscribers)); // Tracks json_t *tracks = json_array(); json_object_set_new(object, "tracks", tracks); char track_uri[kTrackLinkLength]; for (int i = 0; i < sp_playlist_num_tracks(playlist); i++) { sp_track *track = sp_playlist_track(playlist, i); sp_link *track_link = sp_link_create_from_track(track, 0); sp_link_as_string(track_link, track_uri, kTrackLinkLength); json_array_append(tracks, json_string_nocheck(track_uri)); sp_link_release(track_link); } return object; }
/* Save state */ static gboolean save_state(gpointer data) { queue_status qs; gboolean repeat, shuffle; int cur_track; GArray* tracks = NULL; JsonBuilder* jb = NULL; JsonGenerator* gen = NULL; gchar* out = NULL; GError* err = NULL; g_debug("savestate: starting to save the current state..."); /* Get data about the current state */ qs = queue_get_status(NULL, &cur_track, NULL); tracks = queue_tracks(); repeat = queue_get_repeat(); shuffle = queue_get_shuffle(); /* Save them in JSON */ jb = json_builder_new(); json_builder_begin_object(jb); json_builder_set_member_name(jb, "status"); switch (qs) { case STOPPED: json_builder_add_string_value(jb, "stopped"); break; case PLAYING: json_builder_add_string_value(jb, "playing"); break; case PAUSED: json_builder_add_string_value(jb, "paused"); break; default: g_warning("savestate: bad queue_status value: %d", qs); goto savestate_clean; } json_builder_set_member_name(jb, "repeat"); json_builder_add_boolean_value(jb, repeat); json_builder_set_member_name(jb, "shuffle"); json_builder_add_boolean_value(jb, shuffle); json_builder_set_member_name(jb, "current_track"); json_builder_add_int_value(jb, cur_track); json_builder_set_member_name(jb, "tracks"); json_builder_begin_array(jb); int i; for (i=0; i < tracks->len; i++) { sp_track* tr = g_array_index(tracks, sp_track*, i); if (!sp_track_is_loaded(tr)) { g_warning("savestate: queue track %d is not loaded", i); goto savestate_clean; } sp_link* lnk = sp_link_create_from_track(tr, 0); gchar uri[1024]; int uri_len = sp_link_as_string(lnk, uri, 1024); sp_link_release(lnk); if (uri_len >= 1024) { g_warning("savestate: URI too long for track %d", i); goto savestate_clean; } json_builder_add_string_value(jb, uri); } json_builder_end_array(jb); json_builder_end_object(jb); /* Store JSON to file */ gen = json_generator_new(); json_generator_set_root(gen, json_builder_get_root(jb)); out = json_generator_to_data(gen, NULL); if (g_file_set_contents(g_state_file_path, out, -1, &err)) g_debug("savestate: state saved to %s.", g_state_file_path); else g_warning("savestate: unable to dump status to file: %s", err->message); savestate_clean: if (tracks) g_array_free(tracks, TRUE); if (gen) g_object_unref(gen); if (jb) g_object_unref(jb); if (out) g_free(out); return FALSE; }
jobject createJAlbumInstance(JNIEnv *env, sp_album *album) { jclass albumJClass; jobject albumInstance; albumJClass = (*env)->FindClass(env, "jahspotify/media/Album"); if (albumJClass == NULL) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not load jahnotify.media.Album\n"); return NULL; } albumInstance = (*env)->AllocObject(env,albumJClass); if (!albumInstance) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not create instance of jahspotify.media.Album\n"); return NULL; } sp_albumbrowse *albumBrowse = sp_albumbrowse_create(g_sess,album,albumBrowseCompleteCallback,NULL); if (albumBrowse) { sp_albumbrowse_add_ref(albumBrowse); int count = 0; while (!sp_albumbrowse_is_loaded(albumBrowse) && count < 5) { fprintf(stderr,"jahspotify::createJAlbumInstance: waiting for album browse load to complete\n"); sleep(1); count++; } if (count == 5) { sp_albumbrowse_release(albumBrowse); return NULL; } // By now it looks like the album will also be loaded sp_link *albumLink = sp_link_create_from_album(album); if (albumLink) { sp_link_add_ref(albumLink); jobject albumJLink = createJLinkInstance(env, albumLink); setObjectObjectField(env,albumInstance,"id","Ljahspotify/media/Link;",albumJLink); setObjectStringField(env,albumInstance,"name",sp_album_name(album)); setObjectIntField(env,albumInstance,"year",sp_album_year(album)); sp_albumtype albumType = sp_album_type(album); jclass albumTypeJClass = (*env)->FindClass(env, "jahspotify/media/AlbumType"); jmethodID jMethod = (*env)->GetStaticMethodID(env,albumTypeJClass,"fromOrdinal","(I)Ljahspotify/media/AlbumType;"); jobject albumTypeEnum = (jobjectArray)(*env)->CallStaticObjectMethod(env, albumTypeJClass, jMethod,(int)albumType); setObjectObjectField(env,albumInstance,"type","Ljahspotify/media/AlbumType;",albumTypeEnum); sp_link *albumCoverLink = sp_link_create_from_album_cover(album); if (albumCoverLink) { sp_link_add_ref(albumCoverLink); jobject albumCoverJLink = createJLinkInstance(env, albumCoverLink); setObjectObjectField(env,albumInstance,"cover","Ljahspotify/media/Link;",albumCoverJLink); sp_image *albumCoverImage = sp_image_create_from_link(g_sess,albumCoverLink); if (albumCoverImage) { sp_image_add_ref(albumCoverImage); sp_image_add_load_callback(albumCoverImage,imageLoadedCallback,NULL); } sp_link_release(albumCoverLink); } sp_artist *artist = sp_album_artist(album); if (artist) { sp_artist_add_ref(artist); sp_link *artistLink = sp_link_create_from_artist(artist); if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env,artistLink); setObjectObjectField(env,albumInstance,"artist","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); } sp_artist_release(artist); } sp_link_release(albumLink); } int numTracks = sp_albumbrowse_num_tracks(albumBrowse); if (numTracks > 0) { // Add each track to the album - also pass in the disk as need be jmethodID addTrackJMethodID = (*env)->GetMethodID(env,albumJClass,"addTrack","(ILjahspotify/media/Link;)V"); int i = 0; for (i = 0; i < numTracks; i++) { sp_track *track = sp_albumbrowse_track(albumBrowse,i); if (track) { sp_track_add_ref(track); sp_link *trackLink = sp_link_create_from_track(track,0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); (*env)->CallVoidMethod(env, albumInstance, addTrackJMethodID,sp_track_disc(track),trackJLink); sp_link_release(trackLink); } } } } int numCopyrights = sp_albumbrowse_num_copyrights(albumBrowse); if (numCopyrights > 0) { // Add copyrights to album jmethodID addCopyrightMethodID = (*env)->GetMethodID(env,albumJClass,"addCopyright","(Ljava/lang/String;)V"); int i = 0; for (i = 0; i < numCopyrights; i++) { const char *copyright = sp_albumbrowse_copyright(albumBrowse,i); if (copyright) { jstring str = (*env)->NewStringUTF(env, copyright); (*env)->CallVoidMethod(env, albumInstance, addCopyrightMethodID,str); } } } const char *review = sp_albumbrowse_review(albumBrowse); if (review) { setObjectStringField(env,albumInstance,"review",review); } sp_albumbrowse_release(albumBrowse); } return albumInstance; }
jobject createJTrackInstance(JNIEnv *env, sp_track *track) { jclass jClass; jobject trackInstance; jClass = (*env)->FindClass(env, "jahspotify/media/Track"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load jahnotify.media.Track\n"); return NULL; } trackInstance = (*env)->AllocObject(env,jClass); if (!trackInstance) { fprintf(stderr,"jahspotify::createJTrackInstance: could not create instance of jahspotify.media.Track\n"); return NULL; } sp_link *trackLink = sp_link_create_from_track(track,0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); setObjectObjectField(env,trackInstance,"id","Ljahspotify/media/Link;",trackJLink); setObjectStringField(env,trackInstance,"title",sp_track_name(track)); setObjectIntField(env,trackInstance,"length",sp_track_duration(track)); setObjectIntField(env,trackInstance,"popularity",sp_track_popularity(track)); setObjectIntField(env,trackInstance,"trackNumber",sp_track_index(track)); sp_album *album = sp_track_album(track); if (album) { sp_album_add_ref(album); sp_link *albumLink = sp_link_create_from_album(album); if (albumLink) { sp_link_add_ref(albumLink); jobject albumJLink = createJLinkInstance(env,albumLink); jmethodID jMethod = (*env)->GetMethodID(env,jClass,"setAlbum","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n"); return NULL; } // set it on the track (*env)->CallVoidMethod(env,trackInstance,jMethod,albumJLink); sp_link_release(albumLink); } int numArtists = sp_track_num_artists(track); if (numArtists > 0) { jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addArtist","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method addArtist(link) on class Track\n"); return NULL; } int i = 0; for (i = 0; i < numArtists; i++) { sp_artist *artist = sp_track_artist(track,i); if (artist) { sp_artist_add_ref(artist); sp_link *artistLink = sp_link_create_from_artist(artist); if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env,artistLink); // set it on the track (*env)->CallVoidMethod(env,trackInstance,jMethod,artistJLink); sp_link_release(artistLink); } sp_artist_release(artist); } } } sp_album_release(album); } sp_link_release(trackLink); } return trackInstance; }
static void SP_CALLCONV search_complete(sp_search *search, void *userdata) { JNIEnv *env; jclass classLibspotify = find_class_from_native_thread(&env); string &qid = *static_cast<string*>(userdata); jstring j_qid = env->NewStringUTF(qid.c_str()); bool success = (sp_search_error(search) == SP_ERROR_OK) ? true : false; int count = sp_search_num_tracks(search); jstring j_trackname; jstring j_trackuri; jstring j_albumname; jstring j_artistname; sp_track *track; for (int i=0;i< count;i++){ track = sp_search_track(search, i); if (track != 0 && sp_track_error(track) == SP_ERROR_OK){ const char *temp = sp_track_name(track); if (temp != 0 && strlen(temp) != 0){ j_trackname = env->NewStringUTF(temp); } int trackDuration = sp_track_duration(track); if (sp_track_error(track) != SP_ERROR_OK) log("sp_track_error: %s",sp_error_message(sp_track_error(track))); int trackDiscnumber = sp_track_disc(track); if (sp_track_error(track) != SP_ERROR_OK) log("sp_track_error: %s",sp_error_message(sp_track_error(track))); int trackIndex = sp_track_index(track); if (sp_track_error(track) != SP_ERROR_OK) log("sp_track_error: %s",sp_error_message(sp_track_error(track))); char buffer [64]; sp_link *link = sp_link_create_from_track(track, 0); if (link != 0){ sp_link_as_string(link, buffer, 64); } j_trackuri = env->NewStringUTF(buffer); sp_album *album = sp_track_album(track); if (sp_track_error(track) != SP_ERROR_OK) log("sp_track_error: %s",sp_error_message(sp_track_error(track))); int albumYear = 0; if (album != 0){ temp = sp_album_name(album); albumYear = sp_album_year(album); if (temp != 0 && strlen(temp) != 0){ j_albumname = env->NewStringUTF(temp); } } sp_artist *artist = sp_track_artist(track,0); if (sp_track_error(track) != SP_ERROR_OK) log("sp_track_error: %s",sp_error_message(sp_track_error(track))); if (artist != 0){ temp = sp_artist_name(artist); if (temp != 0 && strlen(temp) != 0){ j_artistname = env->NewStringUTF(temp); } } jmethodID methodIdAddResult = env->GetStaticMethodID(classLibspotify, "addResult", "(Ljava/lang/String;Ljava/lang/String;IIILjava/lang/String;Ljava/lang/String;ILjava/lang/String;)V"); env->CallStaticVoidMethod(classLibspotify, methodIdAddResult, j_qid, j_trackname, trackDuration, trackDiscnumber, trackIndex, j_trackuri, j_albumname, albumYear, j_artistname); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); } env->DeleteLocalRef(j_trackname); env->DeleteLocalRef(j_trackuri); env->DeleteLocalRef(j_artistname); env->DeleteLocalRef(j_albumname); j_trackname = NULL; j_trackuri = NULL; j_artistname = NULL; j_albumname = NULL; } } jmethodID methodIdOnResolved = env->GetStaticMethodID(classLibspotify, "onResolved", "(Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;)V"); jstring j_error = env->NewStringUTF(sp_error_message(sp_search_error(search))); jstring j_didyoumean = env->NewStringUTF(sp_search_did_you_mean(search)); env->CallStaticVoidMethod(classLibspotify, methodIdOnResolved, j_qid, success, j_error, j_didyoumean); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); } env->DeleteLocalRef(classLibspotify); env->DeleteLocalRef(j_qid); env->DeleteLocalRef(j_error); env->DeleteLocalRef(j_didyoumean); log("Finished resolving query:'%s', success'%s', track count:'%d', qid:'%s'", sp_search_query(search), (success?"true":"false"), count, qid.c_str()); sp_search_release(search); delete &qid; }
/** * Append the JSON representing the track to param json. * * Returns TRUE if success - FALSE otherwise */ int track_to_json(sp_track* track, char** json, int* json_size, int count) { int track_info_size = 256; char* append = malloc(track_info_size * sizeof(char)); memset(append, '\0', track_info_size * sizeof(char)); if (append == NULL) { fprintf(stderr, "Failed to allocate memory for track info.\n"); return FALSE; } // Print track here (watch for quotes!) strcat(append, "{\"track_name\":\""); append_string_cleanse(&append, &track_info_size, sp_track_name(track)); int j; int nArtists = sp_track_num_artists(track); // Print artists here (watch for quotes!) strcat_resize(&append, &track_info_size, "\",\"artists\":[\""); for (j=0; j<nArtists; j++) { sp_artist *artist = sp_track_artist(track, j); if (artist == NULL) { fprintf(stderr, "track artist retrieved was null.\n"); if (append) free(append); return FALSE; } append_string_cleanse(&append, &track_info_size, sp_artist_name(artist)); if (j < nArtists - 1) strcat_resize(&append, &track_info_size, "\",\""); // sp_artist_release(artist); } // Print album here (watch for quotes!) strcat_resize(&append, &track_info_size, "\"],\"album\":\""); sp_album *album = sp_track_album(track); append_string_cleanse(&append, &track_info_size, sp_album_name(album)); // sp_album_release(album); // Print track url here (probably safe to assume there are no quotes here...) strcat_resize(&append, &track_info_size, "\",\"track_url\":\""); sp_link *l; char url[256]; l = sp_link_create_from_track(track, 0); sp_link_as_string(l, url, sizeof(url)); strcat_resize(&append, &track_info_size, url); // sp_link_release(l); char votes[5]; sprintf(votes, "%d", count); strcat_resize(&append, &track_info_size, "\",\"votes\":\""); strcat_resize(&append, &track_info_size, votes); strcat_resize(&append, &track_info_size, "\"}"); // close track_url quotes strcat_resize(json, json_size, append); if (append) free(append); return TRUE; }
JNIEXPORT int JNICALL Java_jahspotify_impl_JahSpotifyImpl_playTrack (JNIEnv *env, jobject obj, jstring uri) { uint8_t *nativeURI = NULL; fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Initiating play\n"); nativeURI = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL ); fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: uri: %s\n", nativeURI); if (!g_audio_initialized) { audio_init(&g_audiofifo); g_audio_initialized = JNI_TRUE; } // For each track, read out the info and populate all of the info in the Track instance sp_link *link = sp_link_create_from_string(nativeURI); if (link) { sp_track *t = sp_link_as_track(link); if (!t) { fprintf(stderr,"No track from link\n"); return; } while (!sp_track_is_available(g_sess,t)) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Waiting for track ...\n"); sleep(1); } if (sp_track_error(t) != SP_ERROR_OK) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Error with track: %s\n",sp_error_message(sp_track_error(t))); return; } fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: name: %s duration: %d\n",sp_track_name(t),sp_track_duration(t)); if (g_currenttrack == t) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Same track!\n"); return; } // If there is one playing, unload that now if (g_currenttrack && t != g_currenttrack) { // audio_fifo_flush(&g_audiofifo); // Unload the current track now sp_session_player_unload(g_sess); sp_link *currentTrackLink = sp_link_create_from_track(g_currenttrack,0); char *currentTrackLinkStr = NULL; if (currentTrackLink) { currentTrackLinkStr = malloc ( sizeof ( char ) * ( 100 ) ); sp_link_as_string(currentTrackLink,currentTrackLinkStr,100); sp_link_release(currentTrackLink); } signalTrackEnded(currentTrackLinkStr, JNI_TRUE); if (currentTrackLinkStr) { free(currentTrackLinkStr); } sp_track_release(g_currenttrack); g_currenttrack = NULL; } else { // audio_fifo_flush(&g_audiofifo); // audio_close(); // audio_init(&g_audiofifo); } sp_track_add_ref(t); sp_error result = sp_session_player_load(g_sess, t); if (sp_track_error(t) != SP_ERROR_OK) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Issue loading track: %s\n", sp_error_message((sp_track_error(t)))); } fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Track loaded: %s\n", (result == SP_ERROR_OK ? "yes" : "no")); // Update the global reference g_currenttrack = t; // Start playing the next track sp_session_player_play(g_sess, 1); fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Playing track\n"); sp_link_release(link); signalTrackStarted(nativeURI); return 0; } else { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_play: Unable to load link at this point\n"); } return 1; }
void track_get_data(sp_track* track, gchar** name, gchar** artist, gchar** album, gchar** link, guint* duration, int* popularity) { sp_artist** art = NULL; sp_album* alb = NULL; sp_link* lnk; int i; int nb_art = 0; const char* s; sp_track_add_ref(track); if (!sp_track_is_loaded(track)) { sp_track_release(track); return; } /* Begin loading everything */ if (name) { *name = g_strdup(sp_track_name(track)); } if (artist) { nb_art = sp_track_num_artists(track); art = (sp_artist**) malloc(nb_art * sizeof(sp_artist*)); if (!art) g_error("Can't allocate memory."); for (i=0; i < nb_art; i++) { art[i] = sp_track_artist(track, i); sp_artist_add_ref(art[i]); } } if (album) { alb = sp_track_album(track); sp_album_add_ref(alb); } if (link) { GString* tmp; lnk = sp_link_create_from_track(track, 0); if (!lnk) g_error("Can't get URI from track."); tmp = g_string_sized_new(1024); if (sp_link_as_string(lnk, tmp->str, 1024) < 0) g_error("Can't render URI from link."); *link = tmp->str; g_string_free(tmp, FALSE); sp_link_release(lnk); } if (duration) { *duration = sp_track_duration(track); } if (popularity) { *popularity = sp_track_popularity(track); } /* Now create destination strings */ if (artist) { GString* tmp = g_string_new(""); for (i=0; i < nb_art; i++) { if (sp_artist_is_loaded(art[i])) s = sp_artist_name(art[i]); else s = "[artist not loaded]"; if (i != 0) g_string_append(tmp, ", "); g_string_append(tmp, s); sp_artist_release(art[i]); } *artist = tmp->str; g_string_free(tmp, FALSE); } if (album) { if (sp_album_is_loaded(alb)) *album = g_strdup(sp_album_name(alb)); else *album = g_strdup("[album not loaded]"); sp_album_release(alb); } sp_track_release(track); }
void SpotifySearch::searchComplete( sp_search *result, void *userdata ) { UserData* data = reinterpret_cast<UserData*>( userdata ); qDebug() << "Got search result for qid:" << data->qid; // we return the top 50 results for searches, just top 1 for resolve QVariantMap resp; resp[ "qid" ] = data->qid; resp[ "_msgtype" ] = "results"; QVariantList results; // TODO search by popularity! qDebug() << "Got num results:" << sp_search_num_tracks( result ); if( sp_search_num_tracks( result ) > 0 ) {// we have a result int num = qMin( sp_search_num_tracks( result ), data->fulltext ? 50 : 1 ); for( int i = 0; i < num; i++ ) { sp_track *const tr = sp_search_track( result, i ); if( !tr || !sp_track_is_loaded( tr ) ) { qDebug() << "Got still loading track, skipping"; continue; } sp_link* link = sp_link_create_from_track( tr, 0 ); QString uid = data->resolver->addToTrackLinkMap( link ); int duration = sp_track_duration( tr ) / 1000; QVariantMap track; track[ "track" ] = QString::fromUtf8( sp_track_name( tr ) ); track[ "artist" ] = QString::fromUtf8( sp_artist_name( sp_track_artist( tr, 0 ) ) ); track[ "album" ] = QString::fromUtf8( sp_album_name( sp_track_album( tr ) ) ); track[ "albumpos" ] = sp_track_index( tr ); track[ "discnumber"] = sp_track_disc( tr ); track[ "year" ] = sp_album_year( sp_track_album( tr ) ); track[ "mimetype" ] = "audio/basic"; track[ "source" ] = "Spotify"; track[ "url" ] = QString( "http://localhost:%1/sid/%2.wav" ).arg( data->resolver->port() ).arg( uid ); track[ "duration" ] = duration; track[ "score" ] = .95; // TODO track[ "bitrate" ] = 192; // TODO // 8 is "magic" number. we don't know how much spotify compresses or in which format (mp3 or ogg) from their server, but 1/8th is approximately how ogg -q6 behaves, so use that for better displaying quint32 bytes = ( duration * 44100 * 2 * 2 ) / 8; track[ "size" ] = bytes; results << track; data->searchCount = 0; //qDebug() << "Found Track:" << sp_track_name( tr ) << "\n\tReporting:" << track["url"]; } }else { QString didYouMean = QString::fromUtf8(sp_search_did_you_mean( result ) ); if(data->searchCount <= 1 ){ qDebug() << "Try nr." << data->searchCount << " Searched for" << QString::fromUtf8(sp_search_query( result ) ) << "Did you mean?"<< didYouMean; //int distance = QString::compare(QString::fromUtf8(sp_search_query( result ) ), QString::fromUtf8(sp_search_did_you_mean( result ) ), Qt::CaseInsensitive); //qDebug() << "Distance for query is " << distance;//if( distance < 4) sp_search_create( SpotifySession::getInstance()->Session(), sp_search_did_you_mean( result ) , 0, data->fulltext ? 50 : 1, 0, 0, 0, 0, &SpotifySearch::searchComplete, data ); data->searchCount++; return; } } resp[ "results" ] = results; sp_search_release( result ); data->resolver->sendMessage( resp ); }