コード例 #1
0
ファイル: WavReader.php プロジェクト: Samovar/fft-console
 /**
  * {@inheritdoc}
  */
 public function read()
 {
     $chunkID = BinaryReader::read($this->resource, 'a*', 4);
     if ($chunkID !== 'RIFF') {
         return false;
     }
     $this->riff->setChunkID($chunkID);
     $this->riff->setChunkSize(BinaryReader::read($this->resource, 'V', 4));
     $this->riff->setFormat(BinaryReader::read($this->resource, 'a*', 4));
     $this->fmt->setSubchunk1ID(BinaryReader::read($this->resource, 'a*', 4));
     $this->fmt->setSubchunk1Size(BinaryReader::read($this->resource, 'V', 4));
     $this->fmt->setAudioFormat(BinaryReader::read($this->resource, 'v', 2));
     $this->fmt->setNumChannels(BinaryReader::read($this->resource, 'v', 2));
     $this->fmt->setSampleRate(BinaryReader::read($this->resource, 'V', 4));
     $this->fmt->setByteRate(BinaryReader::read($this->resource, 'V', 4));
     $this->fmt->setBlockAlign(BinaryReader::read($this->resource, 'v', 2));
     $this->fmt->setBitsPerSample(BinaryReader::read($this->resource, 'v', 2));
     $this->fmt->setExtraParamSize(0);
     $this->fmt->setExtraParams(null);
     if ($this->fmt->getAudioFormat() != 1) {
         $this->fmt->setExtraParamSize(BinaryReader::read($this->resource, 'v', 2));
         $this->fmt->setExtraParams(BinaryReader::read($this->resource, 'a*', $this->fmt->getExtraParamSize()));
     }
     $this->data->setSubchunk2ID(BinaryReader::read($this->resource, 'a*', 4));
     $this->data->setSubchunk2Size(BinaryReader::read($this->resource, 'V', 4));
     switch ($this->fmt->getAudioFormat()) {
         case 1:
             $this->fmt->setAudioFormat('PCM');
             break;
         case 2:
             $this->fmt->setAudioFormat('Microsoft ADPCM');
             break;
         case 6:
             $this->fmt->setAudioFormat('ITU G.711 a-law');
             break;
         case 7:
             $this->fmt->setAudioFormat('ITU G.711 µ-law');
             break;
         case 17:
             $this->fmt->setAudioFormat('IMA ADPCM');
             break;
         case 20:
             $this->fmt->setAudioFormat('ITU G.723 ADPCM (Yamaha)');
             break;
         case 49:
             $this->fmt->setAudioFormat('GSM 6.10');
             break;
         case 64:
             $this->fmt->setAudioFormat('ITU G.721 ADPCM');
             break;
         case 80:
             $this->fmt->setAudioFormat('MPEG');
             break;
         case 65536:
             $this->fmt->setAudioFormat('Experimental');
             break;
     }
     return $this;
 }
コード例 #2
0
 /**
  * Test read.
  */
 public function testRead()
 {
     // Start wav file
     //          R   I   F   F        integer (4 byte)     WAVE            fmt             16  PCM
     $string = "RIFF`(WAVEfmt ";
     $data = sprintf('data://text/plain;base64,%s', base64_encode($string));
     $resource = fopen($data, 'rb');
     // RIFF (ASCII)
     $this->assertEquals('RIFF', BinaryReader::read($resource, 'a*', 4));
     // Size (4 bytes (Any value in test))
     $this->assertEquals(2646036, BinaryReader::read($resource, 'V', 4));
     // WAVE
     $this->assertEquals('WAVE', BinaryReader::read($resource, 'a*', 4));
     // fmt
     $this->assertEquals('fmt ', BinaryReader::read($resource, 'a*', 4));
     // 16 for PCM
     $this->assertEquals(16, BinaryReader::read($resource, 'V', 4));
     // PCM
     $this->assertEquals(1, BinaryReader::read($resource, 'v', 2));
     fclose($resource);
 }
コード例 #3
0
 /**
  * @param int $width
  * @param int $height
  */
 public function run($width = self::WIDTH_DEFAULT, $height = self::HEIGHT_DEFAULT)
 {
     if (PHP_SAPI !== 'cli') {
         throw new \RuntimeException('Only cli');
     }
     $this->wavReader->read();
     $wavInfo = '';
     if ($fmt = $this->wavReader->getFmt()) {
         $wavInfo = vsprintf("AudioFormat: %s\tNumChannels: %s\tSampleRate: %s\tBitsPerSample:%s", [$fmt->getAudioFormat(), $fmt->getNumChannels(), $fmt->getSampleRate(), $fmt->getBitsPerSample()]);
     }
     $fourier = new FFT($this->dim);
     $peak = 2147483648;
     // pow(256, 4) / 2;
     $half = $this->dim / 2;
     // optimize cycle
     while (true) {
         $function = [];
         for ($dimCounter = 0; $dimCounter < $this->dim; ++$dimCounter) {
             $function[] = BinaryReader::read($this->resource, 'V', 4) / $peak;
         }
         $FFT = $fourier->fft($function);
         $absFFT = $fourier->getAbsFFT($FFT);
         $display = [];
         // clear terminal
         $this->render->renderLine("c");
         // Wav information
         $this->render->renderLine($wavInfo);
         // Build matrix
         // Scale only height
         //
         // ^            ^
         // |  *         |  *
         // | * *   to   | ***
         // |*   *       |*****
         // +------>     +------->
         for ($y = 0; $y < $height; ++$y) {
             for ($x = 0; $x < $half; ++$x) {
                 // scale by height
                 $value = (int) $absFFT[$x] / (max($absFFT) + 1.0E-5) * $height;
                 $display[$y][$x] = false;
                 for ($m = 0; $m <= $value; ++$m) {
                     $display[$m][$x] = true;
                 }
             }
         }
         // TODO: Width scale
         $this->render->render($display, $height, $half);
     }
 }