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; }
/** * 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); }
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); }
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; }
static PyObject * Track_index(Track * self) { return Py_BuildValue("i", sp_track_index(self->_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 ); }