歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> 更多Linux

實例分析:實現一個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




Copyright © Linux教程網 All Rights Reserved