home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / brhist.c < prev    next >
C/C++ Source or Header  |  2000-07-04  |  4KB  |  179 lines

  1. #include <string.h>
  2. #include "brhist.h"
  3. #include "util.h"
  4.  
  5. #if (defined(BRHIST) && !defined(NOTERMCAP))
  6. #include <termcap.h>
  7. #endif
  8.  
  9.  
  10. #define BRHIST_BARMAX 50
  11. long brhist_count[15];
  12. int brhist_vbrmin;
  13. int brhist_vbrmax;
  14. long brhist_max;
  15. char brhist_bps[15][5];
  16. char brhist_backcur[200];
  17. char brhist_bar[BRHIST_BARMAX+10];
  18. char brhist_spc[BRHIST_BARMAX+1];
  19.  
  20. char stderr_buff[BUFSIZ];
  21.  
  22.  
  23. #ifdef _WIN32  
  24. COORD Pos;
  25. HANDLE CH;
  26. CONSOLE_SCREEN_BUFFER_INFO CSBI;
  27. #endif
  28.  
  29. #ifdef NOTERMCAP
  30. /* tgetstr */
  31. char *
  32. tgetstr(char id[2], char **area)
  33. {
  34.       char *result;
  35.       result = NULL;
  36.       if (strncmp(id, "up", 2) == 0) {
  37.               result = "\033[A";
  38.       }
  39.       *area = result;
  40.       return result;
  41. }
  42. #endif /* NOTERMCAP */
  43.  
  44.  
  45. void brhist_init(lame_global_flags *gfp,int br_min, int br_max)
  46. {
  47.   int i;
  48. #ifndef NOTERMCAP
  49.   char term_buff[1024];
  50.   char *termname;
  51. #endif
  52.   char *tp;
  53.   char tc[10];
  54.  
  55.  
  56.   for(i = 0; i < 15; i++)
  57.     {
  58.       sprintf(brhist_bps[i], "%3d", bitrate_table[gfp->version][i]);
  59.       brhist_count[i] = 0;
  60.     }
  61.  
  62.   brhist_vbrmin = br_min;
  63.   brhist_vbrmax = br_max;
  64.  
  65.   brhist_max = 0;
  66.  
  67. #ifdef BRHIST
  68.   memset(&brhist_bar[0], '*', BRHIST_BARMAX);
  69.   brhist_bar[BRHIST_BARMAX] = '\0';
  70.   memset(&brhist_spc[0], ' ', BRHIST_BARMAX);
  71.   brhist_spc[BRHIST_BARMAX] = '\0';
  72.   brhist_backcur[0] = '\0';
  73.  
  74. #ifndef NOTERMCAP
  75.   if ((termname = getenv("TERM")) == NULL)
  76.     {
  77.       ERRORF("can't get TERM environment string.\n");
  78.       gfp->brhist_disp = 0;
  79.       return;
  80.     }
  81.  
  82.   if (tgetent(term_buff, termname) != 1)
  83.     {
  84.       ERRORF("can't find termcap entry: %s\n", termname);
  85.       gfp->brhist_disp = 0;
  86.       return;
  87.     }
  88. #endif /* !NOTERMCAP */
  89.  
  90.   tc[0] = '\0';
  91.   tp = &tc[0];
  92.   tp=tgetstr("up", &tp);
  93.   brhist_backcur[0] = '\0';
  94. #ifdef _WIN32  
  95.   CH= GetStdHandle(STD_ERROR_HANDLE);
  96. #else
  97.   for(i = br_min-1; i <= br_max; i++)
  98.     strcat(brhist_backcur, tp);
  99.   setbuf(stderr, stderr_buff);
  100. #endif
  101. #endif
  102. }
  103.  
  104. void brhist_add_count(int i)
  105. {
  106.   ++brhist_count[i];
  107.   if (brhist_count[i] > brhist_max)
  108.     brhist_max = brhist_count[i];
  109. }
  110.  
  111. void brhist_disp(long totalframes)
  112. {
  113.   int i;
  114.   long full;
  115.   int barlen;
  116.   char brpercent[10];
  117. #ifdef BRHIST
  118.   full = (brhist_max < BRHIST_BARMAX) ? BRHIST_BARMAX : brhist_max;
  119.   fputc('\n', stderr);
  120.   for(i = brhist_vbrmin; i <= brhist_vbrmax; i++)
  121.     {
  122.       barlen = (brhist_count[i]*BRHIST_BARMAX+full-1) / full;
  123.       fputs(brhist_bps[i], stderr);
  124.       sprintf(brpercent,"[%3i%%]",(int)(100*brhist_count[i]/totalframes));
  125.       fputs(brpercent, stderr);
  126.  
  127.       fputs(&brhist_bar[BRHIST_BARMAX - barlen], stderr);
  128.       fputs(&brhist_spc[barlen], stderr);
  129.       fputc('\n', stderr);
  130.     }
  131. #ifdef _WIN32  
  132.   //fflush is not needed
  133.   if(GetFileType(CH)!= FILE_TYPE_PIPE)
  134.   {
  135.     GetConsoleScreenBufferInfo(CH, &CSBI);
  136.     Pos.Y= CSBI.dwCursorPosition.Y-(brhist_vbrmax- brhist_vbrmin)- 2;
  137.     Pos.X= 0;
  138.     SetConsoleCursorPosition(CH, Pos);
  139.   }
  140. #else
  141.   fputs(brhist_backcur, stderr);
  142.   fflush(stderr);
  143. #endif  
  144. #endif
  145. }
  146.  
  147. void brhist_disp_total(lame_global_flags *gfp)
  148. {
  149.   int i;
  150.   FLOAT ave;
  151.   /*  lame_internal_flags *gfc=gfp->internal_flags;*/
  152.  
  153. #ifdef BRHIST
  154.   for(i = brhist_vbrmin; i <= brhist_vbrmax; i++)
  155.     fputc('\n', stderr);
  156. #else
  157.   fprintf(stderr, "\n----- bitrate statistics -----\n");
  158.   fprintf(stderr, " [kbps]      frames\n");
  159.   for(i = brhist_vbrmin; i <= brhist_vbrmax; i++)
  160.     {
  161.       fprintf(stderr, "   %3d  %8ld (%.1f%%)\n",
  162.           bitrate_table[gfp->version][i],
  163.           brhist_count[i],
  164.           (FLOAT)brhist_count[i] / gfp->totalframes * 100.0);
  165.     }
  166. #endif
  167.   ave=0;
  168.   for(i = brhist_vbrmin; i <= brhist_vbrmax; i++)
  169.     ave += bitrate_table[gfp->version][i]*
  170.       (FLOAT)brhist_count[i] / gfp->totalframes;
  171.   fprintf(stderr, "\naverage: %2.0f kbs\n",ave);
  172.  
  173.   fflush(stderr);
  174. }
  175.  
  176.  
  177.  
  178.  
  179.