aboutsummaryrefslogtreecommitdiff
path: root/find_tuning
blob: aee1be3015bd1be12ad6576093bcbb86be69bd36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl -w

# 20110531 bkw: dirt-stupid wrapper for nnls-chroma's tuning estimator.
# Depends on vamp-plugin-sdk and nnls-chroma. Will choke on some filenames
# (e.g. ones with double-quotes, backticks, dollar signs...)

# 20121106 bkw: Adding support for other formats besides wav/ogg/flac.

# Although vamp-simple-host uses libsndfile, which supports ogg/vorbis,
# in practice it doesn't work real well: takes 120x (!) as long to analyze
# an ogg file as it does to extract with oggdec and analyze that.

# Basically, anything that isn't named .wav or .flac will get decoded to wav
# with mplayer. This means we can actually use find_tuning on video files.
# It also means we're trusting the input filename extension to be correct!

if($ARGV[0] && $ARGV[0] eq '-q') {
	$quiet++;
	shift;
}

for(@ARGV) {
	my $infile;
	my $rm_infile = 0;
	my $vamperrfile = "/tmp/find_tuning_$$.vamperrs";

	if(/\.(?:wav|flac)$/i) {
		$infile = $_;
	} else {
		$infile = "/tmp/find_tuning_$$.wav";
		#$rm_infile = 1;
		warn "Decoding $_ with mplayer\n" unless $quiet;
		system("mplayer -really-quiet -benchmark -vo null -ao pcm:fast:file=\"$infile\" \"$_\"");
	}

	warn "Analyzing with nnls-chroma\n" unless $quiet;
	$freq = `vamp-simple-host nnls-chroma.so:tuning "$infile" 2>$vamperrfile`;
	chomp $freq;

	$freq =~ s/.*: //s || do {
		$errs = `cat $vamperrfile`;
		die "Analysis failed: $errs\n";
	};

	$freq =~ s/\s.*//;
	$cents = 1200 * (log($freq/440)/log(2));

	if($quiet) {
		printf "%+2.2f\n", $cents;
	} else {
		printf "\n$_: A = $freq Hz, %+2.2f cents\n", $cents;
	}

	unlink $infile if $rm_infile;
	unlink $vamperrfile;
}