当前位置:Linux教程 - Linux资讯 - 实例分析:实现一个PHP WAV接合方案

实例分析:实现一个PHP WAV接合方案

  WAV PCM文件的最大优点在于,你无需进行任何绑定操作,就能将来自两个文件的二进制音频数据连接起来。你只需把其中一个文件的二进制数据粘合到另一个文件的末尾,再调整一下Subchunk2Size值,即可实现两个文件的接合。唯一的缺点是这两个文件的格式必须相同;在本例中,不涉及问题。    当解析音频文件时,先将二进制音频数据读入一个缓冲数组中,然后再将另一个文件的数据读到这个数组的末端。重复这一过程直到所有的文件都被读取完毕。创建一个FILESTRUCT结构,填入格式信息,调整Subchunk2Size值,将Data成员设为缓冲数组,将FILESTRUCT写为需要的输出格式。对于基于PHP的Web发布系统,其输出为标准输出。以下为示范代码:    function StitchFiles(&$fsFile, &$sFiles) {    $fsFiles = array(); //FILESTRUCT  $lFileSize = 0;  $lOffset = 0;  $bData = array(); //byte    for ($i = 0; $i < count($sFiles); $i++) {  $fsFiles[$i] = new FILESTRUCT();  SetFile($fsFiles[$i], $sFiles[$i]);  $lSize = CalcLittleEndianValue($fsFiles[$i]->Subchunk2Size);  $lFileSize = $lFileSize + $lSize;  $bData = array_merge($bData, $fsFiles[$i]->Data);  $lOffset = $lOffset + $lSize;  }  $fsFile->ChunkID = $GLOBALS["ChunkID_"];  $fsFile->ChunkSize = GetLittleEndianByteArray(36 + $lFileSize);  $fsFile->Format = $GLOBALS["FileFormat_"];  $fsFile->Subchunk1ID = $GLOBALS["Subchunk1ID_"];  $fsFile->Subchunk1Size = array(0x10, 0x0, 0x0, 0x0);  $fsFile->AudioFormat = $GLOBALS["AudioFormat_"];  $fsFile->NumChannels = $GLOBALS["Stereo_"];  $fsFile->SampleRate = $GLOBALS["SampleRate_"];  $fsFile->ByteRate = GetLittleEndianByteArray(    CalcLittleEndianValue($GLOBALS["SampleRate_"]) *  CalcLittleEndianValue($GLOBALS["Stereo_"]) *    (CalcLittleEndianValue($GLOBALS["BitsPerSample_"]) / 8));  $fsFile->BlockAlign =  array_splice(GetLittleEndianByteArray(CalcLittleEndianValue($GLOBALS["Stereo_"]  ) * (CalcLittleEndianValue($GLOBALS["BitsPerSample_"]) / 8)), 0, 2);  $fsFile->BitsPerSample = $GLOBALS["BitsPerSample_"];  $fsFile->Subchunk2ID = $GLOBALS["Subchunk2ID_"];  $fsFile->Subchunk2Size = GetLittleEndianByteArray($lFileSize);  $fsFile->Data = $bData;  }    本调用的第一个参数是对FILESTRUCT对象的引用。第二个参数是对一列文件名字符串的引用。在每次for循环中,我都将列表中的一个文件名载入一个FILESTRUCT中,然后再将每一个FILESTRUCT的Data属性提取出来,并合并到一个现有的缓冲区中。完成以上操作后,我通过一个事先声明的全局变量对FILESTRUCT参数对象的属性进行设定,然后改变Subchunk2Size属性,并将Data属性设为新的缓冲区。    你可能注意到,我使用了一些功能函数来填充FILESTRUCT结构、计算来自little endian字节数组的数值、创建little endian字节数组和拆分字符串为二进制数组。以下为这些函数:    function SetFile(&$fsFile_, $sFileName) {  $lSize = 1;  if (file_exists($sFileName)) {  $fil = fopen($sFileName, "rb");  $contents = fread($fil, count($fsFile_->ChunkID));  $fsFile_->ChunkID = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->ChunkSize));  $fsFile_->ChunkSize = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->Format));  $fsFile_->Format = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->Subchunk1ID));  $fsFile_->Subchunk1ID = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->Subchunk1Size));  $fsFile_->Subchunk1Size = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->AudioFormat));  $fsFile_->AudioFormat = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->NumChannels));  $fsFile_->NumChannels = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->SampleRate));  $fsFile_->SampleRate = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->ByteRate));  $fsFile_->ByteRate = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->BlockAlign));  $fsFile_->BlockAlign = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->BitsPerSample));  $fsFile_->BitsPerSample = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->Subchunk2ID));  $fsFile_->Subchunk2ID = bin_split($contents, 1);  $contents = fread($fil, count($fsFile_->Subchunk2Size));  $fsFile_->Subchunk2Size = bin_split($contents, 1);  $lSize = CalcLittleEndianValue($fsFile_->Subchunk2Size);  $contents = fread($fil, $lSize);  $fsFile_->Data = bin_split($contents, 1);  fclose($fil);  }  }    function CalcLittleEndianValue(&$bValue) {  $lSize_ = 0;  for ($iByte = 0; $iByte < count($bValue); $iByte++) {  $lSize_ += ($bValue[$iByte] * pow(16, ($iByte * 2)));  }  return $lSize_;  }    function GetLittleEndianByteArray($lValue) {  $running = 0;  $b = array(0, 0, 0, 0);  $running = $lValue / pow(16,6);  $b[3] = floor($running);  $running -= $b[3];  $running *= 256;  $b[2] = floor($running);  $running -= $b[2];  $running *= 256;  $b[1] = floor($running);  $running -= $b[1];  $running *= 256;  $b[0] = round($running);  return $b;  }    function bin_split($text, $c)  {  $arr = array();  $len = strlen($text);  $a = 0;  while($a < $len)  {  if ($a + $c > $len)  {  $c = $len - $a;  }  $arr[$a] = ord(substr($text, $a, $c));  $a += $c;  }  return $arr;  }    为了实现这个类的功能,你可以创建一个CStitcher实例并调用StitchFiles()方法,以传递合适的参数:    $ChunkID_ = array(0x52, 0x49, 0x46, 0x46); //"RIFF" big endian  $FileFormat_ = array(0x57, 0x41, 0x56, 0x45); //"WAVE" big endian  $Subchunk1ID_ = array(0x66, 0x6D, 0x74, 0x20); //"fmt" big endian  $AudioFormat_ = array(0x1, 0x0); //PCM = 1 little endian  $Stereo_ = array(0x2, 0x0); //Stereo = 2 little endian  $Mono_ = array(0x1, 0x0); //Mono = 1 little endian  $SampleRate_ = array(0x44, 0xAC, 0x0, 0x0); //44100 little endian  $BitsPerSample_ = array(0x10, 0x0); //16 little endian  $Subchunk2ID_ = array(0x64, 0x61, 0x74, 0x61); //"data" big endian  $files = array("C:\\Inetpub\\wwwroot\\Test\\PHP\\1.wav",  "C:\\Inetpub\\wwwroot\\Test\\PHP\\2.wav");  $Stitcher = new CStitcher();  $file = new FILESTRUCT();  $Stitcher->StitchFiles($file, $files);    可用以下代码将二进制数据写到HTTP输出中:    header('Content-type: audio/x-wav', true);  header('Content-Disposition: attachment;filename=stitch.wav');    foreach($file->ChunkID as $val) {  print chr($val);  }  foreach($file->ChunkSize as $val) {  print chr($val);  }  foreach($file->Format as $val) {  print chr($val);  }  foreach($file->Subchunk1ID as $val) {  print chr($val);  }  foreach($file->Subchunk1Size as $val) {  print chr($val);  }  foreach($file->AudioFormat as $val) {  print chr($val);  }  foreach($file->NumChannels as $val) {  print chr($val);  }  foreach($file->SampleRate as $val) {  print chr($val);  }  foreach($file->ByteRate as $val) {  print chr($val);  }  foreach($file->BlockAlign as $val) {  print chr($val);  }  foreach($file->BitsPerSample as $val) {  print chr($val);  }  foreach($file->Subchunk2ID as $val) {  print chr($val);  }  foreach($file->Subchunk2Size as $val) {  print chr($val);  }  foreach($file->Data as $val) {  print chr($val);  }    以下是一段Html代码,用于测试结果:    <html>  <head>  <sc
[1] [2] 下一页 

(出处:http://www.sheup.com)


上一页 [1] [2]