Completed
Push — master ( 0da1f0...b17898 )
by Patrick
01:56
created

VibratingString::centsToStringLength()   A

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 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ExtendedStrings\Harmonics;
6
7
class VibratingString
8
{
9
    private $frequency;
10
11
    /**
12
     * @param float $frequency
13
     */
14
    public function __construct(float $frequency)
15
    {
16
        $this->frequency = $frequency;
17
    }
18
19
    /**
20
     * @param float $stringLength
21
     *
22
     * @return float
23
     */
24 View Code Duplication
    public function getStoppedFrequency(float $stringLength = 1.0): float
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        if (Math::isZero($stringLength)) {
27
            return 0;
28
        }
29
        $centsOverString = Cent::frequenciesToCents($stringLength, 1);
30
31
        return Cent::centsToFrequency($centsOverString, $this->frequency);
32
    }
33
34
    /**
35
     * @param float $stringLength
36
     *
37
     * @return float
38
     */
39
    public function getHarmonicSoundingFrequency(float $stringLength = 1.0): float
40
    {
41
        return Math::isZero($stringLength)
42
            ? 0
43
            : $this->frequency * self::getHarmonicNumber($stringLength);
44
    }
45
46
    /**
47
     * @param float $frequency
48
     *
49
     * @return float
50
     */
51 View Code Duplication
    public function getStringLength(float $frequency): float
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (Math::isZero($frequency)) {
54
            return 0;
55
        }
56
        $centsOverString = Cent::frequenciesToCents($this->frequency, $frequency);
57
58
        return $this->centsToStringLength($centsOverString);
59
    }
60
61
    /**
62
     * @param float $cents
63
     *   The number of cents between the open string and the stop.
64
     *
65
     * @return float
66
     *   The length of the remaining vibrating string.
67
     */
68
    private function centsToStringLength(float $cents): float
69
    {
70
        return 1 / pow(2, $cents / 1200);
71
    }
72
73
    /**
74
     * @param float $stringLength
75
     *
76
     * @throws \InvalidArgumentException
77
     *
78
     * @return int
79
     */
80
    public static function getHarmonicNumber(float $stringLength): int
81
    {
82
        $number = intval(1 / Math::gcd(1, $stringLength));
83
        if ($number > 100) {
84
            throw new \InvalidArgumentException(sprintf('Invalid string length for a harmonic: %f', $stringLength));
85
        }
86
87
        return $number;
88
    }
89
90
    /**
91
     * @param int $limit
92
     *
93
     * @return float[]
94
     */
95
    public static function getHarmonicSeries(int $limit): array
96
    {
97
        $series = [];
98
        $base = 0;
99
        for ($denominator = 1; $denominator <= $limit; $denominator++) {
100
            $base = $series[$denominator] = $base + 1 / $denominator;
101
        }
102
103
        return $series;
104
    }
105
}
106