replace ::cast_dynamic() with relevant ActionManager::get_*_action() calls
[ardour.git] / tools / resample_session.pl
1 #!/usr/bin/env perl
2 # Ardour session resampler, version 0.1
3 # (c)Sampo Savolainen 2005-2007
4 #
5 # Licensed under the GPL 
6 #
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.
11
12 use FindBin '$Bin';
13 use lib "$Bin";
14 use XML::Parser::PerlSAX;
15 use XML::Handler::XMLWriter;
16 use IO::Handle;
17
18 use File::Basename;
19
20 use ARDOUR::SessionSRHandler;
21 # Let's hope this is never needed
22 #use ARDOUR::AutomationSRConverter;
23
24
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;
29 }
30
31 my ($sourceDirectory, $destDirectory, $sourceSR, $destSR) = @ARGV;
32
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;
38 }
39
40 if ( ! -d $sourceDirectory) {
41         print $sourceDirectory.": directory does not exist!\n";
42         exit;
43 }
44
45 if ( -d $destDirectory) {
46         print $destDirectory.": directory exists!\n";
47         exit;
48 }
49
50 print "Checking source and destination directories\n";
51
52 my @sounds;
53 my @dead_sounds;
54 my @dot_ardour;
55 my @automation;
56
57
58 my $version_099x = 0;
59
60 # Read the names of all audio files in /sounds/
61 if ( -d $sourceDirectory."/sounds/") {
62         $version_099x = 1;
63
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);
68                 }
69         }
70
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);
77                 }
78         }
79
80 }
81 close(SOUNDS);
82
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);
88         }
89 }
90 close(DEAD_SOUNDS);
91
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);
98         }
99 }
100 close(DOT_ARDOUR);
101
102 if ( -d $sourceDirectory."/automation/") {
103         # Read the names of all automation files in /automation/
104         opendir(AUTOMATION,$sourceDirectory."/automation/") || die ($sourceDirectory."/automation: could not open!");
105         while ( my $file=readdir(AUTOMATION) ) {
106                 if ( -f $sourceDirectory."/automation/".$file && 
107                      index($file,".automation") eq (length($file)-11)) {
108                         push(@automation,$file);
109                 }
110         }
111         close(AUTOMATION);
112 }
113
114 # Check for /peaks/
115 if ( ! -d $sourceDirectory."/peaks" ) {
116         print $sourceDirectory.": not a valid session, no peaks/ directory\n";
117         exit;
118 }
119
120 ##########################################
121 # Checks are done, let's go!
122
123 print "Converting session\n";
124 mkdir $destDirectory;
125
126
127 # Run all .ardour files through the SAX parser and write the results in the destination
128 # directory.
129
130 foreach my $xml (@dot_ardour) {
131         print "Doing samplerate conversion to ".$xml."...";
132         open(OUTFILE,">".$destDirectory."/".$xml);
133         my $output = new IO::Handle;
134         $output->fdopen(fileno(OUTFILE),"w");
135         
136         my $handler = ARDOUR::SessionSRHandler->new($sourceSR,$destSR,$output);
137
138         my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
139
140     $parser->parse(Source => { SystemId => $sourceDirectory."/".$xml });
141
142         $output->close();
143         close(OUTFILE);
144         print " done\n";
145 }
146
147 # This code is needed for 0.99.x sessions, thus the code is still here.
148 #
149 #mkdir $destDirectory."/automation";
150 #
151 #foreach my $file (@automation) {
152 #       print "Converting automation file ".$file."...";
153 #       open(INFILE,$sourceDirectory."/automation/".$file) || die "could not open source automation file $file!";
154 #       open(OUTFILE,">".$destDirectory."/automation/".$file) || die "could not open destination automation file $file!";
155 #       my $input=new IO::Handle;
156 #       my $output=new IO::Handle;
157 #
158 #       $input->fdopen(fileno(INFILE),"r");
159 #       $output->fdopen(fileno(OUTFILE),"w");
160 #
161 #       my $converter = ARDOUR::AutomationSRConverter->new($input,$output,$sourceSR,$destSR);
162 #       $converter->convert;
163 #
164 #       $input->close;
165 #       $output->close;
166 #       close(INFILE);
167 #       close(OUTFILE);
168 #       print " done\n";
169 #}
170
171
172 if ($version_099x eq 1) {
173         mkdir $destDirectory."/sounds";
174         foreach my $sound (@sounds) {
175                 my @params=("-to", $destSR,
176                             "-c",  "0",
177                             $sourceDirectory."/sounds/".$sound,
178                             $destDirectory."/sounds/".$sound);
179                 system("sndfile-resample",@params);
180         }
181 } else {
182         my $srcSndDir = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
183         
184         my $dstSndDir = $destDirectory."/interchange/";
185         mkdir $dstSndDir;
186
187         $dstSndDir .= basename($sourceDirectory)."/";
188         mkdir $dstSndDir;
189
190         $dstSndDir .= "audiofiles/";
191         mkdir $dstSndDir;
192         
193         foreach my $sound (@sounds) {
194                 my @params=("-to", $destSR,
195                             "-c",  "0",
196                             $srcSndDir."/".$sound,
197                             $dstSndDir."/".$sound);
198                 system("sndfile-resample",@params);
199         }
200 }
201
202 mkdir $destDirectory."/dead_sounds";
203 mkdir $destDirectory."/peaks";
204
205