FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / utils / parse_testcodec.rb
blob2f46c4ceca0eb99afee620f61d0fd9a98f0f4d51
1 #!/usr/bin/ruby
2 # (c) 2010 by Thomas Martitz
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
10 # parse test codec output files and give wiki or spreadsheet formatted output
12 class CodecResult
13     include Comparable
14 private
16     attr_writer :codec
17     attr_writer :decoded_frames
18     attr_writer :max_frames
19     attr_writer :decode_time
20     attr_writer :file_duration
21     attr_writer :percent_realtime
22     attr_writer :mhz_needed
24     def get_codec(filename)
25         case filename
26             when /.+aache.+/, /nero_he_.+/
27                 self.codec = "Nero AAC-HE"
28             when /a52.+/
29                 self.codec = "AC3 (A52)"
30             when /ape_.+/
31                 self.codec = "Monkey Audio"
32             when /lame_.+/
33                 self.codec = "MP3"
34             when /.+\.m4a/
35                 self.codec = "AAC-LC"
36             when /vorbis.+/
37                 self.codec = "Vorbis"
38             when /wma_.+/
39                 self.codec = "WMA Standard"
40             when /wv_.+/
41                 self.codec = "WAVPACK"
42             when /applelossless.+/
43                 self.codec = "Apple Lossless"
44             when /mpc_.+/
45                 self.codec = "Musepack"
46             when /flac_.+/
47                 self.codec = "FLAC"
48             when /cook_.+/
49                 self.codec = "Cook (RA)"
50             when /atrac3.+/
51                 self.codec = "Atrac3"
52             when /true.+/
53                 self.codec = "True Audio"
54             when /toolame.+/, /pegase_l2.+/
55                 self.codec = "MP2"
56             when /atrack1.+/
57                 self.codec = "Atrac1"
58             when /wmapro.+/
59                 self.codec = "WMA Professional"
60             when /wmal.+/
61                 self.codec = "WMA Lossless"
62             when /speex.+/
63                 self.codec = "Speex"
64             when /pegase_l1.+/
65                 self.codec = "MP1"
66             else
67                 self.codec = "CODEC UNKNOWN (#{name})"
68         end
69     end
71     def file_name=(name)
72         @file_name = name
73         get_codec(name)
74     end
76 public
78     attr_reader :file_name
79     attr_reader :codec
80     attr_reader :decoded_frames
81     attr_reader :max_frames
82     attr_reader :decode_time
83     attr_reader :file_duration
84     attr_reader :percent_realtime
85     attr_reader :mhz_needed
87     # make results comparable, allows for simple faster/slower/equal
88     def <=>(other)
89         if self.file_name != other.file_name
90             raise ArgumentError, "Cannot compare different files"
91         end
92         return self.decode_time <=> other.decode_time
93     end
95     def initialize(text_block, cpu_freq = nil)
96         # we need an Array
97         c = text_block.class
98         if (c != Array && c.superclass != Array)
99             raise ArgumentError,
100                  "Argument must be an array but is " + text_block.class.to_s
101         end
103         #~ lame_192.mp3
104         #~ 175909 of 175960
105         #~ Decode time - 8.84s
106         #~ File duration - 175.96s
107         #~ 1990.49% realtime
108         #~ 30.14MHz needed for realtime (not there in RaaA)
110         # file name
111         self.file_name = text_block[0]
113         # decoded & max frames
114         test = Regexp.new(/(\d+) of (\d+)/)
115         res = text_block[1].match(test)
116         self.decoded_frames = res[1].to_i
117         self.max_frames = res[2].to_i
119         # decode time, in centiseconds
120         test = Regexp.new(/Decode time - ([.\d]+)s/)
121         self.decode_time = text_block[2].match(test)[1].to_f
123         # file duration, in centiseconds
124         test = Regexp.new(/File duration - ([.\d]+)s/)
125         self.file_duration = text_block[3].match(test)[1].to_f
127         # % realtime
128         self.percent_realtime = text_block[4].to_f
130         # MHz needed for rt
131         test = Regexp.new(/[.\d]+MHz needed for realtime/)
132         self.mhz_needed = nil
133         if (text_block[5] != nil && text_block[5].length > 0)
134             self.mhz_needed = text_block[5].match(test)[0].to_f
135         elsif (cpu_freq)
136             # if not given, calculate it as per passed cpu frequency
137             # duration to microseconds
138             speed = self.file_duration / self.decode_time
139             self.mhz_needed = cpu_freq / speed
140         end
141     end
144 class TestCodecResults < Array
145     def initialize(file_name, cpu_freq)
146         super()
147         temp = self.clone
148         # go through the results, create a CodecResult for each block
149         # of text (results for the codecs are seperated by an empty line)
150         File.open(file_name, File::RDONLY) do |file|
151             file.each_chomp do |line|
152                 if (line.length == 0) then
153                     self << CodecResult.new(temp, cpu_freq);temp.clear
154                 else
155                     temp << line
156                 end
157             end
158         end
159     end
161     # sort the results by filename (so files of the same codec are near)
162     def sort
163         super { |x, y| x.file_name <=> y.file_name }
164     end
167 class File
168     # walk through each line but have the \n removed
169     def each_chomp
170         self.each_line do |line|
171             yield(line.chomp)
172         end
173     end
176 class Float
177     alias_method(:old_to_s, :to_s)
178     # add the ability to use a different decimal seperator in to_s
179     def to_s
180         string = old_to_s
181         string.sub!(/[.]/ , @@dec_sep) if @@dec_sep
182         string
183     end
185     @@dec_sep = nil
186     def self.decimal_seperator=(sep)
187         @@dec_sep=sep
188     end
191 #files is an Array of TestCodecResultss
192 def for_calc(files)
193     files[0].each_index do |i|
194         string = files[0][i].file_name + "\t"
195         for f in files
196             string += f[i].percent_realtime.to_s + "%\t"
197         end
198         puts string
199     end
202 #files is an Array of TestCodecResultss
203 def for_wiki(files)
204     basefile = files.shift
205     codec = nil
206     basefile.each_index do |i| res = basefile[i]
207         # make a joined row for each codec
208         if (codec == nil || res.codec != codec) then
209             codec = res.codec
210             puts "| *%s*  ||||%s" % [codec, "|"*files.length]
211         end
212         row = sprintf("| %s | %.2f%%%% realtime | Decode time - %.2fs |" %
213                         [res.file_name, res.percent_realtime, res.decode_time])
214         if (res.mhz_needed != nil) # column for mhz needed, | - | if unknown
215             row += sprintf(" %.2fMHz |" % res.mhz_needed.to_s)
216         else
217             row += " - |"
218         end
219         for f in files  # calculate speed up compared to the rest files
220             delta = (res.percent_realtime / f[i].percent_realtime)*100
221             row += sprintf(" %.2f%%%% |" % delta)
222         end
223         puts row
224     end
227 # for_xml() anyone? :)
229 def help
230     puts "#{$0} [OPTIONS] FILE [FILES]..."
231     puts "Options:\t-w\tOutput in Fosswiki format (default)"
232     puts "\t\t-c\tOutput in Spreadsheet-compatible format (tab-seperated)"
233     puts "\t\t-s=MHZ\tAssume MHZ cpu frequency for \"MHz needed for realtime\" calculation"
234     puts "\t\t\t(if not given by the log files, e.g. for RaaA)"
235     puts "\t\t-d=CHAR\tUse CHAR as decimal seperator in the -c output"
236     puts "\t\t\t(if your spreadsheed tool localized and making problems)"
237     puts
238     puts "\tOne file is needed. This is the basefile."
239     puts "\tIn -c output, the % realtime values of each"
240     puts "\tcodec from each file is printed on the screen onto the screen"
241     puts "\tIn -w output, a wiki table is made from the basefile with one column"
242     puts "\tfor each additional file representing relative speed of the basefile"
243     exit
246 to_call = method(:for_wiki)
247 mhz = nil
248 files = []
250 help if (ARGV.length == 0)
252 ARGV.each do |e|
253     a = e.chars.to_a
254     if (a[0] == '-') # option
255         case a[1]
256             when 'c'
257                 to_call = method(:for_calc)
258             when 'w'
259                 to_call = method(:for_wiki)
260             when 'd'
261                 if (a[2] == '=')
262                     sep = a[3]
263                 else
264                     sep = a[2]
265                 end
266                 Float.decimal_seperator = sep
267             when 's'
268                 if (a[2] == '=')
269                     mhz = a[3..-1].join.to_i
270                 else
271                     mhz = a[2..-1].join.to_i
272                 end
273             else
274                 help
275         end
276     else # filename
277         files << e
278     end
282 tmp = []
283 for file in files do
284     tmp << TestCodecResults.new(file, mhz).sort
286 to_call.call(tmp) # invoke selected method