Completed
Push — master ( 7edb5f...7211b3 )
by Patrick
02:04
created

Harmonic::getSoundingPitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Harmonic
8
{
9
    private $halfStop;
10
    private $baseStop;
11
12
    /**
13
     * @param float $halfStop
14
     * @param float $baseStop
15
     */
16
    public function __construct(float $halfStop, float $baseStop = 1.0)
17
    {
18
        if ($halfStop > $baseStop) {
19
            throw new \InvalidArgumentException("The half-stop's string length cannot be longer than the base stop's.");
20
        }
21
22
        $this->baseStop = $baseStop;
23
        $this->halfStop = $halfStop;
24
    }
25
26
    /**
27
     * @param \ExtendedStrings\Strings\VibratingString $string
28
     *
29
     * @return float
30
     */
31
    public function getSoundingPitch(VibratingString $string): float
32
    {
33
        // Transpose the half-stop onto the new string length, which was formed
34
        // by the stop.
35
        $pseudoString = new VibratingString($string->getStoppedFrequency($this->baseStop));
36
        $pseudoHalfStop = $this->halfStop / $this->baseStop;
37
38
        return $pseudoString->getHarmonicSoundingFrequency($pseudoHalfStop);
39
    }
40
41
    /**
42
     * @param int $limit
43
     *
44
     * @return float[]
45
     */
46
    public static function getSeries(int $limit): array
47
    {
48
        $series = [];
49
        $base = 0;
50
        for ($denominator = 1; $denominator <= $limit; $denominator++) {
51
            $base = $series[$denominator] = $base + 1 / $denominator;
52
        }
53
54
        return $series;
55
    }
56
}
57