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
1617namespace 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
0 commit comments