1 | <?php |
||
7 | class HarmonicCalculator |
||
8 | { |
||
9 | private $minDistance = 1.0; |
||
10 | private $maxDistance = 120.0; |
||
11 | private $minBowedDistance = 20.0; |
||
12 | |||
13 | /** |
||
14 | * Returns a list of possible harmonics that produce a given sounding note. |
||
15 | * |
||
16 | * @param Note $soundingNote The desired sounding note of |
||
17 | * the harmonic. |
||
18 | * @param InstrumentInterface $instrument The instrument. |
||
19 | * @param float $tolerance The maximum deviation (cents) |
||
20 | * between the desired sounding |
||
21 | * note and a natural harmonic. |
||
22 | * |
||
23 | * @return Harmonic[] |
||
24 | */ |
||
25 | public function findHarmonics(Note $soundingNote, InstrumentInterface $instrument, float $tolerance = 50.0): array |
||
26 | { |
||
27 | $harmonics = []; |
||
28 | foreach ($instrument->getStrings() as $string) { |
||
29 | $harmonics = array_merge( |
||
30 | $harmonics, |
||
31 | $this->findNaturalHarmonics($soundingNote, $string, $tolerance), |
||
32 | $this->findArtificialHarmonics($soundingNote, $string) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | $harmonics = array_filter($harmonics, function (Harmonic $harmonic) { |
||
37 | return $this->validateDistance($harmonic); |
||
38 | }); |
||
39 | |||
40 | return $harmonics; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Set the minimum and maximum distance between harmonic stops (in mm). |
||
45 | * |
||
46 | * @param float $minDistance |
||
47 | * @param float $maxDistance |
||
48 | * @param float $minBowedDistance |
||
49 | */ |
||
50 | public function setDistanceConstraints(float $minDistance, float $maxDistance, float $minBowedDistance) |
||
51 | { |
||
52 | $this->minDistance = $minDistance; |
||
53 | $this->maxDistance = $maxDistance; |
||
54 | $this->minBowedDistance = $minBowedDistance; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Check that the harmonic is within the configured distance constraints. |
||
59 | * |
||
60 | * @see HarmonicCalculator::setDistanceConstraints() |
||
61 | * |
||
62 | * @param \ExtendedStrings\Strings\Harmonic $harmonic |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function validateDistance(Harmonic $harmonic): bool |
||
67 | { |
||
68 | $physicalLength = $this->getPhysicalStringLength($harmonic); |
||
69 | $basePhysical = $harmonic->getBaseStop()->getStringLength() * $physicalLength; |
||
70 | $halfStopPhysical = $harmonic->getHalfStop()->getStringLength() * $physicalLength; |
||
71 | $distance = $basePhysical - $halfStopPhysical; |
||
72 | $bowedDistance = $halfStopPhysical; |
||
73 | |||
74 | return ( |
||
75 | $harmonic->isNatural() |
||
76 | || ($distance >= $this->minDistance && $distance <= $this->maxDistance) |
||
77 | ) && $bowedDistance >= $this->minBowedDistance; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Find the physical length of the harmonic's string. |
||
82 | */ |
||
83 | private function getPhysicalStringLength(Harmonic $harmonic): float |
||
84 | { |
||
85 | $string = $harmonic->getString(); |
||
86 | |||
87 | return $string instanceof InstrumentStringInterface |
||
88 | ? $string->getPhysicalLength() |
||
89 | : 500.0; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Find the artificial harmonics that produce the given sounding note. |
||
94 | * |
||
95 | * @param \ExtendedStrings\Strings\Note $soundingNote |
||
96 | * @param \ExtendedStrings\Strings\VibratingStringInterface $string |
||
97 | * |
||
98 | * @return Harmonic[] |
||
99 | */ |
||
100 | private function findArtificialHarmonics(Note $soundingNote, VibratingStringInterface $string): array |
||
118 | |||
119 | /** |
||
120 | * Find the natural harmonics that produce the given sounding note. |
||
121 | * |
||
122 | * @param \ExtendedStrings\Strings\Note $soundingNote |
||
123 | * @param \ExtendedStrings\Strings\InstrumentStringInterface $string |
||
124 | * @param float $tolerance |
||
125 | * |
||
126 | * @return Harmonic[] |
||
127 | */ |
||
128 | private function findNaturalHarmonics(Note $soundingNote, InstrumentStringInterface $string, float $tolerance = 50.0): array |
||
150 | } |
||
151 |