protected function calc()
    {
        if (!file_exists($this->track)) throw new \Exception("File '{$this->track}' doesn't exist");

        $length = $this->track->getLength();
        $rate = null;
        $sample_count = $this->track->getSampleCount();

        $trim = null;
        if ($this->offset || $this->duration) {
            $offset = $this->offset >= 0 ? $this->offset : $length + $this->offset;
            $trim = $offset . ($this->duration ? " " . (float)$this->duration : '');
            
            $newlength = $this->duration ?: $length - $offset;
            $sample_count = floor(($newlength / $length) * $sample_count);
            $length = $newlength;
        }
        
        // Downsample to max 500 samples per pixel with a minimum sample rate of 4k/s
        if ($sample_count / $this->width > 500) {
            $rate = max(($this->width / $length) * 500, 4000);
            $sample_count = $rate * $length;
        }
        
        $this->length = $length;
        
        $this->samples = $this->calcExecute($sample_count, $trim, $rate);
        
        if (!isset($this->level)) {
            $this->level = max(-1 * min($this->samples), max($this->samples));
        }
    }