Advertisement
KeplerBR

[cmus] View SRT - beta 3

Jul 19th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.38 KB | None | 0 0
  1. Index: track_info.c
  2. IDEA additional info:
  3. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  4. <+>UTF-8
  5. ===================================================================
  6. --- track_info.c    (revision 35ca936eb33e206ff2b083a5fc7a4f32e682b82d)
  7. +++ track_info.c    (revision )
  8. @@ -44,6 +44,16 @@
  9.     free(ti);
  10.  }
  11.  
  12. +int getTime(char buffer[], unsigned int start, unsigned int end) {
  13. +   int length = end - start;
  14. +   char timeChar[length];
  15. +   memcpy(timeChar, buffer + start, end);
  16. +   int temp1;
  17. +   sscanf(timeChar, "%d", &temp1);
  18. +
  19. +   return temp1;
  20. +}
  21. +
  22.  struct track_info *track_info_new(const char *filename)
  23.  {
  24.     struct track_info *ti;
  25. @@ -54,6 +64,80 @@
  26.     ti->comments = NULL;
  27.     ti->codec = NULL;
  28.     ti->codec_profile = NULL;
  29. +
  30. +   // Find SRT
  31. +   int filename_length = strlen(filename);
  32. +   int position_extension;
  33. +
  34. +   //Find begin of extension
  35. +   for (position_extension = filename_length; position_extension >= 0; position_extension--) {
  36. +       if (filename[position_extension - 1] == '.')
  37. +           break;
  38. +   }
  39. +
  40. +   if (position_extension <= 0)
  41. +       return ti;
  42. +
  43. +   //Creating name of lyrics
  44. +   char filename_str[position_extension + 4];
  45. +   memcpy(filename_str, filename, sizeof(char) * position_extension);
  46. +   memcpy(filename_str + position_extension, "srt", sizeof(char) * 4);
  47. +
  48. +   // Read SRT
  49. +   ti->count_total_lyrics = 0;
  50. +   if (access(filename_str, F_OK) != -1) {
  51. +       ti->showTextLyric = noneLyric;
  52. +       ti->lastLyric = 0;
  53. +
  54. +       int tempStrFileSize = 30;
  55. +       ti->lyric = malloc(sizeof(struct sLyric) * tempStrFileSize);
  56. +
  57. +       FILE *fp;
  58. +       fp = fopen(filename_str, "r");
  59. +
  60. +       enum {lCounter, lTime, lText, lBlank} currentTypeLine = lCounter;
  61. +
  62. +       char buffer[256];
  63. +       int bufferCount = 0;
  64. +       while ((buffer[bufferCount] = getc(fp)) != EOF) {
  65. +           if (buffer[bufferCount] == '\n') {
  66. +               buffer[bufferCount - 1] = '\0';
  67. +               if (currentTypeLine == lCounter) {
  68. +                   if (ti->count_total_lyrics == tempStrFileSize) {
  69. +                       tempStrFileSize += 5;
  70. +                       ti->lyric = realloc(ti->lyric, sizeof(struct sLyric) * tempStrFileSize);
  71. +                   }
  72. +               } else if (currentTypeLine == lTime) {
  73. +                   int startHour = getTime(buffer, 0, 1);
  74. +                   int startMinutes = getTime(buffer, 3, 4);
  75. +                   int startSeconds = getTime(buffer, 6, 7);
  76. +                   int startHundredths = getTime(buffer, 9, 11);
  77. +                   ti->lyric[ti->count_total_lyrics].timeStart = (startHour * 3600) + (startMinutes * 60) + (startSeconds) + ((double) startHundredths / 1000);
  78. +
  79. +                   int endHour = getTime(buffer, 17, 18);
  80. +                   int endMinutes = getTime(buffer, 20, 21);
  81. +                   int endSeconds = getTime(buffer, 23, 24);
  82. +                   int endHundredths = getTime(buffer, 26, 28);
  83. +                   ti->lyric[ti->count_total_lyrics].timeEnd = (endHour * 3600) + (endMinutes * 60) + (endSeconds) + ((double) endHundredths / 1000);
  84. +               } else if (currentTypeLine == lText) {
  85. +                   strcpy(ti->lyric[ti->count_total_lyrics].messageText, buffer);
  86. +                   ti->count_total_lyrics++;
  87. +               }
  88. +
  89. +               if (currentTypeLine == lBlank) {
  90. +                   currentTypeLine = lCounter;
  91. +               } else {
  92. +                   currentTypeLine++;
  93. +               }
  94. +
  95. +               bufferCount = 0;
  96. +               buffer[bufferCount] = '\0';
  97. +           } else {
  98. +               bufferCount++;
  99. +           }
  100. +       }
  101. +   }
  102. +
  103.     return ti;
  104.  }
  105.  
  106. Index: options.c
  107. IDEA additional info:
  108. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  109. <+>UTF-8
  110. ===================================================================
  111. --- options.c   (revision 35ca936eb33e206ff2b083a5fc7a4f32e682b82d)
  112. +++ options.c   (revision )
  113. @@ -225,6 +225,7 @@
  114.         " %{status} %{?show_playback_position?%{position} %{?duration?/ %{duration} }?%{?duration?%{duration} }}"
  115.         "- %{total} "
  116.         "%{?volume>=0?vol: %{?lvolume!=rvolume?%{lvolume},%{rvolume} ?%{volume} }}"
  117. +       " | %{lyrics} | "
  118.         "%{?stream?buf: %{buffer} }"
  119.         "%{?show_current_bitrate & bitrate>=0? %{bitrate} kbps }"
  120.         "%="
  121. Index: track_info.h
  122. IDEA additional info:
  123. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  124. <+>UTF-8
  125. ===================================================================
  126. --- track_info.h    (revision 35ca936eb33e206ff2b083a5fc7a4f32e682b82d)
  127. +++ track_info.h    (revision )
  128. @@ -22,6 +22,14 @@
  129.  #include <time.h>
  130.  #include <stddef.h>
  131.  
  132. +enum states_lyric {noneLyric, newLyric, oldLyric};
  133. +
  134. +struct sLyric {
  135. +   double timeStart;
  136. +   double timeEnd;
  137. +   char messageText[100];
  138. +};
  139. +
  140.  struct track_info {
  141.     struct keyval *comments;
  142.  
  143. @@ -64,6 +72,12 @@
  144.     unsigned int play_count;
  145.  
  146.     int is_va_compilation : 1;
  147. +
  148. +   int count_total_lyrics;
  149. +   struct sLyric *lyric;
  150. +   int lastLyric;
  151. +   enum states_lyric showTextLyric;
  152. +   char *currentTextLyric;
  153.  };
  154.  
  155.  typedef size_t sort_key_t;
  156. Index: ui_curses.c
  157. IDEA additional info:
  158. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  159. <+>UTF-8
  160. ===================================================================
  161. --- ui_curses.c (revision 35ca936eb33e206ff2b083a5fc7a4f32e682b82d)
  162. +++ ui_curses.c (revision )
  163. @@ -289,6 +289,7 @@
  164.     TF_VOLUME,
  165.     TF_LVOLUME,
  166.     TF_RVOLUME,
  167. +   TF_LYRICS,
  168.     TF_BUFFER,
  169.     TF_REPEAT,
  170.     TF_CONTINUE,
  171. @@ -345,6 +346,7 @@
  172.     DEF_FO_INT('\0', "volume", 1),
  173.     DEF_FO_INT('\0', "lvolume", 1),
  174.     DEF_FO_INT('\0', "rvolume", 1),
  175. +   DEF_FO_STR('\0', "lyrics", 0),
  176.     DEF_FO_INT('\0', "buffer", 1),
  177.     DEF_FO_STR('\0', "repeat", 0),
  178.     DEF_FO_STR('\0', "continue", 0),
  179. @@ -653,8 +655,17 @@
  180.     fopt_set_str(&track_fopts[TF_SHUFFLE], shuffle_strs[shuffle]);
  181.     fopt_set_str(&track_fopts[TF_PLAYLISTMODE], aaa_mode_names[aaa_mode]);
  182.  
  183. -   if (player_info.ti)
  184. +   if (player_info.ti) {
  185.         duration = player_info.ti->duration;
  186. +
  187. +       if (player_info.ti->count_total_lyrics && player_info.ti->showTextLyric == newLyric) {
  188. +           fopt_set_str(&track_fopts[TF_LYRICS], player_info.ti->currentTextLyric);
  189. +           player_info.ti->showTextLyric = oldLyric;
  190. +           refresh();
  191. +       } else if (player_info.ti->showTextLyric == noneLyric) {
  192. +           fopt_set_str(&track_fopts[TF_LYRICS], "");
  193. +       }
  194. +   }
  195.  
  196.     vol_left = vol_right = vol = -1;
  197.     if (soft_vol) {
  198. Index: player.c
  199. IDEA additional info:
  200. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  201. <+>UTF-8
  202. ===================================================================
  203. --- player.c    (revision 35ca936eb33e206ff2b083a5fc7a4f32e682b82d)
  204. +++ player.c    (revision )
  205. @@ -26,6 +26,7 @@
  206.  #include "debug.h"
  207.  #include "compiler.h"
  208.  #include "options.h"
  209. +#include "track_info.h"
  210.  
  211.  #include <stdio.h>
  212.  #include <stdlib.h>
  213. @@ -521,17 +522,29 @@
  214.   */
  215.  static void _consumer_position_update(void)
  216.  {
  217. -   static unsigned int old_pos = -1;
  218. -   unsigned int pos = 0;
  219. +   static double old_pos = -1;
  220. +   double pos = 0;
  221.     long bitrate;
  222.  
  223.     if (consumer_status == CS_PLAYING || consumer_status == CS_PAUSED)
  224. -       pos = consumer_pos / buffer_second_size();
  225. +       pos = (double) consumer_pos / (double) buffer_second_size();
  226. +
  227.     if (pos != old_pos) {
  228.         old_pos = pos;
  229.  
  230.         player_info_lock();
  231. -       player_info.pos = pos;
  232. +       player_info.pos = (int) pos;
  233. +
  234. +       if (player_info.ti->count_total_lyrics && player_info.ti->lastLyric <= player_info.ti->count_total_lyrics) {
  235. +           if (player_info.ti->lastLyric > 0 && player_info.ti->lyric[player_info.ti->lastLyric - 1].timeEnd <= pos)
  236. +               player_info.ti->showTextLyric = noneLyric;
  237. +
  238. +           if (player_info.ti->lyric[player_info.ti->lastLyric].timeStart <= pos) {
  239. +               player_info.ti->currentTextLyric = player_info.ti->lyric[player_info.ti->lastLyric].messageText;
  240. +               player_info.ti->lastLyric++;
  241. +               player_info.ti->showTextLyric = newLyric;
  242. +           }
  243. +       }
  244.  
  245.         if (show_current_bitrate) {
  246.             bitrate = ip_current_bitrate(ip);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement