blib/lib/Gftracks.pm

filter: Statementstmt 59.4% 101 / 170 Branchbran 51.4% 35 / 68 Conditioncond 74.0% 20 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 56.5% 13 / 23 Podpod 92.8% 13 / 14 totaltotal 60.3% 192 / 318 CC 49 SCAR 36.9
CC
49
Cov
59%
shell
182.0
printtracks
16.4
init
15.2
CRAP 40.2
↑ Summary
1 #!/usr/bin/perl
2 package Gftracks;
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 our @ISA=qw / Exporter /;
8 6 our @EXPORT = qw / instime init deltrack printtracks shell/;
9 # Normally sec should only be used internally, but just in case,
10 # we make it possible to pull it in.
11 6 our @EXPORT_OK = qw /sec tidytime /;
12 6 our $VERSION="0.9";
13 # Returns number of secounds calculated from a grf timestamp
14  
15 44 ▶sub sec{
Subroutine sec: called CC=1 SCAR=0.0
Pod undocumented
16 44 my ($h,$m,$s)=split(':',$_[0]);
17 44 $s+=$m*60;
18 44 $s+=$h*3600;
19 44 return $s;
20 }
21  
22 =head1 NAME
23  
24 Gftracks - Perl extention for manipulation of gramofiles .tracks files
25  
26  
27 =head1 SYNOPSIS
28  
29 Usually the interactive shell will be used. The variable TRACKS shall point to
30 the .tracks file to be edited
31  
32 export TRACKS=/home/myhome/myrecord.wav.tracks
33  
34 perl -MGftracks -e shell;
35  
36  
37 Within the shell, press h for help.
38  
39 =head1 DESCRIPTION
40  
41 The .tracks file is read into an array, where the 0th element holds the
42 metadata for the file, and each of the other elements holds the information
43 for the actual track.
44  
45  
46 =head1 SUBROUTINES
47  
48 For all the subroutines the variable $tracks indicates an array build up in the
49 module.
50  
51 Those subroutines with a name starting with _ are not exported.
52  
53  
54 =head2 instime(timestamp)
55  
56 instime inserts a track at a given timestamp
57  
58 ins(\@tracks,$timestamp[,$duration]);
59 If duration is not defined, the end of the track is set to the
60 current end of the track in which the insertion is performed
61  
62 =cut
63  
64 3 ▶sub instime{
Subroutine instime: called CC=9 SCAR=22.7
Pod documented
65 3 my @tracks=@{$_[0]};
66 3 my $timestamp=$_[1];
67 3 ▶ my $duration=$_[2] || undef;
Condition50%1/2
$_[2] || undef
l!l
03
MC/DC0%0/1
$_[2] || undef
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
68 3 ▶ return \@tracks if $duration; # $duration still does not work
Branch: 1 branch
BranchTrueFalseTotal
if $duration 0 3 3
69 3 my $timesec=sec($timestamp);
70 3 foreach (1..$#tracks){
71 # Search until either, we find the track which the new is to be inserted
72 # into, or we are on the last track. (To avoid warning, the checks are done
73 # the other way around
74 next unless ($_ == $#tracks or(
75 sec($tracks[$_]{start})< $timesec)
76 7 ▶ and (sec($tracks[$_+1]{start})> $timesec));
Branch: 1 branch
BranchTrueFalseTotal
unless $_ == $#tracks or sec($tracks[$_]{"start"}) < $timesec and sec($tracks[$_ + 1]{"start"}) > $timesec 4 3 7
Condition83%5/6
$_ == $#tracks or sec($tracks[$_]{"start"}) < $timesec and sec($tracks[$_ + 1]{"start"}) > $timesec
l!l&&r!l&&!r
124
sec($tracks[$_]{"start"}) < $timesec and sec($tracks[$_ + 1]{"start"}) > $timesec
!ll&&!rl&&r
042
MC/DC66%2/3
$_ == $#tracks or sec($tracks[$_]{"start"}) < $timesec and sec($tracks[$_ + 1]{"start"}) > $timesec
$_ == $#tracks sec($tracks[$_]{"start"}) < $timesec sec($tracks[$_ + 1]{"start"}) > $timesec
Truth table75%3/4
A or B and C
A= $_ == $#tracksB= sec($tracks[$_]{"start"}) < $timesecC= sec($tracks[$_ + 1]{"start"}) > $timesec
ABCresult
1XX1
0111
00X0
0100
77 3 ▶ if (sec($tracks[$_]{end})>$timesec){ # Unless the insert is between two tracks
Branch: 1 branch
BranchTrueFalseTotal
if (sec($tracks[$_]{'end'}) > $timesec) { } 2 1 3
78 my $new={start=>$timestamp,
79 2 end=>$tracks[$_]{end}};
80 2 splice(@tracks,$_+1,0,$new);
81 2 $tracks[$_]{end}=$timestamp;
82 }else{
83 1 ▶ if ($duration){
Branch: 1 branch
BranchTrueFalseTotal
if ($duration) 0 1 1
84 my $new={start=>$timestamp,
85 0 end=>$tracks[$_+1]{end}};
86 0 splice(@tracks,$_+1,0,$new);
87 0 $tracks[$_]{end}=$timestamp;
88 }
89 }
90 3 last;
91 }
92 3 $tracks[0]{Number_of_tracks}=$#tracks;
93 3 return \@tracks;
94 }
95  
96 =head2 _spliceback and _splicefwd
97  
98 _spliceback and _splicefwd splices the rest of the array when
99 a track has been deleted. They should only be used internally.
100 In both cases the arguments are a pointer to the tracks array and
101 the index that is to be deleted
102  
103 =cut
104  
105 # _spliceback removes a track by combining it with the previous track
106 2 ▶sub _spliceback{
Subroutine _spliceback: called CC=1 SCAR=0.0
107 2 my $tracks=shift;
108 2 my $delno =shift;
109 2 $tracks->[$delno-1]{end}=$tracks->[$delno]{end};
110 2 splice (@$tracks,$delno,1);
111 2 return $tracks;
112 }
113  
114 # _spliceback removes a track by combining it with the next track
115 1 ▶sub _splicefwd{
Subroutine _splicefwd: called CC=1 SCAR=0.0
116 1 my $tracks=shift;
117 1 my $delno =shift;
118 1 $tracks->[$delno+1]{start}=$tracks->[$delno]{start};
119 1 splice (@$tracks,$delno,1);
120 1 return $tracks;
121 }
122  
123 =head2 deltrack
124  
125 deltrack (\@tracks,$index,$back)
126  
127 Deltrack removes a track by default using spliceback (unless the last track
128 is deleted).
129  
130 =cut
131  
132 5 ▶sub deltrack{
Subroutine deltrack: called CC=5 SCAR=16.1
Pod documented
133 5 my $tracks=shift;
134 5 my $delno =shift;
135 5 ▶ my $back = $delno == $#{$tracks} || shift;
Condition100%3/3
$delno == $#{$tracks;} || shift()
l!l&&r!l&&!r
113
MC/DC100%2/2
$delno == $#{$tracks;} || shift()
$delno == $#{$tracks;} shift()
Truth table100%3/3
A || B
A= $delno == $#{$tracks;}B= shift()
ABresult
1X1
011
000
136 # $tracks->[0] must not be deleted, as it holds the meta info
137 5 ▶ return $tracks unless $delno*1;
Branch: 1 branch
BranchTrueFalseTotal
unless $delno * 1 1 4 5
138 4 ▶ return $tracks if $delno >$#{$tracks};
Branch: 1 branch
BranchTrueFalseTotal
if $delno > $#{$tracks;} 1 3 4
139 3 ▶ $tracks = $back ? _spliceback($tracks,$delno) : _splicefwd($tracks,$delno);
Branch: 1 branch
BranchTrueFalseTotal
$back ? : 2 1 3
140 3 $tracks->[0]{Number_of_tracks}=$#{$tracks};
141 3 return $tracks;
142 }
143  
144 =head2 trackfile
145  
146 trackfile returns the filename as given by $ENV{TRACKS} and does some simple
147 sanity checking on it
148  
149 =cut
150  
151 0 ▶sub _trackfile{
Subroutine _trackfile: not called CC=2 SCAR=17.9
152 # Could add a function that returns a *.track file if that
153 # is the only one found in the active directory
154 0 my $file= $ENV{TRACKS};
155 0 ▶ warn("Is $file a .tracks file?") unless $file=~/.tracks$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /.tracks$/ 0 0 0
156 0 return $file;
157  
158 }
159  
160 =head2 init
161  
162 init($filename) reads the file as specified by filename and returns the array
163 holding all the information in the .tracks-file
164  
165 =cut
166  
167 7 ▶sub init{
Subroutine init: called CC=15 SCAR=27.2
Pod documented
168 7 my (@lines,@tracks,$nooftracks, $comment, %data,$tracks);
169 7 ▶ my $file=$_[0] || _trackfile;
Condition33%1/3
$_[0] || _trackfile()
l!l&&r!l&&!r
700
MC/DC0%0/2
$_[0] || _trackfile()
$_[0] _trackfile()
Truth table33%1/3
A || B
A= $_[0]B= _trackfile()
ABresult
1X1
011
000
170 7 my $savefile=$file.".bak";
171 7 ▶ print "$file\n" if $ENV{GRFDEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GRFDEBUG"} 0 7 7
172 7 ▶ open (FILE,"<$file") || die ("Cannot open $file");
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or die "Cannot open $file" 7 0 7
173 7 ▶ open (BACKUP,">$savefile") || warn ("Cannot create backupfile, $savefile\n");
Branch: 1 branch
BranchTrueFalseTotal
open BACKUP, ">$savefile" or warn "Cannot create backupfile, $savefile\n" 7 0 7
174 7 my $i;
175 7 ▶ while(<FILE>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 252 7 259
176 252 print BACKUP $_;
177 252 ▶ if (!$nooftracks && /Number_of_tracks.(\d+)/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $nooftracks and /Number_of_tracks.(\d+)/) 7 245 252
Condition100%3/3
not $nooftracks and /Number_of_tracks.(\d+)/
!ll&&!rl&&r
841617
MC/DC100%2/2
not $nooftracks and /Number_of_tracks.(\d+)/
not $nooftracks /Number_of_tracks.(\d+)/
Truth table100%3/3
not(A) and B
A= not $nooftracksB= /Number_of_tracks.(\d+)/
ABresult
0X0
100
111
178 {
179 7 $nooftracks=$1;
180 }
181 252 chomp;
182 252 push @lines , $_;
183 252 ▶ $tracks=$tracks || /Track ?\d/;
Condition100%3/3
$tracks || /Track ?\d/
l!l&&r!l&&!r
707175
MC/DC100%2/2
$tracks || /Track ?\d/
$tracks /Track ?\d/
Truth table100%3/3
A || B
A= $tracksB= /Track ?\d/
ABresult
1X1
011
000
184 252 ▶ unless($tracks){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tracks) 175 77 252
185 175 ▶ next if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 77 98 175
186 98 my($key,$var)=split(/=/,$_);
187 98 ▶ $data{$key}=$var if $var;
Branch: 1 branch
BranchTrueFalseTotal
if $var 56 42 98
188 98 next;
189 }
190 77 ▶ $comment=$_ if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 21 56 77
191 77 ▶ if (/^Track(\d+)(start|end)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Track(\d+)(start|end)=(.*)$/) 42 35 77
192 42 $tracks[$1]{$2}=$3;
193 # The last comment is the one connected to the current track
194 42 $tracks[$1]{comment}=$comment;
195 # If the {end} element is not defined for current track, then
196 # we are at start and calculates the start timestamp
197 42 ▶ $tracks[$1]{starttime}=sec($3) unless ($tracks[$1]{end});
Branch: 1 branch
BranchTrueFalseTotal
unless $tracks[$1]{"end"} 21 21 42
198 }
199 }
200 7 $tracks[0]=\%data;
201 7 return \@tracks;
202 }
203  
204 =head2 tidytime
205  
206 $timestamp=tidytime(timestamp)
207  
208 Does some sanitychecking of the time stamp and tidies up a bit so that the
209 returned timestamp is on the form hh:mm:ss.sss
210  
211 =cut
212  
213 7 ▶sub tidytime{
Subroutine tidytime: called CC=5 SCAR=16.1
Pod documented
214 7 my $zerotime="0:00:00.000";
215 7 my $timestamp=shift;
216 7 $timestamp.=' ';
217 7 $timestamp=~m/(\d\D)?(\d{2})\D(\d{2}(\.\d{1,3})?)?/;
218 # print "<$1|$2|$3|$4>\n";
219 7 ▶ my $sec= ($3 || '0');
Condition100%2/2
$3 || '0'
l!l
61
MC/DC100%1/1
$3 || '0'
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
220 7 ▶ $sec.='.' unless $4;
Branch: 1 branch
BranchTrueFalseTotal
unless $4 5 2 7
221 7 ▶ $sec='0'.$sec if $sec < 10;
Branch: 1 branch
BranchTrueFalseTotal
if $sec < 10 1 6 7
222 7 $sec.='0'x(6-length($sec));
223 7 ▶ my $hour= ($1 || '0 ');
Condition100%2/2
$1 || '0 '
l!l
34
MC/DC100%1/1
$1 || '0 '
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
224 7 chop($hour);
225 7 return "$hour:$2:$sec";
226  
227 }
228  
229 =head2 Shellcommands
230  
231 =head3 shellhelp
232  
233 Prints out some basic help for the shell commands
234  
235 =cut
236  
237  
238 0 ▶sub shellhelp{
Subroutine shellhelp: not called CC=1 SCAR=6.9
Pod documented
239 0 print <<ENDHELP
240 h : help
241 a <t> : add a track at given time
242 d <n> : delete the given track
243 n : print number of tracks
244 p : print start and end times for all tracks
245 b <n> <t> : alter beginning of track
246 e <n> <t> : alter end of track
247 s : save file
248 q : quit
249 ---------------------------------------------------------
250 <t> time, must be given as h:mm:ss.ss
251 <n> tracknumber
252 (c) Morten Sickel (cpan\@sickel.net) April 2005
253 The last version should be available at http://sickel.net
254 Licenced under the artistic licence
255  
256 ENDHELP
257 }
258  
259 =head3 shelladjusttime
260  
261 shelladjusttime ($tracks,$command,$end)
262  
263 adjusts the time for start or end of a track. End is either set to 'start' or
264 'end'.
265  
266 =cut
267  
268 0 ▶sub shelladjusttime{
Subroutine shelladjusttime: not called CC=1 SCAR=6.9
Pod documented
269 # adjusts the time for start or end of a track
270 0 my $tracks=shift;
271 0 my $command=shift;
272 0 my $end=shift;
273 0 $command=~/\w+\s+(\d+)\s+(.*)/;
274 0 my $time=tidytime($2);
275 0 $$tracks[$1]{$end}=$time;
276 }
277  
278 =head3 shelladd
279  
280 shelladd($tracks,$timestamp)
281 Adds track at a given time
282  
283 =cut
284  
285  
286 0 ▶sub shelladd{
Subroutine shelladd: not called CC=1 SCAR=6.9
Pod documented
287  
288 0 my $tracks = shift;
289 0 my $timestamp = shift;
290 0 $timestamp=~m/^\w+\s+(.*)/;
291 0 $timestamp=$1;
292 0 $tracks=instime($tracks,$timestamp);
293
294 }
295  
296 =head3 shelldelete
297  
298 shelldelete($tracks,$trackno)
299  
300 uses deltrack() to delete the indicated track
301  
302 =cut
303  
304  
305 0 ▶sub shelldelete{
Subroutine shelldelete: not called CC=1 SCAR=6.9
Pod documented
306 0 my ($tracks,$trackno)=@_;
307 0 $trackno=~m/^\w+\s+(.*)/;
308 0 $trackno=$1;
309 0 $tracks=deltrack($tracks,$trackno);
310 }
311  
312 =head3 shellprint
313 shellprint($tracks)
314 prints out the number of tracks
315  
316 =cut
317  
318 0 ▶sub shellprint{
Subroutine shellprint: not called CC=1 SCAR=6.9
Pod documented
319 0 my $tracks = shift;
320 0 print $#$tracks," tracks\n";
321 }
322  
323 =head3 shellsave
324  
325 shellsave($tracks,$filename)
326 saves the information to $filename
327  
328 =cut
329  
330 0 ▶sub shellsave{
Subroutine shellsave: not called CC=1 SCAR=6.9
Pod documented
331 0 my($tracks,$file)=@_;
332 0 open OUT,">$file";
333 0 print OUT printtracks($tracks);
334 0 close OUT;
335 }
336  
337 =head3 shellprinttracks
338  
339 shellprinttracks($tracks)
340  
341 prints out the beginning and end time of all the tracks
342  
343 =cut
344  
345 0 ▶sub shellprinttracks{
Subroutine shellprinttracks: not called CC=2 SCAR=17.9
Pod documented
346 0 my $tracks=shift;
347 0 my @tracks=@$tracks;
348 0 my $i;
349 0 print "track from"." "x10,"to\n";
350 0 print "-"x(6+4+10+11),"\n";
351 0 foreach $i (1..$#tracks){
352 print " $i"," "x(4-length($i)),$tracks[$i]->{start},
353 0 " - ",$tracks[$i]->{end},"\n";
354 }
355
356  
357 }
358  
359 =head2 shell
360  
361 shell($file)
362  
363 shell will fetch the filename from _trackfile if not given.
364  
365 shell opens up a quite simple interactive shell for editing.
366  
367 The following commands are valid:
368  
369 h : help
370 a <t> : add a track at given time
371 d <n> : delete the given track
372 n : print number of tracks
373 p : print start and end times for all tracks
374 b <n> <t> : alter beginning of track
375 e <n> <t> : alter end of track
376 s : save file
377 q : quit
378 ---------------------------------------------------------
379 <t> time, must be given as h:mm:ss.ss
380 <n> tracknumber
381  
382  
383 =cut
384  
385  
386 0 ▶sub shell{
Subroutine shell: not called CC=13 SCAR=52.0
Pod documented
387 0 ▶ my $file = shift || _trackfile;
Condition0%0/3
shift() || _trackfile()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || _trackfile()
shift() _trackfile()
Truth table0%0/3
A || B
A= shift()B= _trackfile()
ABresult
1X1
011
000
388 0 $file=~tr/ //d;
389 0 ▶ die("Use the environment variable TRACKS to set tracks file\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
390 unless $file;
391 0 my $tracks=init($file);
392 0 ▶ die('Cannot find tracks file, use the environment variable TRACKS')
Branch: 1 branch
BranchTrueFalseTotal
unless $tracks 0 0 0
393 unless $tracks;
394 0 print "press 'h' for help\n";
395 0 while(1){
396 0 print " > ";
397 0 my $command = <>;
398 0 ▶ last if $command=~/^q/i;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^q/i 0 0 0
399 0 ▶ shellhelp if $command =~/^h/i;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^h/i 0 0 0
400 0 ▶ $tracks=shelladd($tracks,$command) if $command =~/^a/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^a/ 0 0 0
401 0 ▶ shellprint($tracks) if $command=~/^n/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^n/ 0 0 0
402 0 ▶ $tracks=shelldelete($tracks,$command) if $command=~/^d/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^d/ 0 0 0
403 0 ▶ shellsave($tracks,$file) if $command=~/^s/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^s/ 0 0 0
404 0 ▶ shellprinttracks($tracks) if $command=~/^p/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^p/ 0 0 0
405 0 ▶ shelladjusttime($tracks,$command,'start') if $command=~/^b/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^b/ 0 0 0
406 0 ▶ shelladjusttime($tracks,$command,'end') if $command=~/^e/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^e/ 0 0 0
407 }
408 }
409  
410 =head2 printtracks
411  
412 printtracks($tracks)
413  
414 returns the information in $tracks. Suitable for saving.
415  
416 =cut
417  
418 0 ▶sub printtracks{
Subroutine printtracks: not called CC=5 SCAR=28.0
Pod documented
419 0 my $tracks=shift;
420 0 my @tracks=@$tracks;
421 0 my $not="Number_of_tracks";
422 0 my $buffer="[Tracks]";
423 0 foreach (keys %{$tracks[0]}){
424 0 ▶ $buffer .= "$_=$$tracks[0]{$_}\n" if $_ ne $not;
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne $not 0 0 0
425 }
426 0 $buffer.= "\n".$not."=".$$tracks[0]{$not}."\n\n";
427 0 my $i;
428 0 foreach $i (1..$#tracks){
429 0 ▶ $i="0".$i if $i<10;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 10 0 0 0
430 6 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
431  
432 0 $buffer.= $tracks[$i]->{comment}."\n";
433 0 $buffer.= "Track${i}start=".$tracks[$i]->{start}."\n";
434 0 $buffer.= "Track${i}end=".$tracks[$i]->{end}."\n\n";
435 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
436 }
437 0 return $buffer;
438 }
439  
440 6 1;
441  
442  
443 =head1 LICENCE
444  
445 (c) Morten Sickel (cpan\@sickel.net) April 2005
446 The last version should be available at http://sickel.net
447 Licenced under the artistic licence
448  
449 =cut
↑ Summary