maximal / audio-waveform-php

Audio waveform generator on PHP with SoX
MIT License
52 stars 12 forks source link

Why create 2 wave forms #8

Open priteshmahajan1806 opened 4 years ago

priteshmahajan1806 commented 4 years ago

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

maximal commented 2 years ago

@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.

DrMickArtisan commented 10 months ago

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);
+                }
             }
         } 
maximal commented 10 months ago

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

webdobe commented 10 months ago

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 commented 10 months ago

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

webdobe commented 10 months ago

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