30 private $presetName =
'';
33 private $messageCode =
'';
63 if (intval($value) > 0)
87 $params = Json::decode($json);
88 if($params[
'type'] == self::AUDIO_TYPE_PRESET)
92 if($params[
'type'] == self::AUDIO_TYPE_FILE)
96 if($params[
'duration'])
98 $this->duration = $params[
'duration'];
117 $this->messageCode = $messageCode;
130 $this->fileId = $fileId;
142 $this->presetName = $presetName;
153 return !!$this->presetName;
171 $url = \CFile::GetPath($this->
getFileId());
172 if ($url && $useAbsoluteUrl && !$this->isRemoteFile($url))
174 $urlManager = \Bitrix\Main\Engine\UrlManager::getInstance();
175 $url = $urlManager->getHostUrl() . $url;
205 return Json::encode($result);
224 return $this->fileId;
233 return $this->presetName;
242 return $this->messageCode;
251 if (!$this->duration)
262 return $this->duration;
279 private function getPresetCode()
295 $fileName = \CFile::GetPath($fileId);
297 if ($this->isRemoteFile($fileName))
299 $tmpFileName = \CFile::GetTempName(
'',
'tmpfile.mp3');
301 "socketTimeout" => 5,
304 $request->download($fileName, $tmpFileName);
305 $fileName = $tmpFileName;
312 $file = fopen($fileName,
"rb");
315 $header = fread($file, 100);
316 $offset = $this->getId3v2TagLength($header);
317 fseek($file, $offset, SEEK_SET);
320 $frame = fread($file, 10);
321 if (mb_strlen($frame,
'latin1') < 10)
327 if (
"\xff" == $frame[0] && (ord($frame[1]) & 0xe0))
329 list($frameLength, $frameDuration) = $this->getFrameInfo(mb_substr($frame, 0, 4,
'latin1'));
334 $offset = $frameLength - 10;
335 $duration += $frameDuration;
339 $offset = (
'TAG' == mb_substr($frame, 0, 3,
'latin1')) ? 118 : -9;
341 fseek($file, $offset, SEEK_CUR);
344 return round($duration);
352 private function getId3v2TagLength($header)
354 if (
"ID3" == mb_substr($header, 0, 3,
'latin1'))
356 $hasExtendedHeader = (ord($header[5]) & 0x10) ? 1 : 0;
357 $lengthByte1 = ord($header[6]);
358 $lengthByte2 = ord($header[7]);
359 $lengthByte3 = ord($header[8]);
360 $lengthByte4 = ord($header[9]);
361 if (!($lengthByte1 & 0x80) && !($lengthByte2 & 0x80) && !($lengthByte3 & 0x80) && !($lengthByte4 & 0x80))
363 $tagHeaderLength = 10 + ($hasExtendedHeader ? 10 : 0);
365 (($lengthByte1 & 0x7f) << 21) +
366 (($lengthByte2 & 0x7f) << 14) +
367 (($lengthByte3 & 0x7f) << 7) +
368 ($lengthByte4 & 0x7f);
370 return $tagHeaderLength + $tagContentLength;
381 private function getFrameInfo($frame)
383 $versions = [0 =>
'2.5', 2 =>
'2', 3 =>
'1'];
384 $layers = [1 =>
'3', 2 =>
'2', 3 =>
'1'];
387 1 => [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448],
388 2 => [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384],
389 3 => [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
392 1 => [0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256],
393 2 => [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
394 3 => [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
398 '1' => [44100, 48000, 32000],
399 '2' => [22050, 24000, 16000],
400 '2.5' => [11025, 12000, 8000]
403 1 => [1 => 384, 2 => 1152, 3 => 1152],
404 2 => [1 => 384, 2 => 1152, 3 => 576]
407 $layerData = ord($frame[1]);
408 $rateData = ord($frame[2]);
410 $version = $versions[($layerData & 0x18) >> 3];
411 $bitrateVersion = ($version ==
'2.5' ? 2 : $version);
413 $layer = $layers[($layerData & 0x06) >> 1];
415 $bitrateIndex = ($rateData & 0xf0) >> 4;
416 $bitrate = $bitrates[$bitrateVersion][$layer][$bitrateIndex] ?: 0;
418 $sampleRateIndex = ($rateData & 0x0c) >> 2;
419 $sampleRate = $sampleRates[$version][$sampleRateIndex] ?: 0;
420 $padding = ($rateData & 0x02) >> 1;
422 if ($sampleRate <> 0)
424 $duration = $samples[$bitrateVersion][$layer] / $sampleRate;
429 $frameLength = intval(((12 * $bitrate * 1000 / $sampleRate) + $padding) * 4);
433 $frameLength = intval(((144 * $bitrate * 1000) / $sampleRate) + $padding);
436 return [$frameLength, $duration ?? 0];
444 private function isRemoteFile($fileName)
446 return preg_match(
'/^(https?):\/\/.*/', $fileName) === 1;