PHP读取wav文件

//读取不超过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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值