This file is indexed.

/usr/share/perl5/Audio/Nama/Edit.pm is in nama 1.078-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
package Audio::Nama::Edit;

# each edit is identified by:
#  -  host track name
#  -  host track version
#  -  edit name (i.e. sax-v1) used as key in %by_name
#

use Modern::Perl;
our $VERSION = 1.0;
use Carp;
no warnings qw(uninitialized);
our @ISA;
use vars qw(%n %by_index %by_name );
use Audio::Nama::Object qw( 
				n
				play_start_mark_name
				rec_start_mark_name
				rec_end_mark_name
				host_track
				host_version
				fades
				 );

sub initialize {
	%n = ();
	%by_name = ();
	@Audio::Nama::edits_data = (); # for save/restore
}

sub next_n {
	my ($trackname, $version) = @_;
	++$n{$trackname}{$version}
}

sub new {
	my $class = shift;	
	my %vals = @_;

	croak "undeclared field: @_" if grep{ ! $_is_field{$_} } keys %vals;
	
	# increment edit version number by host track and host version
	
	my $n = next_n(@vals{qw(host_track host_version)});

	my $self = bless 
		{ 
			n 		=> $n,
		  	fades 	=> [],
			@_ 
		}, $class;

	$by_name{ $self->edit_name } = $self;
	$by_index{ $self->n } = $self;

	#print "self class: $class, self type: ", ref $self, $/;

	my $name = $self->host_track;
	my $host = $Audio::Nama::tn{$name};

# Routing:
#
#    sax-v5-original --+
#                      |
#    sax-v5-edit1 -----+--- sax-v5 (bus/track) --- sax (bus/track) ----- 


	# prepare top-level bus and mix track
	
	$host->busify;     # i.e. sax (bus/track)
	
	# create the version-level bus and mix track
	# i.e. sax-v5 (bus/track)

	# (maybe it already exists)
	
	Audio::Nama::Track->new(
		name 		=> $self->edit_root_name, # i.e. sax-v5
	#	rw			=> 'REC',                 # set by ->busify
		source_type => 'bus',
		source_id 	=> 'bus',
		width		=> 2,                     # default to stereo 
	#	rec_defeat 	=> 1,                     # set by ->busify
		group   	=> $self->host_track,     # i.e. sax
		hide		=> 1,
	); 
	$self->version_mix->busify;                                # create sub-bus

	# create host track alias if necessary

	# To ensure that users don't get into trouble, we would like to 
	# restrict this track:
	#  - version number must *not* be allowed to change
	#  - rw setting must be fixed to 'MON' #
	#  The easiest way may be to subclass the 'set' routine
	
	my $host_track_alias = $Audio::Nama::tn{$self->host_alias} // 
		Audio::Nama::VersionTrack->new(
			name 	=> $self->host_alias,
			version => $host->monitor_version, # static
			target  => $host->name,
			rw		=> 'MON',                  # do not REC
			group   => $self->edit_root_name,  # i.e. sax-v5
			hide 	=> 1,
		);

	# create edit track
	#   - same name as edit
	#   - we expect to record
	#   - source_type and source_id come from host track
	
	my $edit_track = Audio::Nama::EditTrack->new(
		name		=> $self->edit_name,
		rw			=> 'REC',
		source_type => $host->source_type,
		source_id	=> $host->source_id,
		group		=> $self->edit_root_name,  # i.e. sax-v5
		hide		=> 1,
	); 
	$self
}
sub edit_root_name {
	my $self = shift;
	join '-', $self->host_track, 'v'.$self->host_version;
}
sub edit_name {
	my $self = shift;
	join '-', $self->edit_root_name, 'edit'.$self->n
}
sub host_alias {
	my $self = shift;
	join '-', $self->edit_root_name, 'original'
}

# default mark names

sub play_start_name {
	my $self = shift;
	$self->play_start_mark_name || (join '-', $self->edit_name,'play-start')
}
sub rec_start_name {
	my $self = shift;
	$self->rec_start_mark_name || (join '-', $self->edit_name,'rec-start')
}
sub rec_end_name {
	my $self = shift;
	$self->rec_end_mark_name || (join '-', $self->edit_name,'rec-end')
}

sub play_start_mark { $Audio::Nama::Mark::by_name{$_[0]->play_start_name} }
sub rec_start_mark  { $Audio::Nama::Mark::by_name{$_[0]->rec_start_name}  }
sub rec_end_mark    { $Audio::Nama::Mark::by_name{$_[0]->rec_end_name}    }

# the following are unadjusted values

sub play_start_time {
	my $self = shift;
	$self->marktime('play_start_name')
}
sub rec_start_time {
	my $self = shift;
	$self->marktime('rec_start_name')
}
sub rec_end_time {
	my $self = shift;
	$self->marktime('rec_end_name')
}
sub play_end_time {
	my $self = shift;
	$self->marktime('rec_end_name') + $Audio::Nama::edit_playback_end_margin
}

sub marktime { 
	my ($self,$markfield) = @_;
	$Audio::Nama::Mark::by_name{$self->$markfield}->{time}
}

sub store_fades { # replacing previous
	my $edit = shift;
	my @fades = @_;
	my @indices = map{$_->n} @fades;
	$edit->remove_fades;
	$edit->set(fades => \@indices)
}
sub remove_fades {
	my $edit = shift;
	map{ $_->remove } map{ $Audio::Nama::Fade::by_index{$_} } @{$edit->fades};
	$edit->set(fades => []);
}

sub destroy {
	my $edit = shift;

	# remove object from index hash
	delete $by_index{$edit->n};
	delete $by_name{$edit->edit_name};

	# list edit track WAV files
	my @wavs = values %{$edit->edit_track->targets};

	#   track removal also takes care of fades # VERIFY
	#	my $fades = $edit->fades;
	#	map{ $Audio::Nama::Fade::by_index{$_}->remove } @$fades;

	# remove edit track
	$edit->edit_track->remove;

	my @sister_edits = grep{ 
			$edit->host_track   eq $_->host_track 
		and $edit->host_version == $_->host_version 
	} values %by_index;

	# if we are the last edit, remove all auxiliary tracks/buses
	
	if ( ! @sister_edits ){

		$edit->host_alias_track->remove;

		$edit->version_bus->remove;
		# note: bus->remove will not delete a mix track with WAV files

		# The host may have a version symlinked to a WAV file 
		# belonging to the version mix track. So we remove
		# the track, but not the wav files.

		$edit->version_mix->remove if defined $edit->version_mix;

		$edit->host_bus->remove;
	}
	# remove edit track WAV files if we've reached here
	map{ 
		my $file = Audio::Nama::join_path(Audio::Nama::this_wav_dir(), $_);
		say "removing $file";
		#unlink $file;
	} @wavs;
}

sub host	 		{ $Audio::Nama::tn{$_[0]->host_track} } # top-level mix track, i.e. 'sax'
sub host_bus 		{ $Audio::Nama::Bus::by_name{$_[0]->host_track} }  # top-level bus
sub version_mix     { $Audio::Nama::tn{$_[0]->edit_root_name} }        # in top-level bus
sub version_bus     { $Audio::Nama::Bus::by_name{$_[0]->edit_root_name} } # version-level bus
sub host_alias_track{ $Audio::Nama::tn{$_[0]->host_alias} }            # in version_bus
sub edit_track 		{ $Audio::Nama::tn{$_[0]->edit_name} }             # in version_bus

# utility routines

# -------- Edit routines; Main Namespace ------
{
package Audio::Nama;
use Modern::Perl; use Carp;
no warnings 'uninitialized';

our (
	%event_id,
	$term,
	$attribs,
	%tn,
	%ti,
	%bn,
	@edit_points,
	$this_track,
	$regenerate_setup,
	$offset_run_flag,
	$loop_enable,
	$this_edit,
	$offset_run_start_time,
	$offset_run_end_time,
	$offset_mark,
	$edit_crossfade_time,


);
	

sub detect_keystroke_p {
	$event_id{stdin} = AE::io(*STDIN, 0, sub {
		&{$attribs->{'callback_read_char'}}();
		
		abort_set_edit_points(), return
			if $attribs->{line_buffer} eq "q"
			or $attribs->{line_buffer} eq "Q";

		if (   $attribs->{line_buffer} eq "p"
			or $attribs->{line_buffer} eq "P"){ get_edit_mark()}
		else{ reset_input_line() }
	});
}

sub reset_input_line {
	$attribs->{line_buffer} = q();
	$attribs->{point} 		= 0;
	$attribs->{end}   		= 0;
}


{ my $p;
  my @edit_points; 
  my @names = qw(dummy play-start rec-start rec-end);

sub initialize_edit_points {
	$p = 0;
    @edit_points = ();
}
sub abort_set_edit_points {
	say "...Aborting!";
	reset_input_line();
	eval_iam('stop');
	initialize_edit_points();
	detect_spacebar();
}

sub get_edit_mark {
	$p++;
	if($p <= 3){  # record mark
		my $pos = eval_iam('getpos');
		push @edit_points, $pos;
		say " got $names[$p] position ".d1($pos);
		reset_input_line();
		if( $p == 3){ complete_edit_points() }
		else{
			$term->stuff_char(10);
			&{$attribs->{'callback_read_char'}}();
		}
	}
}
sub complete_edit_points {
	@Audio::Nama::edit_points = @edit_points; # save to global
	eval_iam('stop');
	say "\nEngine is stopped\n";
	detect_spacebar();
	print prompt(), " ";
}
}
sub set_edit_points {
	$tn{$this_edit->edit_name}->set(rw => 'OFF') if defined $this_edit;
	say("You must use a playback-only mode to setup edit marks. Aborting"), 
		return 1 if Audio::Nama::ChainSetup::really_recording();
	say("You need stop the engine first. Aborting"), 
		return 1 if engine_running();
	say "Ready to set edit points!";
	sleeper(0.2);
	say q(Press the "P" key three times to mark positions for:
    + play-start
    + record-start
    + record-end

	say q(Press "Q" to quit.)

Engine will start in 2 seconds.);
	initialize_edit_points();
 	$event_id{set_edit_points} = AE::timer(2, 0, 
	sub {
		reset_input_line();
		detect_keystroke_p();
		eval_iam('start');
		say "\n\nEngine is running\n";
		print prompt();
	});
}
sub transfer_edit_points {
	say("Use 'set_edit_points' command to specify edit region"), return
		 unless scalar @edit_points;
	my $edit = shift;
	Audio::Nama::Mark->new( name => $edit->play_start_name, time => $edit_points[0]);
	Audio::Nama::Mark->new( name => $edit->rec_start_name,  time => $edit_points[1]);
	Audio::Nama::Mark->new( name => $edit->rec_end_name,    time => $edit_points[2]);
	@edit_points = ();
}

sub generate_edit_record_setup { # for current edit
	# set edit track to REC
	# set global region start offset
	# set global region length cutoff
	# set regenerate_setup flag
	# insert host track fades
	# mute edit track
	# schedule unmuting at rec-start point - fade-in
	# schedule muting at rec-end point     - fade-out
}

sub new_edit {
	#my @edit_points = @_;

	# abort for many different reasons
	
	say("You must use 'set_edit_points' before creating a new edit. Aborting."),
		return unless @edit_points;
	my $overlap = grep { 
		my $fail;
		my $rst = $_->rec_start_time;
		my $ret = $_->rec_end_time;
		my $nst = $edit_points[1];
		my $net = $edit_points[2];
		my $rst1 = d1($rst);
		my $ret1 = d1($ret);
		my $nst1 = d1($nst);
		my $net1 = d1($net);
		say("New rec-start time $nst1 conflicts with Edit ",
			$_->n, ": $rst1 < $nst1 < $ret1"), $fail++
			if $rst < $nst and $nst < $ret;
		say("New rec-end time $net1 conflicts with Edit ",
			$_->n, ": $rst1 < $net1 < $ret1"), $fail++
			if $rst < $net and $net < $ret;
		say("New rec interval $nst1 - $net1 conflicts with Edit ",
			$_->n, ": $rst1 - $ret1"), $fail++
			if $nst < $rst and $ret < $net;
		$fail
	} grep{ $_->host_track eq $this_track->name} 
		values %Audio::Nama::Edit::by_name;
	say("Aborting."), return if $overlap;
	my $name = $this_track->name;
	my $editre = qr($name-v\d+-edit\d+);
	say("$name: editing of edits is not currently allowed."),
		return if $name =~ /-v\d+-edit\d+/;
	say("$name: must be in MON mode.
Edits will be applied against current version"), 
		return unless $this_track->rec_status eq 'MON' 
			or $this_track->rec_status eq 'REC' and
			grep{ /$editre/ } keys %Audio::Nama::Track::by_name;

	# create edit
	
	my $v = $this_track->monitor_version;
	say "$name: creating new edit against version $v";
	my $edit = Audio::Nama::Edit->new(
		host_track 		=> $this_track->name,
		host_version	=> $v,
	);
	$this_track->current_edit->{$v} = $edit->n;
	$this_edit = $edit;
	transfer_edit_points($edit);
	#select_edit($this_edit->n);
	edit_action('preview_edit_in');
}
{my %edit_actions = 
	(
		record_edit => sub { 
			$this_edit->edit_track->set(rw => 'REC');
			$this_edit->store_fades(std_host_fades(), edit_fades());
		},
		play_edit => sub {
			$this_edit->edit_track->set(rw => 'MON');
			$this_edit->store_fades(std_host_fades(), edit_fades());
		},
		preview_edit_in => sub {
			$this_edit->edit_track->set(rw => 'OFF');
			$this_edit->store_fades(std_host_fades());
		},
		preview_edit_out => sub {
			$this_edit->edit_track->set(rw => 'OFF');
			$this_edit->store_fades(reverse_host_fades());
		},
	);

sub edit_action {
	my $action = shift;
	defined $this_edit or say("Please select an edit and try again."), return;
	set_edit_mode();
	$this_edit->host_alias_track->set(rw => 'MON'); # all 
	$edit_actions{$action}->();
	$regenerate_setup++;

#   TODO: looping
# 	my $is_setup = generate_setup(); 
# 	return unless $is_setup;
# 	if ($action !~ /record/){
# 		$loop_enable++;
# 		@loop_endpoints = (0,$length - 0.05);
# 		#  and transport_start()
# 	}
# 	connect_transport(); 
}
}

sub end_edit_mode  	{ 

	# regenerate fades
	
	$offset_run_flag = 0; 
	$loop_enable = 0;
	offset_run_mode(0);	
	$this_track = $this_edit->host if defined $this_edit;
	undef $this_edit;
	$regenerate_setup++ 
}
sub destroy_edit {
	say("no edit selected"), return unless $this_edit;
	my $reply = $term->readline('destroy edit "'.$this_edit->edit_name.
		qq(" and all its WAV files?? [n] ));
	if ( $reply =~ /y/i ){
		say "permanently removing edit";
		$this_edit->destroy;
	}
	$term->remove_history($term->where_history);
	$this_track = $this_edit->host;
	end_edit_mode();
}
sub set_edit_mode 	{ $offset_run_flag = edit_mode_conditions() ?  1 : 0 }
sub edit_mode		{ $offset_run_flag and defined $this_edit}
sub edit_mode_conditions {        
	defined $this_edit or say('No edit is defined'), return;
	defined $this_edit->play_start_time or say('No edit points defined'), return;
	$this_edit->host_alias_track->rec_status eq 'MON'
		or say('host alias track : ',$this_edit->host_alias,
				" status must be MON"), return;

	# the following conditions should never be triggered 
	
	$this_edit->host_alias_track->monitor_version == $this_edit->host_version
		or die('host alias track: ',$this_edit->host_alias,
				" must be set to version ",$this_edit->host_version), return
	1;
}
sub reverse_host_fades { host_fades('in','out') }

sub std_host_fades { host_fades('out','in') }

sub host_fades {
	my ($first,$second) = @_;
	Audio::Nama::Fade->new(  type => $first,
					mark1 => $this_edit->rec_start_name,
					duration => $edit_crossfade_time,
					relation => 'fade_from_mark',
					track => $this_edit->host_alias,
	), 
	Audio::Nama::Fade->new(  type => $second,
					mark1 => $this_edit->rec_end_name,
					duration => $edit_crossfade_time,
					relation => 'fade_from_mark',
					track => $this_edit->host_alias,
	), 
}
sub edit_fades {
	Audio::Nama::Fade->new(  type => 'in',
					mark1 => $this_edit->rec_start_name,
					duration => $edit_crossfade_time,
					relation => 'fade_from_mark',
					track => $this_edit->edit_name,
	), 
	Audio::Nama::Fade->new(  type => 'out',
					mark1 => $this_edit->rec_end_name,
					duration => $edit_crossfade_time,
					relation => 'fade_from_mark',
					track => $this_edit->edit_name,
	); 
}

### edit region computations

{
# use internal lexical values for the computations

# track values
my( $trackname, $playat, $region_start, $region_end, $length);

# edit values
my( $edit_play_start, $edit_play_end);

# dispatch table
my( %playat, %region_start, %region_end);

# test variables
# my ($index, $new_playat, $new_region_start, $new_region_end);



%region_start = (
    out_of_bounds_near				=> sub{ "*" },
    out_of_bounds_far				=> sub{ "*" },	

	play_start_during_playat_delay	=> sub {$region_start },
	no_region_play_start_during_playat_delay => sub { 0 },

	play_start_within_region 
				=> sub {$region_start + $edit_play_start - $playat },
	no_region_play_start_after_playat_delay
				=> sub {$region_start + $edit_play_start - $playat },
);
%playat = (
    out_of_bounds_near				=> sub{ "*" },
    out_of_bounds_far				=> sub{ "*" },	

	play_start_during_playat_delay	=> sub{ $playat - $edit_play_start },
	no_region_play_start_during_playat_delay
									=> sub{ $playat - $edit_play_start },

	play_start_within_region   				=> sub{ 0 },
	no_region_play_start_after_playat_delay => sub{ 0 },

);
%region_end = (
    out_of_bounds_near				=> sub{ "*" },
    out_of_bounds_far				=> sub{ "*" },	

	play_start_during_playat_delay	
		=> sub { $region_start + $edit_play_end - $playat },
	no_region_play_start_during_playat_delay 
		=> sub {                 $edit_play_end - $playat },

	play_start_within_region 
		=> sub { $region_start + $edit_play_end - $playat },
	no_region_play_start_after_playat_delay
		=> sub {                 $edit_play_end - $playat },
);

sub new_playat       {       $playat{edit_case()}->() };
sub new_region_start { $region_start{edit_case()}->() };
sub new_region_end   
	{   
		my $end = $region_end{edit_case()}->();
		return $end if $end eq '*';
		$end < $length ? $end : $length
	};
# the following value will always allow enough time
# to record the edit. it may be longer than the 
# actual WAV file in some cases. (I doubt that
# will be a problem.)

sub edit_case {

	# logic for no-region case
	
    if ( ! $region_start and ! $region_end  )
	{
		if( $edit_play_end < $playat)
			{ "out_of_bounds_near" }
		elsif( $edit_play_start > $playat + $length)
			{ "out_of_bounds_far" }
		elsif( $edit_play_start >= $playat)
			{"no_region_play_start_after_playat_delay"}
		elsif( $edit_play_start < $playat and $edit_play_end > $playat )
			{ "no_region_play_start_during_playat_delay"}
	} 
	# logic for region present case
	
	elsif ( defined $region_start and defined $region_end )
	{ 
		if ( $edit_play_end < $playat)
			{ "out_of_bounds_near" }
		elsif ( $edit_play_start > $playat + $region_end - $region_start)
			{ "out_of_bounds_far" }
		elsif ( $edit_play_start >= $playat)
			{ "play_start_within_region"}
		elsif ( $edit_play_start < $playat and $playat < $edit_play_end)
			{ "play_start_during_playat_delay"}
		else {carp "$trackname: fell through if-then"}
	}
	else { carp "$trackname: improperly defined region" }
}

sub set_edit_vars {
	my $track = shift;
	$trackname      = $track->name;
	$playat 		= $track->playat_time;
	$region_start   = $track->region_start_time;
	$region_end 	= $track->region_end_time;
	$edit_play_start= play_start_time();
	$edit_play_end	= play_end_time();
	$length 		= wav_length($track->full_path);
}
sub play_start_time {
	defined $this_edit 
		? $this_edit->play_start_time 
		: $offset_run_start_time # zero unless offset run mode
}
sub play_end_time {
	defined $this_edit 
		? $this_edit->play_end_time 
		: $offset_run_end_time   # undef unless offset run mode
}
sub set_edit_vars_testing {
	($playat, $region_start, $region_end, $edit_play_start, $edit_play_end, $length) = @_;
}
}

sub list_edits {
	my @edit_data =
		map{ s/^---//; s/...\s$//; $_ } 
		map{ $_->dump }
		sort{$a->n <=> $b->n} 
		values %Audio::Nama::Edit::by_index;
	pager(@edit_data);
}
sub explode_track {
	my $track = shift;
	
	# quit if I am already a mix track

	say($track->name,": I am already a mix track. I cannot explode!"),return
		if $track->is_mix_track;

	my @versions = @{ $track->versions };

	# quit if I have only one version

	say($track->name,": Only one version. Skipping."), return
		if scalar @versions == 1;

	$track->busify;

	my $host = $track->name;
	my @names = map{ "$host-v$_"} @versions;
	my @exists = grep{ $Audio::Nama::tn{$_} } @names;
	say("@exists: tracks already exist. Aborting."), return if @exists;
	my $current = cwd;
	chdir this_wav_dir();
	for my $i (@versions){

		# make a track

		my $name = "$host-v$i";
		Audio::Nama::Track->new(
			name 	=> $name, 
			rw		=> 'MON',
			group	=> $host,
		);

		# symlink the WAV file we want

		symlink $track->targets->{$i}, "$name.wav";


	}
	chdir $current;
}	

sub select_edit {
	my $n = shift;
	my ($edit) = grep{ $_->n == $n } values %Audio::Nama::Edit::by_name;

	# check that conditions are met
	
	say("Edit $n not found. Skipping."),return if ! $edit;
 	say( qq(Edit $n applies to track "), $edit->host_track, 
 		 qq(" version ), $edit->host_version, ".
This does does not match the current monitor version (",
$edit->host->monitor_version,"). 
Set the correct version and try again."), return
	if $edit->host->monitor_version != $edit->host_version;

	# select edit
	
	$this_edit = $edit;

	# turn on top-level bus and mix track
	
	$edit->host_bus->set(rw => 'REC');

	$edit->host->busify;

	# turn off all version level buses/mix_tracks
	
	map{ $tn{$_}->set(rw => 'OFF');  # version mix tracks
	      $bn{$_}->set(rw => 'OFF'); # version buses
	} $this_edit->host_bus->tracks;  # use same name for track/bus

	# turn on what we want
	
	$edit->version_bus->set(rw => 'REC');

	$edit->version_mix->busify;

	$edit->host_alias_track->set(rw => 'MON');

	$edit->edit_track->set(rw => 'MON');
	
	$this_track = $edit->host;
}
sub apply_fades { 
	# use info from Fade objects in %Audio::Nama::Fade::by_name
	# applying to tracks that are part of current
	# chain setup
	map{ Audio::Nama::Fade::refresh_fade_controller($_) }
	grep{$_->{fader} }  # only if already exists
	Audio::Nama::ChainSetup::engine_tracks();
}
	
sub disable_edits {

	say("Please select an edit and try again."), return
		unless defined $this_edit;
	my $edit = $this_edit;

	$edit->host_bus->set( rw => 'OFF');

	$edit->version_bus->set( rw => 'OFF');

	# reset host track
	
	$edit->host->unbusify;
	
}
sub merge_edits {
	my $edit = $this_edit;
	say("Please select an edit and try again."), return
		unless defined $edit;
	say($edit->host_alias, ": track must be MON status.  Aborting."), return
		unless $edit->host_alias_track->rec_status eq 'MON';
	say("Use exit_edit_mode and try again."), return if edit_mode();

	# create merge message
	my $v = $edit->host_version;
	my %edits = 
		map{ my ($edit) = $tn{$_}->name =~ /edit(\d+)$/;
			 my $ver  = $tn{$_}->monitor_version;
			 $edit => $ver
		} grep{ $tn{$_}->name =~ /edit\d+$/ and $tn{$_}->rec_status eq 'MON'} 
		$edit->version_bus->tracks; 
	my $msg = "merges ".$edit->host_track."_$v.wav w/edits ".
		join " ",map{$_."v$edits{$_}"} sort{$a<=>$b} keys %edits;
	# merges mic_1.wav w/mic-v1-edits 1_2 2_1 
	
	say $msg;

	# cache at version_mix level
	
	my $output_wav = cache_track($edit->version_mix);

	# promote to host track

	my $new_version = $edit->host->last + 1;		
	add_system_version_comment($edit->host, $new_version, $msg);
	add_system_version_comment($edit->version_mix, $edit->version_mix->last, $msg);
	my $old = cwd();
	chdir this_wav_dir();
	my $new_host_wav = $edit->host_track . "_" .  $new_version . ".wav";
	symlink $output_wav, $new_host_wav;

	$edit->host->set(version => undef); # default to latest
	$edit->host->{version_comment}{$new_version}{system} = $msg;
	chdir $old;
	disable_edits();
	$this_track = $edit->host;
	
}
sub show_version_comments {
	my ($t, @v) = @_;
	return unless @v;
	Audio::Nama::pager(map{ $t->version_comment($_) } @v);
}
sub add_version_comment {
	my ($t,$v,$text) = @_;
	$t->targets->{$v} or say("$v: no such version"), return;	
	$t->{version_comment}{$v}{user} = $text;
	$t->version_comment($v);
}
sub add_system_version_comment {
	my ($t,$v,$text) = @_;
	$t->targets->{$v} or say("$v: no such version"), return;	
	$t->{version_comment}{$v}{system} = $text;
	$t->version_comment($v);
}
sub remove_version_comment {
	my ($t,$v) = @_;
	$t->targets->{$v} or say("$v: no such version"), return;	
	delete $t->{version_comment}{$v}{user};
	$t->version_comment($v) || "$v: [comment deleted]\n";
}
sub remove_system_version_comment {
	my ($t,$v) = @_;
	delete $t->{version_comment}{$v}{system} if $t->{version_comment}{$v}
}
# offset recording

# Note that although we use ->adjusted_* methods, all are
# executed outside of edit mode, so we get unadjusted values.

sub setup_length {
	my $length;
	map{  my $l = $_->adjusted_length; $length = $l if $l > $length }
	grep{ $_-> rec_status eq 'MON' }
	Audio::Nama::ChainSetup::engine_tracks();
	$length
}
sub offset_run {
	say("This function not available in edit mode.  Aborting."), 
		return if edit_mode();
	my $markname = shift;
	
	$offset_run_start_time = $Audio::Nama::Mark::by_name{$markname}->time;
	$offset_run_end_time   = setup_length();
	$offset_mark = $markname;
	offset_run_mode(1);
	$regenerate_setup++;
}
sub clear_offset_run_vars {
	$offset_run_start_time = 0;
	$offset_run_end_time   = undef;
	$offset_mark 		   = undef;
}
sub offset_run_mode {
	my $set = shift;
	given($set){
		when(0){  
			undef $offset_run_flag;
			clear_offset_run_vars();
			$regenerate_setup++;
		}
		when(1){
			undef $this_edit; 
			$offset_run_flag++
		}
	}
	$offset_run_flag and ! defined $this_edit
}
	
sub select_edit_track {
	my $track_selector_method = shift;
	print("You need to select an edit first (list_edits, select_edit)\n"),
		return unless defined $this_edit;
	$this_track = $this_edit->$track_selector_method; 
	command_process('show_track');
}

} # end package

1;

__END__