Stop::getStringLength()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Stop
8
{
9
    private $stringLength;
10
11
    /**
12
     * Stop constructor.
13
     *
14
     * @param float $stringLength A "string length": the length of string
15
     *                            between the stop and the bridge, as a fraction
16
     *                            of the whole string. A string length of 1.0
17
     *                            indicates an open string.
18
     */
19
    public function __construct(float $stringLength)
20
    {
21
        if ($stringLength <= 0 || $stringLength > 1) {
22
            throw new \InvalidArgumentException(sprintf('Invalid string length: %f', $stringLength));
23
        }
24
25
        $this->stringLength = $stringLength;
26
    }
27
28
    /**
29
     * Create a Stop instance from a frequency over a string.
30
     *
31
     * @param float                                             $frequency
32
     * @param \ExtendedStrings\Strings\VibratingStringInterface $string
33
     *
34
     * @return \ExtendedStrings\Strings\Stop
35
     */
36
    public static function fromFrequency(float $frequency, VibratingStringInterface $string): self
37
    {
38
        if (Math::isZero($frequency)) {
39
            throw new \InvalidArgumentException(sprintf('Invalid frequency: %f', $frequency));
40
        }
41
        $centsOverString = Cent::frequenciesToCents($string->getFrequency(), $frequency);
42
43
        return new self(self::centsToStringLength($centsOverString));
44
    }
45
46
    /**
47
     * Returns the frequency of the stop (assuming normal stop pressure).
48
     *
49
     * @param VibratingStringInterface $string
50
     *
51
     * @return float
52
     */
53
    public function getFrequency(VibratingStringInterface $string): float
54
    {
55
        $centsOverString = Cent::frequenciesToCents($this->stringLength, 1);
56
57
        return Cent::centsToFrequency($centsOverString, $string->getFrequency());
58
    }
59
60
    /**
61
     * Returns the string length for the stop.
62
     *
63
     * @see Stop::__construct() for a definition of "string length"
64
     *
65
     * @return float
66
     */
67
    public function getStringLength(): float
68
    {
69
        return $this->stringLength;
70
    }
71
72
    /**
73
     * Convert a number of cents over a string to a string length.
74
     *
75
     * @see Stop::__construct() for a definition of "string length"
76
     *
77
     * @param float $cents The number of cents between the open string's pitch
78
     *                     and the stopped pitch.
79
     *
80
     * @return float
81
     */
82
    private static function centsToStringLength(float $cents): float
83
    {
84
        return 1 / pow(2, $cents / 1200);
85
    }
86
}
87