audio-waveform-php icon indicating copy to clipboard operation
audio-waveform-php copied to clipboard

Why create 2 wave forms

Open priteshmahajan1806 opened this issue 5 years ago • 6 comments

I am using this code. This code will create 2 wave form in single image Please check screenshot.

Created : https://prnt.sc/tqkmdp I need only : https://prnt.sc/tqklds

I have passed required params as well but still create 2 images

priteshmahajan1806 avatar Jul 29 '20 13:07 priteshmahajan1806

@priteshmahajan1806, you’re working with the stereo signal, that’s why you have two channels in the generated waveform. Those are left and right channels.

maximal avatar Jan 27 '22 10:01 maximal

Hi, I run in the same problem, and I just modify the getWaveform() method to only have first channel in stereo mode :

--- a/Waveform.php
+++ b/Waveform.php
@@ -119,7 +119,7 @@ class Waveform
      * @return bool Returns `true` on success or `false` on failure.
      * @throws \Exception
      */
-    public function getWaveform($filename, $width, $height, $onePhase = false)
+    public function getWaveform($filename, $width, $height, $onePhase = false, $stereo = true)
     {
         // Calculating parameters
         $needChannels = $this->getChannels() > 1 ? 2 : 1;
@@ -148,8 +148,9 @@ class Waveform
             $center1 = $needChannels === 2 ? ($height / 2 - 1) / 2 : $height / 2;
             $center2 = $needChannels === 2 ? $height - $center1 : null;
         }
-
-        // Drawing channel 1
+        if (!$stereo) {
+            $center1 = $height / 2;
+        }
         for ($i = 0; $i < count($lines1); $i += 2) {
             $x = $i / 2 / self::$linesPerPixel;
             if ($onePhase) {
@@ -161,16 +162,18 @@ class Waveform
                 imageline($img, $x, $center1 - $min * $center1, $x, $center1 - $max * $center1, $color);
             }
         }
-        // Drawing channel 2
-        for ($i = 0; $i < count($lines2); $i += 2) {
-            $x = $i / 2 / self::$linesPerPixel;
-            if ($onePhase) {
-                $max = max($lines2[$i], $lines2[$i + 1]);
-                imageline($img, $x, $center2, $x, $center2 - $max * $center1, $color);
-            } else {
-                $min = $lines2[$i];
-                $max = $lines2[$i + 1];
-                imageline($img, $x, $center2 - $min * $center1, $x, $center2 - $max * $center1, $color);
+        if ($stereo) {
+            // Drawing channel 2
+            for ($i = 0; $i < count($lines2); $i += 2) {
+                $x = $i / 2 / self::$linesPerPixel;
+                if ($onePhase) {
+                    $max = max($lines2[$i], $lines2[$i + 1]);
+                    imageline($img, $x, $center2, $x, $center2 - $max * $center1, $color);
+                } else {
+                    $min = $lines2[$i];
+                    $max = $lines2[$i + 1];
+                    imageline($img, $x, $center2 - $min * $center1, $x, $center2 - $max * $center1, $color);
+                }
             }
         } 

DrMickArtisan avatar Dec 22 '23 09:12 DrMickArtisan

@DrMickArtisan, I think it needs to be configurable, otherwise we’ll break the current intended behaviour.

maximal avatar Dec 22 '23 11:12 maximal

Doesn't the line: public function getWaveform($filename, $width, $height, $onePhase = false, $stereo = true) make it configureable? I literally just started using this and we are actively talking about it.

webdobe avatar Dec 22 '23 22:12 webdobe

The posted patch doesn't account for the 2nd axis line though...

webdobe avatar Dec 22 '23 22:12 webdobe

Added this pull request: https://github.com/maximal/audio-waveform-php/pull/9

webdobe avatar Dec 22 '23 22:12 webdobe