[Muine] [PATCH] Different kind of random mode



Hi all,

Awhile back, I posted a patch to implement a random mode for muine [1].

Since then, the shuffle feature has been added, however, it does not quite satisfy my personal tastes as once the playlist has been shuffled, I know which song will come next. The attached patch implements my old version of random mode for CVS HEAD.

I don't expect that this will be pushed into mainline, but I thought that I'd pass it along again in case anyone's interested.

Thanks,

--
Dave Barry
dave psax org


1. http://mail.gnome.org/archives/muine-list/2004-August/msg00027.html
? random.diff
? libmuine/.deps
? libmuine/.libs
? libmuine/db.lo
? libmuine/gsequence.lo
? libmuine/libmuine.la
? libmuine/metadata.lo
? libmuine/mm-keys.lo
? libmuine/ogg-helper.lo
? libmuine/player-gst.lo
? libmuine/pointer-list-model.lo
? libmuine/rb-cell-renderer-pixbuf.lo
? libmuine/id3-vfs/.deps
? libmuine/id3-vfs/.libs
? libmuine/id3-vfs/id3-vfs.lo
? libmuine/id3-vfs/libid3-vfs.la
? libmuine/id3-vfs/mp3bitrate.lo
? plugins/.deps
? plugins/.libs
? plugins/inotify-glue.lo
? plugins/libinotifyglue.la
? po/stamp-it
Index: data/ui/PlaylistWindow.xml
===================================================================
RCS file: /cvs/gnome/muine/data/ui/PlaylistWindow.xml,v
retrieving revision 1.6
diff -u -r1.6 PlaylistWindow.xml
--- data/ui/PlaylistWindow.xml	4 May 2005 19:05:43 -0000	1.6
+++ data/ui/PlaylistWindow.xml	2 Apr 2006 07:43:53 -0000
@@ -31,6 +31,7 @@
       <separator />
       <menuitem action="ToggleRepeat" />
       <menuitem action="Shuffle" />
+      <menuitem action="ToggleRandom" />
     </menu>
     <menu action="HelpMenu">
       <menuitem action="About" />
Index: src/Actions.cs
===================================================================
RCS file: /cvs/gnome/muine/src/Actions.cs,v
retrieving revision 1.17
diff -u -r1.17 Actions.cs
--- src/Actions.cs	21 Mar 2006 07:04:26 -0000	1.17
+++ src/Actions.cs	2 Apr 2006 07:43:53 -0000
@@ -94,6 +94,9 @@
 
 		private static readonly string string_toggle_repeat =
 			Catalog.GetString ("R_epeat");
+		
+		private static readonly string string_toggle_random =
+			Catalog.GetString ("Ran_dom");
 
 		private static readonly string string_shuffle =
 			Catalog.GetString ("Shu_ffle");
@@ -177,6 +180,9 @@
 
 			new ToggleActionEntry ("ToggleVisible", null, string_toggle_visible,
 				"Escape", null, null, true),
+			
+			new ToggleActionEntry ("ToggleRandom", null, string_toggle_random,
+				"<control>D", null, null, false),
 
 		};
 
@@ -240,6 +246,7 @@
                         this ["About"        ].Activated += new EventHandler (OnAbout        );
                         this ["TogglePlay"   ].Activated += new EventHandler (OnTogglePlay   );
                         this ["ToggleRepeat" ].Activated += new EventHandler (OnToggleRepeat );
+                        this ["ToggleRandom" ].Activated += new EventHandler (OnToggleRandom );
 		}
 
 		// Properties
@@ -625,6 +632,30 @@
 				return;
 
 			Global.Playlist.Repeat = a.Active;
+		}
+		
+		// Handlers :: OnToggleRandom
+		/// <summary>
+		/// 	Handler called when the ToggleRandom action is activated.
+		/// </summary>
+		/// <remarks>
+		///	This sets <see cref="PlaylistWindow.Random" /> to the
+		///	state of the ToggleRandom action.
+		/// </remarks>
+		/// <param name="o">
+		///	The calling object.
+		/// </param>
+		/// <param name="args">
+		///	The <see cref="EventArgs" />.
+		/// </param>
+		private void OnToggleRandom (object o, EventArgs args)
+		{
+			ToggleAction a = (ToggleAction) o;
+
+			if (a.Active == Global.Playlist.Random)
+				return;
+
+			Global.Playlist.Random = a.Active;
 		}
 	}
 }
Index: src/HandleModel.cs
===================================================================
RCS file: /cvs/gnome/muine/src/HandleModel.cs,v
retrieving revision 1.5
diff -u -r1.5 HandleModel.cs
--- src/HandleModel.cs	15 Mar 2005 10:11:13 -0000	1.5
+++ src/HandleModel.cs	2 Apr 2006 07:43:54 -0000
@@ -30,6 +30,8 @@
 		// Events
 		public delegate void PlayingChangedHandler (IntPtr handle);
 		public event         PlayingChangedHandler PlayingChanged;
+		private Random rand;
+		private RecentList recent;
 
 		// Delegates
 		// Delegates :: Public
@@ -42,7 +44,11 @@
 		[DllImport("libmuine")]
 		private static extern IntPtr pointer_list_model_new ();
 
-		public HandleModel () : base (pointer_list_model_new ()) {}
+		public HandleModel () : base (pointer_list_model_new ()) 
+		{
+			rand = new Random ();
+			recent = new RecentList (20,20);
+		}
 
 		// Destructor
 		~HandleModel ()
@@ -307,6 +313,45 @@
 				PlayingChanged (ret);
 
 			return ret;
+		}
+		
+		// Methods :: Public :: Random
+		public IntPtr Random ()
+		{
+			if (this.Length == 1)
+				return pointer_list_model_first (Raw);
+			
+			recent.Trim(this.Length);
+			IntPtr target = new System.IntPtr((int)this.Contents[rand.Next() % this.Length]);
+
+			if (this.Playing == target || recent.Contains(target)) {
+				//recurse until we find one
+				target = Random ();
+			}
+			else {
+				recent.Add(target);
+				this.Playing = target;
+			}
+
+			return (target);
+		}
+
+		internal class RecentList : ArrayList
+		{
+			private int trimCeiling = 0;
+
+			public RecentList (int len, int ceiling) : base (len) 
+			{
+				trimCeiling = ceiling;
+			}
+
+			public void Trim(int length)
+			{
+				if (this.Count >= trimCeiling || this.Count >= length) {
+					this.RemoveAt(0);
+					this.TrimToSize();
+				}
+			}
 		}
 
 		// Internal Classes
Index: src/PlaylistWindow.cs
===================================================================
RCS file: /cvs/gnome/muine/src/PlaylistWindow.cs,v
retrieving revision 1.244
diff -u -r1.244 PlaylistWindow.cs
--- src/PlaylistWindow.cs	21 Mar 2006 08:26:06 -0000	1.244
+++ src/PlaylistWindow.cs	2 Apr 2006 07:43:55 -0000
@@ -62,6 +62,9 @@
 
 		private const string GConfKeyRepeat = "/apps/muine/repeat";
 		private const bool   GConfDefaultRepeat = false;
+		
+		private const string GConfKeyRandom = "/apps/muine/random";
+		private const bool   GConfDefaultRandom = false;
 
 		// Strings
 		private static readonly string string_program = 
@@ -75,6 +78,9 @@
 
 		private static readonly string string_playlist_repeating =
 			Catalog.GetString ("<b>Playlist</b> (Repeating)");
+		
+		private static readonly string string_playlist_random =
+			Catalog.GetString ("<b>Playlist</b> (Random)");
 
 		private static readonly string string_playlist_under_minute =
 			Catalog.GetString ("<b>Playlist</b> (Less than one minute remaining)");
@@ -224,6 +230,7 @@
 		private Hashtable random_sort_keys;
 
 		private bool repeat;
+		private bool random;
 
 		// Constructor
 		public PlaylistWindow () : base (WindowType.Toplevel)
@@ -407,6 +414,21 @@
 
 			get { return repeat; }
 		}
+		
+      // Properties :: Random (set; get;)
+		public bool Random {
+			set {
+				random = value;
+
+				((ToggleAction) Global.Actions ["ToggleRandom"]).Active = value;
+
+				Config.Set (GConfKeyRandom, value);			
+            
+				PlaylistChanged ();
+			}
+
+			get { return random; }
+		}
 
 		// Methods
 		// Methods :: Public
@@ -527,7 +549,9 @@
 		// Methods :: Public :: Next (IPlayer)
 		public void Next ()
 		{
-			if (playlist.Model.HasNext)
+			if (random)
+				playlist.Model.Random ();
+			else if (playlist.Model.HasNext)
 				playlist.Model.Next ();
 
 			else if (repeat && playlist.Model.HasFirst)
@@ -1092,7 +1116,7 @@
 
 			previous_button   .Sensitive = has_first;
 			toggle_play_button.Sensitive = has_first;
-			next_button       .Sensitive = playlist.Model.HasNext || (this.repeat && has_first);
+			next_button       .Sensitive = playlist.Model.HasNext || (this.repeat && has_first) || (this.random && has_first);
 
 			Global.Actions ["TogglePlay"   ].Sensitive = previous_button   .Sensitive;
 			Global.Actions ["Previous"     ].Sensitive = toggle_play_button.Sensitive;
@@ -1353,8 +1377,10 @@
 		// Methods :: Private :: EndOfStream
 		private void EndOfStream (Song song, bool update_time)
 		{
+			if (random) {
+				playlist.Model.Random ();
 			// If we can, go to the next song
-			if (playlist.Model.HasNext) {
+			} else if (playlist.Model.HasNext) {
 				playlist.Model.Next ();
 
 			// If we don't have another song and we are repeating,
@@ -1430,6 +1456,13 @@
 
 			Config.AddNotify (GConfKeyRepeat,
 				new GConf.NotifyEventHandler (OnConfigRepeatChanged));
+
+         // Random
+			Random = (bool) Config.Get (GConfKeyRandom, GConfDefaultRandom);
+
+			Config.AddNotify (GConfKeyRepeat,
+				new GConf.NotifyEventHandler (OnConfigRandomChanged));
+
 		}
 
 		// Handlers
@@ -1666,6 +1699,18 @@
 
 			this.Repeat = val;
 		}
+		
+		private void OnConfigRandomChanged (object o, GConf.NotifyEventArgs args)
+		{
+			// Get new repeat setting from GConf
+			bool val = (bool) args.Value;
+
+			// If it changed, update.
+			if (val == this.random)
+				return;
+
+			this.Random = val;
+		}     
 
 		// Handlers :: OnPlaylistRowActivated
 		private void OnPlaylistRowActivated (object o, RowActivatedArgs args)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]