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.
14 use XML::Parser::PerlSAX;
15 use XML::Handler::XMLWriter;
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";
31 my ($sourceDirectory, $destDirectory, $sourceSR, $destSR) = @ARGV;
36 print "usage: ardour_sr.pl [ardour session directory] [destination directory] [original samplerate] [new samplerate]\n";
40 if ( ! -d $sourceDirectory) {
41 print $sourceDirectory.": directory does not exist!\n";
45 if ( -d $destDirectory) {
46 print $destDirectory.": directory exists!\n";
50 print "Checking source and destination directories\n";
60 # Read the names of all audio files in /sounds/
61 if ( -d $sourceDirectory."/sounds/") {
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 ) {
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 ) {
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);
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);
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);
115 if ( ! -d $sourceDirectory."/peaks" ) {
116 print $sourceDirectory.": not a valid session, no peaks/ directory\n";
120 ##########################################
121 # Checks are done, let's go!
123 print "Converting session\n";
124 mkdir $destDirectory;
127 # Run all .ardour files through the SAX parser and write the results in the destination
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");
136 my $handler = ARDOUR::SessionSRHandler->new($sourceSR,$destSR,$output);
138 my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
140 $parser->parse(Source => { SystemId => $sourceDirectory."/".$xml });
147 # This code is needed for 0.99.x sessions, thus the code is still here.
149 #mkdir $destDirectory."/automation";
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;
158 # $input->fdopen(fileno(INFILE),"r");
159 # $output->fdopen(fileno(OUTFILE),"w");
161 # my $converter = ARDOUR::AutomationSRConverter->new($input,$output,$sourceSR,$destSR);
162 # $converter->convert;
172 if ($version_099x eq 1) {
173 mkdir $destDirectory."/sounds";
174 foreach my $sound (@sounds) {
175 my @params=("-to", $destSR,
177 $sourceDirectory."/sounds/".$sound,
178 $destDirectory."/sounds/".$sound);
179 system("sndfile-resample",@params);
182 my $srcSndDir = $sourceDirectory."/interchange/".basename($sourceDirectory)."/audiofiles/";
184 my $dstSndDir = $destDirectory."/interchange/";
187 $dstSndDir .= basename($sourceDirectory)."/";
190 $dstSndDir .= "audiofiles/";
193 foreach my $sound (@sounds) {
194 my @params=("-to", $destSR,
196 $srcSndDir."/".$sound,
197 $dstSndDir."/".$sound);
198 system("sndfile-resample",@params);
202 mkdir $destDirectory."/dead_sounds";
203 mkdir $destDirectory."/peaks";