示例#1
0
 protected function _createAudio()
 {
     if (is_null($this->_key)) {
         throw new \Exception('Key must be generated');
     }
     // TODO need get IT, DE and ES audio files
     //http://www.gilles-joyeux.fr/MP3/NEn.mp3 letters
     //http://www.gilles-joyeux.fr/MP3/ten.mp3 numbers
     // revoir les sons S et F en anglais ...
     try {
         $globalWavFile = new \WavFile();
         // Set sample rate, bits/sample, and Num of channels // TODO setter and getter this params ?
         $globalWavFile->setSampleRate(8000);
         $globalWavFile->setBitsPerSample(8);
         $globalWavFile->setNumChannels(1);
         $letters = str_split(strtoupper($this->_key));
         foreach ($letters as $letter) {
             if (!file_exists($this->_audioLangDirectory . $letter . '.wav')) {
                 throw new \Exception('Audio file : "' . $this->_audioLangDirectory . $letter . '.wav' . '" miss');
             }
             $l = new \WavFile($this->_audioLangDirectory . $letter . '.wav');
             // append letter to the captcha audio
             $globalWavFile->appendWav($l);
             // random silence  between letters
             $globalWavFile->insertSilence(mt_rand($this->_audioLettersGapMin, $this->_audioLettersGapMax) / 1000.0);
             //rand min and max
         }
         // Add filters
         $filters = array();
         // noise by sound file
         if ($this->_audioNoise) {
             // use background audio
             $wavNoise = new \WavFile($this->_audioNoiseFile, false);
             $wavNoise->setSampleRate($globalWavFile->getSampleRate())->setBitsPerSample($globalWavFile->getBitsPerSample())->setNumChannels($globalWavFile->getNumChannels());
             // start at a random offset from the beginning of the wav file in order to add more randomness
             $randOffset = 0;
             if ($wavNoise->getNumBlocks() > 2 * $globalWavFile->getNumBlocks()) {
                 $randBlock = rand(0, $wavNoise->getNumBlocks() - $globalWavFile->getNumBlocks());
                 $wavNoise->readWavData($randBlock * $wavNoise->getBlockAlign(), $globalWavFile->getNumBlocks() * $wavNoise->getBlockAlign());
             } else {
                 $wavNoise->readWavData();
                 $randOffset = rand(0, $wavNoise->getNumBlocks() - 1);
             }
             $mixOpts = array('wav' => $wavNoise, 'loop' => true, 'blockOffset' => $randOffset);
             $filters[\WavFile::FILTER_MIX] = $mixOpts;
             $filters[\WavFile::FILTER_NORMALIZE] = $this->_audioNoiseMixNormalization;
         }
         // add random noise. Any noise level below 95% is intensely distorted and not pleasing to the ear
         if ($this->_audioDegrade) {
             $filters[\WavFile::FILTER_DEGRADE] = rand(95, 98) / 100.0;
         }
         // apply filters to audio file
         if (count($filters) > 0) {
             $globalWavFile->filter($filters);
         }
         // save
         $this->_audioContents = $globalWavFile->makeHeader();
         $this->_audioContents .= $globalWavFile->getDataSubchunk();
         unset($globalWavFile);
     } catch (\Exception $e) {
         Logger::getInstance()->debug('Security captcha generate audio file for form : "' . $this->getFormName() . '" error : "' . $e . '"', 'security');
         if (file_exists($this->_audioLangDirectory . 'error.wav')) {
             $this->_audioContents = file_get_contents($this->_audioLangDirectory . 'error.wav');
         }
     }
 }