/** * Overwrite the method to set the point of operation within the Ogg bitstream. * * @param integer $offset The new point of operation. * @return void * @throws HausDesign_Io_Exception if an I/O error occurs */ public function setOffset($offset) { $streamSize = 0; for ($i = 0, $pageCount = count($this->_pages); $i < $pageCount; $i++) { if ($streamSize + $this->_pages[$i]['page']->getPageSize() >= $offset) { $this->_currentPageNumber = $i; $this->_currentPagePosition = $offset - $streamSize; parent::setOffset($this->_pages[$i]['offset'] + $this->_pages[$i]['page']->getHeaderSize() + $this->_currentPagePosition); break; } $streamSize += $this->_pages[$i]['page']->getPageSize(); } }
/** * Constructs the class with given filename. * * @param string|resource|HausDesign_Io_Reader $filename The path to the file, * file descriptor of an opened file, or a {@link HausDesign_Io_Reader} instance. * @throws HausDesign_Io_Exception if an error occur in stream handling. * @throws HausDesign_Media_Flac_Exception if an error occurs in vorbis bitstream reading. */ public function __construct($filename) { if ($filename instanceof HausDesign_Io_Reader) { $this->_reader =& $filename; } else { $this->_filename = $filename; require_once 'HausDesign/Io/FileReader.php'; try { $this->_reader = new HausDesign_Io_FileReader($filename); } catch (HausDesign_Io_Exception $e) { $this->_reader = null; require_once 'HausDesign/Media/Flac/Exception.php'; throw new HausDesign_Media_Flac_Exception($e->getMessage()); } } $capturePattern = $this->_reader->read(4); if ($capturePattern != 'fLaC') { require_once 'HausDesign/Media/Flac/Exception.php'; throw new HausDesign_Media_Flac_Exception('Not a valid FLAC bitstream'); } while (true) { $offset = $this->_reader->getOffset(); $last = ($tmp = $this->_reader->readUInt8()) >> 7 & 0x1; $type = $tmp & 0x7f; $size = $this->_reader->readUInt24BE(); $this->_reader->setOffset($offset); switch ($type) { case self::STREAMINFO: // 0 require_once 'HausDesign/Media/Flac/MetadataBlock/Streaminfo.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Streaminfo($this->_reader); break; case self::PADDING: // 1 require_once 'HausDesign/Media/Flac/MetadataBlock/Padding.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Padding($this->_reader); break; case self::APPLICATION: // 2 require_once 'HausDesign/Media/Flac/MetadataBlock/Application.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Application($this->_reader); break; case self::SEEKTABLE: // 3 require_once 'HausDesign/Media/Flac/MetadataBlock/Seektable.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Seektable($this->_reader); break; case self::VORBIS_COMMENT: // 4 require_once 'HausDesign/Media/Flac/MetadataBlock/VorbisComment.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_VorbisComment($this->_reader); break; case self::CUESHEET: // 5 require_once 'HausDesign/Media/Flac/MetadataBlock/Cuesheet.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Cuesheet($this->_reader); break; case self::PICTURE: // 6 require_once 'HausDesign/Media/Flac/MetadataBlock/Picture.php'; $this->_metadataBlocks[] = new HausDesign_Media_Flac_MetadataBlock_Picture($this->_reader); break; default: // break intentionally omitted } $this->_reader->setOffset($offset + 4 + $size); // Jump off the loop if we reached the end of metadata blocks if ($last === 1) { break; } } }
/** * Writes the possibly altered ID3v1 tag back to the file where it was read. * If the class was constructed without a file name, one can be provided * here as an argument. Regardless, the write operation will override * previous tag information, if found. * * @param string $filename The optional path to the file. * @throws HausDesign_Media_Id3_Exception if there is no file to write the tag to */ public function write($filename = null) { if ($filename === null && ($filename = $this->_filename) === null) { require_once 'HausDesign/Media/Id3/Exception.php'; throw new HausDesign_Media_Id3_Exception('No file given to write the tag to'); } require_once 'HausDesign/Io/FileWriter.php'; try { $writer = new HausDesign_Io_FileWriter($filename); $offset = $writer->getSize(); if ($this->_reader !== null) { $offset = -128; } else { $reader = new HausDesign_Io_Reader($writer->getFileDescriptor()); $reader->setOffset(-128); if ($reader->read(3) == 'TAG') { $offset = -128; } } $writer->setOffset($offset); $writer->writeString8('TAG')->writeString8(substr($this->_title, 0, 30), 30)->writeString8(substr($this->_artist, 0, 30), 30)->writeString8(substr($this->_album, 0, 30), 30)->writeString8(substr($this->_year, 0, 4), 4); if ($this->_track) { $writer->writeString8(substr($this->_comment, 0, 28), 28)->writeInt8(0)->writeInt8($this->_track); } else { $writer->writeString8(substr($this->_comment, 0, 30), 30); } $writer->writeInt8($this->_genre); $writer->flush(); } catch (HausDesign_Io_Exception $e) { require_once 'HausDesign/Media/Id3/Exception.php'; throw new HausDesign_Media_Id3_Exception($e->getMessage()); } $this->_filename = $filename; }