Browse Source

add a time factor

Olivier Marty 9 years ago
parent
commit
c72d0798d1
3 changed files with 26 additions and 4 deletions
  1. 10 4
      main.c
  2. 14 0
      time.c
  3. 2 0
      time.h

+ 10 - 4
main.c

@@ -29,6 +29,7 @@ void displayUsage(char *name)
   printf("Options :\n");
   printf("  -s sec\t: skip the first x seconds\n");
   printf("  -d sec\t: wait x seconds before starting (default : 5)\n");
+  printf("  -t x\t\t: time factor x1000\n");
   printf("  -w px\t\t: width of the window\n");
   printf("  -H px\t\t: height of the window\n");
   printf("  -m px\t\t: margin with the bottom of the screen\n");
@@ -42,12 +43,14 @@ void displayUsage(char *name)
 int main(int argc, char **argv)
 {
   int i, shift = 0, delay = 5, width = -1, height = 240, margin_bottom = 50;
+  float factor = 1.0;
+    
   char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
   FILE *f = NULL;
   
   // parse arguments
   int c;
-  while((c = getopt (argc, argv, "s:d:w:H:m:f:i:b:j:h")) != -1)
+  while((c = getopt (argc, argv, "s:d:t:w:H:m:f:i:b:j:h")) != -1)
     switch(c)
     {
       case 's':
@@ -56,6 +59,9 @@ int main(int argc, char **argv)
       case 'd':
         delay = atoi(optarg);
         break;
+      case 't':
+        factor = (float)atoi(optarg)/1000;
+        break;
       case 'w':
         width = atoi(optarg);
         break;
@@ -118,14 +124,14 @@ int main(int argc, char **argv)
   }
   printf("0 !\n");
   printerClean(penv);
-  timeInitialize(-shift);
+  timeInitialize(-factor*shift);
   
   struct SubtitleLine sline;
   int id = 0;
   while(!feof(f))
   {
     id = next(f, id+1, &sline);
-    if(!timeSleepUntil(sline.begin)) // no error and in the future
+    if(!timeSleepUntil(timeFactor(sline.begin, factor))) // no error and in the future
     {
       printf("%ds\n", sline.begin.tv_sec);
       // show
@@ -133,7 +139,7 @@ int main(int argc, char **argv)
       printerShow(penv, sline.text, 0);
       
       // hide
-      timeSleepUntil(sline.end);
+      timeSleepUntil(timeFactor(sline.end, factor));
       // TODO manage when the next subtitle appear before
       printf("\n");
       printerClean(penv);

+ 14 - 0
time.c

@@ -46,6 +46,20 @@ struct timespec timeDiff(struct timespec a, struct timespec b)
   return r;
 }
 
+// f should be >= 0
+struct timespec timeFactor(struct timespec a, double f)
+{
+  struct timespec r;
+  r.tv_sec = f*a.tv_sec;
+  r.tv_nsec = f*a.tv_nsec;
+  while(r.tv_nsec > 1000000000)
+  {
+    r.tv_nsec -= 1000000000;
+    r.tv_sec += 1;
+  }
+  return r;
+}
+
 struct timespec begin;
 void timeInitialize(int rel)
 {

+ 2 - 0
time.h

@@ -33,6 +33,8 @@ mytime timeCreate(time_t s, long ns);
 // return -2 if t is negative
 int timeSleep(mytime t);
 mytime timeDiff(mytime a, mytime b);
+// f should be >= 0
+mytime timeFactor(mytime a, double f);
 
 #endif