ref: 81dba1327121e0f62fdf42de61402979b7de5da6
parent: 5fcf2040b43d13b6410285d45a0510b637d811c3
author: Sigrid <[email protected]>
date: Wed Aug 12 06:43:46 EDT 2020
audio/mp3dec: add -s SECONDS option
--- a/sys/man/1/audio
+++ b/sys/man/1/audio
@@ -4,6 +4,9 @@
.SH SYNOPSIS
.B audio/mp3dec
[
+.B -s
+.I seconds
+] [
.B -d
]
.br
--- a/sys/src/cmd/audio/mp3dec/main.c
+++ b/sys/src/cmd/audio/mp3dec/main.c
@@ -7,6 +7,7 @@
/* Current input file */
vlong offset;
+double seekto = 0.0, curpos = 0.0;
int debug = 0;
int ifd = -1;
@@ -30,6 +31,22 @@
}
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
output(void *, struct mad_header const* header, struct mad_pcm *pcm)
{
static int rate, chans;
@@ -39,6 +56,9 @@
int i, j, n;
uchar *p;
+ if(seekto > 0)
+ return MAD_FLOW_CONTINUE;
+
/* start converter if format changed */
if(rate != pcm->samplerate || chans != pcm->channels){
int pid, pfd[2];
@@ -145,11 +165,15 @@
case 'd':
debug++;
break;
+ case 's':
+ seekto = atof(EARGF(usage()));
+ if(seekto >= 0.0)
+ break;
default:
usage();
}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_finish(&decoder);