summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/onplay.c3
-rw-r--r--apps/tagtree.c58
-rw-r--r--apps/tree.c9
3 files changed, 46 insertions, 24 deletions
diff --git a/apps/onplay.c b/apps/onplay.c
index 045af275bc..ab507f08ac 100644
--- a/apps/onplay.c
+++ b/apps/onplay.c
@@ -1294,7 +1294,8 @@ int onplay(char* file, int attr, int from_context, bool hotkey, int customaction
1294#else 1294#else
1295 (void)hotkey; 1295 (void)hotkey;
1296#endif 1296#endif
1297 if (customaction == ONPLAY_CUSTOMACTION_SHUFFLE_SONGS) { 1297 if (customaction == ONPLAY_CUSTOMACTION_SHUFFLE_SONGS)
1298 {
1298 int returnCode = add_to_playlist(&addtopl_replace_shuffled); 1299 int returnCode = add_to_playlist(&addtopl_replace_shuffled);
1299 if (returnCode == 1) 1300 if (returnCode == 1)
1300 // User did not want to erase his current playlist, so let's show again the database main menu 1301 // User did not want to erase his current playlist, so let's show again the database main menu
diff --git a/apps/tagtree.c b/apps/tagtree.c
index 4a0bff32bd..8f6b1419a9 100644
--- a/apps/tagtree.c
+++ b/apps/tagtree.c
@@ -2131,24 +2131,28 @@ int tagtree_get_custom_action(struct tree_context* c)
2131 return tagtree_get_entry(c, c->selected_item)->customaction; 2131 return tagtree_get_entry(c, c->selected_item)->customaction;
2132} 2132}
2133 2133
2134static void swap_array_bool(bool *a, bool *b) { 2134static void swap_array_bool(bool *a, bool *b)
2135{
2135 bool temp = *a; 2136 bool temp = *a;
2136 *a = *b; 2137 *a = *b;
2137 *b = temp; 2138 *b = temp;
2138} 2139}
2139 2140
2140/** 2141/**
2141 * Randomly shuffle an array using the Fisher-Yates algorithm : https://en.wikipedia.org/wiki/Random_permutation 2142 * Randomly shuffle an array using the Fisher-Yates algorithm : https://en.wikipedia.org/wiki/Random_permutation
2142 * This algorithm has a linear complexity. Don't forget to srand before call to use it with a relevant seed. 2143 * This algorithm has a linear complexity. Don't forget to srand before call to use it with a relevant seed.
2143 */ 2144 */
2144static void shuffle_bool_array(bool array[], int size) { 2145static void shuffle_bool_array(bool array[], int size)
2145 for (int i = size - 1; i > 0; i--) { 2146{
2147 for (int i = size - 1; i > 0; i--)
2148 {
2146 int j = rand() % (i + 1); 2149 int j = rand() % (i + 1);
2147 swap_array_bool(&array[i], &array[j]); 2150 swap_array_bool(&array[i], &array[j]);
2148 } 2151 }
2149} 2152}
2150 2153
2151static bool fill_selective_random_playlist_indexes(int current_segment_n, int current_segment_max_available_space) { 2154static bool fill_selective_random_playlist_indexes(int current_segment_n, int current_segment_max_available_space)
2155{
2152 if (current_segment_n == 0 || current_segment_max_available_space == 0) 2156 if (current_segment_n == 0 || current_segment_max_available_space == 0)
2153 return false; 2157 return false;
2154 if (current_segment_max_available_space > current_segment_n) 2158 if (current_segment_max_available_space > current_segment_n)
@@ -2212,9 +2216,11 @@ static bool insert_all_playlist(struct tree_context *c,
2212 bool will_exceed = n > playlist_get_current()->max_playlist_size; 2216 bool will_exceed = n > playlist_get_current()->max_playlist_size;
2213 fill_randomly = will_exceed; 2217 fill_randomly = will_exceed;
2214 } 2218 }
2215 if (leftovers_segment_size > 0 && fill_randomly) { 2219 if (leftovers_segment_size > 0 && fill_randomly)
2216 // We need to re-balance the segments so the randomness will be coherent and balanced the same through all segments 2220 {
2217 while (leftovers_segment_size + segments_count < segment_size) { 2221 /* We need to re-balance the segments so the randomness will be coherent and balanced the same through all segments */
2222 while (leftovers_segment_size + segments_count < segment_size)
2223 {
2218 segment_size--; // -1 to all other segments 2224 segment_size--; // -1 to all other segments
2219 leftovers_segment_size += segments_count; 2225 leftovers_segment_size += segments_count;
2220 } 2226 }
@@ -2223,14 +2229,19 @@ static bool insert_all_playlist(struct tree_context *c,
2223 segments_count += 1; 2229 segments_count += 1;
2224 int max_available_space = playlist_get_current()->max_playlist_size - playlist_get_current()->amount; 2230 int max_available_space = playlist_get_current()->max_playlist_size - playlist_get_current()->amount;
2225 int max_available_space_per_segment = max_available_space / segments_count; 2231 int max_available_space_per_segment = max_available_space / segments_count;
2226 if (fill_randomly) { 2232 if (fill_randomly)
2233 {
2227 talk_id(LANG_RANDOM_SHUFFLE_RANDOM_SELECTIVE_SONGS_SUMMARY, true); 2234 talk_id(LANG_RANDOM_SHUFFLE_RANDOM_SELECTIVE_SONGS_SUMMARY, true);
2228 splashf(HZ * 3, str(LANG_RANDOM_SHUFFLE_RANDOM_SELECTIVE_SONGS_SUMMARY), max_available_space_per_segment * segments_count); 2235 splashf(HZ * 3, str(LANG_RANDOM_SHUFFLE_RANDOM_SELECTIVE_SONGS_SUMMARY),
2229 //splashf(HZ * 5, "sz=%d lsz=%d sc=%d rcps=%d", segment_size, leftovers_segment_size, segments_count, max_available_space_per_segment); 2236 max_available_space_per_segment * segments_count);
2237 /* logf("sz=%d lsz=%d sc=%d rcps=%d", segment_size, leftovers_segment_size,
2238 segments_count, max_available_space_per_segment); */
2230 } 2239 }
2231 for (int i = 0; i < segments_count; i++) { 2240 for (int i = 0; i < segments_count; i++)
2241 {
2232 bool is_leftovers_segment = leftovers_segment_size > 0 && i + 1 >= segments_count; 2242 bool is_leftovers_segment = leftovers_segment_size > 0 && i + 1 >= segments_count;
2233 if (fill_randomly) { 2243 if (fill_randomly)
2244 {
2234 if (is_leftovers_segment) 2245 if (is_leftovers_segment)
2235 fill_randomly = fill_selective_random_playlist_indexes(leftovers_segment_size, max_available_space_per_segment); 2246 fill_randomly = fill_selective_random_playlist_indexes(leftovers_segment_size, max_available_space_per_segment);
2236 else 2247 else
@@ -2243,12 +2254,15 @@ static bool insert_all_playlist(struct tree_context *c,
2243 cur_segment_end = cur_segment_start + leftovers_segment_size; 2254 cur_segment_end = cur_segment_start + leftovers_segment_size;
2244 else 2255 else
2245 cur_segment_end = cur_segment_start + segment_size; 2256 cur_segment_end = cur_segment_start + segment_size;
2246 for (int j = cur_segment_start; j < cur_segment_end && !exit_loop_now; j++) { 2257 for (int j = cur_segment_start; j < cur_segment_end && !exit_loop_now; j++)
2258 {
2247 if (fill_randomly && !selective_random_playlist_indexes[j % segment_size]) 2259 if (fill_randomly && !selective_random_playlist_indexes[j % segment_size])
2248 continue; 2260 continue;
2249 splash_progress(j, n, "%s (%s)", str(LANG_WAIT), str(LANG_OFF_ABORT)); 2261 splash_progress(j, n, "%s (%s)", str(LANG_WAIT), str(LANG_OFF_ABORT));
2250 if (TIME_AFTER(current_tick, last_tick + HZ/4)) { 2262 if (TIME_AFTER(current_tick, last_tick + HZ/4))
2251 if (action_userabort(TIMEOUT_NOBLOCK)) { 2263 {
2264 if (action_userabort(TIMEOUT_NOBLOCK))
2265 {
2252 exit_loop_now = true; 2266 exit_loop_now = true;
2253 break; 2267 break;
2254 } 2268 }
@@ -2256,13 +2270,16 @@ static bool insert_all_playlist(struct tree_context *c,
2256 } 2270 }
2257 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, j)->extraseek, tcs.type, buf, sizeof buf)) 2271 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, j)->extraseek, tcs.type, buf, sizeof buf))
2258 continue; 2272 continue;
2259 if (playlist == NULL) { 2273 if (playlist == NULL)
2274 {
2260 if (playlist_insert_track(NULL, buf, position, queue, false) < 0) { 2275 if (playlist_insert_track(NULL, buf, position, queue, false) < 0) {
2261 logf("playlist_insert_track failed"); 2276 logf("playlist_insert_track failed");
2262 exit_loop_now = true; 2277 exit_loop_now = true;
2263 break; 2278 break;
2264 } 2279 }
2265 } else if (fdprintf(fd, "%s\n", buf) <= 0) { 2280 }
2281 else if (fdprintf(fd, "%s\n", buf) <= 0)
2282 {
2266 exit_loop_now = true; 2283 exit_loop_now = true;
2267 break; 2284 break;
2268 } 2285 }
@@ -2445,10 +2462,11 @@ static int tagtree_play_folder(struct tree_context* c)
2445 2462
2446 int n = c->filesindir; 2463 int n = c->filesindir;
2447 bool has_playlist_been_randomized = n > playlist_get_current()->max_playlist_size; 2464 bool has_playlist_been_randomized = n > playlist_get_current()->max_playlist_size;
2448 if (has_playlist_been_randomized) { 2465 if (has_playlist_been_randomized)
2466 {
2449 /* We need to recalculate the start index based on a percentage to put the user 2467 /* We need to recalculate the start index based on a percentage to put the user
2450 around its desired start position and avoid out of bounds */ 2468 around its desired start position and avoid out of bounds */
2451 2469
2452 int percentage_start_index = 100 * start_index / n; 2470 int percentage_start_index = 100 * start_index / n;
2453 start_index = percentage_start_index * playlist_get_current()->amount / 100; 2471 start_index = percentage_start_index * playlist_get_current()->amount / 100;
2454 } 2472 }
diff --git a/apps/tree.c b/apps/tree.c
index b4cd9d77b0..721fb8c1ef 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -738,10 +738,13 @@ static int dirbrowse(void)
738 int customaction = ONPLAY_NO_CUSTOMACTION; 738 int customaction = ONPLAY_NO_CUSTOMACTION;
739 bool do_restore_display = true; 739 bool do_restore_display = true;
740 #ifdef HAVE_TAGCACHE 740 #ifdef HAVE_TAGCACHE
741 if (id3db && (button == ACTION_STD_OK || button == ACTION_STD_CONTEXT)) { 741 if (id3db && (button == ACTION_STD_OK || button == ACTION_STD_CONTEXT))
742 {
742 customaction = tagtree_get_custom_action(&tc); 743 customaction = tagtree_get_custom_action(&tc);
743 if (customaction == ONPLAY_CUSTOMACTION_SHUFFLE_SONGS) { 744 if (customaction == ONPLAY_CUSTOMACTION_SHUFFLE_SONGS)
744 button = ACTION_STD_CONTEXT; /** The code to insert shuffled is on the context branch of the switch so we always go here */ 745 {
746 /* The code to insert shuffled is on the context branch of the switch so we always go here */
747 button = ACTION_STD_CONTEXT;
745 do_restore_display = false; 748 do_restore_display = false;
746 } 749 }
747 } 750 }