示例#1
0
文件: Parser.php 项目: nkolosov/wav
 /**
  * @param string $filename path to wav-file
  *
  * @return AudioFile
  * @throws FileIsNotExistsException
  * @throws FileIsNotReadableException
  * @throws FileIsNotWavFileException
  */
 public static function fromFile($filename)
 {
     if (!file_exists($filename)) {
         throw new FileIsNotExistsException('File "' . $filename . '" is not exists.');
     }
     if (!is_readable($filename)) {
         throw new FileIsNotReadableException('File "' . $filename . '" is not readable"');
     }
     if (is_dir($filename)) {
         throw new FileIsNotWavFileException('File "' . $filename . '" is not a wav-file');
     }
     $size = filesize($filename);
     if ($size < AudioFile::HEADER_LENGTH) {
         throw new FileIsNotWavFileException('File "' . $filename . '" is not a wav-file');
     }
     $handle = fopen($filename, 'rb');
     try {
         $header = Header::createFromArray(self::parseHeader($handle));
         $formatSection = FormatSection::createFromArray(self::parseFormatSection($handle));
         $dataSection = DataSection::createFromArray(self::parseDataSection($handle));
     } finally {
         fclose($handle);
     }
     return new AudioFile($header, $formatSection, $dataSection);
 }