-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrp.pl
More file actions
76 lines (62 loc) · 1.81 KB
/
rp.pl
File metadata and controls
76 lines (62 loc) · 1.81 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(floor);
# DTMF frequency rows (low freq)
my @low_freqs = (697, 770, 852, 941);
# DTMF frequency cols (high freq)
my @high_freqs = (1209, 1336, 1477, 1633);
# Corresponding DTMF characters by row,col
my @dtmf_table = (
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D'],
);
sub hex_to_float {
my ($hex) = @_;
# Convert hex string to packed binary, then unpack as float
# Remove leading '0x' if present
$hex =~ s/^0x//;
my $packed = pack("H8", $hex);
return unpack("f>", $packed); # Big-endian float
}
sub closest_freq_index {
my ($freq, $freqs_ref) = @_;
my $min_diff = 1e9;
my $best_idx = -1;
for my $i (0 .. $#$freqs_ref) {
my $diff = abs($freq - $freqs_ref->[$i]);
if ($diff < $min_diff) {
$min_diff = $diff;
$best_idx = $i;
}
}
return $best_idx;
}
print "/*\n";
print " * \$ID: REDPHONE v1.0 1012 Mon Jul 28 2:05:27 CDT chatGPT \$\n";
print " */\n";
print "\n";
print "Paste the output of autovon, without []. Ctrl+D on blank line to end input.\n";
print ">";
my $decoded = '';
while (<STDIN>) {
chomp;
next if /^\s*$/; # skip empty lines
my ($hex1, $hex2) = split /\s*,\s*/;
# Convert to floats
my $f1 = hex_to_float($hex1);
my $f2 = hex_to_float($hex2);
# Identify low and high frequency (low < high)
my ($low, $high) = $f1 < $f2 ? ($f1, $f2) : ($f2, $f1);
# Find closest matching indices
my $low_idx = closest_freq_index($low, \@low_freqs);
my $high_idx = closest_freq_index($high, \@high_freqs);
if ($low_idx == -1 || $high_idx == -1) {
$decoded .= '?'; # unknown tone
next;
}
$decoded .= $dtmf_table[$low_idx][$high_idx];
}
print "\nDecoded DTMF string: $decoded\n";