PHP分析.wav文件并绘制png格式的波形图
用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 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
37 $n = $l * $w;
38 while(1)
39 {
40 if($s == 0) break;
41 if($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 ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

PHP is a powerful programming language that can be used to create dynamic websites and web applications. One of the most powerful features is PHP’s header() method. In this article, we will explore how to use PHP’s header() method to adjust web pages.

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, with jump statements such as "if($_COOKIE[" u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.

Complete list of PHP file download functions: File download example analysis of readfile, header, Content-Disposition and other functions. File download is one of the essential functions in Web applications, and PHP, as a widely used Web development language, provides many A function and method to implement file downloading. This article will introduce commonly used file download functions in PHP, including readfile, header, Content-Dispo

Nginx can distribute requests to different servers based on custom identifiers in the request headers. Specifically, you can use the map directive to map the custom identifier in the request header to a different backend server address, and then use the proxy_pass directive to forward the request to the corresponding backend server. The following is an example configuration file: http{map$http_my_header$backend{defaultbackend1.example.com;value1backend2.example.com;value2backend3.example.com;}upstreambackend1{serv

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.
