audio/mp3dec: add -s SECONDS option

This commit is contained in:
Sigrid 2020-08-12 10:43:46 +02:00
parent 5fcf2040b4
commit 81dba13271
2 changed files with 28 additions and 1 deletions

View file

@ -4,6 +4,9 @@ mp3dec, mp3enc, oggdec, oggenc, flacdec, sundec, wavdec, pcmconv, mixfs \- decod
.SH SYNOPSIS .SH SYNOPSIS
.B audio/mp3dec .B audio/mp3dec
[ [
.B -s
.I seconds
] [
.B -d .B -d
] ]
.br .br

View file

@ -7,6 +7,7 @@
/* Current input file */ /* Current input file */
vlong offset; vlong offset;
double seekto = 0.0, curpos = 0.0;
int debug = 0; int debug = 0;
int ifd = -1; int ifd = -1;
@ -29,6 +30,22 @@ input(void *, struct mad_stream *stream)
return MAD_FLOW_CONTINUE; return MAD_FLOW_CONTINUE;
} }
static enum mad_flow
header(void *, struct mad_header const* header)
{
if(seekto > 0 && (header->duration.seconds > 0 || header->duration.fraction > 0)){
double dur = header->duration.seconds + (double)header->duration.fraction / MAD_TIMER_RESOLUTION;
seekto -= dur;
if(seekto > 0){
curpos += dur;
return MAD_FLOW_IGNORE;
}
fprint(2, "time: %g\n", curpos);
seekto = 0;
}
return MAD_FLOW_CONTINUE;
}
static enum mad_flow static enum mad_flow
output(void *, struct mad_header const* header, struct mad_pcm *pcm) output(void *, struct mad_header const* header, struct mad_pcm *pcm)
{ {
@ -39,6 +56,9 @@ output(void *, struct mad_header const* header, struct mad_pcm *pcm)
int i, j, n; int i, j, n;
uchar *p; uchar *p;
if(seekto > 0)
return MAD_FLOW_CONTINUE;
/* start converter if format changed */ /* start converter if format changed */
if(rate != pcm->samplerate || chans != pcm->channels){ if(rate != pcm->samplerate || chans != pcm->channels){
int pid, pfd[2]; int pid, pfd[2];
@ -145,11 +165,15 @@ main(int argc, char **argv)
case 'd': case 'd':
debug++; debug++;
break; break;
case 's':
seekto = atof(EARGF(usage()));
if(seekto >= 0.0)
break;
default: default:
usage(); usage();
}ARGEND }ARGEND
mad_decoder_init(&decoder, nil, input, nil, nil, output, error, nil); mad_decoder_init(&decoder, nil, input, header, nil, output, error, nil);
mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC); mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
mad_decoder_finish(&decoder); mad_decoder_finish(&decoder);