This article describes the PHP implementation of the WAV file into the image file and display in the page method. Share to everyone for your reference. The specific analysis is as follows:
Requirements: Convert WAV files to PNG files and display them.
wav_to_png.php:
<?php function Wav_graph ($file, $f =0, $w =0) {global $DATA _dir;
if (!is_file ($file)) return 0;
$fp = fopen ($DATA _dir. $file, ' R ');
$raw = Fread ($fp, 36);
$str = '; $header = Unpack (' a4riff/vsize/a4wav/a4head/vheadsize/vpcm/vchannels/vsamplerate/vbyterate/vblockalign/
Vsamplebits ', $raw); foreach ($header as $k => $v) $str. = $k. ': '.
$v. ';
Fseek ($fp, + $header [' headsize ']-16);
$raw = Fread ($fp, 8);
$data = Unpack (' a4data/vdatasize ', $raw); foreach ($data as $k => $v) $str. = $k. ': '.
$v. ';
$b = $header [' samplebits '];
$c = $header [' channels ']; $l = $b * $c/8; Sample frame length in bytes $s = $data [' datasize ']/$l;
Total number of samples $r = $header [' samplerate '];
if ($f) $h = POW (2, $b)/$f;
else {$h = $f = Pow (2, $b-1)/$h;} if ($w = = 0) $w = round ($r/1000);
Default to show 1k sample frames per minute header ("Content-type:image/png");
$im = Imagecreate ($s/$w, $h * $c * 2); ImagecolorAllocate ($im, 0xFF, 0xFF, 0xff); White bg $color = imagecolorallocate ($im, 0, 0, 255);
Black Imagestring ($im, 5, 5, 5, $STR, $color); $x = 0; $y = Array ();
$yn = Array ();
for ($i = 0; $i < $c; $i + +) $y [$i] = $h * $i + $h;
$n = $l * $W;
while (1) {if ($s = = 0) break;
if ($s < $n) $n = $s;
$samples = Fread ($fp, 1000 * $n);
if ($samples = = FALSE) break;
$packed = Unpack ("s*", $samples);
foreach ($packed as $k => $v) {$cnt = ($k-1)% ($w * $l);
if ($cnt > $c-1) continue;
$yn [$CNT] = $h * $cnt + $h-$v/$f;
Imageline ($im, $x, $y [$cnt], $x +1, $yn [$cnt], $color);
$y [$cnt] = $yn [$cnt];
$x + +;
} $s-= $n;
} imagepng ($im);
Imagedestroy ($im); } wav_graph (' Getwave.wav ');?>
The effect chart is as follows:
I hope this article will help you with your PHP program design.