change default overlapping note strategy to "relax" (i.e. do nothing); fix crash...
[ardour2.git] / tools / ARDOUR / SessionSRHandler.pm
blob8bfe665aca39fdadb52bc3daa5dfd8749ee66501
1 package ARDOUR::SessionSRHandler;
4 use XML::Handler::XMLWriter;
5 use POSIX qw(floor);
7 @ISA = qw( XML::Handler::XMLWriter );
9 $VERSION = 0.1;
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 }
21 sub new {
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 );
28 $self->{Debug} = 0;
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;
36 return $self;
39 sub start_element {
40 my $self = shift;
41 my $element = shift;
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});
51 $debug = 0;
54 if ($debug eq 0) {
55 $self->SUPER::start_element($element, @_);
58 if ($element->{Name} eq "events") {
59 $self->{InEvents} = 1;
63 sub end_element {
64 my $self = shift;
65 my $element = shift;
67 if ($self->{Debug} eq 0) {
68 $self->SUPER::end_element($element,@_);
71 if ($element->{Name} eq "events") {
72 $self->{InEvents} = 0;
76 sub start_document {
77 my $self = shift;
79 $self->SUPER::start_document(@_);
81 $self->{Output}->print("<!-- Sample rate converted from $self->{OriginalSR}hz to $self->{TargetSR}hz -->\n");
84 sub end_document {
85 my $self = shift;
87 $self->SUPER::end_document(@_);
90 sub characters {
91 my $self = shift;
92 my $c = shift;
94 if ($self->{InEvents} > 0) {
95 my $converted = "";
97 foreach my $foo (split(' ',$c->{Data})) {
98 if ($self->{InEvents} eq 1) {
99 $converted .= floor($foo * $self->{Ratio})." ";
100 $self->{InEvents} = 2;
101 } else {
102 $converted .= $foo." ";
103 $self->{InEvents} = 1;
107 $c->{Data} = $converted;
111 if ($self->{Debug} eq 0) {
112 $self->SUPER::characters($c, @_);