summaryrefslogtreecommitdiff
path: root/tools/resample_session.pl
blob: 60bb212b62d4ce3f452cb448730deb777fde9ab9 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env perl
# Ardour session resampler, version 0.1
# (c)Sampo Savolainen 2005-2007
#
# Licensed under the GPL 
#
# Copies the session to another directory and changes it's sampling rate in all respects.
# The frames in .ardour and .automation files are converted according to the conversion ration.
# The peakfiles and dead_sounds aren't copied. Only "identified" files are copied, instant.xml's
# or .bak's aren't copied either.

use FindBin '$Bin';
use lib "$Bin";
use XML::Parser::PerlSAX;
use XML::Handler::XMLWriter;
use IO::Handle;

use File::Basename;

use ARDOUR::SessionSRHandler;
# Let's hope this is never needed
#use ARDOUR::AutomationSRConverter;


if ( ! -f "/usr/bin/sndfile-resample" &&
     ! -f "/usr/local/bin/sndfile-resample") {
	print "You don't have sndfile-resample installed. This script will not work without it, please install it and try again\n";
	exit;
}

my ($sourceDirectory, $destDirectory, $sourceSR, $destSR) = @ARGV;

if ((@ARGV+0) ne 4 ||
    ($sourceSR+0) eq 0 ||
	($destSR+0)   eq 0) {
	print "usage: ardour_sr.pl [ardour session directory] [destination directory] [original samplerate] [new samplerate]\n";
	exit;
}

if ( ! -d $sourceDirectory) {
	print $sourceDirectory.": directory does not exist!\n";
	exit;
}

if ( -d $destDirectory) {
	print $destDirectory.": directory exists!\n";
	exit;
}

print "Checking source and destination directories\n";

my @sounds;
my @dead_sounds;
my @dot_ardour;
my @automation;


my $version_099x = 0;

# Read the names of all audio files in /sounds/
if ( -d $sourceDirectory."/sounds/") {
	$version_099x = 1;

	opendir(SOUNDS,$sourceDirectory."/sounds/") || die ($sourceDirectory.": not a valid session, no sounds/ directory");
	while ( my $file=readdir(SOUNDS) ) {
		if ( -f $sourceDirectory."/sounds/".$file ) {
			push(@sounds,$file);
		}
	}

} else {
	my $dirname = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
	opendir(SOUNDS,$dirname) || die ($sourceDirectory.": not a valid session, no sounds/ directory");
	while ( my $file=readdir(SOUNDS) ) {
		if ( -f $dirname.$file ) {
			push(@sounds,$file);
		}
	}

}
close(SOUNDS);

# Read the names of all audio files in /dead_sounds/
opendir(DEAD_SOUNDS,$sourceDirectory."/dead_sounds/") || die ($sourceDirectory.": not a valid session, no dead_sounds/ directory");
while ( my $file=readdir(DEAD_SOUNDS) ) {
	if ( -f $sourceDirectory."/dead_sounds/".$file ) {
		push(@dead_sounds,$file);
	}
}
close(DEAD_SOUNDS);

# Read the names of all .ardour files in /
opendir(DOT_ARDOUR,$sourceDirectory) || die ($sourceDirectory.": could not open!");
while ( my $file=readdir(DOT_ARDOUR) ) {
	if ( -f $sourceDirectory."/".$file && 
	     index($file,".ardour") eq (length($file)-7)) {
		push(@dot_ardour,$file);
	}
}
close(DOT_ARDOUR);

if ( -d $sourceDirectory."/automation/") {
	# Read the names of all automation files in /automation/
	opendir(AUTOMATION,$sourceDirectory."/automation/") || die ($sourceDirectory."/automation: could not open!");
	while ( my $file=readdir(AUTOMATION) ) {
		if ( -f $sourceDirectory."/automation/".$file && 
		     index($file,".automation") eq (length($file)-11)) {
			push(@automation,$file);
		}
	}
	close(AUTOMATION);
}

# Check for /peaks/
if ( ! -d $sourceDirectory."/peaks" ) {
	print $sourceDirectory.": not a valid session, no peaks/ directory\n";
	exit;
}

##########################################
# Checks are done, let's go!

print "Converting session\n";
mkdir $destDirectory;


# Run all .ardour files through the SAX parser and write the results in the destination
# directory.

foreach my $xml (@dot_ardour) {
	print "Doing samplerate conversion to ".$xml."...";
	open(OUTFILE,">".$destDirectory."/".$xml);
	my $output = new IO::Handle;
	$output->fdopen(fileno(OUTFILE),"w");
	
	my $handler = ARDOUR::SessionSRHandler->new($sourceSR,$destSR,$output);

	my $parser = XML::Parser::PerlSAX->new( Handler => $handler );

    $parser->parse(Source => { SystemId => $sourceDirectory."/".$xml });

	$output->close();
	close(OUTFILE);
	print " done\n";
}

# This code is needed for 0.99.x sessions, thus the code is still here.
#
#mkdir $destDirectory."/automation";
#
#foreach my $file (@automation) {
#	print "Converting automation file ".$file."...";
#	open(INFILE,$sourceDirectory."/automation/".$file) || die "could not open source automation file $file!";
#	open(OUTFILE,">".$destDirectory."/automation/".$file) || die "could not open destination automation file $file!";
#	my $input=new IO::Handle;
#	my $output=new IO::Handle;
#
#	$input->fdopen(fileno(INFILE),"r");
#	$output->fdopen(fileno(OUTFILE),"w");
#
#	my $converter = ARDOUR::AutomationSRConverter->new($input,$output,$sourceSR,$destSR);
#	$converter->convert;
#
#	$input->close;
#	$output->close;
#	close(INFILE);
#	close(OUTFILE);
#	print " done\n";
#}


if ($version_099x eq 1) {
	mkdir $destDirectory."/sounds";
	foreach my $sound (@sounds) {
		my @params=("-to", $destSR,
	        	    "-c",  "0",
			    $sourceDirectory."/sounds/".$sound,
			    $destDirectory."/sounds/".$sound);
		system("sndfile-resample",@params);
	}
} else {
	my $srcSndDir = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
	
	my $dstSndDir = $destDirectory."/interchange/";
	mkdir $dstSndDir;

	$dstSndDir .= basename($sourceDirectory)."/";
	mkdir $dstSndDir;

	$dstSndDir .= "audiofiles/";
	mkdir $dstSndDir;
	
	foreach my $sound (@sounds) {
		my @params=("-to", $destSR,
	        	    "-c",  "0",
			    $srcSndDir."/".$sound,
			    $dstSndDir."/".$sound);
		system("sndfile-resample",@params);
	}
}

mkdir $destDirectory."/dead_sounds";
mkdir $destDirectory."/peaks";