Skip to content

Instantly share code, notes, and snippets.

@windytan
Last active December 31, 2023 02:54
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save windytan/5276653 to your computer and use it in GitHub Desktop.
Save windytan/5276653 to your computer and use it in GitHub Desktop.
use warnings;
use Getopt::Std;
getopt('xytGgwsf',\%opts);
# pcm file = $opts{f}
# samples per pixel x
$xscale = $opts{x} // 1200;
# quant levels per pixel y
$yscale = $opts{y} // 100;
# oversampling target rate
$targetrate = $opts{t} // 1_099_961;
# wave preamplification, dB
$ygain = $opts{G} // 0;
# brightness added by one sample
$gain = $opts{g} // 6;
# img width
$w = $opts{w} // 1000;
# skip amount of seconds
$skip_sec = $opts{s} // 0.05;
############
# turquoise tint
for (0..127) { @{$gradient[$_]} = ($_/2, $_*1.5 ,$_*1.5); }
for (128..255) { @{$gradient[$_]} = (64+ ($_-128)*1.5, 192+($_-128)/2, 192+($_-128)/2); }
open(S,"sox \"".$opts{f}."\" -r $targetrate -b 16 -c 2 -t .raw -e signed - trim $skip_sec gain $ygain|");
$n=0;
while(not eof(S)) {
read(S,$a,2);
$a = -unpack("s",$a);
# pixel position of this sample
$x = $n/$xscale;
$y = ($a+32768)/$yscale;
# bilinear interpolation
$xdec = $x-int($x);
$ydec = $y-int($y);
$pix[$x][$y] += (1-$xdec) * (1-$ydec);
$pix[$x+1][$y] += ($xdec) * (1-$ydec);
$pix[$x][$y+1] += (1-$xdec) * ($ydec);
$pix[$x+1][$y+1] += ($xdec) * ($ydec);
last if ($n/$xscale > $w);
read(S,$a,2);
$n++;
}
close(S);
open(U,"|convert -depth 8 -size ".$w."x".int(65536/$yscale)." rgb:- osc.png");
for $y (0..65536/$yscale-1) {
for $x (0..$w-1) {
$p = ($pix[$x][$y] // 0) * $gain;
$p = 255 if ($p > 255);
if ($y == round(65536/$yscale/2)) {
@a = @{$gradient[$p]};
for (@a) { $_ += 64; $_ = 255 if ($_ > 255); }
print U pack("CCC",@a);
} else {
print U pack("CCC",@{$gradient[$p]});
}
}
}
close(U);
sub round { int($_[0]+.5); }
@thodnev
Copy link

thodnev commented May 8, 2016

I've seen your article, and that's great. But in my opinion in would be nice if you could also write a textual/pseudocode algorithm to make it easier to understand. There aren't much perl jedi knights nowadays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment