вот код:
<?php
define('ZOOM',10); // Image is drawn ZOOM times bigger and then resized
define('ACCURACY',100); // Data point is the average of ACCURACY points in the data block
define('WIDTH', 400); // image width
define('HEIGHT',40); // image heigt
define('FOREGROUND', '#700000');
define('BACKGROUND', '#FFFFFF'); // blank for transparent
if (($argc != 3) || !file_exists($argv[1]))
die('invalid file specified, need two arguments being a .wav file and a writable png file');
drawWaveform('Музыкальный файл с расширением wav.wav', 'testpng.png');
/**
* Wave file reading based on a post by "zvoneM" on http://forums.devshed.com/php-development-5/reading-16-bit-wav-file-318740.html
* Completely rewritten the file read loop, kept the header reading intact.
*
* Reads WIDTH * ZOOM * ACCURACY data points from the file and takes the peak value of ACCURACY values. The peak is
* the highest value if mean is > 127 and the lowest value otherwise.
*
* Waveform drawing based on http://andrewfreiday.com/2010/04/29/generating-mp3-waveforms-with-php/
*
* @param string $wavfilename The .wav file
* @param string $pngfilename The .wav file
*
*/
function drawWaveform($wavfilename, $pngfilename)
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Create image
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$img = imagecreatetruecolor(WIDTH*ZOOM, HEIGHT*ZOOM);
// fill background of image
if (BACKGROUND == "") {
imagesavealpha($img, true);
$transparentColor = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparentColor);
} else {
list($r, $g, $b) = html2rgb(BACKGROUND);
imagefilledrectangle($img, 0, 0, WIDTH*ZOOM, HEIGHT*ZOOM, imagecolorallocate($img, $r, $g, $b));
}
// generate foreground color
list($r, $g, $b) = html2rgb(FOREGROUND);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read wave header
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$handle = fopen($wavfilename, "rb");
$heading[] = fread ($handle, 4);
$heading[] = bin2hex(fread ($handle, 4));
$heading[] = fread ($handle, 4);
$heading[] = fread ($handle, 4);
$heading[] = bin2hex(fread ($handle, 4));
$heading[] = bin2hex(fread ($handle, 2));
$heading[] = bin2hex(fread ($handle, 2));
$heading[] = bin2hex(fread ($handle, 4));
$heading[] = bin2hex(fread ($handle, 4));
$heading[] = bin2hex(fread ($handle, 2));
$heading[] = bin2hex(fread ($handle, 2));
$heading[] = fread ($handle, 4);
$heading[] = bin2hex(fread ($handle, 4));
if ($heading[5] != '0100') die("ERROR: wave file should be a PCM file");
$peek = hexdec(substr($heading[10], 0, 2));
$byte = $peek / 8;
$channel = hexdec(substr($heading[6], 0, 2));
// point = one data point (pixel), WIDTH * ZOOM total
// block = one block, there are $accuracy blocks per point
// chunk = one data point 8 or 16 bit, mono or stereo
$filesize = filesize($wavfilename);
$chunksize = $byte * $channel;
$file_chunks = ($filesize - 44) / $chunksize;
if ($file_chunks < WIDTH*ZOOM) die("ERROR: wave file has $file_chunks chunks, ".(WIDTH*ZOOM)." required.");
if ($file_chunks < WIDTH*ZOOM*ACCURACY) $accuracy = 1; else $accuracy = ACCURACY;
$point_chunks = $file_chunks/ (WIDTH*ZOOM);
$block_chunks = $file_chunks/ (WIDTH*ZOOM*$accuracy);
$blocks = array();
$points = 0;
$current_file_position = 44.0; // float, because chunks/point and clunks/block are floats too.
fseek($handle, 44);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read the data points and draw the image
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
while(!feof($handle))
{
// The next file position is the float value rounded to the closest chunk
// Read the next block, take the first value (of the first channel)
$real_pos_diff = ($current_file_position-44) % $chunksize;
if ($real_pos_diff > ($chunksize/2)) $real_pos_diff -= $chunksize;
fseek($handle, $current_file_position - $real_pos_diff);
$chunk = fread($handle, $chunksize);
if (feof($handle) && !strlen($chunk)) break;
$current_file_position += $block_chunks * $chunksize;
if ($byte == 1)
$blocks[] = ord($chunk[0]); // 8 bit
else
$blocks[] = ord($chunk[1]) ^ 128; // 16 bit
// Do we have enough blocks for the current point?
if (count($blocks) >= $accuracy)
{
// Calculate the mean and add the peak value to the array of blocks
sort($blocks);
$mean = (count($blocks) % 2) ? $blocks[(count($blocks)-1) / 2]
: ($blocks[count($blocks) / 2] + $blocks[count($blocks) / 2 - 1]) / 2
;
if ($mean > 127) $point = array_pop($blocks); else $point = array_shift($blocks);
// Draw
$lineheight = round($point / 255 * HEIGHT*ZOOM);
imageline($img, $points, 0 + (HEIGHT*ZOOM - $lineheight), $points, HEIGHT*ZOOM - (HEIGHT*ZOOM - $lineheight), imagecolorallocate($img, $r, $g, $b));
// update vars
$points++;
$blocks = array();
}
}
// close wave file
fclose ($handle);
if (ZOOM > 1)
{
// resample the image to the proportions defined in the form
$rimg = imagecreatetruecolor(WIDTH, HEIGHT);
// save alpha from original image
imagesavealpha($rimg, true);
imagealphablending($rimg, false);
// copy to resized
imagecopyresampled($rimg, $img, 0, 0, 0, 0, WIDTH, HEIGHT, WIDTH*ZOOM, HEIGHT*ZOOM);
imageline($rimg, 0, round(HEIGHT/2), WIDTH*ZOOM,round(HEIGHT/2), imagecolorallocate($img, $r, $g, $b));
imagepng($rimg, $pngfilename);
imagedestroy($rimg);
}
else
{
imageline($img, 0, round(HEIGHT/2), WIDTH*ZOOM,round(HEIGHT/2), imagecolorallocate($img, $r, $g, $b));
imagepng($img, $pngfilename);
}
imagedestroy($img);
}
/**
* Great function slightly modified as posted by Minux at
* http://forums.clantemplates.com/showthread.php?t=133805
*/
function html2rgb($input) {
$input=($input[0]=="#")?substr($input, 1,6):substr($input, 0,6);
return array(
hexdec( substr($input, 0, 2) ),
hexdec( substr($input, 2, 2) ),
hexdec( substr($input, 4, 2) )
);
}
?>
скрипт дальше вот этой команды:
if (($argc != 3) || !file_exists($argv[1]))
die('invalid file specified, need two arguments being a .wav file and a writable png file');
почему-то не выполняется.. :( :( :( :(
а вот этот код
листинг1(сам класс):
<?php
/*
$File: class.wave.php
$Date: 09-02-08
*/
class wave {
var $fp, $filesize;
var $data, $blocktotal, $blockfmt, $blocksize;
function __construct($file) {
if(!$this->fp = @fopen($file, 'rb')) {
return false;
}
$this->filesize = filesize($file);
}
function wavechunk() {
rewind($this->fp);
$riff_fmt = 'a4ID/VSize/a4Type';
$riff_cnk = @unpack($riff_fmt, fread($this->fp, 12));
if($riff_cnk['ID'] != 'RIFF' || $riff_cnk['Type'] != 'WAVE') {
return -1;
}
$format_header_fmt = 'a4ID/VSize';
$format_header_cnk = @unpack($format_header_fmt, fread($this->fp, 8));
if($format_header_cnk['ID'] != 'fmt ' || !in_array($format_header_cnk['Size'], array(16, 18))) {
return -2;
}
$format_fmt = 'vFormatTag/vChannels/VSamplesPerSec/VAvgBytesPerSec/vBlockAlign/vBitsPerSample'.($format_header_cnk['Size'] == 18 ? '/vExtra' : '');
$format_cnk = @unpack($format_fmt, fread($this->fp, $format_header_cnk['Size']));
if($format_cnk['FormatTag'] != 1) {
return -3;
}
if(!in_array($format_cnk['Channels'], array(1, 2))) {
return -4;
}
$fact_fmt = 'a4ID/VSize/Vdata';
$fact_cnk = @unpack($fact_fmt, fread($this->fp, 12));
if($fact_cnk['ID'] != 'fact') {
fseek($this->fp, ftell($this->fp) - 12);
}
$data_fmt = 'a4ID/VSize';
$data_cnk = @unpack($data_fmt, fread($this->fp, 8));
if($data_cnk['ID'] != 'data') {
return -5;
}
if($data_cnk['Size'] % $format_cnk['BlockAlign'] != 0) {
return -6;
}
$this->data = fread($this->fp, $data_cnk['Size']);
$this->blockfmt = $format_cnk['Channels'] == 1 ? 'sLeft' : 'sLeft/sRight';
$this->blocktotal = $data_cnk['Size'] / 4;
$this->blocksize = $format_cnk['BlockAlign'];
$return = array
(
'Channels' => $format_cnk['Channels'],
'SamplesPerSec' => $format_cnk['SamplesPerSec'],
'AvgBytesPerSec' => $format_cnk['AvgBytesPerSec'],
'BlockAlign' => $format_cnk['BlockAlign'],
'BitsPerSample' => $format_cnk['BitsPerSample'],
'Extra' => $format_cnk['Extra'],
'seconds' => ($data_cnk['Size'] / $format_cnk['AvgBytesPerSec'])
);
return $return;
}
function waveimage($channel = 'Left', $width = 1000, $height = 300, $bgcolor = array(255, 255, 255), $cenlinecolor = array(180, 180, 180), $imgcolor = array(0, 0, 0)) {
if(!$this->data) {
if(!is_array($this->wavechunk())) {
return false;
}
}
$width = max(10, $width);
$height = max(10, $height);
$border = 1;
$center = ($height - $border * 2) / 2;
$num_merge = max(1, round($this->blocktotal / $width));
$width = min($width, ceil($this->blocktotal / $num_merge));
$max = $min = 0;
$x = 1;
$pixels = array();
$wavetotal = 0;
for($i = 0; $i < $this->blocktotal; $i++) {
$blocks[] = @unpack($this->blockfmt, substr($this->data, $i * 4, $this->blocksize));
}
if(!isset($blocks[0][$channel])) {
return false;
}
for($i = 0; $i < $this->blocktotal; ) {
for($j = 0; $j < $num_merge; $j++) {
if(isset($blocks[$i + $j])) {
$wavetotal += $blocks[$i + $j][$channel];
}
}
$y = round($wavetotal / ($j + 1));
$pixels[] = array($x, $y);
$max = max($max, $y);
$min = min($min, $y);
$x++;
$i += $num_merge;
$wavetotal = 0;
}
$maxblock = max(abs($max), abs($min));
$zoom = $maxblock / $center;
$im = imagecreate($width, $height);
$background = imagecolorallocate($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
if($cenlinecolor) {
$cenline = imagecolorallocate($im, $cenlinecolor[0], $cenlinecolor[1], $cenlinecolor[2]);
imageline($im, 1, $center, $width, $center, $cenline);
}
$color = imagecolorallocate($im, $imgcolor[0], $imgcolor[1], $imgcolor[2]);
$y_old = 0;
foreach($pixels as $pixel) {
$y = $center - round($pixel[1] / $zoom);
imagesetpixel($im, $pixel[0], $y, $color);
if($pixel[0] > 1) {
imageline($im, $pixel[0] - 1, $y_old, $pixel[0], $y, $color);
}
$y_old = $y;
}
return $im;
}
}
?>
листинг2(пример,так сказать):
<?php
if(($file = $_FILES['upload']) && $file['name'] != 'none' && $file['size'] && $file['tmp_name']) {
include 'class.wave.php';
$wave = new wave($file['tmp_name']);
$chunk = $wave->wavechunk();
if(!is_array($chunk)) {
exit('This is not a wav file.');
}
if($chunk['seconds'] > 999) {
echo "The wav is too long.\r\n\r\n";
print_r($chunk);
} else {
set_time_limit(999);
if($im = $wave->waveimage()) {
header('Content-Type: image/png');
imagepng($im);
} else {
echo "wrong!\r\n\r\n";
print_r($chunk);
}
}
} else {
?>
<form action="?" id="upload_form" method="post" enctype="multipart/form-data">
<input type="file" name="upload" /><br />
<button type="submit">Submit Wav file</button>
</form>
<?php
}
?>
при загрузке абсолютно любого wave-файла говорит,что это не wave файл,хотя даже самые истинные wave-файлы пробовала брать-все равно говорит одно и тоже...почему так может быть? :unsure: :unsure: :unsure: :unsure: