Command line options for min/max of range

This commit is contained in:
Mark Hills 2011-12-18 12:02:11 +00:00
parent 4597d2f89c
commit 9c5dbc91a0
3 changed files with 29 additions and 7 deletions

View File

@ -15,5 +15,5 @@ set -u
TMP=`mktemp -t bpm.XXXXXX` || exit 1
trap "rm $TMP" 0
sox "$1" -r 44100 -e float -c 1 -t raw - | bpm -g $TMP
sox "$1" -r 44100 -e float -c 1 -t raw - | bpm -m 60 -x 180 -g $TMP
gnuplot -p -e "set style data lines; plot \"$TMP\""

13
bpm-tag
View File

@ -33,6 +33,8 @@ Tag an audio file with tempo (in beats-per-minute, BPM)
-f Ignore existing BPM value
-n Display BPM only, don't tag
-m Minimum detected BPM
-x Maximum detected BPM
-h Display this help message and exit
END
@ -42,8 +44,9 @@ END
FORCE=false
WRITE=true
ARGS=""
while getopts "fhn" OPT; do
while getopts "fhnm:x:" OPT; do
case "$OPT" in
f)
FORCE=true
@ -51,6 +54,12 @@ while getopts "fhn" OPT; do
n)
WRITE=false
;;
m)
ARGS="$ARGS -m $OPTARG"
;;
x)
ARGS="$ARGS -x $OPTARG"
;;
h)
usage
exit 0
@ -96,7 +105,7 @@ fi
# Analyse the BPM
BPM=`sox -V1 "$FILE" -r 44100 -e float -c 1 -t raw - | bpm`
BPM=`sox -V1 "$FILE" -r 44100 -e float -c 1 -t raw - | bpm $ARGS`
if [ -z "$BPM" ]; then
exit 1
fi

21
bpm.c
View File

@ -29,6 +29,8 @@
#define NAME "bpm"
#define RATE 44100 /* of input data */
#define LOWER 72.0
#define UPPER 168.0
#define BLOCK 4096
#define INTERVAL 128
@ -165,8 +167,11 @@ void usage(FILE *f)
" -g <path> Output autodifference data to file\n"
" -e <path> Output energy data to file\n"
" -f Print format for final BPM value (default \"%%0.1f\")\n"
" -m <f> Minimum detected BPM (default %0.0f)\n"
" -x <f> Maximum detected BPM (default %0.0f)\n"
" -v Print progress information to stderr\n"
" -h Display this help message and exit\n\n");
" -h Display this help message and exit\n\n",
LOWER, UPPER);
fprintf(f, "Incoming audio is raw audio on stdin at %dHz, mono, 32-bit float; eg.\n"
" $ sox file.mp3 -t raw -r %d -e float -c 1 - | ./" NAME "\n\n",
@ -183,14 +188,14 @@ int main(int argc, char *argv[])
float *nrg = NULL;
size_t len = 0, buf = 0;
off_t n = 0;
double bpm, v = 0.0;
double bpm, min = LOWER, max = UPPER, v = 0.0;
const char *format = "%0.3f";
FILE *fdiff = NULL, *fnrg = NULL;
for (;;) {
int c;
c = getopt(argc, argv, "vf:g:e:h");
c = getopt(argc, argv, "vf:g:e:m:x:h");
if (c == -1)
break;
@ -215,6 +220,14 @@ int main(int argc, char *argv[])
}
break;
case 'm':
min = atof(optarg);
break;
case 'x':
max = atof(optarg);
break;
case 'h':
usage(stdout);
return 0;
@ -276,7 +289,7 @@ int main(int argc, char *argv[])
nrg[len++] = v;
}
bpm = scan_for_bpm(nrg, len, 72.0, 168.0, 1024, 1024, fdiff);
bpm = scan_for_bpm(nrg, len, min, max, 1024, 1024, fdiff);
printf(format, bpm);
putc('\n', stdout);