php javascript wav波形绘制,PHP分析.wav文件并绘制png格式的波形图

以下为引用的内容:

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 ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值