aboutsummaryrefslogtreecommitdiff
path: root/soxdial
blob: b631449908b047d15e79c9c63b4e54a06456227e (plain)
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
#!/usr/bin/perl -w

# constuct and execute a sox command to dial a phone number with DTMF tones.

$VERSION = "0.1.0";
($SELF = $0) =~ s,.*/,,;

$|++;

=pod

=head1 NAME

soxdial - generate DTMF (touchtone, dialtone, or blue box) audio

=head1 SYNOPSIS

B<soxdial> [I<global-options>] [ [I<dial-options>] [I<dial-string>] ... ]

B<soxdial> [I<utility-option>]

=head1 DESCRIPTION

B<soxdial> uses B<sox>(1) to generate DTMF tones (aka touchtone
dialling), dialtone, and/or "blue box" tones. Output can be played through the
system sound card, or saved to a file of any type supported by sox.

By default, letters are accepted and converted to numbers, according
to the layout of a touchtone phone (e.g. B<A>, B<B>, B<C> are I<2>;
B<D>, B<E>, B<F> are I<3>, etc). If the extended touchtone digits B<A>,
B<B>, B<C>, and B<D> are needed, they can be enabled with the B<-x>,
B<--extended> option (see below).

The generated audio is monophonic by default, though stereo output
can be done with the B<-S>, B<--stereo>; B<-L>, B<--left>; or B<-R>,
B<--right> options (see below).

The time each digit is pressed and the timing between the digits can
be controlled (B<-l>, B<--length>; B<-d>, B<--delay>, and can also be
randomized (B<-X>, B<--random>). Longer pauses can be added
between digits by including a comma, or using the B<-s>, B<--silence>
option.

=head1 OPTIONS

Note that option bundling is not supported. Use e.g. B<-v -n>,
not B<-vn>. Spaces are allowed but not required between options and
their arguments: B<-b16> and B<-b 16> are equivalent. Also, GNU-style
B<--option=value> is supported (and works the same as B<--option
value>).

=head2 Utility Options

=over 4

=item B<-V>, B<--version>

Print the version number of B<soxdial>, then exit.

=item B<--help>

Prints this help text, via B<perldoc>(1), then exit.

=item B<--man>

Prints this help text as a man page, via B<pod2man>(1), then exit. Suggested use:

  soxdial --man > soxdial.1

Then B<soxdial.1> can be installed in e.g. /usr/man/man1 or
/usr/share/man/man1 or wherever your OS keeps its man pages.

=back

=head2 Global Options

These options affect the entire output. They should only be given once,
before any dial strings or dial options.

=over 4

=item B<-o>, B<--output> I<output>

Write sox's output to a file, rather than playing it.  B<sox> uses
the the filename extension to determine the file format: use B<.wav>
for RIFF WAVE output, B<.flac> for FLAC, B<.ogg> for Ogg Vorbis,
B<.raw> (or no extension) for raw audio samples, etc. The special
filename B<-> writes raw samples to standard output. Note that nothing
else ever gets written to standard output, so you won't get verbose
messages mixed with audio samples.

=item B<--stdout>

Write sox's output to standard output. Same as B<-o -> or B<--output ->.

=item B<-r>, B<--rate> I<rate>

Set the bitrate in hertz (or kilohertz if followed by B<k>. Default is
8000 (or 8k). You should probably stick with standard bitrates such
as 22050, 44100, 48000, etc, although this is not enforced. If this
option is used, it B<must> occur on the command line B<before> any
dial string.

=item B<-b>, B<--bits> I<bits>

Set the bits per sample. Default is 8. The only other choice is 16.
8-bit samples will be encoded as unsigned, and 16-bit will be encoded
as signed. If this option is used, it B<must> occur on the command
line B<before> any dial string.

=item B<-S>, B<--stereo>

Output stereo audio, with the generated tones on both the left and
right channels. Use this if you're piping or importing the audio into
some other piece of software that expects stereo input.

=item B<-L>, B<--left>

Output stereo audio, with the generated tones on the left channel and silence
on the right channel.

=item B<-R>, B<--right>

Output stereo audio, with the generated tones on the right channel and silence
on the left channel.

=item B<-v>, B<--verbose>

Print verbose information, including the generated B<sox> command, on standard
error.

=item B<-n>, B<--no-exec>

Do not execute the generated B<sox> command. This option also
enables B<-v>, B<--verbose>.

=item B<--sox> I<sox-executable>

Explicitly set the location of the sox binary. Useful if it's in a
nonstandard location. If this doesn't contain any slashes, B<PATH> is
used to search for it. If it does contain slashes, it's used as a full
path (e.g. B<--sox /usr/local/audiostuff/sox/bin/sox>.

This option does the same thing as the B<SOX> environment variable,
but it will override the environment.

=back

=head2 Dial Options

These options can be mixed freely with dial strings, and are applied
as they're found on the command line. Each one affects the rest of the
dial strings, until the same option is seen again.

=over 4

=item B<-l>, B<--length> I<sec> | I<millisec>B<ms>

Sets the time each digit's tones are played. Default is 0.25
or 250ms. Can be randomized with B<-X>, B<--random>.

=item B<-d>, B<--delay> I<sec> | I<millisec>B<ms>

Sets the delay between consecutive digits. Default is 0.1
or 100ms. Can be randomized with B<-X>, B<--random>.

=item B<-c>, B<--comma> I<sec> | I<millisec>B<ms>

Sets the delay added by commas in the dial strings. Default is 0.5
or 500ms. Can be randomized with B<-X>, B<--random>.

=item B<-f>, B<--fast>

Decreases the digit, delay, and comma times by 50%.

=item B<-X>, B<--random>

Randomize the timing. Digit length (B<-l>, B<--length>), inter-digit timing
(B<-d>, B<--delay>), and the comma (B<-c>, B<--comma>) will vary from 1x to 2x the amount
specified. This does a pretty good impression of a human dialling a
number manually.

=item B<-Y>, B<--no-random>

Disable randomized timing. This is the default; this option exists
to turn off a prior B<-X>, B<--random> option.

=item B<-x>, B<--extended>

Allows the extended touchtone pad keys B<A>, B<B>, B<C>, and B<D>. B<Disables>
letter-to-number conversions.

=item B<-a>, B<--alphabet>

Disables extended touchtone pad keys B<A>, I<B>, I<C>, and I<D>; re-enables
letter-to-number conversions. This is the default; this option exists
to turn off a prior B<-x>, B<--extended> option.

=item B<-t>, B<--dialtone> I<sec> | I<millisec>B<ms>

Play I<sec> (or I<millisec> ms) of dialtone before the next dial string.

=item B<-D>, B<--dialtone-type> I<us> | I<uk> | I<fr> | I<eu> | I<jp>

Set the type of dialtone to be played by B<-d>, B<--dialtone>. Default
is B<us>.

=item B<-s>, B<--silence> I<sec> | I<millisec>B<ms>

Play I<sec> (or I<millisec> ms) of silence before the next dial string.
Unlike the comma, this is not affected by B<-X>, B<--random>.

=item B<-B>, B<--bluebox>

Switch to bluebox dialling mode. See B<BLUEBOX MODE> below for details.

=item B<-N>, B<--normal>

Switch to normal touchtone dialling mode. This is the default; this
option exists to turn off a prior B<-B>, B<--bluebox>.

=item B<-i>, B<--input> I<file>

Read whitespace-separated dial strings from I<file>. Use B<-> to read
from standard input. This option can be used more than once, and can
be mixed with dial string arguments.

=item B<--stdin>

Read whitespace-separated dial strings from standard input. Same as
B<-i -> or B<--input ->.

=back

=head2 Dial Strings

These are the actual digits to be dialled. Each dial string consists
of one or more digits (as many as desired), and they will be played
consecutively.

DTMF digits B<0> to B<9>, B<#>, and B<*> are supported. By default, letters are
also supported, and will be converted to digits according to the
standard layout of touchtone phones (e.g. B<A> through B<C> convert to I<2>,
B<D> through B<F> are I<3>, etc).

B<Q> and B<Z> weren't present on classic touchtone phones, but they will be
converted to I<7> and I<9>, respectively (like modern cell phones).

To add an extra delay between digits, use a comma (and see the B<-c>,
B<--comma> option to set the length of the delay), or the B<-s>,
B<--silence> option.

To play the extra DTMF tones for the extended 16-digit keypad (which has
B<A>, B<B>, B<C>, and B<D> keys), use the B<-x>, B<--extended> option. This disables
letter-to-number conversion, but it can be re-enabled later on the
command line with the B<-a>, B<--alphabet> option.

All characters that aren't mentioned above, will be silently
ignored. This allows you to paste a phone number in the form B<(555)
555-1212> and have it work correctly.

=head1 BLUEBOX MODE

A blue box is a phone phreaking device that generates tones (formerly)
used for in-band signaling within the telephone network. B<soxdial>
can generate these tones.

To enable bluebox mode, use the B<-B>, B<--bluebox> option.

Unlike touchtone dialling, bluebox signals have a timing
specification. When entering bluebox mode, the digit time and
inter-digit time are set to 60ms (except the B<kp> and B<kp2>
tones, which I<always> sound for 100ms) and randomization is
disabled. Bluebox timing can be changed in the same way as DTMF
timing, if you can think of a reason to do it.

To exit bluebox mode, use the B<-N>, B<--normal> option. This resets
the timing and randomization to whatever it was before entering
bluebox mode.

In bluebox mode, there is no letter-to-number conversion.

The bluebox keypad has digits B<0-9>, which can be combined in a
single dial string. The keys B<kp>, B<kp2>, B<st>, B<st2>, and B<st3>
must appear alone as separate dial strings (meaning, put a space
before and after them).

The alternate names for B<0> (I<10>), B<st3> (I<11>), and B<st2>
(I<12>) are not supported, since there's no way to tell e.g. B<11>
from two B<1>'s.

=head1 ENVIRONMENT

=over 4

=item B<PATH>

By default, B<sox> is searched for in B<PATH>.

=item B<SOX>

Sets the path to the B<sox> executable. Same as the B<--sox> option, q.v.

=item B<AUDIODRIVER>, B<AUDIODEV>, B<SOX_OPTS>, etc.

These options affect B<sox>; see the B<sox>(1) man page for details.

=back

=head1 NOTES

B<1.> Any trailing silence at the end of the audio is removed, but
silence at the start is left intact: the only way to get silence at
the start is to use a comma or B<-s>, B<--silence>, which means the
user I<wants> the leading silence.

B<2.> B<soxdial> works by iterating over the words on the command
line, and building up an array of B<sox> commands for each dial string
or segment of dial tone. At the end, all the B<sox> commands are run
and their combined output (as raw samples) is piped to another B<sox>
command that writes the output as a single audio stream (raw, .wav,
or whatever format the B<-o>, B<--output> filename indicates). It has
to be done this way because B<sox> doesn't allow multiple B<synth>
arguments in the same command (or, it does, but they don't work as
expected, or at all). Because the final B<sox> command reads only raw
audio, it's impossible to change the bitrate or sample size in between
dial strings.

B<3.> If anything on the command line starts with B<-> but isn't
a recognized option, it's not an error: it gets treated as a
dial string. This allows e.g. I<555 -1212> to work correctly, but
mistyped options will result in them being dialled as alphabetic
characters. This may be a bit surprising the first time it happens.
If you use B<-v>, B<--verbose>, you'll get warned about it at least.

B<4.> I haven't been able to test this with a real land-line phone to
see whether it will actually dial out.

B<5.> I know nothing about blueboxes other than what I've read on
Wikipedia, and there's no way I know of to test whether the bluebox
tones are correct (you'd need a time machine).

=head1 AUTHOR

B<soxdial> was written by B. Watson <urchlay@slackware.uk> and released
under the WTFPL: Do WTF you want with this.

=head1 SEE ALSO

B<sox>(1)

The Wikipedia articles on DTMF, dialtone, and the Blue Box:

  https://en.wikipedia.org/wiki/Dtmf
  https://en.wikipedia.org/wiki/Bluebox
  https://en.wikipedia.org/wiki/Dialtone

=cut

%freqs = (
	1   => [697, 1209],
	2   => [697, 1336],
	3   => [697, 1477],
	4   => [770, 1209],
	5   => [770, 1336],
	6   => [770, 1477],
	7   => [852, 1209],
	8   => [852, 1336],
	9   => [852, 1477],
	'*' => [941, 1209],
	0   => [941, 1336],
	'#' => [941, 1477],
	A   => [697, 1633],
	B   => [770, 1633],
	C   => [852, 1633],
	D   => [941, 1633],
);

%bluefreqs = (
	1   => [70, 900],
	2   => [700, 1100],
	3   => [900, 1100],
	4   => [700, 1300],
	5   => [900, 1300],
	6   => [1100, 1300],
	7   => [700, 1500],
	8   => [900, 1500],
	9   => [1100, 1500],
	0   => [1300, 1500],
	11  => [700, 1700],
	12  => [900, 1700],
	kp   => [1100, 1700],
	kp2  => [1300, 1700],
	st   => [1500, 1700],
);

@freqs1 = ();
@freqs2 = ();
@delays = ();
$time = 0;
$pausetime = 0.5;
$digittime = 0.25;
$intertime = 0.1;
$bits = 8;
$encoding = "-eun";
$rate = 8000;
$output = "-d";
$dialtone_type = "us";

sub letter2number {
	my $ret;
	my $l = uc shift;
	return $l unless $l =~ /[A-Z]/;
	for($l) {
		if(/[A-O]/) {
			$ret = int((ord($_) - 65) / 3 + 2) . "";
		} elsif(/[P-S]/) {
			$ret = "7";
		} elsif(/[T-V]/) {
			$ret = "8";
		} else { # /[W-Z]/
			$ret = "9";
		}
	}
	warn "$SELF: letter2number('$l') => '$ret'\n" if $verbose;
	return $ret;
}

sub randomize {
	my $time = shift;
	return $time unless $random;

	my $newtime = $time * (rand() + 1);
	warn "$SELF: randomized $time to $newtime.\n" if $verbose;
	return $newtime;
}

# sox -n -d synth 0.25 sine 697 sine 1209 sine 770 sine 1477 delay 0 0 .35 .35 remix -
# ...plays DTMF 1 and 6, for 0.25 sec each, with a 0.10 sec delay between them.

sub add_digit {
	my $d = shift;
	if($d eq ',') {
		# pause
		push @freqs1, 0;
		push @freqs2, 0;
		push @delays, $time;

		$time += randomize($pausetime);
	} else {
		push @freqs1, ($freqs{$d}->[0]);
		push @freqs2, ($freqs{$d}->[1]);

		push @delays, $time;

		$time += randomize($digittime);
		$time += randomize($intertime);
	}
	warn "$SELF: added digit '$d', time now $time\n" if $verbose;
}

sub add_bluebox_digit {
	my $d = shift;
	if(!$bluefreqs{$d}) {
		return;
	}
	push @freqs1, ($bluefreqs{$d}->[0]);
	push @freqs2, ($bluefreqs{$d}->[1]);

	push @delays, $time;

	$time += $digittime;
	$time += $intertime;
	warn "$SELF: added bluebox digit '$d', time now $time\n" if $verbose;
}

# ( sox -n -p synth 2 sine 350 sine 440; sox -n -p synth 2 sine 697 sine 1209 ) | sox -G -p -d
# ...plays 2 sec of dialtone followed by 2 sec of DTMF key 1. Change the -d
# to 1.wav to save to a file. Prefix it with -rXXXX -sX if needed.

# each dial string (or --dialtone, or --silence) creates a
# subcommand. they get executed in a subshell, writing raw audio
# to stdout, and the final sox command reads all the audio from its
# stdin. looks like:
# ( <subcommand1> ; <subcommand2> ; ... ) | sox <final output args>

# --norm is needed because otherwise, the volume decreases as the number of
# digits (tones for "synth") goes up. --norm=-3 helps avoid clipping.
sub make_sox_subcmd {
	return ":" unless @freqs1;

	my $length = shift || $digittime;
	my $cmd = "$SOX -n -b$bits $encoding -r$rate -c1 -traw - --norm=-3 ";
	my $synth = " synth $length ";
	my $delay = "delay ";

	for(0..$#freqs1) {
		my $f1 = $freqs1[$_];
		my $f2 = $freqs2[$_];
		my $d = $delays[$_];

		$synth .= "sine $f1 sine $f2 "; 
		$delay .= "$d $d ";
	}

	$time = 0;
	@freqs1 = @freqs2 = @delays = ();
	return $cmd . $synth . $delay;
}

# used to add the silence after the last digit in a dial string
sub silence_subcmd {
	my $s = shift;
	if(!defined $s || ($s + 0) <= 0) {
		warn "$SELF: ignoring invalid --silence argument.\n";
		return;
	}
	warn "$SELF: adding $s sec silence.\n" if $verbose;
	return "$SOX -n -b$bits $encoding -r$rate -c1 -traw - trim 0 $s";
}

sub dialtone_subcmd {
	my $sec = shift;
	if(!defined $sec || ($sec + 0) <= 0) {
		warn "$SELF: ignoring invalid --dialtone argument.\n";
		return;
	}
	warn "$SELF: adding $sec sec of dial tone, type '$dialtone_type'.\n" if $verbose;
	my $cmd = "$SOX -n -b$bits $encoding -r$rate -c1 -traw - synth $sec ";
	for($dialtone_type) {
		/^us$/ && do { $cmd .= "sine 350 sine 440" };
		/^uk$/ && do { $cmd .= "sine 350 sine 450" };
		/^eu$/ && do { $cmd .= "sine 425" };
		/^fr$/ && do { $cmd .= "sine 440" };
		/^jp$/ && do { $cmd .= "sine 400" };
	}

	return $cmd;
}

# these are all the dialtone types I know about (or, that wikipedia knows about).
sub set_dialtone_type {
	my $type = shift || "";
	$type = lc $type;
	if($type !~ /^(?:us|uk|eu|fr|jp)$/) {
		die "$SELF: invalid dialtone type '$type'.\n";
	}
	$dialtone_type = $type;
	warn "$SELF: set dialtone type to '$type'.\n" if $verbose;
}

# final sox command, to which we pipe all the others.
sub make_sox_cmd {
	if($output ne '-d') {
		# support quotes, spaces, etc in filenames.
		# this can probably be fooled by a determined luser.
		$output =~ s,",\\",g;
		$output = "\"$output\"";

		if($output !~ /\./) {
			$output = "-t raw $output";
		}
	}

	my $ch = $stereo ? "channels 2" : "";

	my $remix = "";
	if($left) {
		$remix = "remix 1 1v0";
	} elsif($right) {
		$remix = "remix 1v0 1";
	}

	# the reverse/silence stuff trims trailing silence.
	# we don't trim leading silence.
	my $cmd = "$SOX -traw -b$bits $encoding -r$rate -c1 - " .
	   "$output $ch $remix reverse silence 1 0 0 reverse";
	my $subcmds = join(" ; ", @sox_subcmds);
	return "( " . $subcmds . " ) | " . $cmd;
}

# support either seconds (possibly with decimal point, e.g. 0.5) or
# milliseconds (e.g. 500ms).
sub parse_sec {
	no warnings "numeric";
	$_ = shift || 0;
	if(/^(.+)ms$/) {
		$_ = $1 / 1000;
	} else {
		$_ += 0;
	}
	return $_;
}

# support either Hz (e.g. 8000) or KHz (e.g. 8k or 8K).
sub parse_rate {
	no warnings "numeric";
	$_ = shift || 0;
	if(/^(.+)k$/i) {
		$_ = $1 * 1000;
	} else {
		$_ += 0;
	}
	return $_;
}

sub check_stereo {
	die "$SELF: can't enable stereo after a dial string.\n" if @sox_subcmds;
}

sub read_file {
	my $file = shift;
	my $result = shift;
	my $filename = ($file eq '-' ? "stdin" : "file '$file'");

	warn "$SELF: reading $filename.\n";

	open my $fh, "<$file" or die "$SELF: $file: $!\n";
	while(<$fh>) {
		chomp;
		push @$result, split " ";
	}

	warn "$SELF: read " . @$result . " dial strings from $filename.\n" if $verbose;

	close $fh;
}

# main()
$SOX = $ENV{SOX} || "sox";

for(@ARGV) {
	if(/--?(?:V|version)$/) {
		print "$SELF $VERSION\n";
		exit 0;
	} elsif(/^--?man$/) {
		exec "pod2man --stderr -s6 -cUrchlaysStuff -r$VERSION -u $0";
		exit 1;
	} elsif(/^--?(?:\?|h)/) {
		exec "perldoc $0";
		exit 1;
	}
}

if(system("$SOX --version > /dev/null 2>&1") != 0) {
	die "$SELF: can't execute $SOX, is it installed?\n";
}

if(!@ARGV) {
	warn "$SELF: no dial strings. Try $SELF --help.\n";
	exit 1;
}

# we can't use Getopt::Long here because we can apply the same dial options
# multiple times, between dial strings.
# preprocess @ARGV, convert e.g. -b16 to -b 16, --foo=bar to --foo bar.
@newargv = ();
for(@ARGV) {
	if($input_seen) {
		read_file($_, \@newargv);
		$input_seen = 0;
	} elsif(/^--?v(?:erbose)?$/) {
		$verbose = 1;
	} elsif(/^--?stdin$/) {
		read_file("-", \@newargv);
	} elsif(/^--?stdout$/) {
		push @newargv, "-o", "-";
	} elsif(/^--?i(?:nput)?=?(.+)?$/) {
		if($1) {
			read_file($1, \@newargv);
		} else {
			$input_seen = 1;
		}
	} elsif(/^(--?\w+)=(.+)$/) {
		push @newargv, $1, $2;
	} elsif(/^(--?[blcdts])([\d.]+(?:ms)?)$/i) {
		push @newargv, $1, $2;
	} elsif(/^(--?[r])(\d+(?:k)?)$/i) {
		push @newargv, $1, $2;
	} elsif(/^(--?[D])(\w\w)$/i) {
		push @newargv, $1, $2;
	} elsif(/^(--?o)(.+)$/) {
		if($2 eq 'utput') {
			push @newargv, $_;
		} else {
			push @newargv, $1, $2;
		}
	} else {
		push @newargv, $_;
	}
}

if($input_seen) {
	die "$SELF: missing file argument for -i/--input.\n";
}

@ARGV = @newargv;

# this is a big ugly mess. should be refactored. works, though.
for ($argc = 0; $argc < @ARGV; $argc++) {
	$_ = $ARGV[$argc];

	if(/^--?sox$/) {
		$SOX = $ARGV[++$argc];
		die "$SELF: missing argument for --sox.\n" unless defined $SOX;
		warn "$SELF: sox executable set to '$SOX'\n" if $verbose;
	} elsif(/^--?n(?:oexec)?$/) {
		$verbose = 1;
		$noexec = 1;
	} elsif(/^--?o(?:utput)?$/) {
		$output = $ARGV[++$argc];
		die "$SELF: missing argument for --output.\n" unless defined $output;
		warn "$SELF: output set to '$output'\n" if $verbose;
	} elsif(/^--?b(?:its)?$/) {
		if(@sox_subcmds) {
			die "$SELF: can't change bits after a dial string.\n";
		}
		$bits = $ARGV[++$argc];
		if($bits == 8) {
			$encoding = "-eun";
		} elsif($bits == 16) {
			$encoding = "-esig";
		} else {
			die "$SELF: bad -b/--bits, only 8 or 16 is allowed.\n";
		}
		warn "$SELF: bits set to '$bits'\n" if $verbose;
	} elsif(/^--?r(?:ate)?$/) {
		if(@sox_subcmds) {
			die "$SELF: can't change bitrate after a dial string.\n";
		}
		$rate = parse_rate($ARGV[++$argc]);
		die "$SELF: invalid bitrate.\n" unless $rate > 0;
		warn "$SELF: bitrate set to '$rate'\n" if $verbose;
	} elsif(/^--?l(?:ength)?$/) {
		$digittime = parse_sec($ARGV[++$argc]);
		die "$SELF: invalid --length argument.\n" unless $digittime > 0;
		warn "$SELF: digit length set to '$digittime'\n" if $verbose;
	} elsif(/^--?(?:x|extended)$/) {
		$extended = 1;
		warn "$SELF: extended DTMF (ABCD) enabled.\n" if $verbose;
	} elsif(/^--?a(?:lphabet)?$/) {
		$extended = 0;
		warn "$SELF: extended DTMF (ABCD) disabled.\n" if $verbose;
	} elsif(/^--?d(?:elay)?$/) {
		$intertime = parse_sec($ARGV[++$argc]);
		die "$SELF: invalid --delay argument.\n" unless $intertime > 0;
		warn "$SELF: inter-digit delay set to '$intertime'\n" if $verbose;
	} elsif(/^--?c(?:omma)?$/) {
		$pausetime = parse_sec($ARGV[++$argc]);
		die "$SELF: invalid --comma argument.\n" unless $pausetime > 0;
		warn "$SELF: comma delay set to '$pausetime'\n" if $verbose;
	} elsif(/^--?(?:D|dialtone-type)$/) {
		set_dialtone_type($ARGV[++$argc]);
	} elsif(/^--?(?:t|dialtone)$/) {
		push @sox_subcmds, dialtone_subcmd(parse_sec($ARGV[++$argc]));
	} elsif(/^--?s(?:ilence)?$/) {
		push @sox_subcmds, silence_subcmd(parse_sec($ARGV[++$argc]));
	} elsif(/^--?(?:S|stereo)$/) {
		check_stereo();
		$stereo = 1;
	} elsif(/^--?(?:L|left)$/) {
		check_stereo();
		$right = 0;
		$stereo = $left = 1;
	} elsif(/^--?(?:R|right)$/) {
		check_stereo();
		$left = 0;
		$stereo = $right = 1;
	} elsif(/^--?(?:X|random)$/) {
		# sorry I had to use -X for this, -R was already taken.
		$random = 1;
	} elsif(/^--?(?:Y|no-random)$/) {
		$random = 0;
	} elsif(/^--?f(?:ast)?$/) {
		$intertime /= 2;
		$digittime /= 2;
		$pausetime /= 2;
	} elsif(/^--?(?:B|bluebox)$/) {
		@oldsettings = ($digittime, $intertime, $randomize);
		$bluebox = 1;
		$digittime = $intertime = 0.06;
		$randomize = 0;
		warn "$SELF: bluebox mode enabled.\n" if $verbose;
	} elsif(/^--?(?:N|normal)$/) {
		$bluebox = 0;
		($digittime, $intertime, $randomize) = @oldsettings if @oldsettings;
		warn "$SELF: bluebox mode disabled (touchtone enabled).\n" if $verbose;
	} else {
		$_ = lc $_;

		# paranoia: in case we get non-printable characters, we don't want
		# to print them in verbose mode (yes, I use /dev/urandom for testing).
		($printable = $_) =~ s,[^\x20-\x7e],.,g;

		if($verbose && (/^--?[a-z]/i)) {
			warn "$SELF: treating '$printable' as a dial string (might be a typo?)\n";
		}
		warn "$SELF: start dial string '$printable'\n" if $verbose;

		my $time_override;
		if($bluebox) {
			if(/^(?:st[23]?|kp2?)/) {
				$time_override = 0.1 if $_ eq 'kp' || $_ eq 'kp2';
				add_bluebox_digit($_);
			} else {
				add_bluebox_digit($_) for split "", $_;
			}
		} else {
			for (split "", $_) {
				my $digit = uc $_;
				if($extended) {
					next if $digit !~ /[0-9,#*A-D]/;
				} else {
					$digit = letter2number($digit);
					next if $digit !~ /[0-9,#*]/;
				}
				add_digit($digit);
			}
		}
		if(@freqs1) {
			push @sox_subcmds, make_sox_subcmd($time_override);
			push @sox_subcmds, silence_subcmd(randomize($intertime));
		} else {
			warn "$SELF: no valid digits in dial string.\n" if $verbose;
		}

		warn "$SELF: end dial string '$printable'\n" if $verbose;
	}
}

if(!@sox_subcmds) {
	die "$SELF: no digits or dialtone to generate.\n";
}

my $cmd = make_sox_cmd();

if($verbose) {
	warn "$SELF: sox command is:\n  $cmd\n";
} else {
	$cmd .= " 2>/dev/null";
}

if(!$noexec) {
	system($cmd);
}