JNIEXPORT jobject JNICALL Java_jahspotify_impl_JahSpotifyImpl_retrieveArtist ( JNIEnv *env, jobject obj, jstring uri) { jobject artistInstance; uint8_t *nativeUri = NULL; nativeUri = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL ); sp_link *link = sp_link_create_from_string(nativeUri); if (link) { sp_artist *artist= sp_link_as_artist(link); if (artist) { sp_artist_add_ref(artist); artistInstance = createJArtistInstance(env, artist); sp_artist_release(artist); } sp_link_release(link); } if (nativeUri) (*env)->ReleaseStringUTFChars(env, uri,nativeUri); return artistInstance; }
SP_LIBEXPORT(void) sp_artistbrowse_release(sp_artistbrowse *arb) { int i; assert(arb->ref_count > 0); arb->ref_count--; if(arb->ref_count) return; if(arb->artist) { DSFYDEBUG("Unreferencing artist at %p\n", arb->artist); sp_artist_release(arb->artist); } for(i = 0; i < arb->num_tracks; i++) sp_track_release(arb->tracks[i]); if(arb->num_tracks) free(arb->tracks); for(i = 0; i < arb->num_portraits; i++) free(arb->portraits[i]); if(arb->num_portraits) free(arb->portraits); for(i = 0; i < arb->num_similar_artists; i++) sp_artist_release(arb->similar_artists[i]); if(arb->num_similar_artists) free(arb->similar_artists); for(i = 0; i < arb->num_albums; i++) sp_album_release(arb->albums[i]); if(arb->num_albums) free(arb->albums); DSFYDEBUG("Deallocating artistbrowse at %p\n", arb); free(arb); }
SpotifySong::~SpotifySong() { boost::lock_guard<boost::mutex> lock(this->spotify->spotifyApiMutex); sp_track_release(this->track); if(this->artist != nullptr) sp_artist_release(this->artist); if(this->album != nullptr) sp_album_release(this->album); }
PHP_METHOD(SpotifyArtist, __destruct) { spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC); if (p->artistbrowse != NULL) { sp_artistbrowse_release(p->artistbrowse); } sp_artist_release(p->artist); }
SP_LIBEXPORT(void) sp_search_release(sp_search *search) { int i; assert(search->ref_count > 0); search->ref_count--; if(search->ref_count) return; free(search->query); for(i = 0; i < search->num_albums; i++) sp_album_release(search->albums[i]); if(search->num_albums) free(search->albums); for(i = 0; i < search->num_artists; i++) sp_artist_release(search->artists[i]); if(search->num_artists) free(search->artists); for(i = 0; i < search->num_tracks; i++) sp_track_release(search->tracks[i]); if(search->num_tracks) free(search->tracks); if(search->did_you_mean) free(search->did_you_mean); free(search); }
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); }
Artist::~Artist() { sp_artist_release( m_pArtist ); }
jobject createJArtistInstance(JNIEnv *env, sp_artist *artist) { jclass jClass; jobject artistInstance = createInstance(env,"jahspotify/media/Artist"); sp_link *artistLink = sp_link_create_from_artist(artist); jClass = (*env)->FindClass(env, "jahspotify/media/Artist"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJArtistInstance: could not load jahnotify.media.Artist\n"); return NULL; } if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env, artistLink); setObjectObjectField(env,artistInstance,"id","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); setObjectStringField(env,artistInstance,"name",sp_artist_name(artist)); sp_artistbrowse *artistBrowse = sp_artistbrowse_create(g_sess,artist,artistBrowseCompleteCallback,NULL); if (artistBrowse) { sp_artistbrowse_add_ref(artistBrowse); while (!sp_artistbrowse_is_loaded(artistBrowse)) { usleep(100); } int numSimilarArtists = sp_artistbrowse_num_similar_artists(artistBrowse); if (numSimilarArtists > 0) { jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addSimilarArtist","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n"); return NULL; } // Load the artist links int count = 0; for (count = 0; count < numSimilarArtists; count++) { sp_artist *similarArtist = sp_artistbrowse_similar_artist(artistBrowse,0); if (similarArtist) { sp_artist_add_ref(similarArtist); sp_link *similarArtistLink = sp_link_create_from_artist(similarArtist); if (similarArtistLink) { sp_link_add_ref(similarArtistLink); jobject similarArtistJLink = createJLinkInstance(env,similarArtistLink); // set it on the track (*env)->CallVoidMethod(env,artistInstance,jMethod,similarArtistJLink); sp_link_release(similarArtistLink); } sp_artist_release(similarArtist); } } } int numPortraits = sp_artistbrowse_num_portraits(artistBrowse); if (numPortraits > 0) { // Load portrait url const byte *portraitURI = sp_artistbrowse_portrait(artistBrowse,0); if (portraitURI) { char *portraitURIStr = toHexString((byte*)portraitURI); const char spotifyURI[] = "spotify:image:"; int len = strlen(spotifyURI) + strlen(portraitURIStr); char *dest = malloc(len+1); dest[0] = 0; strcat(dest,spotifyURI); strcat(dest,portraitURIStr); sp_link *portraitLink = sp_link_create_from_string(dest); sp_image *portrait = sp_image_create_from_link(g_sess,portraitLink); if (portrait) { sp_image_add_ref(portrait); sp_image_add_load_callback(portrait,imageLoadedCallback,NULL); } free(dest); free(portraitURIStr); if (portraitLink) { sp_link_add_ref(portraitLink); jobject portraitJLlink = createJLinkInstance(env,portraitLink); setObjectObjectField(env,artistInstance,"portrait","Ljahspotify/media/Link;",portraitJLlink); sp_link_release(portraitLink); } } } // sp_artistbrowse_num_albums() // sp_artistbrowse_album() const char *bios = sp_artistbrowse_biography(artistBrowse); if (bios) { setObjectStringField(env,artistInstance,"bios",bios); } // sp_artistbrowse_num_tracks() // sp_artistbrowse_track() sp_artistbrowse_release(artistBrowse); } } return artistInstance; }
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; }
Artist::~Artist() { sp_artist_release(artist_); }