Skip to content

Commit 8a87e52

Browse files
committed
Added one-phase functionality for getting only positive waveform data and images (#4)
1 parent d409eab commit 8a87e52

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

Waveform.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*
44
*
55
* @author MaximAL
6+
* @since 2019-02-13 Added `$onePhase` parameters to get only positive waveform data and image
67
* @since 2018-10-22 Added `getWaveformData()` method and `$soxCommand` configuration
78
* @since 2016-11-21
89
* @date 2016-11-21
910
* @time 19:08
1011
* @link http://maximals.ru
1112
* @link http://sijeko.ru
1213
* @link https://github.com/maximal/audio-waveform-php
13-
* @copyright © MaximAL, Sijeko 2016
14+
* @copyright © MaximAL, Sijeko 2016-2019
1415
*/
1516

1617
namespace maximal\audio;
@@ -104,11 +105,11 @@ public function getDuration()
104105
return $this->duration;
105106
}
106107

107-
public function getWaveform($filename, $width, $height)
108+
public function getWaveform($filename, $width, $height, $onePhase = false)
108109
{
109110
// Calculating parameters
110111
$needChannels = $this->getChannels() > 1 ? 2 : 1;
111-
$data = $this->getWaveformData($width);
112+
$data = $this->getWaveformData($width, $onePhase);
112113
$lines1 = $data['lines1'];
113114
$lines2 = $data['lines2'];
114115

@@ -126,24 +127,37 @@ public function getWaveform($filename, $width, $height)
126127
imagefill($img, 0, 0, $back);
127128

128129
// Center Ys
129-
$center1 = $needChannels === 2 ? ($height / 2 - 1) / 2 : $height / 2;
130-
$center2 = $needChannels === 2 ? $height - $center1 : null;
130+
if ($onePhase) {
131+
$center1 = $needChannels === 2 ? $height / 2 - 1: $height - 1;
132+
$center2 = $needChannels === 2 ? $height - 1 : null;
133+
} else {
134+
$center1 = $needChannels === 2 ? ($height / 2 - 1) / 2 : $height / 2;
135+
$center2 = $needChannels === 2 ? $height - $center1 : null;
136+
}
131137

132138
// Drawing channel 1
133139
for ($i = 0; $i < count($lines1); $i += 2) {
134-
$min = $lines1[$i];
135-
$max = $lines1[$i+1];
136140
$x = $i / 2 / self::$linesPerPixel;
137-
138-
imageline($img, $x, $center1 - $min * $center1, $x, $center1 - $max * $center1, $color);
141+
if ($onePhase) {
142+
$max = max($lines1[$i], $lines1[$i + 1]);
143+
imageline($img, $x, $center1, $x, $center1 - $max * $center1, $color);
144+
} else {
145+
$min = $lines1[$i];
146+
$max = $lines1[$i + 1];
147+
imageline($img, $x, $center1 - $min * $center1, $x, $center1 - $max * $center1, $color);
148+
}
139149
}
140150
// Drawing channel 2
141151
for ($i = 0; $i < count($lines2); $i += 2) {
142-
$min = $lines2[$i];
143-
$max = $lines2[$i + 1];
144152
$x = $i / 2 / self::$linesPerPixel;
145-
146-
imageline($img, $x, $center2 - $min * $center1, $x, $center2 - $max * $center1, $color);
153+
if ($onePhase) {
154+
$max = max($lines2[$i], $lines2[$i + 1]);
155+
imageline($img, $x, $center2, $x, $center2 - $max * $center1, $color);
156+
} else {
157+
$min = $lines2[$i];
158+
$max = $lines2[$i + 1];
159+
imageline($img, $x, $center2 - $min * $center1, $x, $center2 - $max * $center1, $color);
160+
}
147161
}
148162

149163
// Axis
@@ -155,7 +169,7 @@ public function getWaveform($filename, $width, $height)
155169
return imagepng($img, $filename);
156170
}
157171

158-
public function getWaveformData($width)
172+
public function getWaveformData($width, $onePhase = false)
159173
{
160174
// Calculating parameters
161175
$needChannels = $this->getChannels() > 1 ? 2 : 1;
@@ -196,11 +210,22 @@ public function getWaveformData($width)
196210
$channel1 []= $sample;
197211
}
198212
}
199-
$lines1 []= min($channel1);
200-
$lines1 []= max($channel1);
201-
if ($needChannels === 2) {
202-
$lines2 []= min($channel2);
203-
$lines2 []= max($channel2);
213+
if ($onePhase) {
214+
// Rectifying to get positive values only
215+
$lines1 []= abs(min($channel1));
216+
$lines1 []= abs(max($channel1));
217+
if ($needChannels === 2) {
218+
$lines2 []= abs(min($channel2));
219+
$lines2 []= abs(max($channel2));
220+
}
221+
} else {
222+
// Two phases
223+
$lines1 []= min($channel1);
224+
$lines1 []= max($channel1);
225+
if ($needChannels === 2) {
226+
$lines2 []= min($channel2);
227+
$lines2 []= max($channel2);
228+
}
204229
}
205230
}
206231

thumbnailer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
echo 'Error: audio and thumbnail files not specified!', PHP_EOL;
3434
echo 'Usage: php ', $argv[0],
3535
' <audio file> <thumbnail file> [<thumbnail size in pixels, default is ',
36-
$width, '×', $height, '>]',
36+
$width, '×', $height, '>] [two-phase|positive, default is `two-phase`]',
3737
PHP_EOL;
3838
exit(1);
3939
}
@@ -57,6 +57,14 @@
5757
}
5858
}
5959

60+
// Parsing phase setting
61+
$onePhase = false;
62+
if (count($argv) > 4) {
63+
if (strtolower($argv[4]) === 'positive') {
64+
$onePhase = true;
65+
}
66+
}
67+
6068
if (!is_file($filename)) {
6169
echo 'Error: file «', $filename, '» not found!', PHP_EOL;
6270
exit(3);
@@ -67,7 +75,7 @@
6775
// Some settings
6876
//Waveform::$color = [255, 0, 0, 0.5];
6977
//Waveform::$backgroundColor = [0, 0, 0, 0];
70-
if (!$waveform->getWaveform($thumbnail, $width, $height)) {
78+
if (!$waveform->getWaveform($thumbnail, $width, $height, $onePhase)) {
7179
exit(4);
7280
}
7381

0 commit comments

Comments
 (0)