ClientResponse* SpotifyPlaylistContainer::playPlaylist(PlaylistTask* task) {
	StatusResponse* response = new StatusResponse(false,
			"Playlist Id is out of range!");

	int playlistNum = task->getCommandInfo().playlist;
	if (playlistNum >= 0
			&& playlistNum
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				playlistNum);
		if (sp_playlist_num_tracks(playlist) > 0) {
			sp_track* track = sp_playlist_track(playlist, 0);
			spotifySession->getSpotifyPlayer()->clearPlayQueue();
			spotifySession->getSpotifyPlayer()->playTrack(track);
			for (int i = 1; i < sp_playlist_num_tracks(playlist); i++) {
				spotifySession->getSpotifyPlayer()->addTrackToQueue(
						sp_playlist_track(playlist, i));
			}
			response->setSuccess(true);
			response->setMessage(nullptr);
		} else {
			response->setMessage("Playlist does not have any tracks");
		}
	}

	return response;
}
示例#2
0
static void playlist_browse_try(void)
{
	int i, tracks;

	metadata_updated_fn = playlist_browse_try;
	if(!sp_playlist_is_loaded(playlist_browse))
		return;

	tracks = sp_playlist_num_tracks(playlist_browse);
	for(i = 0; i < tracks; i++) {
		sp_track *t = sp_playlist_track(playlist_browse, i);
		if (!sp_track_is_loaded(t))
			return;
	}

	printf("\tPlaylist and metadata loaded\n");

	for(i = 0; i < tracks; i++) {
		sp_track *t = sp_playlist_track(playlist_browse, i);
		
		printf(" %5d: ", i + 1);
		print_track(t);
	}
	sp_playlist_remove_callbacks(playlist_browse, &pl_callbacks, NULL);

	sp_playlist_release(playlist_browse);
	playlist_browse = NULL;
	metadata_updated_fn = NULL;
	cmd_done();
}
示例#3
0
void SxPlaylist::reLoad() {
    Logger::printOut("reload play");
    if (m_isValid && !isFolder()) {
        m_thumb = NULL;
        //TODO fix a thumb, why is it never returning any images?
        byte image[20];
        if (sp_playlist_get_image(m_spPlaylist, image)) {
            m_thumb = ThumbStore::getInstance()->getThumb(image);
        }
        Logger::printOut("reload play 3");

        vector<SxTrack*> newTracks;
        for (int index = 0; index < sp_playlist_num_tracks(m_spPlaylist); index++) {
            sp_track* spTrack = sp_playlist_track(m_spPlaylist, index);
            if (!sp_track_get_availability(Session::getInstance()->getSpSession(), spTrack)) continue;
            SxTrack* track = TrackStore::getInstance()->getTrack(spTrack);
            if (track) {
                newTracks.push_back(track);
                //no thumb, lets pick one from the track list
                if (m_thumb == NULL)
                    if (track->getThumb() != NULL)
                        //no need to add ref to the thumb, when the track disappears the playlist will switch thumb
                        m_thumb = track->getThumb();
            }
        }

        removeAllTracks();

        Logger::printOut("reload play done");
        m_tracks = newTracks;
    }
    Utils::updatePlaylist(m_index);
}
示例#4
0
int cmd_playlist(int argc, char **argv)
{
	int index, i;
	sp_track *track;
	sp_playlist *playlist;
	sp_playlistcontainer *pc = sp_session_playlistcontainer(g_session);

	if (argc < 1) {
		printf("playlist [playlist index]\n");
		return 0;
	}

	index = atoi(argv[1]);
	if (index < 0 || index > sp_playlistcontainer_num_playlists(pc)) {
		printf("invalid index\n");
		return 0;
	}
	playlist = sp_playlistcontainer_playlist(pc, index);
	printf("Playlist %s by %s%s%s\n",
		   sp_playlist_name(playlist),
		   sp_user_display_name(sp_playlist_owner(playlist)),
		   sp_playlist_is_collaborative(playlist) ? " (collaborative)" : "",
		   sp_playlist_has_pending_changes(playlist) ? " with pending changes" : ""
		   );
	for (i = 0; i < sp_playlist_num_tracks(playlist); ++i) {
		track = sp_playlist_track(playlist, i);
		printf("%d. %c %s%s %s\n", i,
			   sp_track_is_starred(g_session, track) ? '*' : ' ',
			   sp_track_is_local(g_session, track) ? "local" : "     ",
			   sp_track_is_autolinked(g_session, track) ? "autolinked" : "          ",
			   sp_track_name(track));
	}
	return 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;
}
示例#6
0
/*
 * Add an entire playlist to the queue
 */
void queue_add_playlist(sp_playlist *playlist)
{
    int i = 0;
    
    for(i = 0; i < sp_playlist_num_tracks(playlist) - 1; i++) {
        queue_add(sp_playlist_track(playlist, i));
    }
}
示例#7
0
static void fill_track_tokens_from_playlist(struct track_tokens_t *src,
                                            sp_playlist *playlist) {
  int num_tracks = sp_playlist_num_tracks(playlist);
  init_track_tokens(src, num_tracks);

  for (int i = 0; i < num_tracks; i++) {
    sp_track *track = sp_playlist_track(playlist, i);
    sp_track_add_ref(track);
    src->tracks[i] = track;
  }
}
ClientResponse* SpotifyPlaylistContainer::playTrack(PlaylistTask* task) {
	StatusResponse* response = new StatusResponse(false,
			"Playlist Id is out of range!");

	PlayTrackCommandInfo info = task->getCommandInfo().playTrackCommandInfo;
	if (info.playlistId > -1
			&& info.playlistId
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				info.playlistId);
		if (info.trackId > -1
				&& info.trackId < sp_playlist_num_tracks(playlist)) {
			spotifySession->getSpotifyPlayer()->clearPlayQueue();
			for (int i = 0; i < info.trackId; i++) {
				spotifySession->getSpotifyPlayer()->addTrackToPlayedQueue(
						sp_playlist_track(playlist, i));
			}

			sp_track* track = sp_playlist_track(playlist, info.trackId);
			spotifySession->getSpotifyPlayer()->playTrack(track);
			for (int i = info.trackId + 1; i < sp_playlist_num_tracks(playlist);
					i++) {
				spotifySession->getSpotifyPlayer()->addTrackToQueue(
						sp_playlist_track(playlist, i));
			}
            if(spotifySession->getSpotifyPlayer()->isShuffled()) {
                spotifySession->getSpotifyPlayer()->setShuffle(true);
            }
			response->setSuccess(true);
			response->setMessage(nullptr);
		} else {
			response->setMessage("Track does not exist!");
		}
	}

	return response;
}
示例#9
0
// ----------------------------------------------------------------------------
//
TrackArray SpotifyEngine::getTracks( sp_playlist* pl ) {
    TrackArray tracks;

    int num_tracks = sp_playlist_num_tracks( pl );

    for (int i=0; i < num_tracks; i++ ) {
        sp_track* track = sp_playlist_track( pl, i );

        sp_track_availability availability = sp_track_get_availability( m_spotify_session, track );

        if ( availability == SP_TRACK_AVAILABILITY_AVAILABLE )
            tracks.push_back( track );
    }

    return tracks;
}
示例#10
0
/**
 * Called on various events to start playback if it hasn't been started already.
 *
 * The function simply starts playing the first track of the playlist.
 */
static void try_jukebox_start(void)
{
	sp_track *t;

	if (!g_jukeboxlist)
		return;

	if (!sp_playlist_num_tracks(g_jukeboxlist)) {
		fprintf(stderr, "jukebox: No tracks in playlist. Waiting\n");
		return;
	}

	if (sp_playlist_num_tracks(g_jukeboxlist) < g_track_index) {
		fprintf(stderr, "jukebox: No more tracks in playlist. Waiting\n");
		return;
	}

	t = sp_playlist_track(g_jukeboxlist, g_track_index);

	if (g_currenttrack && t != g_currenttrack) {
		/* Someone changed the current track */
		audio_fifo_flush(&g_audiofifo);
		sp_session_player_unload(g_sess);
		g_currenttrack = NULL;
	}

	if (!t)
		return;

	if (sp_track_error(t) != SP_ERROR_OK)
		return;

	if (g_currenttrack == t)
		return;

	g_currenttrack = t;

	printf("jukebox: Now playing \"%s\"...\n", sp_track_name(t));
	fflush(stdout);

	sp_session_player_load(g_sess, t);
	sp_session_player_play(g_sess, 1);
}
示例#11
0
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;
}
int
sp_playlistcontainer_get_unseen_tracks(sp_playlistcontainer *pc, sp_playlist *_playlist, sp_track **tracks, int num_tracks)
{
  sp_playlistcontainer_playlist_t *playlist = sp_mock_playlistcontainer_find_playlist(pc, _playlist);
  bool seen = false;
  int unseen_count = 0, i = 0, j = 0;

  if ( ! playlist)
  {
    return -1;
  }

  for (i = 0; i < sp_playlist_num_tracks(_playlist); ++i)
  {
    sp_track *track = sp_playlist_track(_playlist, i);
    seen = false;

    if ( ! track)
    {
      continue;
    }

    for (j = 0; j < playlist->num_seen_tracks; ++j)
    {
      if (playlist->seen_tracks[j] == track)
      {
        seen = true;
      }
    }

    if ( ! seen)
    {
      if (unseen_count < num_tracks)
      {
        tracks[unseen_count] = track;
      }

      unseen_count += 1;
    }
  }

  return unseen_count;
}
示例#13
0
void search_for_tracks_at(sp_session *session, char *buf, size_t len, size_t i, bool (*f)(sp_track *))
{
	if(search == NULL)
	{
		return;
	}
	buf[0] = '\0';
	if(search_is_track(i) && sp_track_is_loaded(sp_search_track(search, i)))
	{
		f(sp_search_track(search, i));
		if(buf != NULL)
		{
			track_to_str(buf, len, sp_search_track(search, i));
		}
	}
	if(search_is_album(i) && sp_album_is_loaded(sp_search_album(search, i-sp_search_num_tracks(search))))
	{
		sp_albumbrowse_create(session, sp_search_album(search, i-sp_search_num_tracks(search))
				,on_albumbrowse_complete, f);
		if(buf != NULL)
		{
			album_to_str(buf, len, sp_search_album(search, i-sp_search_num_tracks(search)));
		}
	}
	if(search_is_playlist(i))
	{
		sp_playlist *pl = sp_search_playlist(search, 
					i-sp_search_num_tracks(search)-sp_search_num_albums(search));
		if(sp_playlist_is_loaded(pl))
		{
			int j;
			for(j = 0; j < sp_playlist_num_tracks(pl); ++j)
			{
				f(sp_playlist_track(pl, j));
			}
			if(buf != NULL)
			{
				playlist_to_str(buf, len, pl);
			}
		}
	}
}
示例#14
0
文件: spotify.c 项目: theodoor/spop
/*********************
 * Tracks management *
 *********************/
GArray* tracks_get_playlist(sp_playlist* pl) {
    GArray* tracks;
    sp_track* tr;
    int i, n;

    if (!sp_playlist_is_loaded(pl))
        return NULL;

    n = sp_playlist_num_tracks(pl);
    tracks = g_array_sized_new(FALSE, FALSE, sizeof(sp_track*), n);
    if (!tracks)
        g_error("Can't allocate array of %d tracks.", n);

    for (i=0; i < n; i++) {
        tr = sp_playlist_track(pl, i);
        sp_track_add_ref(tr);
        g_array_append_val(tracks, tr);
    }

    return tracks;
}
示例#15
0
void Spotify::tryLoadPlaylist()
{
    if ((currentPlaylistIdx < 0) || (currentPlaylist == 0)) {
        return;
    }

    if (sp_playlist_is_loaded(currentPlaylist)) {
        QList<SpotifyTrackInfo> tracks;

        int numTracks = sp_playlist_num_tracks(currentPlaylist);
        for (int idx = 0; idx < numTracks; ++idx) {
            sp_track * track = sp_playlist_track(currentPlaylist, idx);
            if (!track) {
                fprintf(stderr, "Spotify: failed to get track #%d\n", idx + 1);
                continue;
            }

            QString name(sp_track_name(track));
            if (name.isEmpty()) {
                fprintf(stderr, "Spotify: got empty track name\n");
                continue;
            }

            SpotifyTrackInfo info;
            info.name = name;
            info.URI = uriFromTrack(track);
            if (info.URI.isEmpty()) {
                continue;
            }

            tracks.append(info);
        }

        emit currentPlaylistUpdated(tracks);

        fprintf(stderr, "Spotify: loaded playlist %s (%d tracks)\n",
                sp_playlist_name(currentPlaylist),
                tracks.size());
    }
}
示例#16
0
bool XplodifyPlaylist::load_tracks() {
    int n;

    if(m_loading) {
        return false;
    }

    boost::shared_ptr<XplodifySession> sess(m_session.lock());

    n = sp_playlist_num_tracks(m_playlist);
    lock();
    for(int i=0 ; i<n ; i++) {
        sp_track * t = sp_playlist_track(m_playlist, i);

        track_cache_by_rand& tr_cache_rand = m_track_cache.get<0>();
        std::pair<track_r_iterator, bool> p;

        boost::shared_ptr<XplodifyTrack> tr(new XplodifyTrack(sess));
        if(tr->load(t)){
            std::string trname(tr->get_name());
            track_entry tr_entry(trname, tr);
            p = tr_cache_rand.push_back(tr_entry);
            if(p.second) {
#ifdef _DEBUG
                std::cout << "Track " << tr->get_name() << " loaded for playlist "
                    << get_name() << std::endl;
#endif
            } else {
                m_pending_tracks.push_back(tr);
            }
        } else {
            m_pending_tracks.push_back(tr);
        }
    }
    sess->update_state_ts();
    unlock();
    return true;
}
void SpotifyGuiController::RefreshPlaylistTracks ( sp_playlist *plist )
{
	if ( plist && plist != selected_playlist )
	{

		if ( trackTree ) trackTree->removeAllItem();

		SpotifyUserData *sp_data = (SpotifyUserData *)spotify_userdata;

		//ho la playlist, carico le tracce
		sp_playlist_add_callbacks(plist, sp_data->playlist_cb, this);

		if ( sp_playlist_is_loaded(plist) )
		{
			//aggiungo tracce
			int n_tracks = sp_playlist_num_tracks(plist);

			for ( int nt = 0 ; nt < n_tracks ; nt++ )
			{
				sp_track *track = sp_playlist_track(plist, nt);

				if ( track && sp_track_is_loaded(track) )
				{
					if ( trackTree )
					{
						TCHAR trackname[1001] = {0};
						memset(trackname, 0, sizeof(TCHAR)*1001);

						_sntprintf_s(trackname, 1000, TEXT("%hs"), sp_track_name(track));
						trackTree->addItemAsLast(NULL, trackname, track, 0);
					}
				}
			}
		}

	}

}
int
sp_playlistcontainer_clear_unseen_tracks(sp_playlistcontainer *pc, sp_playlist *_playlist)
{
  sp_playlistcontainer_playlist_t *playlist = sp_mock_playlistcontainer_find_playlist(pc, _playlist);
  int i = 0;

  if ( ! playlist)
  {
    return -1;
  }

  playlist->num_seen_tracks = 0;
  xfree(playlist->seen_tracks);

  playlist->num_seen_tracks = sp_playlist_num_tracks(_playlist);
  playlist->seen_tracks = ALLOC_N(sp_track *, playlist->num_seen_tracks);

  for (i = 0; i < playlist->num_seen_tracks; ++i)
  {
    playlist->seen_tracks[i] = sp_playlist_track(_playlist, i);
  }

  return 0;
}
示例#19
0
void append_playlist_tracks(sp_playlist *playlist, svn_stringbuf_t *buf) {
  int num_tracks = sp_playlist_num_tracks(playlist);

  for (int i = 0; i < num_tracks; i++)
    append_track(sp_playlist_track(playlist, i), buf);
}
示例#20
0
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;

}
示例#21
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;
}
示例#22
0
static void try_jukebox_start(void)
{
	sp_track *t;

	printf("try_jukebox_start\n");

	if (!g_jukeboxlist)
		return;

	int nt = sp_playlist_num_tracks(g_jukeboxlist);

	if (!nt) {
		fprintf(stderr, "jukebox: No tracks in playlist. Waiting\n");
		return;
	}

	if (nt > 2) {
		int tmp = g_track_index;
		while (tmp == g_track_index)
			tmp = rand() % nt;
		g_track_index = tmp;
	} else {
		g_track_index = rand() % nt;
	}

	printf("play track %d of %d\n", g_track_index, nt);

	t = sp_playlist_track(g_jukeboxlist, g_track_index);
	printf("t=%X\n", t);
	if (g_currenttrack && t != g_currenttrack) {
		printf("stopping currently playing..\n");
		/* Someone changed the current track */
		// audio_fifo_flush(&g_musicfifo);
		// sp_session_player_unload(g_sess);
		// g_currenttrack = NULL;
	}

	if (!t)
		return;

	int err = sp_track_error(t);
	if (err != SP_ERROR_OK /*&& err != SP_ERROR_IS_LOADING*/) {
		printf("track error? %d\n", err);
		return;
	}

	if (g_currenttrack == t) {
		printf("not starting the same track.\n");
		return;
	}

	g_currenttrack = t;

	printf("jukebox: Now playing \"%s\"...\n", sp_track_name(t));

	sp_artist *art = sp_track_artist(t, 0);
	if (art != NULL) {
		printf("jukebox: By \"%s\"...\n", sp_artist_name(art));
		sprintf( g_last_track_name, "%s by %s", sp_track_name(t), sp_artist_name(art) );
	} else {
		sprintf( g_last_track_name, "%s", sp_track_name(t) );
	}



	fflush(stdout);

	sp_session_player_load(g_sess, t);
	usleep(100000);
	sp_session_player_play(g_sess, 1);
	usleep(100000);
	g_is_playing = 1;

	notify_main_thread(g_sess);

	printf("after try_jukebox_start\n");
	tuner_debug();
}
示例#23
0
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;
}