|
| 1 | +#include "WaveformAligner.hpp" |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <iterator> |
| 5 | + |
| 6 | +namespace libprojectM { |
| 7 | +namespace Audio { |
| 8 | + |
| 9 | +WaveformAligner::WaveformAligner() |
| 10 | +{ |
| 11 | + static const size_t maxOctaves{10}; |
| 12 | + static const size_t numOctaves{static_cast<size_t>(std::floor(std::log(AudioBufferSamples - WaveformSamples) / std::log(2.0f)))}; |
| 13 | + m_octaves = numOctaves > maxOctaves ? maxOctaves : numOctaves; |
| 14 | + |
| 15 | + m_aligmentWeights.resize(m_octaves); |
| 16 | + m_firstNonzeroWeights.resize(m_octaves); |
| 17 | + m_lastNonzeroWeights.resize(m_octaves); |
| 18 | + m_octaveSamples.resize(m_octaves); |
| 19 | + m_octaveSampleSpacing.resize(m_octaves); |
| 20 | + m_oldWaveformMips.resize(m_octaves); |
| 21 | + |
| 22 | + m_octaveSamples[0] = AudioBufferSamples; |
| 23 | + m_octaveSampleSpacing[0] = AudioBufferSamples - WaveformSamples; |
| 24 | + for (size_t octave = 1; octave < m_octaves; octave++) |
| 25 | + { |
| 26 | + m_octaveSamples[octave] = m_octaveSamples[octave - 1] / 2; |
| 27 | + m_octaveSampleSpacing[octave] = m_octaveSampleSpacing[octave - 1] / 2; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void WaveformAligner::Align(WaveformBuffer& newWaveform) |
| 32 | +{ |
| 33 | + if (m_octaves < 4) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + int alignOffset{}; |
| 39 | + |
| 40 | + std::vector<WaveformBuffer> newWaveformMips(m_octaves, WaveformBuffer()); |
| 41 | + std::copy(newWaveform.begin(), newWaveform.end(), newWaveformMips[0].begin()); |
| 42 | + |
| 43 | + // Calculate mip levels |
| 44 | + for (size_t octave = 1; octave < m_octaves; octave++) |
| 45 | + { |
| 46 | + for (size_t sample = 0; sample < m_octaveSamples[octave]; sample++) |
| 47 | + { |
| 48 | + newWaveformMips[octave][sample] = 0.5f * (newWaveformMips[octave - 1][sample * 2] + newWaveformMips[octave - 1][sample * 2 + 1]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (!m_alignWaveReady) |
| 53 | + { |
| 54 | + m_alignWaveReady = true; |
| 55 | + for (size_t octave = 0; octave < m_octaves; octave++) |
| 56 | + { |
| 57 | + // For example: |
| 58 | + // m_octaveSampleSpacing[octave] == 4 |
| 59 | + // m_octaveSamples[octave] == 36 |
| 60 | + // (so we test 32 samples, w/4 offsets) |
| 61 | + size_t const compareSamples = m_octaveSamples[octave] - m_octaveSampleSpacing[octave]; |
| 62 | + |
| 63 | + for (size_t sample = 0; sample < compareSamples; sample++) |
| 64 | + { |
| 65 | + auto& tempVal = m_aligmentWeights[octave][sample]; |
| 66 | + |
| 67 | + // Start with pyramid-shaped PDF, from 0..1..0 |
| 68 | + if (sample < compareSamples / 2) |
| 69 | + { |
| 70 | + tempVal = static_cast<float>(sample * 2) / static_cast<float>(compareSamples); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + tempVal = static_cast<float>((compareSamples - 1 - sample) * 2) / static_cast<float>(compareSamples); |
| 75 | + } |
| 76 | + |
| 77 | + // TWEAK how much the center matters, vs. the edges: |
| 78 | + tempVal = (tempVal - 0.8f) * 5.0f + 0.8f; |
| 79 | + |
| 80 | + // Clip |
| 81 | + if (tempVal > 1.0f) |
| 82 | + { |
| 83 | + tempVal = 1.0f; |
| 84 | + } |
| 85 | + if (tempVal < 0.0f) |
| 86 | + { |
| 87 | + tempVal = 0.0f; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + size_t sample{}; |
| 92 | + while (m_aligmentWeights[octave][sample] == 0 && sample < compareSamples) |
| 93 | + { |
| 94 | + sample++; |
| 95 | + } |
| 96 | + m_firstNonzeroWeights[octave] = sample; |
| 97 | + |
| 98 | + sample = compareSamples - 1; |
| 99 | + while (m_aligmentWeights[octave][sample] == 0 && sample >= 0) |
| 100 | + { |
| 101 | + sample--; |
| 102 | + } |
| 103 | + m_lastNonzeroWeights[octave] = sample; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + int sample1{}; |
| 108 | + int sample2{static_cast<int>(m_octaveSampleSpacing[m_octaves - 1])}; |
| 109 | + |
| 110 | + // Find best match for alignment |
| 111 | + for (int octave = static_cast<int>(m_octaves) - 1; octave >= 0; octave--) |
| 112 | + { |
| 113 | + int lowestErrorOffset{-1}; |
| 114 | + float lowestErrorAmount{}; |
| 115 | + |
| 116 | + for (int sample = sample1; sample < sample2; sample++) |
| 117 | + { |
| 118 | + float errorSum{}; |
| 119 | + |
| 120 | + for (size_t i = m_firstNonzeroWeights[octave]; i <= m_lastNonzeroWeights[octave]; i++) |
| 121 | + { |
| 122 | + errorSum += std::abs((newWaveformMips[octave][i + sample] - m_oldWaveformMips[octave][i + sample]) * m_aligmentWeights[octave][i]); |
| 123 | + } |
| 124 | + |
| 125 | + if (lowestErrorOffset == -1 || errorSum < lowestErrorAmount) |
| 126 | + { |
| 127 | + lowestErrorOffset = static_cast<int>(sample); |
| 128 | + lowestErrorAmount = errorSum; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Now use 'lowestErrorOffset' to guide bounds of search in next octave: |
| 133 | + // m_octaveSampleSpacing[octave] == 8 |
| 134 | + // m_octaveSamples[octave] == 72 |
| 135 | + // -say 'lowestErrorOffset' was 2 |
| 136 | + // -that corresponds to samples 4 & 5 of the next octave |
| 137 | + // -also, expand about this by 2 samples? YES. |
| 138 | + // (so we'd test 64 samples, w/8->4 offsets) |
| 139 | + if (octave > 0) |
| 140 | + { |
| 141 | + sample1 = lowestErrorOffset * 2 - 1; |
| 142 | + sample2 = lowestErrorOffset * 2 + 2 + 1; |
| 143 | + if (sample1 < 0) |
| 144 | + { |
| 145 | + sample1 = 0; |
| 146 | + } |
| 147 | + if (sample2 > static_cast<int>(m_octaveSampleSpacing[octave - 1])) |
| 148 | + { |
| 149 | + sample2 = static_cast<int>(m_octaveSampleSpacing[octave - 1]); |
| 150 | + } |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + alignOffset = lowestErrorOffset; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Store mip levels for the next frame. |
| 159 | + m_oldWaveformMips.clear(); |
| 160 | + std::copy(newWaveformMips.begin(), newWaveformMips.end(), std::back_inserter(m_oldWaveformMips)); |
| 161 | + |
| 162 | + // Finally, apply the results by scooting the aligned samples so that they start at index 0. |
| 163 | + if (alignOffset > 0) |
| 164 | + { |
| 165 | + for (size_t sample = 0; sample < WaveformSamples; sample++) |
| 166 | + { |
| 167 | + newWaveform[sample] = newWaveform[sample + alignOffset]; |
| 168 | + } |
| 169 | + |
| 170 | + // Set remaining samples to zero. |
| 171 | + std::fill_n(newWaveform.begin() + WaveformSamples, AudioBufferSamples - WaveformSamples, 0.0f); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +} // namespace Audio |
| 177 | +} // namespace libprojectM |
0 commit comments