1 package ARDOUR
::SessionSRHandler
;
4 use XML
::Handler
::XMLWriter
;
7 @ISA = qw( XML::Handler::XMLWriter );
11 # This table maps the "names of XML elements" to lists of "names of attributes" which should
12 # be converted according to the SR change.
13 # TODO: The table is a bit dirty, i have to figure out how to do it cleanly
14 my $conversion_table = {
15 "Location" => { "end" => 0, "start" => 0 },
16 "Region" => { "length" => 0, "start" => 0, "position" => 0, "sync-position" => 0 },
17 "Crossfade" => { "left" => 0, "right" => 0 }
22 my ($type, $original_sr, $new_sr, $output) = @_;
24 #my $self = XML::Handler::XMLWriter->new( { Output => $output } );
26 my $self = $type->SUPER::new
( Output
=> $output );
29 $self->{Ratio
} = ($new_sr+0)/($original_sr+0);
30 $self->{OriginalSR
} = $original_sr;
31 $self->{TargetSR
} = $new_sr;
32 $self->{Output
} = $output;
34 $self->{InEvents
} = 0;
43 my $debug = $self->{Debug
};
45 my $atts = $element->{Attributes
};
47 my $convert_attributes = $conversion_table->{$element->{Name
}};
49 foreach my $cAtt (keys %$convert_attributes) {
50 $atts->{$cAtt} = sprintf("%.0f", $atts->{$cAtt} * $self->{Ratio
});
55 $self->SUPER::start_element
($element, @_);
58 if ($element->{Name
} eq "events") {
59 $self->{InEvents
} = 1;
67 if ($self->{Debug
} eq 0) {
68 $self->SUPER::end_element
($element,@_);
71 if ($element->{Name
} eq "events") {
72 $self->{InEvents
} = 0;
79 $self->SUPER::start_document
(@_);
81 $self->{Output
}->print("<!-- Sample rate converted from $self->{OriginalSR}hz to $self->{TargetSR}hz -->\n");
87 $self->SUPER::end_document
(@_);
94 if ($self->{InEvents
} > 0) {
97 foreach my $foo (split(' ',$c->{Data
})) {
98 if ($self->{InEvents
} eq 1) {
99 $converted .= floor
($foo * $self->{Ratio
})." ";
100 $self->{InEvents
} = 2;
102 $converted .= $foo." ";
103 $self->{InEvents
} = 1;
107 $c->{Data
} = $converted;
111 if ($self->{Debug
} eq 0) {
112 $self->SUPER::characters
($c, @_);