Exemplo n.º 1
0
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;

}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: delrtye/Spot
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);
}
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;
}
Exemplo n.º 5
0
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();
}
Exemplo n.º 6
0
// ----------------------------------------------------------------------------
//
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;
}
Exemplo n.º 7
0
bool QSpotifyAlbum::updateData()
{
    bool updated = false;

    bool isAvailable = sp_album_is_available(m_sp_album);
    sp_artist *a = sp_album_artist((m_sp_album));
    QString artist;
    if (a)
        artist = QString::fromUtf8(sp_artist_name(a));
    QString name = QString::fromUtf8(sp_album_name(m_sp_album));
    int year = sp_album_year(m_sp_album);
    Type type = Type(sp_album_type(m_sp_album));

    // Get cover
    const byte *album_cover_id = sp_album_cover(m_sp_album, SP_IMAGE_SIZE_NORMAL);
    if (album_cover_id != 0 && m_coverId.isEmpty()) {
        sp_link *link = sp_link_create_from_album_cover(m_sp_album, SP_IMAGE_SIZE_NORMAL);
        if (link) {
            char buffer[200];
            int uriSize = sp_link_as_string(link, &buffer[0], 200);
            m_coverId = QString::fromUtf8(&buffer[0], uriSize);
            sp_link_release(link);
            updated = true;
        }
    }

    if (isAvailable != m_isAvailable) {
        m_isAvailable = isAvailable;
        updated = true;
    }
    if (artist != m_artist) {
        m_artist = artist;
        updated = true;
    }
    if (name != m_name) {
        m_name = name;
        updated = true;
    }
    if (year != m_year) {
        m_year = year;
        updated = true;
    }
    if (type != m_type) {
        m_type = type;
        updated = true;
    }

    return updated;
}
Exemplo n.º 8
0
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!
}
Exemplo n.º 9
0
static sp_playlist *find_album_playlist(sp_playlistcontainer *list_container, int *spiffify_start_index, int spiffify_end_index, sp_album *al) 
{
	if (al == NULL) {
		return NULL;
	}
	int j=*spiffify_start_index;
	sp_artist *a = sp_album_artist(al);
	const char *artist = sp_artist_name(a);
	int artist_end = 0;
	sp_uint64 artist_folder_id = 0;
	for(; j < spiffify_end_index; j++) {
		sp_playlist *p = sp_playlistcontainer_playlist(list_container, j);
		if (p == NULL) {
			continue;
		}
		sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container,j);
		if (type == SP_PLAYLIST_TYPE_START_FOLDER) {
			char folder_name[256];
			sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, j, folder_name, 255);
			if (strcmp(folder_name, artist) == 0) {
				artist_folder_id = sp_playlistcontainer_playlist_folder_id(list_container, j);
			}
		}
		else if (type == SP_PLAYLIST_TYPE_END_FOLDER) {
			if (artist_folder_id == sp_playlistcontainer_playlist_folder_id(list_container, j)) {
				artist_end = j;
			}
		}
		const char *plname = sp_playlist_name(p);
		if (plname != NULL) {
			const char *alname = sp_album_name(al);
			if (strcmp(plname, alname) == 0) {
				return p;
			}
		}
	}
	// in the case where we don't yet have the playlist for this album and we are about to create it, send back the end of this artist folder
	if (artist_end != 0) {
		*spiffify_start_index = artist_end-1;
	}
	return NULL;
}
Exemplo n.º 10
0
/**
 * Print the given album browse result and as much information as possible
 *
 * @param  browse  The browse result
 */
static void print_albumbrowse(sp_albumbrowse *browse)
{
	int i;

	printf("Album browse of \"%s\" (%d)\n", 
	       sp_album_name(sp_albumbrowse_album(browse)), 
	       sp_album_year(sp_albumbrowse_album(browse)));

	for (i = 0; i < sp_albumbrowse_num_copyrights(browse); ++i)
		printf("  Copyright: %s\n", sp_albumbrowse_copyright(browse, i));

	printf("  Tracks: %d\n", sp_albumbrowse_num_tracks(browse));
	printf("  Review: %.60s...\n", sp_albumbrowse_review(browse));
	puts("");

	for (i = 0; i < sp_albumbrowse_num_tracks(browse); ++i)
		print_track(sp_albumbrowse_track(browse, i));

	puts("");
}
Exemplo n.º 11
0
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;
}
Exemplo n.º 12
0
/**
 * 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;
}
Exemplo n.º 13
0
static PyObject *
Album_name(Album * self)
{
    return PyUnicode_FromString(sp_album_name(self->_album));
}
Exemplo n.º 14
0
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;
}
	std::string Album::GetName()
	{
		return sp_album_name( m_pAlbum );
	}	
Exemplo n.º 16
0
static void print_album(int index, sp_album *album)
{
	printf("  Album %3d: \"%s\" by \"%s\"\n", index, sp_album_name(album), 
	       sp_artist_name(sp_album_artist(album)));
}
Exemplo n.º 17
0
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;
}
Exemplo n.º 18
0
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);
}
Exemplo n.º 19
0
/**
 * Print the given album metadata
 *
 * @param  album  The album object
 */
static void print_album(sp_album *album)
{
  printf("  Album \"%s\" (%d)\n",
         sp_album_name(album),
         sp_album_year(album));
}
Exemplo n.º 20
0
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;

}
Exemplo n.º 21
0
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 );
}