Cent   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A frequenciesToCents() 0 4 2
A frequencyToCents() 0 4 1
A centsToFrequency() 0 4 1
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