diff --git a/phpmp3.php b/phpmp3.php index 682e7ac..4bf07e1 100644 --- a/phpmp3.php +++ b/phpmp3.php @@ -1,10 +1,13 @@ binaryTable = array(); + for($i = 0; $i < 256; $i++) + { + $this->binaryTable[chr($i)] = sprintf('%08b', $i); + } + if($path != '') { $this->str = file_get_contents($path); @@ -43,7 +53,7 @@ public function getStart() $parts = array(); for($i = 0; $i < $strlen; $i++) { - $parts[] = $this->decbinFill(ord($str[$i]), 8); + $parts[] = $this->binaryTable[$str[$i]]; } if ($this->doFrameStuff($parts) === false) @@ -69,7 +79,7 @@ public function setFileInfoExact() $parts = array(); for($i = 0; $i < $strlen; $i++) { - $parts[] = $this->decbinFill(ord($str[$i]), 8); + $parts[] = $this->binaryTable[$str[$i]]; } if($parts[0] != '11111111') @@ -113,26 +123,14 @@ public function extract($start, $length) { $endCount = $currentStrPos - $startCount; } - $doFrame = true; $str = substr($this->str, $currentStrPos, 4); $strlen = strlen($str); $parts = array(); for($i = 0; $i < $strlen; $i++) { - $parts[] = $this->decbinFill(ord($str[$i]), 8); - } - if($parts[0] != '11111111') - { - if(($maxStrLen-128) > $currentStrPos) - { - $doFrame = false; - } - else - { - $doFrame = false; - } + $parts[] = $this->binaryTable[$str[$i]]; } - if($doFrame) + if($parts[0] == '11111111') { $a = $this->doFrameStuff($parts); $currentStrPos += $a[0]; @@ -156,20 +154,6 @@ public function extract($start, $length) return $mp3; } - private function decbinFill($dec, $length = 0) - { - $str = decbin($dec); - $nulls = $length - strlen($str); - if($nulls > 0) - { - for($i = 0; $i < $nulls; $i++) - { - $str = '0' . $str; - } - } - return $str; - } - private function doFrameStuff($parts) { //Get Audio Version @@ -289,7 +273,6 @@ private function doFrameStuff($parts) $padding = substr($parts[2], 6, 1); if($layer == 3 || $layer == 2) { - //FrameLengthInBytes = 144 * BitRate / SampleRate + Padding // TODO: remove this line $frameLength = 144 * $bitRate * 1000 / $freq + $padding; } $frameLength = floor($frameLength); @@ -531,12 +514,13 @@ public function striptags() public function save($path) { $fp = fopen($path, 'w'); - fwrite($fp, $this->str); + $bytesWritten = fwrite($fp, $this->str); fclose($fp); + return $bytesWritten == strlen($this->str); } //join various MP3s - public function multiJoin($newpath,$array) + public function multiJoin($newpath, $array) { foreach ($array as $path) {