most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[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 # 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);
108         }
109 }
110 close(AUTOMATION);
111
112 # Check for /peaks/
113 if ( ! -d $sourceDirectory."/peaks" ) {
114         print $sourceDirectory.": not a valid session, no peaks/ directory\n";
115         exit;
116 }
117
118 ##########################################
119 # Checks are done, let's go!
120
121 print "Converting session\n";
122 mkdir $destDirectory;
123
124
125 # Run all .ardour files through the SAX parser and write the results in the destination
126 # directory.
127
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");
133         
134         my $handler = ARDOUR::SessionSRHandler->new($sourceSR,$destSR,$output);
135
136         my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
137
138     $parser->parse(Source => { SystemId => $sourceDirectory."/".$xml });
139
140         $output->close();
141         close(OUTFILE);
142         print " done\n";
143 }
144
145 # This code is needed for 0.99.x sessions, thus the code is still here.
146 #
147 #mkdir $destDirectory."/automation";
148 #
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;
155 #
156 #       $input->fdopen(fileno(INFILE),"r");
157 #       $output->fdopen(fileno(OUTFILE),"w");
158 #
159 #       my $converter = ARDOUR::AutomationSRConverter->new($input,$output,$sourceSR,$destSR);
160 #       $converter->convert;
161 #
162 #       $input->close;
163 #       $output->close;
164 #       close(INFILE);
165 #       close(OUTFILE);
166 #       print " done\n";
167 #}
168
169
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);
178         }
179 } else {
180         my $srcSndDir = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
181         
182         my $dstSndDir = $destDirectory."/interchange/";
183         mkdir $dstSndDir;
184
185         $dstSndDir .= basename($sourceDirectory)."/";
186         mkdir $dstSndDir;
187
188         $dstSndDir .= "audiofiles/";
189         mkdir $dstSndDir;
190         
191         foreach my $sound (@sounds) {
192                 my @params=("-to", $destSR,
193                             "-c",  "0",
194                             $srcSndDir."/".$sound,
195                             $dstSndDir."/".$sound);
196                 system("sndfile-resample",@params);
197         }
198 }
199
200 mkdir $destDirectory."/dead_sounds";
201 mkdir $destDirectory."/peaks";
202
203