xref: /plan9-contrib/sys/src/games/mp3enc/timestatus.c (revision 8f5875f3e9b20916b4c52ad4336922bc8653eb7b)
1 /*
2  *	time status related function source file
3  *
4  *	Copyright (c) 1999 Mark Taylor
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 /* $Id: timestatus.c,v 1.32 2001/03/11 11:24:25 aleidinger Exp $ */
23 
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 
28 
29 /* Hope it works now, otherwise complain or flame ;-)
30  */
31 
32 
33 #if 1
34 # define SPEED_CHAR	"x"	/* character x */
35 # define SPEED_MULT	1.
36 #else
37 # define SPEED_CHAR	"%%"
38 # define SPEED_MULT	100.
39 #endif
40 
41 #include <assert.h>
42 #include <time.h>
43 
44 #include "lame.h"
45 #include "main.h"
46 #include "lametime.h"
47 #include "timestatus.h"
48 
49 #if defined(BRHIST)
50 # include "brhist.h"
51 #endif
52 
53 #ifdef WITH_DMALLOC
54 #include <dmalloc.h>
55 #endif
56 
57 typedef struct {
58     double  start_time;			  // start time of converting [s]
59     double  elapsed_time;		  // current time - start time [s]
60     double  estimated_time;		  // estimated total duration time [s]
61     double  speed_index;		  // speed relative to realtime coding [100%]
62 } timestatus_t;
63 
64 /*
65  *  Calculates from the input (see below) the following values:
66  *    - total estimated time
67  *    - a speed index
68  */
69 
ts_calc_times(timestatus_t * const tstime,const int sample_freq,const int frameNum,const int totalframes,const int framesize)70 static void  ts_calc_times (
71     timestatus_t* const  tstime,           // tstime->elapsed_time: elapsed time
72     const int            sample_freq,      // sample frequency [Hz/kHz]
73     const int            frameNum,         // Number of the current Frame
74     const int            totalframes,      // total umber of Frames
75     const int            framesize )       // Size of a frame [bps/kbps]
76 {
77     assert ( sample_freq >= 8000  &&  sample_freq <= 48000 );
78 
79     if ( frameNum > 0  &&  tstime->elapsed_time > 0 ) {
80         tstime->estimated_time = tstime->elapsed_time * totalframes / frameNum;
81         tstime->speed_index    = framesize * frameNum / (sample_freq * tstime->elapsed_time);
82     } else {
83         tstime->estimated_time = 0.;
84         tstime->speed_index    = 0.;
85     }
86 }
87 
88 /* Decomposes a given number of seconds into a easy to read hh:mm:ss format
89  * padded with an additional character
90  */
91 
ts_time_decompose(const unsigned long time_in_sec,const char padded_char)92 static void  ts_time_decompose ( const unsigned long time_in_sec, const char padded_char )
93 {
94     const unsigned long hour = time_in_sec / 3600;
95     const unsigned int  min  = time_in_sec / 60 % 60;
96     const unsigned int  sec  = time_in_sec % 60;
97 
98     if ( hour == 0 )
99         fprintf ( stderr,    "   %2u:%02u%c",       min, sec, padded_char );
100     else if ( hour < 100 )
101         fprintf ( stderr, "%2lu:%02u:%02u%c", hour, min, sec, padded_char );
102     else
103         fprintf ( stderr,         "%6lu h%c", hour,           padded_char );
104 }
105 
timestatus(const int samp_rate,const int frameNum,const int totalframes,const int framesize)106 void timestatus ( const int samp_rate,
107                   const int frameNum,
108                   const int totalframes,
109                   const int framesize )
110 {
111     static timestatus_t  real_time;
112     static timestatus_t  proc_time;
113     int                  percent;
114     static int           init = 0; /* What happens here? A work around instead of a bug fix ??? */
115 
116     if ( frameNum == 0 ) {
117         real_time.start_time = GetRealTime ();
118         proc_time.start_time = GetCPUTime  ();
119     }
120 
121     real_time.elapsed_time = GetRealTime () - real_time.start_time;
122     proc_time.elapsed_time = GetCPUTime  () - proc_time.start_time;
123 
124     if ( frameNum == 0 && init == 0 ) {
125         fprintf ( stderr,
126 	    "\r"
127 	    "    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA \n"
128             "     0/       ( 0%%)|    0:00/     :  |    0:00/     :  |         " SPEED_CHAR "|     :  \r"
129 	    /* , Console_IO.str_clreoln, Console_IO.str_clreoln */ );
130 	init = 1;
131         return;
132     }
133     /* reset init counter for next time we are called with frameNum==0 */
134     if (frameNum > 0)
135         init = 0;
136 
137     ts_calc_times ( &real_time, samp_rate, frameNum, totalframes, framesize );
138     ts_calc_times ( &proc_time, samp_rate, frameNum, totalframes, framesize );
139 
140     if ( frameNum < totalframes  ) {
141         percent = (int) (100. * frameNum / totalframes + 0.5 );
142     } else {
143         percent = 100;
144     }
145 
146     fprintf ( stderr, "\r%6i/%-6i", frameNum, totalframes );
147     fprintf ( stderr, percent < 100 ? " (%2d%%)|" : "(%3.3d%%)|", percent );
148     ts_time_decompose ( (unsigned long)proc_time.elapsed_time  , '/' );
149     ts_time_decompose ( (unsigned long)proc_time.estimated_time, '|' );
150     ts_time_decompose ( (unsigned long)real_time.elapsed_time  , '/' );
151     ts_time_decompose ( (unsigned long)real_time.estimated_time, '|' );
152     fprintf ( stderr, proc_time.speed_index <= 1.  ?
153               "%9.4f" SPEED_CHAR "|"  :  "%#9.5g" SPEED_CHAR "|",
154 	      SPEED_MULT * proc_time.speed_index );
155     ts_time_decompose ( (unsigned long)(real_time.estimated_time - real_time.elapsed_time), ' ' );
156     fflush  ( stderr );
157 }
158 
timestatus_finish(void)159 void timestatus_finish ( void )
160 {
161     fprintf ( stderr, "\n" );
162     fflush  ( stderr );
163 }
164 
timestatus_klemm(const lame_global_flags * const gfp)165 void timestatus_klemm ( const lame_global_flags* const gfp )
166 {
167     static double  last_time = 0.;
168 
169     if ( !silent )
170         if ( gfp->frameNum == 0  ||
171   	     gfp->frameNum == 9  ||
172   	     GetRealTime () - last_time >= update_interval  ||
173 	     GetRealTime () - last_time <  0 ) {
174 #ifdef BRHIST
175             brhist_jump_back();
176 #endif
177             timestatus ( lame_get_out_samplerate( gfp ),
178                          gfp->frameNum,
179                          gfp->totalframes,
180                          gfp->framesize );
181 #ifdef BRHIST
182             if ( brhist ) {
183 	        brhist_disp ( gfp );
184 	    }
185 #endif
186             last_time = GetRealTime ();  /* from now! disp_time seconds */
187         }
188 }
189 
190 /* these functions are used in get_audio.c */
191 
decoder_progress(const lame_global_flags * const gfp,const mp3data_struct * const mp3data)192 void decoder_progress ( const lame_global_flags* const gfp, const mp3data_struct* const mp3data )
193 {
194     static int  last;
195     fprintf ( stderr, "\rFrame#%6i/%-6i %3i kbps",
196               mp3data->framenum, mp3data->totalframes, mp3data->bitrate );
197 
198     // Programmed with a single frame hold delay
199     // Attention: static data
200 
201     // MP2 Playback is still buggy.
202     // "'00' subbands 4-31 in intensity_stereo, bound==4"
203     // is this really intensity_stereo or is it MS stereo?
204 
205     if ( mp3data->mode == JOINT_STEREO ) {
206         int         curr = mp3data->mode_ext;
207         fprintf ( stderr, "  %s  %c" ,
208                   curr&2  ?  last&2 ? " MS " : "LMSR"  :  last&2 ? "LMSR" : "L  R",
209                   curr&1  ?  last&1 ? 'I'    : 'i'     :  last&1 ? 'i'    : ' ' );
210         last = curr;
211     } else {
212         fprintf ( stderr, "         " );
213 	last = 0;
214     }
215 //    fprintf ( stderr, "%s", Console_IO.str_clreoln );
216       fprintf ( stderr, "        \b\b\b\b\b\b\b\b" );
217       fflush  ( stderr );
218 }
219 
decoder_progress_finish(const lame_global_flags * const gfp)220 void decoder_progress_finish ( const lame_global_flags* const gfp )
221 {
222     fprintf ( stderr, "\n" );
223 }
224 
225