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 TMP=`mktemp -t bpm.XXXXXX` || exit 1
trap "rm $TMP" 0 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\"" 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 -f Ignore existing BPM value
-n Display BPM only, don't tag -n Display BPM only, don't tag
-m Minimum detected BPM
-x Maximum detected BPM
-h Display this help message and exit -h Display this help message and exit
END END
@ -42,8 +44,9 @@ END
FORCE=false FORCE=false
WRITE=true WRITE=true
ARGS=""
while getopts "fhn" OPT; do while getopts "fhnm:x:" OPT; do
case "$OPT" in case "$OPT" in
f) f)
FORCE=true FORCE=true
@ -51,6 +54,12 @@ while getopts "fhn" OPT; do
n) n)
WRITE=false WRITE=false
;; ;;
m)
ARGS="$ARGS -m $OPTARG"
;;
x)
ARGS="$ARGS -x $OPTARG"
;;
h) h)
usage usage
exit 0 exit 0
@ -96,7 +105,7 @@ fi
# Analyse the BPM # 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 if [ -z "$BPM" ]; then
exit 1 exit 1
fi fi

21
bpm.c
View File

@ -29,6 +29,8 @@
#define NAME "bpm" #define NAME "bpm"
#define RATE 44100 /* of input data */ #define RATE 44100 /* of input data */
#define LOWER 72.0
#define UPPER 168.0
#define BLOCK 4096 #define BLOCK 4096
#define INTERVAL 128 #define INTERVAL 128
@ -165,8 +167,11 @@ void usage(FILE *f)
" -g <path> Output autodifference data to file\n" " -g <path> Output autodifference data to file\n"
" -e <path> Output energy data to file\n" " -e <path> Output energy data to file\n"
" -f Print format for final BPM value (default \"%%0.1f\")\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" " -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" 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", " $ 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; float *nrg = NULL;
size_t len = 0, buf = 0; size_t len = 0, buf = 0;
off_t n = 0; off_t n = 0;
double bpm, v = 0.0; double bpm, min = LOWER, max = UPPER, v = 0.0;
const char *format = "%0.3f"; const char *format = "%0.3f";
FILE *fdiff = NULL, *fnrg = NULL; FILE *fdiff = NULL, *fnrg = NULL;
for (;;) { for (;;) {
int c; int c;
c = getopt(argc, argv, "vf:g:e:h"); c = getopt(argc, argv, "vf:g:e:m:x:h");
if (c == -1) if (c == -1)
break; break;
@ -215,6 +220,14 @@ int main(int argc, char *argv[])
} }
break; break;
case 'm':
min = atof(optarg);
break;
case 'x':
max = atof(optarg);
break;
case 'h': case 'h':
usage(stdout); usage(stdout);
return 0; return 0;
@ -276,7 +289,7 @@ int main(int argc, char *argv[])
nrg[len++] = v; 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); printf(format, bpm);
putc('\n', stdout); putc('\n', stdout);