"RIFF"
fmt
"data"
class Header { ... /** * @var string */ protected $id; /** * @var int */ protected $size; /** * @var string */ protected $format; ...
class FormatSection { ... /** * @var string */ protected $id; /** * @var int */ protected $size; /** * @var int */ protected $audioFormat; /** * @var int */ protected $numberOfChannels; /** * @var int */ protected $sampleRate; /** * @var int */ protected $byteRate; /** * @var int */ protected $blockAlign; /** * @var int */ protected $bitsPerSample; ...
class DataSection { ... /** * @var string */ protected $id; /** * @var int */ protected $size; /** * @var int[] */ protected $raw; ...
class Helper { ... public static function readString($handle, $length) { return self::readUnpacked($handle, 'a*', $length); } public static function readLong($handle) { return self::readUnpacked($handle, 'V', 4); } public static function readWord($handle) { return self::readUnpacked($handle, 'v', 2); } protected function readUnpacked($handle, $type, $length) { $data = unpack($type, fread($handle, $length)); return array_pop($data); } ... }
class Parser { ... public static function fromFile($filename) { ... $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); } protected static function parseHeader($handle) { return [ 'id' => Helper::readString($handle, 4), 'size' => Helper::readLong($handle), 'format' => Helper::readString($handle, 4), ]; } protected static function parseFormatSection($handle) { return [ 'id' => Helper::readString($handle, 4), 'size' => Helper::readLong($handle), 'audioFormat' => Helper::readWord($handle), 'numberOfChannels' => Helper::readWord($handle), 'sampleRate' => Helper::readLong($handle), 'byteRate' => Helper::readLong($handle), 'blockAlign' => Helper::readWord($handle), 'bitsPerSample' => Helper::readWord($handle), ]; } 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; }
echo $audio->getSampleRate();
class Note { const C = 261.63; const C_SHARP = 277.18; const D = 293.66; const D_FLAT = self::C_SHARP; const D_SHARP = 311.13; const E = 329.63; const E_FLAT = self::D_SHARP; const E_SHARP = self::F; const F = 346.23; const F_FLAT = self::E; const F_SHARP = 369.99; const G = 392.00; const G_FLAT = self::F_SHARP; const G_SHARP = 415.30; const A = 440.00; const A_FLAT = self::G_SHARP; const A_SHARP = 466.16; const H = 493.88; const H_FLAT = self::A_SHARP; public static function get($note) { switch ($note) { case 'C': return self::C; case 'C#': return self::C_SHARP; case 'D': return self::D; case 'D#': return self::D_SHARP; case 'E': return self::E; case 'E#': return self::E_SHARP; case 'F': return self::F; case 'F#': return self::F_SHARP; case 'G': return self::G; case 'G#': return self::G_SHARP; case 'A': return self::A; case 'A#': return self::A_SHARP; case 'B': return self::H_FLAT; case 'H': return self::H; } } }
class Piano extends Generator { ... public function getDampen($sampleRate = null, $frequency = null, $volume = null) { return pow(0.5 * log(($frequency * $volume) / $sampleRate), 2); } ... public function getWave($sampleRate, $frequency, $volume, $i) { $base = $this->getModulations()[0]; return call_user_func_array($base, [ $i, $sampleRate, $frequency, pow(call_user_func_array($base, [$i, $sampleRate, $frequency, 0]), 2) + 0.75 * call_user_func_array($base, [$i, $sampleRate, $frequency, 0.25]) + 0.1 * call_user_func_array($base, [$i, $sampleRate, $frequency, 0.5]) ]); } ... protected function getModulations() { return [ function($i, $sampleRate, $frequency, $x) { return 1 * sin(2 * M_PI * (($i / $sampleRate) * $frequency) + $x); }, ... ]; } }
class SampleBuilder { /** * @var Generator */ protected $generator; ... 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())); } }
$sampleBuilder = new \Wav\SampleBuilder(\Wav\Generator\Piano::NAME); $samples = [ $sampleBuilder->note('E', 5, 0.3), $sampleBuilder->note('D#', 5, 0.3), $sampleBuilder->note('E', 5, 0.3), $sampleBuilder->note('D#', 5, 0.3), $sampleBuilder->note('E', 5, 0.3), $sampleBuilder->note('H', 4, 0.3), $sampleBuilder->note('D', 5, 0.3), $sampleBuilder->note('C', 5, 0.3), $sampleBuilder->note('A', 4, 1), ]; $builder = (new Wav\Builder()) ->setAudioFormat(\Wav\WaveFormat::PCM) ->setNumberOfChannels(1) ->setSampleRate(\Wav\Builder::DEFAULT_SAMPLE_RATE) ->setByteRate(\Wav\Builder::DEFAULT_SAMPLE_RATE * 1 * 16 / 8) ->setBlockAlign(1 * 16 / 8) ->setBitsPerSample(16) ->setSamples($samples); $audio = $builder->build(); $audio->returnContent();
composer require nkolosov/wav
Source: https://habr.com/ru/post/282922/
All Articles