aboutsummaryrefslogtreecommitdiff
path: root/find_key
diff options
context:
space:
mode:
Diffstat (limited to 'find_key')
-rwxr-xr-xfind_key48
1 files changed, 32 insertions, 16 deletions
diff --git a/find_key b/find_key
index 5e5e035..c391e65 100755
--- a/find_key
+++ b/find_key
@@ -55,8 +55,9 @@ $0: Find key of music in audio (or maybe video) files.
Usage: $0 [-m|-M] file.wav [file.wav ...]
--m: Force major keys to relative minor
--M: Force minor keys to relative major
+-q: Quiet, only output the most likely key.
+-m: Force major keys to relative minor.
+-M: Force minor keys to relative major.
EOF
exit 0;
}
@@ -70,6 +71,9 @@ for(@ARGV) {
$fold_major = 0;
$fold_minor = 1;
next;
+ } elsif(/^-q/) {
+ $quiet++;
+ next;
}
my $sec;
@@ -79,22 +83,23 @@ for(@ARGV) {
$sf = Audio::SndFile->open("<", $_);
};
if($@) {
- warn "Extracting to tmp.wav\n";
- system("mplayer -vo null -ao pcm:fast:file=tmp.wav \"$_\"");
+ my $rq = $quiet ? "-really-quiet" : "";
+ warn "Extracting to tmp.wav\n" unless $quiet;
+ system("mplayer $rq -vo null -ao pcm:fast:file=tmp.wav \"$_\"");
$sf = Audio::SndFile->open("<", "tmp.wav");
$_ = "tmp.wav";
}
$sec = ($sf->frames) * (1 / $sf->samplerate);
}
- printf "file is %03.2f sec\n", $sec;
+ printf "file is %03.2f sec\n", $sec unless $quiet;
my $oldstamp = -1;
my $oldkey = -1;
- my $got = `vamp-simple-host qm-vamp-plugins:qm-keydetector "$_" 2`;
+ my $got = `vamp-simple-host qm-vamp-plugins:qm-keydetector "$_" 2 2>/dev/null`;
die "Analysis failed\n" unless defined $got;
- print "\n";
+ print "\n" unless $quiet;
my @got = split "\n", $got;
## for my $line (@got) {
@@ -122,24 +127,25 @@ for(@ARGV) {
} elsif($fold_minor) {
$key = get_relative_minor($key);
}
- if($key =~ /m$/) {
- $relkey = get_relative_major($key);
- } else {
- $relkey = get_relative_minor($key);
- }
+ $relkey = get_relative_key($key);
if($oldstamp != -1) {
$times{$oldkey} += ($stamp - $oldstamp);
}
$oldstamp = $stamp;
$oldkey = $key;
- print "$stamp: $key ($relkey)\n";
+ print "$stamp: $key ($relkey)\n" unless $quiet;
}
$times{$oldkey} += ($sec - $oldstamp);
- print "\n";
- for(sort { $times{$b} <=> $times{$a} } keys %times) {
- printf "%3s: %4.2f sec, %4.2f%%\n", $_, $times{$_}, $times{$_} / $sec * 100;
+ print "\n" unless $quiet;
+ my @sorted = sort { $times{$b} <=> $times{$a} } keys %times;
+ if($quiet) {
+ print $sorted[0] . " (" . get_relative_key($sorted[0]) . ")\n";
+ } else {
+ for(@sorted) {
+ printf "%3s: %4.2f sec, %4.2f%%\n", $_, $times{$_}, $times{$_} / $sec * 100;
+ }
}
}
@@ -153,6 +159,16 @@ sub get_relative_minor {
return $relative_minors{$key} || $key;
}
+sub get_relative_key {
+ my $key = shift || die "missing key";
+ if($key =~ /m$/) {
+ $relkey = get_relative_major($key);
+ } else {
+ $relkey = get_relative_minor($key);
+ }
+ return $relkey;
+}
+
__END__
Output of plugin:
- Estimated key (from C major = 1 to B major = 12 and C minor = 13 to B minor = 24)