Fix previous.
[ardour2.git] / tools / resample_session.pl
blobeadc5369857f9130e16f122916dc11ec7d8d475a
1 #!/usr/bin/env perl
2 # Ardour session resampler, version 0.1
3 # (c)Sampo Savolainen 2005-2007
5 # Licensed under the GPL
7 # Copies the session to another directory and changes it's sampling rate in all respects.
8 # The frames in .ardour and .automation files are converted according to the conversion ration.
9 # The peakfiles and dead_sounds aren't copied. Only "identified" files are copied, instant.xml's
10 # or .bak's aren't copied either.
12 use FindBin '$Bin';
13 use lib "$Bin";
14 use XML::Parser::PerlSAX;
15 use XML::Handler::XMLWriter;
16 use IO::Handle;
18 use File::Basename;
20 use ARDOUR::SessionSRHandler;
21 # Let's hope this is never needed
22 #use ARDOUR::AutomationSRConverter;
25 if ( ! -f "/usr/bin/sndfile-resample" &&
26 ! -f "/usr/local/bin/sndfile-resample") {
27 print "You don't have sndfile-resample installed. This script will not work without it, please install it and try again\n";
28 exit;
31 my ($sourceDirectory, $destDirectory, $sourceSR, $destSR) = @ARGV;
33 if ((@ARGV+0) ne 4 ||
34 ($sourceSR+0) eq 0 ||
35 ($destSR+0) eq 0) {
36 print "usage: ardour_sr.pl [ardour session directory] [destination directory] [original samplerate] [new samplerate]\n";
37 exit;
40 if ( ! -d $sourceDirectory) {
41 print $sourceDirectory.": directory does not exist!\n";
42 exit;
45 if ( -d $destDirectory) {
46 print $destDirectory.": directory exists!\n";
47 exit;
50 print "Checking source and destination directories\n";
52 my @sounds;
53 my @dead_sounds;
54 my @dot_ardour;
55 my @automation;
58 my $version_099x = 0;
60 # Read the names of all audio files in /sounds/
61 if ( -d $sourceDirectory."/sounds/") {
62 $version_099x = 1;
64 opendir(SOUNDS,$sourceDirectory."/sounds/") || die ($sourceDirectory.": not a valid session, no sounds/ directory");
65 while ( my $file=readdir(SOUNDS) ) {
66 if ( -f $sourceDirectory."/sounds/".$file ) {
67 push(@sounds,$file);
71 } else {
72 my $dirname = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
73 opendir(SOUNDS,$dirname) || die ($sourceDirectory.": not a valid session, no sounds/ directory");
74 while ( my $file=readdir(SOUNDS) ) {
75 if ( -f $dirname.$file ) {
76 push(@sounds,$file);
81 close(SOUNDS);
83 # Read the names of all audio files in /dead_sounds/
84 opendir(DEAD_SOUNDS,$sourceDirectory."/dead_sounds/") || die ($sourceDirectory.": not a valid session, no dead_sounds/ directory");
85 while ( my $file=readdir(DEAD_SOUNDS) ) {
86 if ( -f $sourceDirectory."/dead_sounds/".$file ) {
87 push(@dead_sounds,$file);
90 close(DEAD_SOUNDS);
92 # Read the names of all .ardour files in /
93 opendir(DOT_ARDOUR,$sourceDirectory) || die ($sourceDirectory.": could not open!");
94 while ( my $file=readdir(DOT_ARDOUR) ) {
95 if ( -f $sourceDirectory."/".$file &&
96 index($file,".ardour") eq (length($file)-7)) {
97 push(@dot_ardour,$file);
100 close(DOT_ARDOUR);
102 # Read the names of all automation files in /automation/
103 opendir(AUTOMATION,$sourceDirectory."/automation/") || die ($sourceDirectory."/automation: could not open!");
104 while ( my $file=readdir(AUTOMATION) ) {
105 if ( -f $sourceDirectory."/automation/".$file &&
106 index($file,".automation") eq (length($file)-11)) {
107 push(@automation,$file);
110 close(AUTOMATION);
112 # Check for /peaks/
113 if ( ! -d $sourceDirectory."/peaks" ) {
114 print $sourceDirectory.": not a valid session, no peaks/ directory\n";
115 exit;
118 ##########################################
119 # Checks are done, let's go!
121 print "Converting session\n";
122 mkdir $destDirectory;
125 # Run all .ardour files through the SAX parser and write the results in the destination
126 # directory.
128 foreach my $xml (@dot_ardour) {
129 print "Doing samplerate conversion to ".$xml."...";
130 open(OUTFILE,">".$destDirectory."/".$xml);
131 my $output = new IO::Handle;
132 $output->fdopen(fileno(OUTFILE),"w");
134 my $handler = ARDOUR::SessionSRHandler->new($sourceSR,$destSR,$output);
136 my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
138 $parser->parse(Source => { SystemId => $sourceDirectory."/".$xml });
140 $output->close();
141 close(OUTFILE);
142 print " done\n";
145 # This code is needed for 0.99.x sessions, thus the code is still here.
147 #mkdir $destDirectory."/automation";
149 #foreach my $file (@automation) {
150 # print "Converting automation file ".$file."...";
151 # open(INFILE,$sourceDirectory."/automation/".$file) || die "could not open source automation file $file!";
152 # open(OUTFILE,">".$destDirectory."/automation/".$file) || die "could not open destination automation file $file!";
153 # my $input=new IO::Handle;
154 # my $output=new IO::Handle;
156 # $input->fdopen(fileno(INFILE),"r");
157 # $output->fdopen(fileno(OUTFILE),"w");
159 # my $converter = ARDOUR::AutomationSRConverter->new($input,$output,$sourceSR,$destSR);
160 # $converter->convert;
162 # $input->close;
163 # $output->close;
164 # close(INFILE);
165 # close(OUTFILE);
166 # print " done\n";
170 if ($version_099x eq 1) {
171 mkdir $destDirectory."/sounds";
172 foreach my $sound (@sounds) {
173 my @params=("-to", $destSR,
174 "-c", "0",
175 $sourceDirectory."/sounds/".$sound,
176 $destDirectory."/sounds/".$sound);
177 system("sndfile-resample",@params);
179 } else {
180 my $srcSndDir = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
182 my $dstSndDir = $destDirectory."/interchange/";
183 mkdir $dstSndDir;
185 $dstSndDir .= basename($sourceDirectory)."/";
186 mkdir $dstSndDir;
188 $dstSndDir .= "audiofiles/";
189 mkdir $dstSndDir;
191 foreach my $sound (@sounds) {
192 my @params=("-to", $destSR,
193 "-c", "0",
194 $srcSndDir."/".$sound,
195 $dstSndDir."/".$sound);
196 system("sndfile-resample",@params);
200 mkdir $destDirectory."/dead_sounds";
201 mkdir $destDirectory."/peaks";