int compare_tracks(const void* a, const void* b) { // type safety??! this is C god dammit! sp_track *left = *((sp_track**)a); sp_track *right = *((sp_track**)b); sp_artist *lefta = sp_track_artist(left,0); // get first artist on track because I am lazy sp_album *leftal = sp_track_album(left); sp_artist *righta = sp_track_artist(right, 0); sp_album *rightal = sp_track_album(right); const char *left_artist = sp_artist_name(lefta); const char *right_artist = sp_artist_name(righta); int artist_compare = strcmp(left_artist, right_artist); if (artist_compare != 0) { return artist_compare; } const char *left_album = sp_album_name(leftal); const char *right_album = sp_album_name(rightal); int album_compare = strcmp(left_album, right_album); if (album_compare != 0) { return album_compare; } // for the last tier we use track number, not name. int left_track = sp_track_index(left); int right_track = sp_track_index(right); return left < right ? -1 : left > right ? 1 : 0; }
ListResponse<TrackInfo*>* SpotifyPlaylistContainer::listTracks( PlaylistTask* task) { ListResponse<TrackInfo*>* response = new ListResponse<TrackInfo*>(); response->setListType(ListTypeSong); ListPlaylistTrackInfo info = task->getCommandInfo().listPlaylistTrackInfo; int playlistId = info.playlist; if (playlistId > -1 && playlistId < sp_playlistcontainer_num_playlists(playlistContainer)) { bool sendName = ((info.trackInfoFlags & TrackInfoName) == TrackInfoName); bool sendArtist = ((info.trackInfoFlags & TrackInfoArtists) == TrackInfoArtists); bool sendAlbum = ((info.trackInfoFlags & TrackInfoAlbum) == TrackInfoAlbum); bool sendDuration = ((info.trackInfoFlags & TrackInfoDuration) == TrackInfoDuration); bool sendArtwork = ((info.trackInfoFlags & TrackInfoArtwork) == TrackInfoArtwork); sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer, playlistId); int numTracks = sp_playlist_num_tracks(playlist); for (int i = 0; i < numTracks; i++) { TrackInfo* trackInfo = new TrackInfo(); trackInfo->id = i; sp_track* track = sp_playlist_track(playlist, i); if (sendName) { trackInfo->name = sp_track_name(track); } if (sendArtist) { trackInfo->numArtists = sp_track_num_artists(track); trackInfo->artists = new const char*[trackInfo->numArtists]; for (int j = 0; j < trackInfo->numArtists; j++) { trackInfo->artists[j] = sp_artist_name( sp_track_artist(track, j)); } } if (sendAlbum) { trackInfo->album = sp_album_name(sp_track_album(track)); } if (sendDuration) { trackInfo->duration = sp_track_duration(track); } if (sendArtwork) { trackInfo->artwork = sp_image_create(session, sp_album_cover(sp_track_album(track), SP_IMAGE_SIZE_NORMAL)); } response->addMember(trackInfo); } return response; } delete response; return nullptr; }
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); }
Album* Track::GetAlbum() { Album* pAlbum = m_pSession->CreateAlbum(); pAlbum->Load( sp_track_album( m_pTrack ) ); return pAlbum; }
void play(sp_session *session, sp_track *track) { sp_error error = sp_session_player_load(session, track); if (error != SP_ERROR_OK) { fprintf(stderr, "Error: %s\n", sp_error_message(error)); exit(1); } sp_artist *artist = sp_track_artist(track, 0); sp_album *album = sp_track_album(track); int secs = sp_track_duration(track) / 1000; int mins = 0; while (secs >= 60) { mins++; secs -= 60; } printf("\n"); printf(" Track: %s\n", sp_track_name(track)); printf(" Artist: %s\n", sp_artist_name(artist)); printf(" Album: %s\n", sp_album_name(album)); printf(" Year: %d\n", sp_album_year(album)); printf(" Popularity: %d / 100\n", sp_track_popularity(track)); printf(" Duration: %02d:%02d\n", mins, secs); printf("\n"); printf("Playing...\n"); sp_session_player_play(session, 1); }
sp_image* track_get_image(sp_track* track) { sp_album* alb = NULL; sp_image* img = NULL; const void* img_id = NULL; /* Get album */ alb = sp_track_album(track); if (!alb) g_error("Can't get track album."); sp_album_add_ref(alb); if (!sp_album_is_loaded(alb)) g_error("Album not loaded."); /* Get image */ img_id = sp_album_cover(alb, SP_IMAGE_SIZE_NORMAL); if (!img_id) { /* Since the album is loaded, a NULL here indicates that there is no cover for this album. */ sp_album_release(alb); return NULL; } img = sp_image_create(g_session, img_id); sp_album_release(alb); if (!img) g_error("Can't create image."); return img; }
bool SpotifySong::updateMetadata() { if(sp_track_is_loaded(this->track)) { this->duration = sp_track_duration(this->track); this->title = sp_track_name(this->track); if(this->artist == nullptr) { this->artist = sp_track_artist(this->track,0); sp_artist_add_ref(this->artist); } if(this->album == nullptr) { this->album = sp_track_album(this->track); sp_album_add_ref(this->album); } } else return false; if(this->artist != nullptr && sp_artist_is_loaded(this->artist)) this->artistName = sp_artist_name(this->artist); else return false; if(this->album != nullptr && sp_album_is_loaded(this->album)) this->albumName = sp_album_name(this->album); else return false; return true; }
static int try_play(sp_session *session, sp_track *t,int offset) { sp_error err; fprintf(stderr,"try play\n"); err = sp_session_player_load(session,t); if( err != SP_ERROR_OK) { fprintf(stderr,"Load failed %d\n",err); } if(!sp_track_is_loaded(t)) { if(g_track_to_be_played != t) { if(g_track_to_be_played) sp_track_release(g_track_to_be_played); g_track_to_be_played = t; //FIXME: Do I need to addref? sp_track_add_ref(g_track_to_be_played); } } else { //FIXME: Seek! // g_current_fifo_idx = (g_current_fifo_idx+1) % 2; sp_session_player_seek(session,offset); sp_session_player_play(session,true); sp_album *album = sp_track_album(t); if(album) { const byte *id = sp_album_cover(album); g_gazify.current_t = t; if(id) { sp_image *img = sp_image_create(session,id); sp_image_add_load_callback(img,&image_loaded,&g_gazify); } } g_track_to_be_played = NULL; } return err; }
QString Track::album() const { if(isValid()) { sp_album *album = sp_track_album(m_spTrack); if (album) { return QString::fromUtf8(sp_album_name(album)); } } return QString(); }
sp_image *Track::albumImage(SpotifySession *session) const { if(isValid()) { sp_album *album = sp_track_album(m_spTrack); if (album) { const byte *id = sp_album_cover(album); return sp_image_create(session->session(), id); } } return NULL; }
static PyObject * Track_album(Track * self) { sp_album *album; album = sp_track_album(self->_track); if (!album) Py_RETURN_NONE; PyObject *a = Album_FromSpotify(album); return a; }
// ---------------------------------------------------------------------------- // bool DMX_PLAYER_API GetTrackInfo( LPCSTR track_link, LPSTR track_name, size_t track_name_size, LPSTR artist_name, size_t artist_name_size, LPSTR album_name, size_t album_name_size, DWORD* track_duration_ms, bool* starred ) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); sp_track* track = theApp.m_spotify.linkToTrack( track_link ); if ( !track ) return false; if ( track_name ) { LPCSTR title = sp_track_name( track ); if ( title == NULL ) return false; errno_t err = strncpy_s( track_name, track_name_size, title, strlen(title) ); if ( err != 0 ) return false; } if ( artist_name ) { if ( sp_track_num_artists( track ) == 0 ) *artist_name = '\0'; else { sp_artist *artist = sp_track_artist( track, 0 ); if ( artist == NULL ) return false; LPCSTR title = sp_artist_name( artist ); errno_t err = strncpy_s( artist_name, artist_name_size, title, strlen(title) ); if ( err != 0 ) return false; } } if ( album_name ) { sp_album *album = sp_track_album( track ); if ( album == NULL ) return false; LPCSTR title = sp_album_name( album ); errno_t err = strncpy_s( album_name, album_name_size, title, strlen(title) ); if ( err != 0 ) return false; } if ( track_duration_ms ) *track_duration_ms = theApp.m_spotify.getTrackLength( track ); if ( starred ) *starred = theApp.m_spotify.isTrackStarred( track ); return true; }
static void image_loaded(sp_image *img, void *user) { gazify_t *g = (gazify_t*)user; size_t img_size; const void *data = sp_image_data(img,&img_size); sp_artist *a = sp_track_artist(g->current_t,0); sp_album *alb = sp_track_album(g->current_t); g->g->postTrackInfo(sp_artist_name(a),sp_album_name(alb),sp_track_name(g->current_t),data,img_size); //sp_image_remove_load_callback(img,&image_loaded,user); // sp_image_release(img); //fiuckit, let's leak! }
static bool validate_complete_load() { if (g_list_container == NULL) { return false; } sp_playlistcontainer *list_container = g_list_container; int pl_count = sp_playlistcontainer_num_playlists(list_container); int i=0; for(; i < pl_count; i++) { sp_playlist *l = sp_playlistcontainer_playlist(list_container, i); sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container, i); if (type == SP_PLAYLIST_TYPE_PLAYLIST && !sp_playlist_is_loaded(l)) { return false; } } sp_playlist *source_list = NULL; i=0; for(; i < pl_count; i++) { sp_playlist *l = sp_playlistcontainer_playlist(list_container, i); const char* pname = sp_playlist_name(l); if (strcmp(pname, playlist_name) == 0) { source_list = l; break; } } if (source_list == NULL) { fprintf(stderr, "Cannot find source list: %s\n", playlist_name); exit(1); } int nt = sp_playlist_num_tracks(source_list); i=0; for(; i < nt; i++) { sp_track *t = sp_playlist_track(source_list, i); sp_artist *a = sp_track_artist(t,0); // get first artist on track because I am lazy sp_album *al = sp_track_album(t); if (al == NULL || a == NULL) { return false; } } return true; }
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 ); }
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; }
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); }
/** * 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; }
static bool spiffify() { if (!validate_complete_load()) { return false; } sp_playlistcontainer *list_container = g_list_container; // check to see if all playlists have been loaded int pl_count = sp_playlistcontainer_num_playlists(list_container); int i=0; sp_playlist *source_list = NULL; sp_playlist *spiffify_list = NULL; char folder_name[256]; int spiffify_start_index = -1; int spiffify_end_index = -1; sp_uint64 spiffify_folder_id = -1; i=0; for(; i < pl_count; i++) { sp_playlist *l = sp_playlistcontainer_playlist(list_container, i); const char* pname = sp_playlist_name(l); if (strcmp(pname, playlist_name) == 0) { source_list = l; } sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container, i); if (type == SP_PLAYLIST_TYPE_START_FOLDER) { sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, i, folder_name, 256); if (error == SP_ERROR_OK) { if (strcmp(folder_name, SPIFFIFY_PLAYLIST_NAME) == 0) { spiffify_list = l; spiffify_start_index = i; spiffify_folder_id = sp_playlistcontainer_playlist_folder_id(list_container, i); } } } else if (type == SP_PLAYLIST_TYPE_END_FOLDER) { sp_uint64 id = sp_playlistcontainer_playlist_folder_id(list_container, i); if (id == spiffify_folder_id) { spiffify_end_index = i; } } } if (source_list == NULL) { fprintf(stderr, "Cannot find source list: %s\n", playlist_name); exit(1); } if (spiffify_list != NULL && spiffify_start_index != -1) { // smoke the list and start over every time.. it's just not worth the effort of inserting changes. int x=spiffify_end_index; for(; x >= spiffify_start_index; x--) { sp_playlistcontainer_remove_playlist(list_container, x); } spiffify_list = NULL; spiffify_start_index = -1; spiffify_end_index = -1; } pl_count = sp_playlistcontainer_num_playlists(list_container); // reset count. if (spiffify_list == NULL) { // make the Spiffify list for this user; sp_error error = sp_playlistcontainer_add_folder(list_container, pl_count, SPIFFIFY_PLAYLIST_NAME); if (error == SP_ERROR_OK) { spiffify_list = sp_playlistcontainer_playlist(list_container, pl_count); spiffify_start_index = pl_count; spiffify_end_index = spiffify_start_index+1; } if (spiffify_list == NULL) { fprintf(stderr, "Cannot create '%s' playlist\n", SPIFFIFY_PLAYLIST_NAME); exit(1); } } // iterate the source playlist int nt = sp_playlist_num_tracks(source_list); // allocate storage for tracks sp_track **tracks = (sp_track **) malloc(nt * sizeof(sp_track *)); i=0; for(; i < nt; i++) { sp_track *t = sp_playlist_track(source_list, i); tracks[i] = t; // store in the array } // :) mergesort(tracks, nt, sizeof(sp_track *), &compare_tracks); i=0; for(; i < nt; i++) { sp_track *t = tracks[i]; //sp_playlist_track(source_list, i); sp_artist *a = sp_track_artist(t,0); // get first artist on track because I am lazy // find artist folder inside our spiffify list bool haveartist = false; int j=spiffify_start_index; char artist[256]; if (a != NULL) { strcpy(artist, sp_artist_name(a)); } else { strcpy(artist, "Unknown Artist"); } for(; j < spiffify_end_index; j++) { sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, j, folder_name, 256); if (strcmp(artist, folder_name) == 0) { haveartist = true; break; } } sp_album *al = sp_track_album(t); while (al == NULL) { printf("Cannot find album or it's not loaded.. forcing an event loop\n"); return false; } if (!haveartist) { int artist_pos = spiffify_end_index; sp_playlistcontainer_add_folder(list_container, artist_pos, artist); spiffify_end_index+=2; // a folder adds 2 indexes. pl_count = sp_playlistcontainer_num_playlists(list_container); sp_playlist* album = sp_playlistcontainer_add_new_playlist(list_container, sp_album_name(al)); sp_playlist_add_tracks(album, &t, 1, 0, session); sp_playlistcontainer_move_playlist(list_container, pl_count, artist_pos+1, false); spiffify_end_index++; // one more for the album. } else { int artist_pos = j; sp_playlist *albumpl = find_album_playlist(list_container, &artist_pos, spiffify_end_index, al); if (albumpl == NULL) { pl_count = sp_playlistcontainer_num_playlists(list_container); albumpl = sp_playlistcontainer_add_new_playlist(list_container, sp_album_name(al)); sp_playlistcontainer_move_playlist(list_container, pl_count, artist_pos+1, false); spiffify_end_index++; // one more for the album. } sp_playlist_add_tracks(albumpl, &t, 1, 0, session); } } free(tracks); return true; }
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; }
char *getTrackInfos( void ) { TRACE_2( PLAYERMANAGER , "getTrackInfo()."); char *buff = ( char * )zmalloc( 512 * sizeof( char ) ); LOCK_MUTEX( PLAYERMANAGER , &mutexSession ); //If the return of sp_track_name is a empty string, then an error occured if( strcmp( sp_track_name( currentTrack ) , "" ) == 0 ) { snprintf( buff , 512 , "Cannot get metadata from track."); } else { sprintf( buff , "%s , %s , %s" , sp_track_name( currentTrack ) , sp_artist_name( sp_track_artist( currentTrack , 0 ) ) , sp_album_name( sp_track_album( currentTrack ) ) ); } UNLOCK_MUTEX( PLAYERMANAGER , &mutexSession ); return buff; }