用php分析并绘制音频文件的波形图,网上还是很少见到。其实只要根据wav文件的规范,用php的fseek,fopen,fopen,pack/unpack等函数,以及强大的gd图形库,这些都是很容易的。很多人可能对pack/unpack函数不熟悉;这其实是php借用perl的,他们提供了使用脚本语言访问复杂二进制数据结构的方法。我的这段简化的程序只能处理pcm格式的riff音频文件(这也是最常见的wav格式) ,不限声道,但是比特率(bitspersample)最好是16。
这里有wave file format 和 microsoft wave soundfile format可以参考。这里是一个实际的例子(下载放大看)
1 <?php
2
3 function wav_graph($file, $f=0, $w=0)
4 {
5 global $data_dir;
6
7 if(!is_file($file)) return 0;
8 $fp = fopen($data_dir.$file, 'r');
9 $raw = fread($fp, 36);
10 $str = '';
11 $header = unpack('a4riff/vsize/a4wav/a4head/vheadsize/vpcm/vchannels/vsamplerate/vbyterate/vblockalign/vsamplebits', $raw);
12 foreach($header as $k=>$v)
13 $str .= $k.': '. $v.' ';
14 fseek($fp, 36 + $header['headsize'] - 16);
15 $raw = fread($fp, 8);
16 $data = unpack('a4data/vdatasize', $raw);
17 foreach($data as $k=>$v)
18 $str .= $k.': '. $v.' ';
19
20 $b = $header['samplebits'];
21 $c = $header['channels'];
22 $l = $b * $c / 8; // sample frame length in bytes
23 $s = $data['datasize'] / $l; // total number of samples
24 $r = $header['samplerate'];
25 if($f) $h = pow(2, $b) / $f;
26 else { $h = 200; $f = pow(2, $b - 1) / $h; }
27 if($w == 0) $w = round($r / 1000); // default to show 1k sample frames per minute
28
29 header("content-type: image/png");
30 $im = imagecreate($s / $w, $h * $c * 2);
31 imagecolorallocate($im, 0xff, 0xff, 0xff); // white bg
32 $color = imagecolorallocate($im, 0, 0, 255); // black
33 //imagestring($im, 5, 5, 5, $str, $color);
34
35 $x=0; $y = array(); $yn = array();
36 for($i = 0; $i < $c; $i++) $y[$i] = $h * $i + $h;
37 $n = $l * $w;
38 while(1)
39 {
40 if($s == 0) break;
41 if($s < $n) $n = $s;
42 $samples = fread($fp, 1000 * $n);
43 if($samples === false) break;
44 $packed = unpack("s*", $samples);
45 foreach($packed as $k=>$v)
46 {
47 $cnt = ($k-1) % ($w * $l) ;
48 if( $cnt > $c - 1) continue;
49 $yn[$cnt] = $h * $cnt + $h - $v / $f;
50 imageline($im, $x, $y[$cnt], $x+1, $yn[$cnt], $color);
51 $y[$cnt] = $yn[$cnt];
52 $x++;
53 }
54 $s -= $n;
55 }
56
57 imagepng($im);
58 imagedestroy($im);
59 }
60
61 //wav_graph('audio2.wav');
62 ?>
新闻热点
疑难解答