| 1 |
/* |
| 2 |
common: misc stuff... audio flush, status display... |
| 3 |
|
| 4 |
copyright ?-2023 by the mpg123 project |
| 5 |
free software under the terms of the LGPL 2.1 |
| 6 |
|
| 7 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 8 |
initially written by Michael Hipp |
| 9 |
*/ |
| 10 |
|
| 11 |
/* Need snprintf. */ |
| 12 |
#define _DEFAULT_SOURCE |
| 13 |
#define _BSD_SOURCE |
| 14 |
#include "mpg123app.h" |
| 15 |
#include "out123.h" |
| 16 |
#include <sys/stat.h> |
| 17 |
#include "common.h" |
| 18 |
#include "terms.h" |
| 19 |
#include "metaprint.h" |
| 20 |
|
| 21 |
#include "common/debug.h" |
| 22 |
|
| 23 |
enum player_state playstate = STATE_PLAYING; |
| 24 |
const char playsym[STATE_COUNT] = { '>', '_', '=', '?' }; |
| 25 |
int muted = 0; |
| 26 |
// On LFS conversion trouble with large files, print_stat() gets disabled. |
| 27 |
// Some heuristic re-enables it (when you print headers). |
| 28 |
static int print_stat_disabled = FALSE; |
| 29 |
|
| 30 |
const char* rva_name[3] = { "off", "mix", "album" }; |
| 31 |
static const char* rva_statname[3] = { "---", "mix", "alb" }; |
| 32 |
static const char *modes[5] = {"Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel", "Invalid" }; |
| 33 |
static const char *smodes[5] = { "stereo", "j-s", "dual", "mono", "o.O" }; |
| 34 |
static const char *layers[4] = { "Unknown" , "I", "II", "III" }; |
| 35 |
static const char *versions[4] = {"1.0", "2.0", "2.5", "x.x" }; |
| 36 |
static const int samples_per_frame[4][4] = |
| 37 |
{ |
| 38 |
{ -1,384,1152,1152 }, /* MPEG 1 */ |
| 39 |
{ -1,384,1152,576 }, /* MPEG 2 */ |
| 40 |
{ -1,384,1152,576 }, /* MPEG 2.5 */ |
| 41 |
{ -1,-1,-1,-1 }, /* Unknown */ |
| 42 |
}; |
| 43 |
|
| 44 |
/* concurring to print_rheader... here for control_generic */ |
| 45 |
const char* remote_header_help = "S <mpeg-version> <layer> <sampling freq> <mode(stereo/mono/...)> <mode_ext> <framesize> <stereo> <copyright> <error_protected> <emphasis> <bitrate> <extension> <vbr(0/1=yes/no)>"; |
| 46 |
void print_remote_header(mpg123_handle *mh) |
| 47 |
{ |
| 48 |
struct mpg123_frameinfo i; |
| 49 |
mpg123_info(mh, &i); |
| 50 |
if(i.mode >= 4 || i.mode < 0) i.mode = 4; |
| 51 |
if(i.version >= 3 || i.version < 0) i.version = 3; |
| 52 |
generic_sendmsg("S %s %d %ld %s %d %d %d %d %d %d %d %d %d", |
| 53 |
versions[i.version], |
| 54 |
i.layer, |
| 55 |
i.rate, |
| 56 |
modes[i.mode], |
| 57 |
i.mode_ext, |
| 58 |
i.framesize, |
| 59 |
i.mode == MPG123_M_MONO ? 1 : 2, |
| 60 |
i.flags & MPG123_COPYRIGHT ? 1 : 0, |
| 61 |
i.flags & MPG123_CRC ? 1 : 0, |
| 62 |
i.emphasis, |
| 63 |
i.bitrate, |
| 64 |
i.flags & MPG123_PRIVATE ? 1 : 0, |
| 65 |
i.vbr); |
| 66 |
print_stat_disabled=FALSE; |
| 67 |
} |
| 68 |
|
| 69 |
void print_header(mpg123_handle *mh) |
| 70 |
{ |
| 71 |
struct mpg123_frameinfo i; |
| 72 |
mpg123_info(mh, &i); |
| 73 |
if(i.mode > 4 || i.mode < 0) i.mode = 4; |
| 74 |
if(i.version > 3 || i.version < 0) i.version = 3; |
| 75 |
if(i.layer > 3 || i.layer < 0) i.layer = 0; |
| 76 |
fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", |
| 77 |
versions[i.version], |
| 78 |
layers[i.layer], i.rate, |
| 79 |
modes[i.mode],i.mode_ext,i.framesize); |
| 80 |
fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n", |
| 81 |
i.mode == MPG123_M_MONO ? 1 : 2,i.flags & MPG123_COPYRIGHT ? "Yes" : "No", |
| 82 |
i.flags & MPG123_ORIGINAL ? "Yes" : "No", i.flags & MPG123_CRC ? "Yes" : "No", |
| 83 |
i.emphasis); |
| 84 |
fprintf(stderr,"Bitrate: "); |
| 85 |
switch(i.vbr) |
| 86 |
{ |
| 87 |
case MPG123_CBR: |
| 88 |
if(i.bitrate) fprintf(stderr, "%d kbit/s", i.bitrate); |
| 89 |
else fprintf(stderr, "%d kbit/s (free format)", (int)((double)(i.framesize+4)*8*i.rate*0.001/samples_per_frame[i.version][i.layer]+0.5)); |
| 90 |
break; |
| 91 |
case MPG123_VBR: fprintf(stderr, "VBR"); break; |
| 92 |
case MPG123_ABR: fprintf(stderr, "%d kbit/s ABR", i.abr_rate); break; |
| 93 |
default: fprintf(stderr, "???"); |
| 94 |
} |
| 95 |
fprintf(stderr, " Extension value: %d\n", i.flags & MPG123_PRIVATE ? 1 : 0); |
| 96 |
print_stat_disabled=FALSE; |
| 97 |
} |
| 98 |
|
| 99 |
void print_header_compact(mpg123_handle *mh) |
| 100 |
{ |
| 101 |
struct mpg123_frameinfo i; |
| 102 |
mpg123_info(mh, &i); |
| 103 |
if(i.mode > 4 || i.mode < 0) i.mode = 4; |
| 104 |
if(i.version > 3 || i.version < 0) i.version = 3; |
| 105 |
if(i.layer > 3 || i.layer < 0) i.layer = 0; |
| 106 |
|
| 107 |
fprintf(stderr,"MPEG %s L %s ", versions[i.version], layers[i.layer]); |
| 108 |
switch(i.vbr) |
| 109 |
{ |
| 110 |
case MPG123_CBR: |
| 111 |
if(i.bitrate) fprintf(stderr, "cbr%d", i.bitrate); |
| 112 |
else fprintf(stderr, "cbr%d", (int)((double)i.framesize*8*i.rate*0.001/samples_per_frame[i.version][i.layer]+0.5)); |
| 113 |
break; |
| 114 |
case MPG123_VBR: fprintf(stderr, "vbr"); break; |
| 115 |
case MPG123_ABR: fprintf(stderr, "abr%d", i.abr_rate); break; |
| 116 |
default: fprintf(stderr, "???"); |
| 117 |
} |
| 118 |
fprintf(stderr," %ld %s\n", i.rate, smodes[i.mode]); |
| 119 |
print_stat_disabled=FALSE; |
| 120 |
} |
| 121 |
|
| 122 |
unsigned int roundui(double val) |
| 123 |
{ |
| 124 |
double base = floor(val); |
| 125 |
return (unsigned int) ((val-base) < 0.5 ? base : base + 1 ); |
| 126 |
} |
| 127 |
|
| 128 |
/* Split into mm:ss.xx or hh:mm:ss, depending on value. */ |
| 129 |
static void settle_time(double tim, unsigned long *times, char *sep) |
| 130 |
{ |
| 131 |
if(tim >= 3600.) |
| 132 |
{ |
| 133 |
*sep = ':'; |
| 134 |
times[0] = (unsigned long) tim/3600; |
| 135 |
tim -= times[0]*3600; |
| 136 |
times[1] = (unsigned long) tim/60; |
| 137 |
tim -= times[1]*60; |
| 138 |
times[2] = (unsigned long) tim; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
*sep = '.'; |
| 143 |
times[0] = (unsigned long) tim/60; |
| 144 |
times[1] = (unsigned long) tim%60; |
| 145 |
times[2] = (unsigned long) (tim*100)%100; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* Print output buffer fill. */ |
| 150 |
void print_buf(const char* prefix, out123_handle *ao) |
| 151 |
{ |
| 152 |
long rate; |
| 153 |
int framesize; |
| 154 |
double tim; |
| 155 |
unsigned long times[3]; |
| 156 |
char timesep; |
| 157 |
size_t buffsize; |
| 158 |
|
| 159 |
buffsize = out123_buffered(ao); |
| 160 |
if(out123_getformat(ao, &rate, NULL, NULL, &framesize)) |
| 161 |
return; |
| 162 |
tim = (double)(buffsize/framesize)/rate; |
| 163 |
settle_time(tim, times, ×ep); |
| 164 |
fprintf( stderr, "\r%s[%02lu:%02lu%c%02lu]" |
| 165 |
, prefix, times[0], times[1], timesep, times[2] ); |
| 166 |
} |
| 167 |
|
| 168 |
// This is a massively complicated function just for telling where we are. |
| 169 |
// Blame buffering. Blame format conversion. Blame the universe. |
| 170 |
int position_info( mpg123_handle *fr, off_t offset, out123_handle *ao |
| 171 |
, off_t *frame, off_t *frame_remain |
| 172 |
, double *seconds, double *seconds_remain, double *seconds_buffered, double *seconds_total ) |
| 173 |
{ |
| 174 |
size_t buffered; |
| 175 |
off_t decoded; |
| 176 |
double elapsed; |
| 177 |
double remain; |
| 178 |
double length; |
| 179 |
off_t frameo; |
| 180 |
off_t frames; |
| 181 |
off_t rframes; |
| 182 |
int framesize; |
| 183 |
int spf; |
| 184 |
long inrate; |
| 185 |
long rate; |
| 186 |
if(mpg123_getformat(fr, &inrate, NULL, NULL) || inrate < 1) |
| 187 |
return -1; |
| 188 |
if(out123_getformat(ao, &rate, NULL, NULL, &framesize) || rate < 1 || framesize < 1) |
| 189 |
return -1; |
| 190 |
buffered = out123_buffered(ao)/framesize; |
| 191 |
decoded = mpg123_tell(fr); |
| 192 |
length = (double)mpg123_length(fr)/inrate; |
| 193 |
frameo = mpg123_tellframe(fr); |
| 194 |
frames = mpg123_framelength(fr); |
| 195 |
spf = mpg123_spf(fr); |
| 196 |
if(decoded < 0 || length < 0 || frameo < 0 || frames <= 0 || spf <= 0) |
| 197 |
{ |
| 198 |
merror("Failed to gather position data: %s", mpg123_strerror(fr)); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
frameo += offset; |
| 202 |
if(frameo < 0) |
| 203 |
frameo = 0; |
| 204 |
/* Some sensible logic around offsets and time. |
| 205 |
Buffering makes the relationships between the numbers non-trivial. */ |
| 206 |
rframes = frames-frameo; |
| 207 |
// May be negative, a countdown. Buffer only confuses in paused (looping) mode, though. |
| 208 |
elapsed = (double)(decoded + offset*spf)/inrate - (double)(playstate==STATE_LOOPING ? 0 : buffered)/rate; |
| 209 |
remain = elapsed > 0 ? length - elapsed : length; |
| 210 |
|
| 211 |
if(frame) |
| 212 |
*frame = frameo; |
| 213 |
if(frame_remain) |
| 214 |
*frame_remain = rframes; |
| 215 |
if(seconds) |
| 216 |
*seconds = elapsed; |
| 217 |
if(seconds_remain) |
| 218 |
*seconds_remain = remain; |
| 219 |
if(seconds_buffered) |
| 220 |
*seconds_buffered = (double)buffered/rate; |
| 221 |
if(seconds_total) |
| 222 |
*seconds_total = length; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/* Note about position info with buffering: |
| 229 |
Negative positions mean that the previous track is still playing from the |
| 230 |
buffer. It's a countdown. The frame counter always relates to the last |
| 231 |
decoded frame, what entered the buffer right now. */ |
| 232 |
void print_stat(mpg123_handle *fr, long offset, out123_handle *ao, int draw_bar |
| 233 |
, struct parameter *param) |
| 234 |
{ |
| 235 |
if(print_stat_disabled) |
| 236 |
return; |
| 237 |
static int old_term_width = -1; |
| 238 |
double basevol, realvol; |
| 239 |
double elapsed; |
| 240 |
double remain; |
| 241 |
double bufsec; |
| 242 |
double length; |
| 243 |
off_t frame; |
| 244 |
off_t rframes; |
| 245 |
struct mpg123_frameinfo mi; |
| 246 |
char linebuf[256]; |
| 247 |
char *line = NULL; |
| 248 |
|
| 249 |
#ifndef __OS2__ |
| 250 |
#ifndef _WIN32 |
| 251 |
#ifndef GENERIC |
| 252 |
/* Only generate new stat line when stderr is ready... don't overfill... */ |
| 253 |
{ |
| 254 |
struct timeval t; |
| 255 |
fd_set serr; |
| 256 |
int n,errfd = fileno(stderr); |
| 257 |
|
| 258 |
t.tv_sec=t.tv_usec=0; |
| 259 |
|
| 260 |
FD_ZERO(&serr); |
| 261 |
FD_SET(errfd,&serr); |
| 262 |
n = select(errfd+1,NULL,&serr,NULL,&t); |
| 263 |
if(n <= 0) return; |
| 264 |
} |
| 265 |
#endif |
| 266 |
#endif |
| 267 |
#endif |
| 268 |
if(position_info(fr, offset, ao, &frame, &rframes, &elapsed, &remain, &bufsec, &length)) |
| 269 |
{ |
| 270 |
debug("position_info() failed"); |
| 271 |
print_stat_disabled=TRUE; |
| 272 |
return; |
| 273 |
} |
| 274 |
if( MPG123_OK == mpg123_info(fr, &mi) |
| 275 |
&& MPG123_OK == mpg123_getvolume(fr, &basevol, &realvol, NULL) ) |
| 276 |
{ |
| 277 |
char framefmt[10]; |
| 278 |
char framestr[2][32]; |
| 279 |
int linelen; |
| 280 |
int maxlen; |
| 281 |
int len; |
| 282 |
int ti; |
| 283 |
/* Deal with overly long times. */ |
| 284 |
double tim[3]; |
| 285 |
unsigned long times[3][3]; |
| 286 |
char timesep[3]; |
| 287 |
char sign[3] = {' ', ' ', ' '}; |
| 288 |
|
| 289 |
/* 255 is enough for the data I prepare, if there is no terminal width to |
| 290 |
fill */ |
| 291 |
maxlen = term_width(STDERR_FILENO); |
| 292 |
if(draw_bar && maxlen > 0 && maxlen < old_term_width) |
| 293 |
{ |
| 294 |
// Hack about draw_bar: That's the normal print_stat that is not followed by |
| 295 |
// metadata anyway. No need to double things. |
| 296 |
print_stat(fr, offset, ao, 0, param); |
| 297 |
if(param->verbose > 2) |
| 298 |
fprintf(stderr,"Note: readjusting for smaller terminal (%d to %d)\n", old_term_width, maxlen); |
| 299 |
fprintf(stderr, "\n\n\n"); |
| 300 |
if(param->verbose > 1) |
| 301 |
print_header(fr); |
| 302 |
else |
| 303 |
print_header_compact(fr); |
| 304 |
print_id3_tag(fr, param->long_id3, stderr, maxlen); |
| 305 |
} |
| 306 |
if(draw_bar) |
| 307 |
old_term_width = maxlen; |
| 308 |
linelen = maxlen > 0 ? maxlen : (sizeof(linebuf)-1); |
| 309 |
line = linelen >= sizeof(linebuf) |
| 310 |
? malloc(linelen+1) /* Only malloc if it is a really long line. */ |
| 311 |
: linebuf; /* Small buffer on stack is enough. */ |
| 312 |
|
| 313 |
tim[0] = elapsed; |
| 314 |
tim[1] = remain; |
| 315 |
tim[2] = bufsec; |
| 316 |
for(ti=0; ti<3; ++ti) |
| 317 |
{ |
| 318 |
if(tim[ti] < 0.){ sign[ti] = '-'; tim[ti] = -tim[ti]; } |
| 319 |
settle_time(tim[ti], times[ti], ×ep[ti]); |
| 320 |
} |
| 321 |
/* Taking pains to properly size the frame number fields. */ |
| 322 |
len = snprintf( framefmt, sizeof(framefmt) |
| 323 |
, "%%0%d" PRIiMAX, (int)log10(frame+rframes)+1 ); |
| 324 |
if(len < 0 || len >= sizeof(framefmt)) |
| 325 |
memcpy(framefmt, "%05" PRIiMAX, sizeof("%05" PRIiMAX)); |
| 326 |
snprintf( framestr[0], sizeof(framestr[0])-1, framefmt, (intmax_t)frame); |
| 327 |
framestr[0][sizeof(framestr[0])-1] = 0; |
| 328 |
snprintf( framestr[1], sizeof(framestr[1])-1, framefmt, (intmax_t)rframes); |
| 329 |
framestr[1][sizeof(framestr[1])-1] = 0; |
| 330 |
/* Now start with the state line. */ |
| 331 |
memset(line, 0, linelen+1); /* Always one zero more. */ |
| 332 |
/* Start with position info. */ |
| 333 |
len = snprintf( line, linelen |
| 334 |
, "%c %s+%s %c%02lu:%02lu%c%02lu+%02lu:%02lu%c%02lu" |
| 335 |
, playsym[playstate] |
| 336 |
, framestr[0], framestr[1] |
| 337 |
, sign[0] |
| 338 |
, times[0][0], times[0][1], timesep[0], times[0][2] |
| 339 |
, times[1][0], times[1][1], timesep[1], times[1][2] |
| 340 |
); |
| 341 |
/* Just cut it. */ |
| 342 |
if(len >= linelen) |
| 343 |
len=linelen; |
| 344 |
if(len >= 0 && param->usebuffer && len < linelen ) |
| 345 |
{ /* Buffer info. */ |
| 346 |
int len_add = snprintf( line+len, linelen-len |
| 347 |
, " [%02lu:%02lu%c%02lu]" |
| 348 |
, times[2][0], times[2][1], timesep[2], times[2][2] ); |
| 349 |
if(len_add > 0) |
| 350 |
len += len_add; |
| 351 |
} |
| 352 |
if(len >= 0 && len < linelen) |
| 353 |
{ /* Volume info. */ |
| 354 |
int len_add = snprintf( line+len, linelen-len |
| 355 |
, " %s %03u%c%03u" |
| 356 |
, rva_statname[param->rva] |
| 357 |
, roundui(basevol*100), muted ? 'm' : '=' |
| 358 |
, roundui(realvol*100) ); |
| 359 |
if(len_add > 0) |
| 360 |
len += len_add; |
| 361 |
} |
| 362 |
if(len >= 0 && len < linelen) |
| 363 |
{ /* Bitrate. */ |
| 364 |
int len_add = snprintf( line+len, linelen-len |
| 365 |
, " %3d kb/s", mi.bitrate ); |
| 366 |
if(len_add > 0) |
| 367 |
len += len_add; |
| 368 |
} |
| 369 |
if(len >= 0 && len < linelen) |
| 370 |
{ /* Size of frame in bytes. */ |
| 371 |
int len_add = snprintf( line+len, linelen-len |
| 372 |
, " %4d B", mi.framesize ); |
| 373 |
if(len_add > 0) |
| 374 |
len += len_add; |
| 375 |
} |
| 376 |
if(len >= 0 && len < linelen) |
| 377 |
{ /* Size of frame in bytes. */ |
| 378 |
int len_add = 0; |
| 379 |
long res = 0; |
| 380 |
if(mpg123_getstate(fr, MPG123_ACCURATE, &res, NULL) == MPG123_OK) |
| 381 |
len_add = snprintf( line+len, linelen-len |
| 382 |
, " %s", res ? "acc" : "fuz" ); |
| 383 |
if(len_add > 0) |
| 384 |
len += len_add; |
| 385 |
} |
| 386 |
if(len >= 0 && len < linelen) |
| 387 |
{ /* Size of frame in bytes. */ |
| 388 |
int len_add = 0; |
| 389 |
long res = mpg123_clip(fr); |
| 390 |
if(res >= 0) |
| 391 |
len_add = snprintf( line+len, linelen-len |
| 392 |
, " %4ld clip", res ); |
| 393 |
if(len_add > 0) |
| 394 |
len += len_add; |
| 395 |
} |
| 396 |
if(len >= 0 && len < linelen) |
| 397 |
{ /* Size of frame in bytes. */ |
| 398 |
int len_add = 0; |
| 399 |
len_add = snprintf( line+len, linelen-len |
| 400 |
, " p%+.3f", param->pitch ); |
| 401 |
if(len_add > 0) |
| 402 |
len += len_add; |
| 403 |
} |
| 404 |
if(len >= 0) |
| 405 |
{ |
| 406 |
if(maxlen > 0 && len > maxlen) |
| 407 |
{ |
| 408 |
/* Emergency cut to avoid terminal scrolling. */ |
| 409 |
int i; |
| 410 |
/* Blank a word that would have been cut off. */ |
| 411 |
for(i=maxlen; i>=0; --i) |
| 412 |
{ |
| 413 |
char old = line[i]; |
| 414 |
line[i] = ' '; |
| 415 |
if(old == ' ') |
| 416 |
break; |
| 417 |
} |
| 418 |
line[maxlen] = 0; |
| 419 |
len = maxlen; |
| 420 |
} |
| 421 |
/* Ensure that it is filled with spaces if we got some line length. |
| 422 |
Shouldn't we always fill to maxlen? */ |
| 423 |
if(maxlen > 0) |
| 424 |
memset(line+len, ' ', linelen-len); |
| 425 |
draw_bar = draw_bar && term_have_fun(STDERR_FILENO,param->term_visual); |
| 426 |
/* Use inverse color to draw a progress bar. */ |
| 427 |
if(maxlen > 0 && draw_bar) |
| 428 |
{ |
| 429 |
char old; |
| 430 |
int barlen = 0; |
| 431 |
if(length > 0 && elapsed > 0) |
| 432 |
{ |
| 433 |
if(elapsed < length) |
| 434 |
barlen = (int)((double)elapsed/length * maxlen); |
| 435 |
else |
| 436 |
barlen = maxlen; |
| 437 |
} |
| 438 |
old = line[barlen]; |
| 439 |
fprintf(stderr, "\x1b[7m"); |
| 440 |
line[barlen] = 0; |
| 441 |
fprintf(stderr, "\r%s", line); |
| 442 |
line[barlen] = old; |
| 443 |
fprintf(stderr, "\x1b[0m"); |
| 444 |
fprintf(stderr, "%s", line+barlen); |
| 445 |
} |
| 446 |
else |
| 447 |
fprintf(stderr, "\r%s", line); |
| 448 |
} |
| 449 |
} |
| 450 |
if(line && line != linebuf) |
| 451 |
free(line); |
| 452 |
} |
| 453 |
|
| 454 |
void clear_stat(void) |
| 455 |
{ |
| 456 |
int len = term_width(STDERR_FILENO); |
| 457 |
if(len > 0) |
| 458 |
{ |
| 459 |
char fmt[20]; |
| 460 |
int flen; |
| 461 |
if( (flen=snprintf(fmt, sizeof(fmt), "\r%%%ds\r", len)) > 0 |
| 462 |
&& flen < sizeof(fmt) ) |
| 463 |
fprintf(stderr, fmt, " "); |
| 464 |
} |
| 465 |
} |