Skip to content

Instantly share code, notes, and snippets.

@windytan
Last active December 31, 2023 02:54
  • Star 35 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
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); }
@kragen
Copy link

kragen commented Dec 9, 2013

@alankila
Copy link

alankila commented Feb 2, 2014

Would you have the audio file for this? I'd like to see if I can replicate this with direct sinc interpolation.

@tvillingett
Copy link

I could not get this to work, I'm new to perl also... I installed perl 5 on my mac, and then run the oscillo.pl name of the .wav file as an argument.
First it wrote: print() on closed filehandle U at oscillo.pl line 71.
all over all the time (until i quit with ctrl+c)
then i added use autodie; in row 3 to try to catch the real problem, and then I got:
MacBook-Air:perl_test tvillingett$ perl oscillo.pl 198376__bunting__dscn0099.wav Use of uninitialized value $opts{"f"} in concatenation (.) or string at oscillo.pl line 36. sh: sox: command not found Can't close filehandle 'S': '' at oscillo.pl line 60

Any ideas?

@arunganesan
Copy link

@tvillingett you need to use the "-f" option for specifying the wave file. It is something like this:

perl oscillo.pl -f file.wav

All the other options change how the script works. Also make sure you have imagemagick and sox installed.

Works and looks great, windytan! Thank you!

@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