-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplay-pixels
84 lines (64 loc) · 2.36 KB
/
play-pixels
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
#!/usr/bin/env perl
# Write up: https://ology.github.io/2020/03/23/hearing-an-image-one-pixel-at-a-time/
use strict;
use warnings;
# Requires https://metacpan.org/pod/Math::Fractal::Noisemaker
use Imager;
use Math::Utils ':utility';
use lib "$ENV{HOME}/sandbox/MIDI-Util/lib";
use MIDI::Util qw(setup_score); # https://metacpan.org/release/MIDI-Util
my $type = shift || 'worley'; # Noisemaker type
my $size = shift || 8; # Side width of Noisemaker image
my $bpm = shift || 100; # Beats per minute
# Get a MIDI score
my $score = setup_score(bpm => $bpm, patch => 5);
my $file = 'make-noise.png';
# Make a grayscale fractal image
system('make-noise', '-out', $file, '-type', $type, '-len', $size, '-format', 'png') == 0
or die "system failed: $?";
# Open the image
my $img = Imager->new;
$img->read(file => $file)
or die "Can't read $file: ", $img->errstr;
# Remove the image file
unlink $file;
my $s_field = length $size;
my $i_field = length($size * $size);
my $i = 0;
# Add a note to the score for each pixel color
for my $y (0 .. $size - 1) {
for my $x (0 .. $size - 1) {
$i++;
my $color = $img->getpixel(x => $x, y => $y);
my ($red) = $color->rgba;
# Scale from grays range to MIDI note range
my $n = sprintf '%.0f', uniform_scaling([0, 255], [60, 83], $red);
printf "%*d. [%*d,%*d] %3d -> %d\n",
$i_field, $i,
$s_field, $x, $s_field, $y,
$red, $n;
$score->n('qn', $n);
}
}
$score->write_score("$0.mid");
__END__
Noise Types:
* white ## pseudo-random values
* wavelet ## band-limited ortho
* gradient ## persistent gradient noise
* simplex ## continuous gradient noise
* simplex2 ## interpolated simplex
* square ## diamond-square algorithm
* gel ## self-displaced smooth
* sgel ## self-displaced diamond-square
* dmandel ## "deep" mandelbrot
* djulia ## "deep" julia
* dla ## diffusion-limited aggregation
* worley ## voronoi cell noise
* wgel ## self-displaced cell noise
! multires ## multi-resolution
! ridged ## ridged multifractal
! block ## unsmoothed multi-res
! pgel ## self-displaced multi-res
! fur ## inspired by "Perlin Worms"
! tesla ## worms/fur variant