//读取不超过10s的wav文件中波形数据
public function read_wav($filename){
//读取云端文件,创建临时文件,直接调用云端wav文件出来的data有缺失,可以考虑下怎么处理
$tt=time();
$file=$tt.'.wav';
file_put_contents($file,file_get_contents($filename));//读取云端wav数据写入临时文件
$files = fopen($file, 'rd');
//$files = fopen($filename, 'rd');
//头部信息
$content = fread($files,44);
$header['ChunkID'] = substr($content, 0, 4);
$header['ChunkSize'] = $this -> str2dec(substr($content, 4, 4), true);
$header['Subchunk1ID'] = substr($content, 12, 4);
// 小端序 - 整型
$header['Subchunk1Size'] = $this->str2dec(substr($content, 16, 4), true);
$header['AudioFormat'] = $this->str2dec(substr($content, 20, 2), true);
$header['NumChannels'] = $this->str2dec(substr($content, 22, 2), true);
$header['SampleRate'] = $this->str2dec(substr($content, 24, 4), true);
$header['ByteRate'] = $this->str2dec(substr($content, 28, 4), true);
$header['BlockAlign'] = $this->str2dec(substr($content, 32, 2), true);
$header['BitsPerSample'] = $this->str2dec(substr($content, 34, 2), true);
$header['Subchunk2ID'] = substr($content, 36, 4);
$header['Subchunk2Size'] = $this -> str2dec(substr($content, 40, 4), true);
//读取data数据
fseek ($this -> file_handler,44);//设置文件指针到第44字节后
$sp_data = fread ($files,$header['ByteRate']*10) ;//不超过10s
for($i=0;$i<=$header['ByteRate']*10;$i=$i+2){
if(substr($sp_data, $i, 2)){
$t=$this->str2dec(substr($sp_data, $i, 2), true);
$header['data'][$i/2] =($t>32767?$t-65536:$t)/32767;//大于32767(头部信息不一样有区别)为负数
}else{
break;
}
}
fclose($files);
unlink('./'.$file);
return $header;
}
public function str2dec($str, $isLittleEndian = false){
if ($isLittleEndian) {
$str = strrev($str);
}
return hexdec(bin2hex($str));$header['Format'] = substr($content, 8, 4);
}
PHP读取wav文件
于 2022-09-19 14:47:43 首次发布