File Coverage

blib/lib/Audio/Nama/Sequence.pm
Criterion Covered Total %
statement 24 118 20.3
branch 0 24 0.0
condition 0 18 0.0
subroutine 8 24 33.3
pod 0 14 0.0
total 32 198 16.1


line stmt bran cond sub pod time code
1             package Audio::Nama::Sequence;
2 1     1   5 use Modern::Perl; use Carp;
  1     1   2  
  1         6  
  1         107  
  1         2  
  1         53  
3 1     1   6 use Audio::Nama::Assign qw(json_out);
  1         1  
  1         109  
4 1     1   4 use Audio::Nama::Log qw(logsub logpkg);
  1         2  
  1         46  
5 1     1   5 use Audio::Nama::Effect qw(fxn modify_effect);
  1         2  
  1         64  
6 1     1   4 use Audio::Nama::Object qw( items clip_counter );
  1         2  
  1         6  
7 1     1   5 use Audio::Nama::Globals qw(:trackrw);
  1         2  
  1         134  
8             our @ISA = 'Audio::Nama::SubBus';
9             our $VERSION = 1.0;
10 1     1   5 use SUPER;
  1         3  
  1         6  
11             our %by_name; # alias to %Audio::Nama::Bus::by_name
12             *by_name = \%Audio::Nama::Bus::by_name;
13              
14             sub new {
15 0     0 0   my ($class,%args) = @_;
16             # take out args we will process
17 0           my $items = delete $args{items};
18 0           my $counter = delete $args{clip_counter};
19             #logpkg(__FILE__,__LINE__,'debug', "items: ",map{json_out($_->as_hash)}map{$Audio::Nama::tn{$_}}@$items) if $items;
20 0   0       $items //= [];
21 0           @_ = ($class, %args);
22 0           my $self = super();
23 0           logpkg(__FILE__,__LINE__,'debug',"new object: ", json_out($self->as_hash));
24 0           logpkg(__FILE__,__LINE__,'debug', "items: ",json_out($items));
25 0           $self->{clip_counter} = $counter;
26 0           $self->{items} = $items;
27 0           $Audio::Nama::this_sequence = $self;
28 0           $self;
29             }
30             sub clip {
31 0     0 0   my ($self, $index) = @_;
32 0 0         return 0 if $index <= 0;
33 0           $Audio::Nama::tn{$self->{items}->[$index - 1]}
34             }
35             sub rw {
36 0     0 0   my $self = shift;
37             $Audio::Nama::mode->{offset_run} ? OFF : $self->{rw}
38 0 0         }
39             # perl indexes arrays at zero, for nama users we number items from one
40             sub insert_item {
41 0     0 0   my $self = shift;
42 0           my ($item, $index) = @_;
43 0 0         $self->append_item($item), return if $index == @{$self->{items}} + 1;
  0            
44 0 0         $self->verify_item($index) or die "$index: sequence index out of range";
45 0           splice @{$self->{items}}, $index - 1,0, $item->name
  0            
46             }
47             sub verify_item {
48 0     0 0   my ($self, $index) = @_;
49 0 0         $index >= 1 and $index <= scalar @{$self->items}
  0            
50             }
51             sub delete_item {
52 0     0 0   my $self = shift;
53 0           my $index = shift;
54 0 0         $self->verify_item($index) or die "$index: sequence index out of range";
55 0           my $trackname = splice(@{$self->{items}}, $index - 1, 1);
  0            
56 0 0         $Audio::Nama::tn{$trackname} and $Audio::Nama::tn{$trackname}->remove;
57             }
58             sub append_item {
59 0     0 0   my $self = shift;
60 0           my $item = shift;
61 0           push( @{$self->{items}}, $item->name );
  0            
62             }
63             sub item {
64 0     0 0   my $self = shift;
65 0           my $index = shift;
66 0 0         return 0 if $index <= 0;
67 0           $Audio::Nama::tn{$self->{items}->[$index - 1]};
68             }
69             sub list_output {
70 0     0 0   my $self = shift;
71 0           my $i;
72             join "\n","Sequence $self->{name} clips:",
73             map { join " ",
74             ++$i,
75             $Audio::Nama::tn{$_}->n,
76             $_,
77 0           sprintf("%.3f %.3f", $Audio::Nama::tn{$_}->duration, $Audio::Nama::tn{$_}->endpoint),
78 0           } @{$self->items}
  0            
79             }
80             sub remove {
81 0     0 0   my $sequence = shift;
82              
83             # delete all clips
84 0           map{$Audio::Nama::tn{$_}->remove } $by_name{$sequence->name}->tracks;
  0            
85              
86             # delete clip array
87 0           delete $sequence->{items};
88            
89 0           my $mix_track = $Audio::Nama::tn{$sequence->name};
90              
91 0 0         if ( defined $mix_track ){
92            
93 0           $mix_track->unbusify;
94            
95             # remove mix track unless it has some WAV files
96              
97 0 0         $mix_track->remove unless scalar @{ $mix_track->versions };
  0            
98             }
99              
100             # remove sequence from index
101            
102 0           delete $by_name{$sequence->name};
103             }
104             sub new_clip {
105 0     0 0   my ($self, $track, %args) = @_; # $track can be object or name
106 0           my $markpair = delete $args{region};
107 0           logpkg(__FILE__,__LINE__,'debug',json_out($self->as_hash), json_out($track->as_hash));
108 0 0 0       ref $track or $track = $Audio::Nama::tn{$track}
109             or die("$track: track not found.");
110 0   0       my %region_args = (
      0        
111             region_start => $markpair && $markpair->[0]->name || $track->region_start,
112             region_end => $markpair && $markpair->[1]->name || $track->region_end
113             );
114 0           my $clip = Audio::Nama::Clip->new(
115             target => $track->basename,
116             name => $self->unique_clip_name($track->name, $track->monitor_version),
117             rw => PLAY,
118             group => $self->name,
119             version => $track->monitor_version,
120             hide => 1,
121             %region_args,
122             %args
123             );
124 0           modify_effect( $clip->vol, 1, undef, fxn($track->vol)->params->[0]);
125 0           modify_effect( $clip->pan, 1, undef, fxn($track->pan)->params->[0]);
126 0           $clip
127             }
128             sub new_spacer {
129 0     0 0   my( $self, %args ) = @_;
130 0           my $position = delete $args{position};
131             my $spacer = Audio::Nama::Spacer->new(
132             duration => $args{duration},
133 0           name => $self->unique_spacer_name(),
134             rw => OFF,
135             group => $self->name,
136             );
137 0   0       $self->insert_item( $spacer, $position || ( scalar @{ $self->{items} } + 1 ))
138             }
139             sub unique_clip_name {
140 0     0 0   my ($self, $trackname, $version) = @_;
141 0           join '-', $self->name , ++$self->{clip_counter}, $trackname, 'v'.$version;
142             }
143             sub unique_spacer_name {
144 0     0 0   my $self = shift;
145 0           join '-', $self->name, ++$self->{clip_counter}, 'spacer';
146             }
147             package Audio::Nama;
148              
149             sub new_sequence {
150              
151 0     0     my %args = @_;
152 0           my $name = $args{name};
153 0 0         my @tracks = defined $args{tracks} ? @{ $args{tracks} } : ();
  0            
154 0   0       my $group = $args{group} || 'Main';
155 0   0       my $mix_track = $tn{$name} || add_track($name, group => $group);
156 0           $mix_track->set( rw => MON);
157 0           my $sequence = Audio::Nama::Sequence->new(
158             name => $name,
159             send_type => 'track',
160             send_id => $name,
161             );
162             ;
163 0           map{ $sequence->append_item($_) }
164 0           map{ $sequence->new_clip($_)} @tracks;
  0            
165 0           $this_sequence = $sequence;
166              
167             }
168             sub compose_sequence {
169 0     0     my ($sequence_name, $track, $markpairs) = @_;
170 0           logpkg(__FILE__,__LINE__,'debug',"sequence_name: $sequence_name, track:", $track->name,
171             ", markpairs: ",Audio::Nama::Dumper $markpairs);
172              
173 0           my $sequence = new_sequence( name => $sequence_name);
174 0           logpkg(__FILE__,__LINE__,'debug',"sequence\n",Audio::Nama::Dumper $sequence);
175             my @clips = map {
176 0           $sequence->new_clip($track, region => $_)
  0            
177             } @$markpairs;
178 0           map{ $sequence->append_item($_) } @clips;
  0            
179 0           $sequence
180             }
181             1
182             __END__