/** * @param resource $handle * * @return array */ protected static function parseDataSection($handle) { $data = ['id' => Helper::readString($handle, 4), 'size' => Helper::readLong($handle)]; if ($data['size'] > 0) { $data['raw'] = fread($handle, $data['size']); } return $data; }
/** * @param string $note * @param int $octave * @param number $duration * * @return Sample */ public function note($note, $octave, $duration) { $result = new \SplFixedArray((int) ceil($this->getSampleRate() * $duration * 2)); $octave = min(8, max(1, $octave)); $frequency = Note::get($note) * pow(2, $octave - 4); $attack = $this->generator->getAttack($this->getSampleRate(), $frequency, $this->getVolume()); $dampen = $this->generator->getDampen($this->getSampleRate(), $frequency, $this->getVolume()); $attackLength = (int) ($this->getSampleRate() * $attack); $decayLength = (int) ($this->getSampleRate() * $duration); for ($i = 0; $i < $attackLength; $i++) { $value = $this->getVolume() * ($i / ($this->getSampleRate() * $attack)) * $this->getGenerator()->getWave($this->getSampleRate(), $frequency, $this->getVolume(), $i); $result[$i << 1] = Helper::packChar($value); $result[($i << 1) + 1] = Helper::packChar($value >> 8); } for (; $i < $decayLength; $i++) { $value = $this->getVolume() * pow(1 - ($i - $this->getSampleRate() * $attack) / ($this->getSampleRate() * ($duration - $attack)), $dampen) * $this->getGenerator()->getWave($this->getSampleRate(), $frequency, $this->getVolume(), $i); $result[$i << 1] = Helper::packChar($value); $result[($i << 1) + 1] = Helper::packChar($value >> 8); } return new Sample($result->getSize(), implode('', $result->toArray())); }
/** * @param resource $handle */ protected function writeDataSection($handle) { Helper::writeString($handle, $this->dataSection->getId()); Helper::writeLong($handle, $this->dataSection->getSize()); Helper::writeString($handle, $this->dataSection->getRaw()); }