summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2023-01-14 03:56:31 -0500
committerWilliam Wilgus <wilgus.william@gmail.com>2023-01-14 10:31:56 -0500
commitcc79f1b543535e8d7e5b9875af9043c16267e429 (patch)
treea511c6312af2b9522f9221d0c6cc770d2a158a23
parent6e08731835172bc68f40eba1ceedca8f5db1ac38 (diff)
downloadrockbox-cc79f1b543535e8d7e5b9875af9043c16267e429.tar.gz
rockbox-cc79f1b543535e8d7e5b9875af9043c16267e429.zip
on_play.c clean up playing_time
move seconds and sizes to arrays indiexed by enums use a loop for the display and talk of each stop exiting on SYS_EVENTs Change-Id: I49d5b9827df4e711b38326e5fef3c54292000370
-rw-r--r--apps/onplay.c205
1 files changed, 123 insertions, 82 deletions
diff --git a/apps/onplay.c b/apps/onplay.c
index 56809e45cd..ce2b3310f3 100644
--- a/apps/onplay.c
+++ b/apps/onplay.c
@@ -198,21 +198,36 @@ static int bookmark_menu_callback(int action,
198 return action; 198 return action;
199} 199}
200 200
201enum ePT_SECS {
202 ePT_SECS_TTL = 0,
203 ePT_SECS_BEF,
204 ePT_SECS_AFT,
205 ePT_SECS_COUNT
206};
207
208enum ePT_KBS {
209 /* Note: Order matters (voicing order of LANG_PLAYTIME_STORAGE) */
210 ePT_KBS_TTL = 0,
211 ePT_KBS_BEF,
212 ePT_KBS_AFT,
213 ePT_KBS_COUNT
214};
215
201/* playing_time screen context */ 216/* playing_time screen context */
202struct playing_time_info { 217struct playing_time_info {
203 int curr_playing; /* index of currently playing track in playlist */ 218 int curr_playing; /* index of currently playing track in playlist */
204 int nb_tracks; /* how many tracks in playlist */ 219 int nb_tracks; /* how many tracks in playlist */
205 /* seconds before and after current position, and total. Datatype 220 /* seconds total, before, and after current position. Datatype
206 allows for values up to 68years. If I had kept it in ms 221 allows for values up to 68years. If I had kept it in ms
207 though, it would have overflowed at 24days, which takes 222 though, it would have overflowed at 24days, which takes
208 something like 8.5GB at 32kbps, and so we could conceivably 223 something like 8.5GB at 32kbps, and so we could conceivably
209 have playlists lasting longer than that. */ 224 have playlists lasting longer than that. */
210 long secs_bef, secs_aft, secs_ttl; 225 long secs[ePT_SECS_COUNT];
211 long trk_secs_bef, trk_secs_aft, trk_secs_ttl; 226 long trk_secs[ePT_SECS_COUNT];
212 /* kilobytes played before and after current pos, and total. 227 /* kilobytes played total, before, and after current pos.
213 Kilobytes because bytes would overflow. Data type range is up 228 Kilobytes because bytes would overflow. Data type range is up
214 to 2TB. */ 229 to 2TB. */
215 long kbs_bef, kbs_aft, kbs_ttl; 230 long kbs[ePT_KBS_COUNT];
216}; 231};
217 232
218/* list callback for playing_time screen */ 233/* list callback for playing_time screen */
@@ -220,114 +235,139 @@ static const char * playing_time_get_or_speak_info(int selected_item, void * dat
220 char *buf, size_t buffer_len, 235 char *buf, size_t buffer_len,
221 bool say_it) 236 bool say_it)
222{ 237{
238 long elapsed_pct; /* percentage of duration elapsed */
223 struct playing_time_info *pti = (struct playing_time_info *)data; 239 struct playing_time_info *pti = (struct playing_time_info *)data;
224 switch(selected_item) { 240 switch(selected_item) {
225 case 0: { /* elapsed and total time */ 241 case 0: { /* elapsed and total time */
226 char timestr1[25], timestr2[25]; 242 char timestr1[25], timestr2[25];
227 format_time_auto(timestr1, sizeof(timestr1), pti->secs_bef, 243 format_time_auto(timestr1, sizeof(timestr1),
228 UNIT_SEC, false); 244 pti->secs[ePT_SECS_BEF], UNIT_SEC, false);
229 format_time_auto(timestr2, sizeof(timestr2), pti->secs_ttl, 245
230 UNIT_SEC, false); 246 format_time_auto(timestr2, sizeof(timestr2),
231 long elapsed_perc; /* percentage of duration elapsed */ 247 pti->secs[ePT_SECS_TTL], UNIT_SEC, false);
232 if (pti->secs_ttl == 0) 248
233 elapsed_perc = 0; 249 if (pti->secs[ePT_SECS_TTL] == 0)
234 else if (pti->secs_ttl <= 0xFFFFFF) 250 elapsed_pct = 0;
235 elapsed_perc = pti->secs_bef *100 / pti->secs_ttl; 251 else if (pti->secs[ePT_SECS_TTL] <= 0xFFFFFF)
252 {
253 elapsed_pct = (pti->secs[ePT_SECS_BEF] * 100
254 / pti->secs[ePT_SECS_TTL]);
255 }
236 else /* sacrifice some precision to avoid overflow */ 256 else /* sacrifice some precision to avoid overflow */
237 elapsed_perc = (pti->secs_bef>>7) *100 /(pti->secs_ttl>>7); 257 {
258 elapsed_pct = (pti->secs[ePT_SECS_BEF] >> 7) * 100
259 / (pti->secs[ePT_SECS_TTL] >> 7);
260 }
238 snprintf(buf, buffer_len, str(LANG_PLAYTIME_ELAPSED), 261 snprintf(buf, buffer_len, str(LANG_PLAYTIME_ELAPSED),
239 timestr1, timestr2, elapsed_perc); 262 timestr1, timestr2, elapsed_pct);
263
240 if (say_it) 264 if (say_it)
241 talk_ids(false, LANG_PLAYTIME_ELAPSED, 265 talk_ids(false, LANG_PLAYTIME_ELAPSED,
242 TALK_ID(pti->secs_bef, UNIT_TIME), 266 TALK_ID(pti->secs[ePT_SECS_BEF], UNIT_TIME),
243 VOICE_OF, 267 VOICE_OF,
244 TALK_ID(pti->secs_ttl, UNIT_TIME), 268 TALK_ID(pti->secs[ePT_SECS_TTL], UNIT_TIME),
245 VOICE_PAUSE, 269 VOICE_PAUSE,
246 TALK_ID(elapsed_perc, UNIT_PERCENT)); 270 TALK_ID(elapsed_pct, UNIT_PERCENT));
247 break; 271 break;
248 } 272 }
249 case 1: { /* playlist remaining time */ 273 case 1: { /* playlist remaining time */
250 char timestr[25]; 274 char timestr[25];
251 format_time_auto(timestr, sizeof(timestr), pti->secs_aft, 275 format_time_auto(timestr, sizeof(timestr), pti->secs[ePT_SECS_AFT],
252 UNIT_SEC, false); 276 UNIT_SEC, false);
253 snprintf(buf, buffer_len, str(LANG_PLAYTIME_REMAINING), 277 snprintf(buf, buffer_len, str(LANG_PLAYTIME_REMAINING), timestr);
254 timestr); 278
255 if (say_it) 279 if (say_it)
256 talk_ids(false, LANG_PLAYTIME_REMAINING, 280 talk_ids(false, LANG_PLAYTIME_REMAINING,
257 TALK_ID(pti->secs_aft, UNIT_TIME)); 281 TALK_ID(pti->secs[ePT_SECS_AFT], UNIT_TIME));
258 break; 282 break;
259 } 283 }
260 case 2: { /* track elapsed and duration */ 284 case 2: { /* track elapsed and duration */
261 char timestr1[25], timestr2[25]; 285 char timestr1[25], timestr2[25];
262 format_time_auto(timestr1, sizeof(timestr1), pti->trk_secs_bef, 286
263 UNIT_SEC, false); 287 format_time_auto(timestr1, sizeof(timestr1), pti->trk_secs[ePT_SECS_BEF],
264 format_time_auto(timestr2, sizeof(timestr2), pti->trk_secs_ttl, 288 UNIT_SEC, false);
265 UNIT_SEC, false); 289 format_time_auto(timestr2, sizeof(timestr2), pti->trk_secs[ePT_SECS_TTL],
266 long elapsed_perc; /* percentage of duration elapsed */ 290 UNIT_SEC, false);
267 if (pti->trk_secs_ttl == 0) 291
268 elapsed_perc = 0; 292 if (pti->trk_secs[ePT_SECS_TTL] == 0)
269 else if (pti->trk_secs_ttl <= 0xFFFFFF) 293 elapsed_pct = 0;
270 elapsed_perc = pti->trk_secs_bef *100 / pti->trk_secs_ttl; 294 else if (pti->trk_secs[ePT_SECS_TTL] <= 0xFFFFFF)
295 {
296 elapsed_pct = (pti->trk_secs[ePT_SECS_BEF] * 100
297 / pti->trk_secs[ePT_SECS_TTL]);
298 }
271 else /* sacrifice some precision to avoid overflow */ 299 else /* sacrifice some precision to avoid overflow */
272 elapsed_perc = (pti->trk_secs_bef>>7) *100 /(pti->trk_secs_ttl>>7); 300 {
301 elapsed_pct = (pti->trk_secs[ePT_SECS_BEF] >> 7) * 100
302 / (pti->trk_secs[ePT_SECS_TTL] >> 7);
303 }
273 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRK_ELAPSED), 304 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRK_ELAPSED),
274 timestr1, timestr2, elapsed_perc); 305 timestr1, timestr2, elapsed_pct);
306
275 if (say_it) 307 if (say_it)
276 talk_ids(false, LANG_PLAYTIME_TRK_ELAPSED, 308 talk_ids(false, LANG_PLAYTIME_TRK_ELAPSED,
277 TALK_ID(pti->trk_secs_bef, UNIT_TIME), 309 TALK_ID(pti->trk_secs[ePT_SECS_BEF], UNIT_TIME),
278 VOICE_OF, 310 VOICE_OF,
279 TALK_ID(pti->trk_secs_ttl, UNIT_TIME), 311 TALK_ID(pti->trk_secs[ePT_SECS_TTL], UNIT_TIME),
280 VOICE_PAUSE, 312 VOICE_PAUSE,
281 TALK_ID(elapsed_perc, UNIT_PERCENT)); 313 TALK_ID(elapsed_pct, UNIT_PERCENT));
282 break; 314 break;
283 } 315 }
284 case 3: { /* track remaining time */ 316 case 3: { /* track remaining time */
285 char timestr[25]; 317 char timestr[25];
286 format_time_auto(timestr, sizeof(timestr), pti->trk_secs_aft, 318 format_time_auto(timestr, sizeof(timestr), pti->trk_secs[ePT_SECS_AFT],
287 UNIT_SEC, false); 319 UNIT_SEC, false);
288 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRK_REMAINING), 320 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRK_REMAINING), timestr);
289 timestr); 321
290 if (say_it) 322 if (say_it)
291 talk_ids(false, LANG_PLAYTIME_TRK_REMAINING, 323 talk_ids(false, LANG_PLAYTIME_TRK_REMAINING,
292 TALK_ID(pti->trk_secs_aft, UNIT_TIME)); 324 TALK_ID(pti->trk_secs[ePT_SECS_AFT], UNIT_TIME));
293 break; 325 break;
294 } 326 }
295 case 4: { /* track index */ 327 case 4: { /* track index */
296 int track_perc = (pti->curr_playing+1) *100 / pti->nb_tracks; 328 int track_pct = (pti->curr_playing + 1) * 100 / pti->nb_tracks;
297 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRACK), 329 snprintf(buf, buffer_len, str(LANG_PLAYTIME_TRACK),
298 pti->curr_playing + 1, pti->nb_tracks, track_perc); 330 pti->curr_playing + 1, pti->nb_tracks, track_pct);
331
299 if (say_it) 332 if (say_it)
300 talk_ids(false, LANG_PLAYTIME_TRACK, 333 talk_ids(false, LANG_PLAYTIME_TRACK,
301 TALK_ID(pti->curr_playing+1, UNIT_INT), 334 TALK_ID(pti->curr_playing + 1, UNIT_INT),
302 VOICE_OF, 335 VOICE_OF,
303 TALK_ID(pti->nb_tracks, UNIT_INT), 336 TALK_ID(pti->nb_tracks, UNIT_INT),
304 VOICE_PAUSE, 337 VOICE_PAUSE,
305 TALK_ID(track_perc, UNIT_PERCENT)); 338 TALK_ID(track_pct, UNIT_PERCENT));
306 break; 339 break;
307 } 340 }
308 case 5: { /* storage size */ 341 case 5: { /* storage size */
309 char str1[10], str2[10], str3[10]; 342 int i;
310 output_dyn_value(str1, sizeof(str1), pti->kbs_ttl, kibyte_units, 3, true); 343 char kbstr[ePT_KBS_COUNT][10];
311 output_dyn_value(str2, sizeof(str2), pti->kbs_bef, kibyte_units, 3, true); 344
312 output_dyn_value(str3, sizeof(str3), pti->kbs_aft, kibyte_units, 3, true); 345 for (i = 0; i < ePT_KBS_COUNT; i++) {
346 output_dyn_value(kbstr[i], sizeof(kbstr[i]),
347 pti->kbs[i], kibyte_units, 3, true);
348 }
313 snprintf(buf, buffer_len, str(LANG_PLAYTIME_STORAGE), 349 snprintf(buf, buffer_len, str(LANG_PLAYTIME_STORAGE),
314 str1,str2,str3); 350 kbstr[ePT_KBS_TTL], kbstr[ePT_KBS_BEF],kbstr[ePT_KBS_AFT]);
351
315 if (say_it) { 352 if (say_it) {
316 talk_id(LANG_PLAYTIME_STORAGE, false); 353 int32_t voice_ids[ePT_KBS_COUNT];
317 output_dyn_value(NULL, 0, pti->kbs_ttl, kibyte_units, 3, true); 354 voice_ids[ePT_KBS_TTL] = LANG_PLAYTIME_STORAGE;
318 talk_ids(true, VOICE_PAUSE, VOICE_PLAYTIME_DONE); 355 voice_ids[ePT_KBS_BEF] = VOICE_PLAYTIME_DONE;
319 output_dyn_value(NULL, 0, pti->kbs_bef, kibyte_units, 3, true); 356 voice_ids[ePT_KBS_AFT] = LANG_PLAYTIME_REMAINING;
320 talk_id(LANG_PLAYTIME_REMAINING, true); 357
321 output_dyn_value(NULL, 0, pti->kbs_aft, kibyte_units, 3, true); 358 for (i = 0; i < ePT_KBS_COUNT; i++) {
359 talk_ids(i > 0, VOICE_PAUSE, voice_ids[i]);
360 output_dyn_value(NULL, 0, pti->kbs[i], kibyte_units, 3, true);
361 }
322 } 362 }
323 break; 363 break;
324 } 364 }
325 case 6: { /* Average track file size */ 365 case 6: { /* Average track file size */
326 char str[10]; 366 char str[10];
327 long avg_track_size = pti->kbs_ttl /pti->nb_tracks; 367 long avg_track_size = pti->kbs[ePT_KBS_TTL] / pti->nb_tracks;
328 output_dyn_value(str, sizeof(str), avg_track_size, kibyte_units, 3, true); 368 output_dyn_value(str, sizeof(str), avg_track_size, kibyte_units, 3, true);
329 snprintf(buf, buffer_len, str(LANG_PLAYTIME_AVG_TRACK_SIZE), 369 snprintf(buf, buffer_len, str(LANG_PLAYTIME_AVG_TRACK_SIZE), str);
330 str); 370
331 if (say_it) { 371 if (say_it) {
332 talk_id(LANG_PLAYTIME_AVG_TRACK_SIZE, false); 372 talk_id(LANG_PLAYTIME_AVG_TRACK_SIZE, false);
333 output_dyn_value(NULL, 0, avg_track_size, kibyte_units, 3, true); 373 output_dyn_value(NULL, 0, avg_track_size, kibyte_units, 3, true);
@@ -336,9 +376,10 @@ static const char * playing_time_get_or_speak_info(int selected_item, void * dat
336 } 376 }
337 case 7: { /* Average bitrate */ 377 case 7: { /* Average bitrate */
338 /* Convert power of 2 kilobytes to power of 10 kilobits */ 378 /* Convert power of 2 kilobytes to power of 10 kilobits */
339 long avg_bitrate = pti->kbs_ttl / pti->secs_ttl *1024 *8 /1000; 379 long avg_bitrate = (pti->kbs[ePT_KBS_TTL] / pti->secs[ePT_SECS_TTL]
340 snprintf(buf, buffer_len, str(LANG_PLAYTIME_AVG_BITRATE), 380 * 1024 * 8 / 1000);
341 avg_bitrate); 381 snprintf(buf, buffer_len, str(LANG_PLAYTIME_AVG_BITRATE), avg_bitrate);
382
342 if (say_it) 383 if (say_it)
343 talk_ids(false, LANG_PLAYTIME_AVG_BITRATE, 384 talk_ids(false, LANG_PLAYTIME_AVG_BITRATE,
344 TALK_ID(avg_bitrate, UNIT_KBIT)); 385 TALK_ID(avg_bitrate, UNIT_KBIT));
@@ -373,21 +414,20 @@ static bool playing_time(void)
373 struct playlist_track_info pltrack; 414 struct playlist_track_info pltrack;
374 struct mp3entry id3; 415 struct mp3entry id3;
375 int i, fd; 416 int i, fd;
376 bool ret;
377 417
378 pti.nb_tracks = playlist_amount(); 418 pti.nb_tracks = playlist_amount();
379 playlist_get_resume_info(&pti.curr_playing); 419 playlist_get_resume_info(&pti.curr_playing);
380 struct mp3entry *curr_id3 = audio_current_track(); 420 struct mp3entry *curr_id3 = audio_current_track();
381 if (pti.curr_playing == -1 || !curr_id3) 421 if (pti.curr_playing == -1 || !curr_id3)
382 return false; 422 return false;
383 pti.secs_bef = pti.trk_secs_bef = curr_id3->elapsed/1000; 423 pti.secs[ePT_SECS_BEF] = pti.trk_secs[ePT_SECS_BEF] = curr_id3->elapsed / 1000;
384 pti.secs_aft = pti.trk_secs_aft 424 pti.secs[ePT_SECS_AFT] = pti.trk_secs[ePT_SECS_AFT]
385 = (curr_id3->length -curr_id3->elapsed)/1000; 425 = (curr_id3->length -curr_id3->elapsed) / 1000;
386 pti.kbs_bef = curr_id3->offset/1024; 426 pti.kbs[ePT_KBS_BEF] = curr_id3->offset / 1024;
387 pti.kbs_aft = (curr_id3->filesize -curr_id3->offset)/1024; 427 pti.kbs[ePT_KBS_AFT] = (curr_id3->filesize -curr_id3->offset) / 1024;
388 428
389 splash(0, ID2P(LANG_WAIT)); 429 splash(0, ID2P(LANG_WAIT));
390 430 splash_progress_set_delay(5 * HZ);
391 /* Go through each file in the playlist and get its stats. For 431 /* Go through each file in the playlist and get its stats. For
392 huge playlists this can take a while... The reference position 432 huge playlists this can take a while... The reference position
393 is the position at the moment this function was invoked, 433 is the position at the moment this function was invoked,
@@ -398,10 +438,10 @@ static bool playing_time(void)
398 "%s (%s)", str(LANG_WAIT), str(LANG_OFF_ABORT)); 438 "%s (%s)", str(LANG_WAIT), str(LANG_OFF_ABORT));
399 439
400 /* Voice equivalent */ 440 /* Voice equivalent */
401 if (TIME_AFTER(current_tick, talked_tick+5*HZ)) { 441 if (TIME_AFTER(current_tick, talked_tick + 5 * HZ)) {
402 talked_tick = current_tick; 442 talked_tick = current_tick;
403 talk_ids(false, LANG_LOADING_PERCENT, 443 talk_ids(false, LANG_LOADING_PERCENT,
404 TALK_ID(i*100/pti.nb_tracks, UNIT_PERCENT)); 444 TALK_ID(i * 100 / pti.nb_tracks, UNIT_PERCENT));
405 } 445 }
406 if (action_userabort(TIMEOUT_NOBLOCK)) 446 if (action_userabort(TIMEOUT_NOBLOCK))
407 goto exit; 447 goto exit;
@@ -411,7 +451,7 @@ static bool playing_time(void)
411 451
412 if (playlist_get_track_info(NULL, i, &pltrack) >= 0) 452 if (playlist_get_track_info(NULL, i, &pltrack) >= 0)
413 { 453 {
414 ret = false; 454 bool ret = false;
415 if ((fd = open(pltrack.filename, O_RDONLY)) >= 0) 455 if ((fd = open(pltrack.filename, O_RDONLY)) >= 0)
416 { 456 {
417 ret = get_metadata(&id3, fd, pltrack.filename); 457 ret = get_metadata(&id3, fd, pltrack.filename);
@@ -419,11 +459,11 @@ static bool playing_time(void)
419 if (ret) 459 if (ret)
420 { 460 {
421 if (i < pti.curr_playing) { 461 if (i < pti.curr_playing) {
422 pti.secs_bef += id3.length/1000; 462 pti.secs[ePT_SECS_BEF] += id3.length / 1000;
423 pti.kbs_bef += id3.filesize/1024; 463 pti.kbs[ePT_KBS_BEF] += id3.filesize / 1024;
424 } else { 464 } else {
425 pti.secs_aft += id3.length/1000; 465 pti.secs[ePT_SECS_AFT] += id3.length / 1000;
426 pti.kbs_aft += id3.filesize/1024; 466 pti.kbs[ePT_KBS_AFT] += id3.filesize / 1024;
427 } 467 }
428 } 468 }
429 } 469 }
@@ -447,9 +487,9 @@ static bool playing_time(void)
447 } 487 }
448 488
449 pti.nb_tracks -= error_count; 489 pti.nb_tracks -= error_count;
450 pti.secs_ttl = pti.secs_bef +pti.secs_aft; 490 pti.secs[ePT_SECS_TTL] = pti.secs[ePT_SECS_BEF] + pti.secs[ePT_SECS_AFT];
451 pti.trk_secs_ttl = pti.trk_secs_bef +pti.trk_secs_aft; 491 pti.trk_secs[ePT_SECS_TTL] = pti.trk_secs[ePT_SECS_BEF] + pti.trk_secs[ePT_SECS_AFT];
452 pti.kbs_ttl = pti.kbs_bef +pti.kbs_aft; 492 pti.kbs[ePT_KBS_TTL] = pti.kbs[ePT_KBS_BEF] + pti.kbs[ePT_KBS_AFT];
453 493
454 struct gui_synclist pt_lists; 494 struct gui_synclist pt_lists;
455 int key; 495 int key;
@@ -468,13 +508,13 @@ static bool playing_time(void)
468 talk_force_shutup(); 508 talk_force_shutup();
469 return(default_event_handler(key) == SYS_USB_CONNECTED); 509 return(default_event_handler(key) == SYS_USB_CONNECTED);
470 } 510 }
511
471 } 512 }
472 513
473 exit: 514 exit:
474 return false; 515 return false;
475} 516}
476 517
477
478/* CONTEXT_WPS playlist options */ 518/* CONTEXT_WPS playlist options */
479static bool shuffle_playlist(void) 519static bool shuffle_playlist(void)
480{ 520{
@@ -485,6 +525,7 @@ static bool shuffle_playlist(void)
485 525
486 return false; 526 return false;
487} 527}
528
488static bool save_playlist(void) 529static bool save_playlist(void)
489{ 530{
490 /* save_playlist_screen should load the newly saved playlist and resume */ 531 /* save_playlist_screen should load the newly saved playlist and resume */