LuaSession: allow multi-line commands and functions
[ardour.git] / tools / ARDOUR / SessionSRHandler.pm
1 package ARDOUR::SessionSRHandler;
2
3
4 use XML::Handler::XMLWriter;
5 use POSIX qw(floor);
6
7 @ISA = qw( XML::Handler::XMLWriter );
8
9 $VERSION = 0.1;
10
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 }
18         };
19
20
21 sub new {
22     my ($type, $original_sr, $new_sr, $output) = @_;
23
24         #my $self = XML::Handler::XMLWriter->new( { Output => $output } );
25         
26         my $self = $type->SUPER::new( Output => $output );
27         
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;
33         
34         $self->{InEvents} = 0;
35
36         return $self;
37 }
38
39 sub start_element {
40         my $self = shift;
41         my $element = shift;
42
43         my $debug = $self->{Debug};
44
45         my $atts = $element->{Attributes};
46
47         my $convert_attributes = $conversion_table->{$element->{Name}};
48
49         foreach my $cAtt (keys %$convert_attributes) {
50                 $atts->{$cAtt} = sprintf("%.0f", $atts->{$cAtt} * $self->{Ratio});
51                 $debug = 0;
52         }
53
54         if ($debug eq 0) {
55                 $self->SUPER::start_element($element, @_);
56         }
57
58         if ($element->{Name} eq "events") {
59                 $self->{InEvents} = 1;
60         }
61 }
62
63 sub end_element {
64         my $self = shift;
65         my $element = shift;
66
67         if ($self->{Debug} eq 0) {
68                 $self->SUPER::end_element($element,@_);
69         }
70
71         if ($element->{Name} eq "events") {
72                 $self->{InEvents} = 0;
73         }
74 }
75
76 sub start_document {
77         my $self = shift;
78
79         $self->SUPER::start_document(@_);
80         
81         $self->{Output}->print("<!-- Sample rate converted from $self->{OriginalSR}hz to $self->{TargetSR}hz -->\n");
82 }
83
84 sub end_document {
85         my $self = shift;
86
87         $self->SUPER::end_document(@_);
88 }
89
90 sub characters {
91         my $self = shift;
92         my $c = shift;
93
94         if ($self->{InEvents} > 0) {
95                 my $converted = "";
96
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;
104                         }
105                 }
106
107                 $c->{Data} = $converted;
108         }
109
110
111         if ($self->{Debug} eq 0) {
112                 $self->SUPER::characters($c, @_);
113         }
114
115 }
116
117 1;
118
119
120