Beispiel #1
0
		internal AlbumBrowse(IntPtr albumBrowsePtr)
		{
			if(albumBrowsePtr == IntPtr.Zero)
				throw new ArgumentException("albumBrowsePtr can not be zero");
			
			lock(libspotify.Mutex)
			{
				IntPtr strPtr = IntPtr.Zero;
				
				error = libspotify.sp_albumbrowse_error(albumBrowsePtr);
				album = new Album(libspotify.sp_albumbrowse_album(albumBrowsePtr));
				artist = new Artist(libspotify.sp_albumbrowse_artist(albumBrowsePtr));
				
				copyrights = new string[libspotify.sp_albumbrowse_num_copyrights(albumBrowsePtr)];
				for(int i = 0; i < copyrights.Length; i++)
				{
					strPtr = libspotify.sp_albumbrowse_copyright(albumBrowsePtr, i);
					copyrights[i] = libspotify.GetString(strPtr, string.Empty);
				}
				
				tracks = new Track[libspotify.sp_albumbrowse_num_tracks(albumBrowsePtr)];
				for(int i = 0; i < tracks.Length; i++)
				{
					IntPtr trackPtr = libspotify.sp_albumbrowse_track(albumBrowsePtr, i);
					tracks[i] = new Track(trackPtr);
				}
				
				strPtr = libspotify.sp_albumbrowse_review(albumBrowsePtr);
                review = libspotify.GetString(strPtr, string.Empty);
				
				libspotify.sp_albumbrowse_release(albumBrowsePtr);
			}
		}
		internal TracksEventArgs(Track[] tracks, int[] trackIndices, int position, int newPosition, Track[] currentTracks)
		{
			this.tracks = tracks;
			this.trackIndices = trackIndices;
			this.position = position;
			this.newPosition = newPosition;
			this.currentTracks = currentTracks;
		}
        internal ArtistBrowse(IntPtr artistBrowsePtr)
        {
            if(artistBrowsePtr == IntPtr.Zero)
                throw new ArgumentException("artistBrowsePtr can not be zero");

            lock(libspotify.Mutex)
            {
                IntPtr strPtr = IntPtr.Zero;

                error = libspotify.sp_artistbrowse_error(artistBrowsePtr);
                artist = new Artist(libspotify.sp_artistbrowse_artist(artistBrowsePtr));

                portraitIds = new List<string>(libspotify.sp_artistbrowse_num_portraits(artistBrowsePtr));
                for(int i = 0; i < portraitIds.Count; i++)
                {
                    IntPtr portraitIdPtr = libspotify.sp_artistbrowse_portrait(artistBrowsePtr, i);
                    byte[] portraitId = new byte[20];
                    Marshal.Copy(portraitIdPtr, portraitId, 0, portraitId.Length);
                    portraitIds.Add(libspotify.ImageIdToString(portraitId));
                }

                tracks = new Track[libspotify.sp_artistbrowse_num_tracks(artistBrowsePtr)];
                for(int i = 0; i < tracks.Length; i++)
                {
                    IntPtr trackPtr = libspotify.sp_artistbrowse_track(artistBrowsePtr, i);
                    tracks[i] = new Track(trackPtr);
                }

                albums = new Album[libspotify.sp_artistbrowse_num_albums(artistBrowsePtr)];
                for(int i = 0; i < albums.Length; i++)
                {
                    IntPtr albumPtr = libspotify.sp_artistbrowse_album(artistBrowsePtr, i);
                    albums[i] = new Album(albumPtr);
                }

                similarArtists = new Artist[libspotify.sp_artistbrowse_num_similar_artists(artistBrowsePtr)];
                for(int i = 0; i < similarArtists.Length; i++)
                {
                    IntPtr artistPtr = libspotify.sp_artistbrowse_similar_artist(artistBrowsePtr, i);
                    similarArtists[i] = new Artist(artistPtr);
                }

                strPtr = libspotify.sp_albumbrowse_review(artistBrowsePtr);
                biography = libspotify.GetString(strPtr, string.Empty);

                libspotify.sp_artistbrowse_release(artistBrowsePtr);
            }
        }
Beispiel #4
0
		public static Track CreateFromLink(Link link)
		{
			Track result = null;
			
			if(link.linkPtr != IntPtr.Zero)
			{
				lock(libspotify.Mutex)
				{
					IntPtr trackPtr = libspotify.sp_link_as_track(link.linkPtr);
					if(trackPtr != IntPtr.Zero)
						result = new Track(trackPtr);
				}
			}
			
			return result;
		}
Beispiel #5
0
        internal Search(IntPtr searchPtr)
        {
            if (searchPtr == IntPtr.Zero)
                throw new ArgumentException("searchPtr can not be zero");

			this.searchPtr = searchPtr;
			
            lock (libspotify.Mutex)
            {
                IntPtr strPtr = IntPtr.Zero;

                error = libspotify.sp_search_error(searchPtr);

                tracks = new Track[libspotify.sp_search_num_tracks(searchPtr)];
                for (int i = 0; i < tracks.Length; i++)
                {
                    IntPtr trackPtr = libspotify.sp_search_track(searchPtr, i);
                    tracks[i] = new Track(trackPtr);
                }

                albums = new Album[libspotify.sp_search_num_albums(searchPtr)];
                for (int i = 0; i < albums.Length; i++)
                {
                    IntPtr albumPtr = libspotify.sp_search_album(searchPtr, i);
                    albums[i] = new Album(albumPtr);
                }

                artists = new Artist[libspotify.sp_search_num_artists(searchPtr)];
                for (int i = 0; i < artists.Length; i++)
                {
                    IntPtr artistPtr = libspotify.sp_search_artist(searchPtr, i);
                    artists[i] = new Artist(artistPtr);
                }

                strPtr = libspotify.sp_search_query(searchPtr);
                query = libspotify.GetString(strPtr, string.Empty);

                strPtr = libspotify.sp_search_did_you_mean(searchPtr);
                didYouMean = libspotify.GetString(strPtr, string.Empty);

                totalTracks = libspotify.sp_search_total_tracks(searchPtr);                
            }
        }
Beispiel #6
0
		public static Track CreateFromLink(Link link, out int offset)
		{
			Track result = null;
			offset = 0;
			
			if(link.linkPtr != IntPtr.Zero)
			{
				IntPtr offsetPtr = IntPtr.Zero;
				
				lock(libspotify.Mutex)
				{
					IntPtr trackPtr = libspotify.sp_link_as_track_and_offset(link.linkPtr, out offsetPtr);
					if(trackPtr != IntPtr.Zero)
						result = new Track(trackPtr);
				}
				
				offset = offsetPtr.ToInt32();
			}
			
			return result;
		}
Beispiel #7
0
        internal void StreamTrackAsync(Track track)
        {
            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("StreamTrackAsync");
            #endif

            // Prepare to play the track.
            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("Playing track: " + track.Name);
            #endif

            // Configure the LAME MP3 encoder.
            LameEncoder = new LameProgram(track);

            // Reset the variable used to block until playback done.
            _playbackDone.Reset();

            // Load the track.
            sp_error loadResult = Session.PlayerLoad(track);
            #if DEBUG
            if (Log.IsDebugEnabled) Log.Debug("Load: " + loadResult);
            #endif

            // Play the track.
            sp_error playResult = Session.PlayerPlay(true);
            #if DEBUG
            if (Log.IsDebugEnabled) Log.Debug("Play: " + playResult);
            #endif
        }
Beispiel #8
0
        public void Load(string URI)
        {
            currentTrack = Track.CreateFromLink(Link.Create(URI));
             	SpotifySession.PlayerLoad(currentTrack);
            try
            {
                Status = "Loading '" + currentTrack.Name + " - " + currentTrack.Artists[0].Name + "' from Spotify..";
            }
            catch

            { }
        }
Beispiel #9
0
        private static void TracksMovedCallback(IntPtr playlistPtr, IntPtr trackIndicesPtr, int num_tracks, int new_position, IntPtr userDataPtr)
        {
            Playlist pl = GetPlaylist(playlistPtr);

            if(pl != null)
            {
                Track[] tracks;
                Track[] currentTracks;
                int[] indices;

                lock(libspotify.Mutex)
                {
                    currentTracks = pl.CurrentTracks;

                    tracks = new Track[num_tracks];
                    indices = new int[num_tracks];

                    for(int i = 0; i < num_tracks; i++)
                    {
                        int index = Marshal.ReadInt32(trackIndicesPtr, i);
                        indices[i] = index;
                        tracks[i] = currentTracks[index];
                    }
                }

                pl.owningSession.EnqueueEventWorkItem(new EventWorkItem(pl.OnTracksMoved,
                    new object[] {pl, new TracksEventArgs(tracks, indices, -1, new_position, currentTracks) }));
            }
        }
Beispiel #10
0
        private static void TracksAddedCallback(IntPtr playlistPtr, ref IntPtr tracksPtr, int num_tracks, int position, IntPtr userDataPtr)
        {
            Playlist pl = GetPlaylist(playlistPtr);

            if(pl != null)
            {
                Track[] tracks;
                Track[] currentTracks;
                int[] indices;

                lock(libspotify.Mutex)
                {
                    currentTracks = pl.CurrentTracks;
                    tracks = new Track[num_tracks];
                    indices = new int[num_tracks];

                    for (int i = 0; i < num_tracks; i++)
                    {
                        IntPtr trackPtr = libspotify.sp_playlist_track(playlistPtr, position + i);
                        Track t = new Track(trackPtr);
                        tracks[i] = t;
                        indices[i] = position + i;
                    }
                }

                pl.owningSession.EnqueueEventWorkItem(new EventWorkItem(pl.OnTracksAdded,
                    new object[] {pl, new TracksEventArgs(tracks, indices, position, -1, currentTracks) }));
            }
        }
Beispiel #11
0
        public sp_error AddTracks(Track[] tracks, int position)
        {
            sp_error result = sp_error.INVALID_INDATA;

            if(tracks != null && tracks.Length > 0)
            {
                lock(libspotify.Mutex)
                {
                    IntPtr arrayPtr = IntPtr.Zero;

                    try
                    {
                        if(position < 0)
                            position = 0;

                        if(position > TrackCount)
                            position = TrackCount;

                        int[] array = new int[tracks.Length];
                        for(int i = 0; i < tracks.Length; i++)
                        {
                            array[i] = tracks[i].trackPtr.ToInt32();
                        }

                        int size = Marshal.SizeOf(arrayPtr) * array.Length;
                        arrayPtr = Marshal.AllocHGlobal(size);
                        Marshal.Copy(array, 0, arrayPtr, array.Length);
                        result = libspotify.sp_playlist_add_tracks(playlistPtr, arrayPtr, array.Length, position, owningSession.sessionPtr);
                    }
                    finally
                    {
                        if(arrayPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(arrayPtr);
                        }
                    }
                }
            }

            return result;
        }
Beispiel #12
0
        internal sp_error PlayerLoad(Track track)
        {
            PlayerUnload();

            lock(libspotify.Mutex)
            {
                sp_error err = libspotify.sp_session_player_load(sessionPtr, track.trackPtr);
                if (err == sp_error.OK)
                    track.CheckLoaded();

                return err;
            }
        }
Beispiel #13
0
        internal LameProgram(Track spotifyTrack)
        {
            // We need the expected size to pad the result to match the inexact time
            // that is specified by Spotify for the track.
            _expectedSize = Program.GetExpectedBytesEncoded(spotifyTrack.Duration);
            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("Expected size = " + _expectedSize);
            #endif

            // Build up the arguments string.
            StringBuilder args = new StringBuilder();
            args.Append("-r "); // input is raw pcm
            args.Append("--cbr -b ").Append(BitRate).Append(' ');
            args.Append("- -"); // stdin & stdout

            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("Lame args: " + args);
            #endif

            // Prepare the output data queue.
            OutputQueue = new Queue<IAudioData>();

            // "lame.exe" or "lame"
            string lame = "lame.exe";
            if (!File.Exists(Program.ApplicationPath + @"\lame.exe"))
                lame = "lame";

            // Prepare to start the process.
            _process = new Process();
            _process.StartInfo.WorkingDirectory = Program.ApplicationPath + '\\';
            _process.StartInfo.FileName = lame;
            _process.StartInfo.Arguments = args.ToString();
            _process.StartInfo.UseShellExecute = false;
            _process.StartInfo.CreateNoWindow = true;
            _process.StartInfo.RedirectStandardInput = true;
            _process.StartInfo.RedirectStandardOutput = true;
            _process.StartInfo.RedirectStandardError = true;

            // Start the process.
            _process.Start();
            _process.StandardInput.AutoFlush = true;

            // Start reading from standard output.
            if (_process.StartInfo.RedirectStandardOutput)
            {
                _stdOutThread = new Thread(ReadStandardOutput);
                _stdOutThread.IsBackground = true;
                _stdOutThread.Start();
            }

            // Start reading from standard error.
            if (!_process.StartInfo.RedirectStandardError)
            {
                _stdErrThread = new Thread(ReadStandardError);
                _stdErrThread.IsBackground = true;
                _stdErrThread.Start();
            }

            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("LameProgram started.");
            #endif
        }
Beispiel #14
0
 static double CalculateScore(Track result, string artist, string track, string album)
 {
     double artistScore = SingleScore(artist.ToLower(), result.Artists[0].Name.ToLower());
     double trackScore = SingleScore(track.ToLower(), result.Name.ToLower());
     double albumScore = string.IsNullOrEmpty(album) ? 1.0 : SingleScore(album.ToLower(), result.Album.Name.ToLower());
     return (artistScore + trackScore + albumScore) / 3.0;
 }
Beispiel #15
0
		static void HandleOnSearchComplete(Session sender, SearchEventArgs e)
        {
            Console.WriteLine("Search returned:{0}{1}", Environment.NewLine, e.Result);
			
			if(e.Result.Tracks.Length > 0)
			{
				currentTrack = e.Result.Tracks[0];
				Console.WriteLine("Will play track:");
				Console.WriteLine(currentTrack.ToString());
				
				Console.WriteLine("Album:");
				Console.WriteLine(currentTrack.Album.ToString());
				
				Console.WriteLine("Load: " + sender.PlayerLoad(currentTrack));
				Console.WriteLine("Play: " + sender.PlayerPlay(true));
				if(sender.LoadImage(currentTrack.Album.CoverId, 12345))
					Console.WriteLine("Cover requested");
				else
					Console.WriteLine("Cover request failed");
				
			}
		}