|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * |
| 5 | + * @author MaximAL |
| 6 | + * @since 2016-11-21 |
| 7 | + * @date 2016-11-21 |
| 8 | + * @time 19:08 |
| 9 | + * @link http://maximals.ru |
| 10 | + * @link http://sijeko.ru |
| 11 | + * @link https://github.com/maximal/audio-waveform-php |
| 12 | + * @copyright © MaximAL, Sijeko 2016 |
| 13 | + */ |
| 14 | + |
| 15 | +namespace maximal\audio; |
| 16 | + |
| 17 | +class Waveform |
| 18 | +{ |
| 19 | + protected $filename; |
| 20 | + protected $info; |
| 21 | + protected $channels; |
| 22 | + protected $samples; |
| 23 | + protected $sampleRate; |
| 24 | + protected $duration; |
| 25 | + |
| 26 | + public static $linesPerPixel = 8; |
| 27 | + public static $samplesPerLine = 512; |
| 28 | + |
| 29 | + // Colors in CSS `rgba(red, green, blue, opacity)` format |
| 30 | + public static $color = [95, 95, 95, 0.5]; |
| 31 | + public static $backgroundColor = [245, 245, 245, 1]; |
| 32 | + public static $axisColor = [0, 0, 0, 0.1]; |
| 33 | + |
| 34 | + |
| 35 | + public function __construct($filename) |
| 36 | + { |
| 37 | + $this->filename = $filename; |
| 38 | + } |
| 39 | + |
| 40 | + public function getInfo() |
| 41 | + { |
| 42 | + $out = null; |
| 43 | + $ret = null; |
| 44 | + exec('sox --i ' . escapeshellarg($this->filename) . ' 2>&1', $out, $ret); |
| 45 | + $str = implode('|', $out); |
| 46 | + |
| 47 | + $match = null; |
| 48 | + if (preg_match('/Channels?\s*\:\s*(\d+)/ui', $str, $match)) { |
| 49 | + $this->channels = intval($match[1]); |
| 50 | + } |
| 51 | + |
| 52 | + $match = null; |
| 53 | + if (preg_match('/Sample\s*Rate\s*\:\s*(\d+)/ui', $str, $match)) { |
| 54 | + $this->sampleRate = intval($match[1]); |
| 55 | + } |
| 56 | + |
| 57 | + $match = null; |
| 58 | + if (preg_match('/Duration.*[^\d](\d+)\s*samples?/ui', $str, $match)) { |
| 59 | + $this->samples = intval($match[1]); |
| 60 | + } |
| 61 | + |
| 62 | + if ($this->samples && $this->sampleRate) { |
| 63 | + $this->duration = 1.0 * $this->samples / $this->samples; |
| 64 | + } |
| 65 | + |
| 66 | + if ($ret !== 0) { |
| 67 | + throw new \Exception('Failed to get audio info.' . PHP_EOL . 'Error: ' . implode(PHP_EOL, $out) . PHP_EOL); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function getSampleRate() |
| 72 | + { |
| 73 | + if (!$this->sampleRate) { |
| 74 | + $this->getInfo(); |
| 75 | + } |
| 76 | + return $this->sampleRate; |
| 77 | + } |
| 78 | + |
| 79 | + public function getChannels() |
| 80 | + { |
| 81 | + if (!$this->channels) { |
| 82 | + $this->getInfo(); |
| 83 | + } |
| 84 | + return $this->channels; |
| 85 | + } |
| 86 | + |
| 87 | + public function getSamples() |
| 88 | + { |
| 89 | + if (!$this->samples) { |
| 90 | + $this->getInfo(); |
| 91 | + } |
| 92 | + return $this->samples; |
| 93 | + } |
| 94 | + |
| 95 | + public function getDuration() |
| 96 | + { |
| 97 | + if (!$this->duration) { |
| 98 | + $this->getInfo(); |
| 99 | + } |
| 100 | + return $this->duration; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public function getWaveform($filename, $width, $height) |
| 105 | + { |
| 106 | + // Calculating parameters |
| 107 | + $needChannels = $this->getChannels() > 1 ? 2 : 1; |
| 108 | + $samplesPerPixel = self::$samplesPerLine * self::$linesPerPixel; |
| 109 | + $needRate = 1.0 * $width * $samplesPerPixel * $this->getSampleRate() / $this->getSamples(); |
| 110 | + |
| 111 | + //if ($needRate > 4000) { |
| 112 | + // $needRate = 4000; |
| 113 | + //} |
| 114 | + |
| 115 | + // Command text |
| 116 | + $command = 'sox ' . escapeshellarg($this->filename) . |
| 117 | + ' -c ' . $needChannels . |
| 118 | + ' -r ' . $needRate . ' -e floating-point -t raw -'; |
| 119 | + |
| 120 | + //var_dump($command); |
| 121 | + |
| 122 | + $outputs = [ |
| 123 | + 1 => ['pipe', 'w'], // stdout |
| 124 | + 2 => ['pipe', 'w'], // stderr |
| 125 | + ]; |
| 126 | + $pipes = null; |
| 127 | + $proc = proc_open($command, $outputs, $pipes); |
| 128 | + if (!$proc) { |
| 129 | + throw new \Exception('Failed to run sox command'); |
| 130 | + } |
| 131 | + |
| 132 | + $lines1 = []; |
| 133 | + $lines2 = []; |
| 134 | + while ($chunk = fread($pipes[1], 4 * $needChannels * self::$samplesPerLine)) { |
| 135 | + $data = unpack('f*', $chunk); |
| 136 | + $channel1 = []; |
| 137 | + $channel2 = []; |
| 138 | + foreach ($data as $index => $sample) { |
| 139 | + if ($needChannels === 2 && $index % 2 === 0) { |
| 140 | + $channel2 []= $sample; |
| 141 | + } else { |
| 142 | + $channel1 []= $sample; |
| 143 | + } |
| 144 | + } |
| 145 | + $lines1 []= min($channel1); |
| 146 | + $lines1 []= max($channel1); |
| 147 | + if ($needChannels === 2) { |
| 148 | + $lines2 []= min($channel2); |
| 149 | + $lines2 []= max($channel2); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + $err = stream_get_contents($pipes[2]); |
| 154 | + $ret = proc_close($proc); |
| 155 | + |
| 156 | + if ($ret !== 0) { |
| 157 | + throw new \Exception('Failed to run `sox` command. Error:' . PHP_EOL . $err); |
| 158 | + } |
| 159 | + |
| 160 | + // Creating image |
| 161 | + $img = imagecreatetruecolor($width, $height); |
| 162 | + imagesavealpha($img, true); |
| 163 | + //if (function_exists('imageantialias')) { |
| 164 | + // imageantialias($img, true); |
| 165 | + //} |
| 166 | + |
| 167 | + // Colors |
| 168 | + $back = self::rgbaToColor($img, self::$backgroundColor); |
| 169 | + $color = self::rgbaToColor($img, self::$color); |
| 170 | + $axis = self::rgbaToColor($img, self::$axisColor); |
| 171 | + imagefill($img, 0, 0, $back); |
| 172 | + |
| 173 | + // Center Ys |
| 174 | + $center1 = $needChannels === 2 ? ($height / 2 - 1) / 2 : $height / 2; |
| 175 | + $center2 = $needChannels === 2 ? $height - $center1 : null; |
| 176 | + |
| 177 | + // Drawing channel 1 |
| 178 | + for ($i = 0; $i < count($lines1); $i += 2) { |
| 179 | + $min = $lines1[$i]; |
| 180 | + $max = $lines1[$i+1]; |
| 181 | + $x = $i / 2 / self::$linesPerPixel; |
| 182 | + |
| 183 | + imageline($img, $x, $center1 - $min * $center1, $x, $center1 - $max * $center1, $color); |
| 184 | + } |
| 185 | + // Drawing channel 2 |
| 186 | + for ($i = 0; $i < count($lines2); $i += 2) { |
| 187 | + $min = $lines2[$i]; |
| 188 | + $max = $lines2[$i + 1]; |
| 189 | + $x = $i / 2 / self::$linesPerPixel; |
| 190 | + |
| 191 | + imageline($img, $x, $center2 - $min * $center1, $x, $center2 - $max * $center1, $color); |
| 192 | + } |
| 193 | + |
| 194 | + // Axis |
| 195 | + imageline($img, 0, $center1, $width - 1, $center1, $axis); |
| 196 | + if ($center2 !== null) { |
| 197 | + imageline($img, 0, $center2, $width - 1, $center2, $axis); |
| 198 | + } |
| 199 | + |
| 200 | + return imagepng($img, $filename); |
| 201 | + } |
| 202 | + |
| 203 | + public static function rgbaToColor($img, $rgba) |
| 204 | + { |
| 205 | + return imagecolorallocatealpha($img, $rgba[0], $rgba[1], $rgba[2], round((1 - $rgba[3]) * 127)); |
| 206 | + } |
| 207 | +} |
0 commit comments