-
Notifications
You must be signed in to change notification settings - Fork 34
/
image2audio.pl
161 lines (117 loc) · 4.6 KB
/
image2audio.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
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
#!/usr/bin/perl
# Convert an image to an audio spectrogram.
# Algorithm from:
# https://github.com/alexadam/img-encode/blob/master/v1-python/imgencode.py
# The spectrogram can be viewed in a program, like Audacity.
# Inspired by the hidden message in the movie "Leave the world behind":
# https://www.reddit.com/r/MrRobot/comments/18hnn3q/minor_spoiler_leave_the_world_behind_hidden/
use 5.036;
use Imager;
use Audio::Wav;
use List::Util qw(min max);
use Getopt::Long qw(GetOptions);
my $max_height = 300; # resize images larger than this
my $sample_rate = 44100;
my $bits_sample = 16;
my $frequency_band = $sample_rate / 2; # in Hz
my $channels = 1;
my $duration_factor = 1;
my $output_wav = 'output.wav';
sub help ($code) {
print <<"EOT";
usage: $0 [options] [images]
options:
-o --output=s : output audio file (default: $output_wav)
-f --freq=i : frequency band in Hz (default: $frequency_band)
-d --duration=f : duration multiplication factor (default: $duration_factor)
-b --bits=i : bits sample (default: $bits_sample)
-s --sample=i : sample rate (default: $sample_rate)
-c --channels=i : number of channels (default: $channels)
EOT
exit($code);
}
GetOptions(
'o|output=s' => \$output_wav,
'f|frequency=i' => \$frequency_band,
'd|duration-factor=f' => \$duration_factor,
'b|bits-sample=i' => \$bits_sample,
's|sample-rate=i' => \$sample_rate,
'c|channels=i' => \$channels,
'h|help' => sub { help(0) },
)
or die("Error in command line arguments");
sub range_map ($value, $in_min, $in_max, $out_min, $out_max) {
($value - $in_min) * ($out_max - $out_min) / ($in_max - $in_min) + $out_min;
}
sub image2spectrogram ($input_file, $write) {
say "\n:: Processing: $input_file";
my $img = Imager->new(file => $input_file)
or die "Can't open file <<$input_file>> for reading: $!";
my $width = $img->getwidth;
my $height = $img->getheight;
my $duration = $duration_factor * ($width / $height);
say "-> Duration: $duration seconds";
if ($height > $max_height) {
$img = $img->scale(ypixels => $max_height, qtype => 'mixing');
($width, $height) = ($img->getwidth, $img->getheight);
}
my $min_size = min($width, $height);
$width = int($duration * $min_size);
$height = $min_size;
say "-> Resizing the image to: $width x $height";
$img = $img->scale(xpixels => $width, ypixels => $height, qtype => 'mixing', type => 'nonprop');
my @data;
my $maxFreq = 0;
my $numSamples = int($sample_rate * $duration);
my $samplesPerPixel = $numSamples / $width;
my $C = $frequency_band / $height;
my @img;
foreach my $y (0 .. $height - 1) {
my @line = $img->getscanline(y => $y);
foreach my $pixel (@line) {
my ($R, $G, $B) = $pixel->rgba;
## push @{$img[$y]}, ((($R + $G + $B) / 3) * 100 / 255)**2;
## push @{$img[$y]}, ((0.5 * max($R, $G, $B) + 0.5 * min($R, $G, $B)) * 100 / 255)**2;
## push @{$img[$y]}, (sqrt(0.299 * $R**2 + 0.587 * $G**2 + 0.114 * $B**2) * 100 / 255)**2;
push @{$img[$y]}, ((0.299 * $R + 0.587 * $G + 0.114 * $B) * 100 / 255)**2;
}
}
say "-> Converting the pixels to spectrogram frequencies";
my $tau = 2 * atan2(0, -1);
foreach my $x (0 .. $numSamples - 1) {
my $rez = 0;
my $pixel_x = int($x / $samplesPerPixel);
foreach my $y (0 .. $height - 1) {
my $volume = $img[$y][$pixel_x] || next;
my $freq = sprintf('%.0f', $C * ($height - $y + 1));
$rez += sprintf('%.0f', $volume * cos($freq * $tau * $x / $sample_rate));
}
push @data, $rez;
if (abs($rez) > $maxFreq) {
$maxFreq = abs($rez);
}
}
say "-> Maximum frequency: $maxFreq";
my $max_no = 2**($bits_sample - 1) - 1;
#my $min = min(@data);
#my $max = max(@data);
my $min = -$maxFreq;
my $max = $maxFreq;
foreach my $val (@data) {
## $write->write(sprintf('%.0f', $max_no * $val / $maxFreq));
$write->write(range_map($val, $min, $max, -$max_no, $max_no));
}
return 1;
}
@ARGV || help(2);
my $details = {
'bits_sample' => $bits_sample,
'sample_rate' => $sample_rate,
'channels' => $channels,
};
my $wav = Audio::Wav->new;
my $write = $wav->write($output_wav, $details);
foreach my $input_img (@ARGV) {
image2spectrogram($input_img, $write);
}
$write->finish();