duration(); $n = (int)round(self::SLICE * $wav->rate); $loud = fn (float $at): bool => self::rms($wav->samples($at, $n)) >= self::QUIET; $start = 0.0; for ($t = 0.0; $t < min(self::LOOK, $length); $t += self::SLICE) { if ($loud($t)) { $start = max(0.0, $t - self::SLICE); break; } } $end = 0.0; for ($t = $length - self::SLICE; $t > max(0.0, $length - self::LOOK); $t -= self::SLICE) { if ($loud($t)) { $end = $t + 2 * self::SLICE; break; } } // a skip worth it only: half a second of silence at least return [$start >= 0.5 ? round($start, 2) : 0.0, $end > 0 && $length - $end >= 0.5 ? round($end, 2) : 0.0]; } /** @param list $samples */ private static function rms(array $samples): float { if ($samples === []) { return 0.0; } $sum = 0.0; foreach ($samples as $s) { $sum += $s * $s; } return sqrt($sum / count($samples)); } }