Completed
Push — master ( be78bc...e1eb62 )
by Patrick
02:17
created

Cent::centsToFrequency()   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 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Cent
8
{
9
    /**
10
     * Returns the number of cents between two frequencies.
11
     *
12
     * @param float $lower The lower frequency (in Hz).
13
     * @param float $upper The upper frequency (in Hz).
14
     *
15
     * @return float
16
     */
17
    public static function frequenciesToCents(float $lower, float $upper): float
18
    {
19
        return Math::isZero($lower) ? 0 : 1200 * (log($upper / $lower) / log(2));
20
    }
21
22
    /**
23
     * Converts a frequency to a number of cents above C4.
24
     *
25
     * @param float $frequency The frequency to convert.
26
     * @param float $A4        The reference frequency of A4 (defaults to 440).
27
     *
28
     * @return float
29
     */
30
    public static function frequencyToCents(float $frequency, float $A4 = 440.0): float
31
    {
32
        return self::frequenciesToCents($A4, $frequency) + 900;
33
    }
34
35
    /**
36
     * Calculates the frequency of a number of cents above a base frequency.
37
     *
38
     * @param float $cents A number of cents.
39
     * @param float $base  The base frequency (in Hz).
40
     *
41
     * @return float The calculated frequency (in Hz).
42
     */
43
    public static function centsToFrequency(float $cents, float $base): float
44
    {
45
        return $base * pow(2, $cents / 1200);
46
    }
47
}
48