foobar2000 SDK  2015-01-14
playback_control.cpp
Go to the documentation of this file.
1 #include "foobar2000.h"
2 
3 static double parseFraction(const char * fraction) {
4  unsigned v = 0, d = 1;
5  while(pfc::char_is_numeric( *fraction) ) {
6  d *= 10;
7  v *= 10;
8  v += (unsigned) ( *fraction - '0' );
9  ++fraction;
10  }
11  PFC_ASSERT( *fraction == 0 );
12  return (double)v / (double)d;
13 }
14 
15 static double parse_time(const char * time) {
16  unsigned vTotal = 0, vCur = 0;
17  for(;;) {
18  char c = *time++;
19  if (c == 0) return (double) (vTotal + vCur);
20  else if (pfc::char_is_numeric( c ) ) {
21  vCur = vCur * 10 + (unsigned)(c-'0');
22  } else if (c == ':') {
23  if (vCur >= 60) {PFC_ASSERT(!"Invalid input"); return 0; }
24  vTotal += vCur; vCur = 0; vTotal *= 60;
25  } else if (c == '.') {
26  return (double) (vTotal + vCur) + parseFraction(time);
27  } else {
28  PFC_ASSERT(!"Invalid input"); return 0;
29  }
30  }
31 }
32 
34 {
35  double rv = 0;
37  if (get_now_playing(ptr))
38  {
39  rv = ptr->get_length();
40  }
41  return rv;
42 }
43 
45  double rv = 0;
47  if (get_now_playing(ptr))
48  {
49  rv = ptr->get_length();
50  if (rv <= 0) {
51  pfc::string8 temp;
52  titleformat_object::ptr script;
53  static_api_ptr_t<titleformat_compiler>()->compile_force(script, "[%length_ex%]");
54  this->playback_format_title(NULL, temp, script, NULL, display_level_titles);
55  if (temp.length() > 0) rv = parse_time(temp);
56  }
57  }
58  return rv;
59 }
display_level_basic + dynamic track titles on e.g. live streams
double playback_get_length()
Helper; retrieves length of currently playing item.
virtual bool get_now_playing(metadb_handle_ptr &p_out)=0
Retrieves now playing item handle.
static double parseFraction(const char *fraction)
virtual bool playback_format_title(titleformat_hook *p_hook, pfc::string_base &p_out, const service_ptr_t< class titleformat_object > &p_script, titleformat_text_filter *p_filter, t_display_level p_level)=0
Renders information about currently playing item.
static double parse_time(const char *time)
double playback_get_length_ex()
bool char_is_numeric(char_t p_char)
Definition: string_base.h:99
Helper template used to easily access core services. Usage: static_api_ptr_t api; api->doso...
Definition: service.h:533
t_size length() const
For compatibility with old conventions.
Definition: string_base.h:208