1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
package ARDOUR::SessionSRHandler;
use XML::Handler::XMLWriter;
use POSIX qw(floor);
@ISA = qw( XML::Handler::XMLWriter );
$VERSION = 0.1;
# This table maps the "names of XML elements" to lists of "names of attributes" which should
# be converted according to the SR change.
# TODO: The table is a bit dirty, i have to figure out how to do it cleanly
my $conversion_table = {
"Location" => { "end" => 0, "start" => 0 },
"Region" => { "length" => 0, "start" => 0, "position" => 0, "sync-position" => 0 },
"Crossfade" => { "left" => 0, "right" => 0 }
};
sub new {
my ($type, $original_sr, $new_sr, $output) = @_;
#my $self = XML::Handler::XMLWriter->new( { Output => $output } );
my $self = $type->SUPER::new( Output => $output );
$self->{Debug} = 0;
$self->{Ratio} = ($new_sr+0)/($original_sr+0);
$self->{OriginalSR} = $original_sr;
$self->{TargetSR} = $new_sr;
$self->{Output} = $output;
$self->{InEvents} = 0;
return $self;
}
sub start_element {
my $self = shift;
my $element = shift;
my $debug = $self->{Debug};
my $atts = $element->{Attributes};
my $convert_attributes = $conversion_table->{$element->{Name}};
foreach my $cAtt (keys %$convert_attributes) {
$atts->{$cAtt} = sprintf("%.0f", $atts->{$cAtt} * $self->{Ratio});
$debug = 0;
}
if ($debug eq 0) {
$self->SUPER::start_element($element, @_);
}
if ($element->{Name} eq "events") {
$self->{InEvents} = 1;
}
}
sub end_element {
my $self = shift;
my $element = shift;
if ($self->{Debug} eq 0) {
$self->SUPER::end_element($element,@_);
}
if ($element->{Name} eq "events") {
$self->{InEvents} = 0;
}
}
sub start_document {
my $self = shift;
$self->SUPER::start_document(@_);
$self->{Output}->print("<!-- Sample rate converted from $self->{OriginalSR}hz to $self->{TargetSR}hz -->\n");
}
sub end_document {
my $self = shift;
$self->SUPER::end_document(@_);
}
sub characters {
my $self = shift;
my $c = shift;
if ($self->{InEvents} > 0) {
my $converted = "";
foreach my $foo (split(' ',$c->{Data})) {
if ($self->{InEvents} eq 1) {
$converted .= floor($foo * $self->{Ratio})." ";
$self->{InEvents} = 2;
} else {
$converted .= $foo." ";
$self->{InEvents} = 1;
}
}
$c->{Data} = $converted;
}
if ($self->{Debug} eq 0) {
$self->SUPER::characters($c, @_);
}
}
1;
|