-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode-fast.pl
executable file
·58 lines (43 loc) · 1.09 KB
/
encode-fast.pl
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
#!/usr/bin/perl
use warnings;
use strict;
use constant SAMPLING_RATE => 88200;
use constant BLOCKS_PER_SECOND => 1;
use constant SAMPLES_PER_BLOCK => SAMPLING_RATE / BLOCKS_PER_SECOND;
{
my $num_packet = 0;
sub next_data {
$num_packet++;
return 0 if $num_packet <= 4;
return 1 if $num_packet == 5;
read(STDIN, my $char, 1) == 1 or return;
return ord($char) - 65;
}
}
sub sum { my $s = 0; $s += $_ for @_; $s }
sub calc_output { scalar `./clean/generate-sines @{[ join " ", SAMPLING_RATE, SAMPLES_PER_BLOCK, @_ ]}` }
sub binary {
my $data = shift;
my @bits;
while($data > 0) {
if($data % 2 == 1) {
push @bits, 1;
$data--;
} else {
push @bits, 0;
}
$data /= 2;
}
return @bits;
}
my @freqs = @ARGV;
my @output;
for my $data (0 .. 2**@freqs - 1) {
my @bits = binary($data);
warn $data;
push @output, calc_output(map { $freqs[$_] } grep { $bits[$_] } 0..$#bits);
}
while(1) {
defined(my $data = next_data()) or last;
print $output[$data];
}