Audacity 3.2.0
MirUtils.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 MirUtils.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11#include "MirUtils.h"
12
13#include <cmath>
14#include <numeric>
15
16namespace MIR
17{
18namespace
19{
20constexpr auto bpmStdDev = 29.7953;
21constexpr auto pi = 3.141592653589793;
22
24{
25 while (n % 2 == 0)
26 n /= 2;
27 while (n % 3 == 0)
28 n /= 3;
29 return n == 1;
30}
31
32static_assert(IsPrimeDecompositionTwoThreeOnly(1));
33static_assert(IsPrimeDecompositionTwoThreeOnly(2));
34static_assert(IsPrimeDecompositionTwoThreeOnly(3));
35static_assert(IsPrimeDecompositionTwoThreeOnly(4));
36static_assert(!IsPrimeDecompositionTwoThreeOnly(5));
37static_assert(IsPrimeDecompositionTwoThreeOnly(6));
38static_assert(!IsPrimeDecompositionTwoThreeOnly(7));
39static_assert(IsPrimeDecompositionTwoThreeOnly(8));
40static_assert(IsPrimeDecompositionTwoThreeOnly(9));
41
42// Function to generate numbers whose prime factorization contains only twos or
43// threes
44std::vector<int> GetPowersOf2And3(int lower, int upper)
45{
46 std::vector<int> result;
47 for (int i = lower; i <= upper; ++i)
49 result.push_back(i);
50 return result;
51}
52} // namespace
53
54std::vector<int> GetPossibleBarDivisors(int lower, int upper)
55{
56 auto result = GetPowersOf2And3(lower, upper);
57 // Remove divisors that have more than two triplet levels. E.g. 3/4s are
58 // okay, 3/4s with swung ryhthms too, but beyond that it's probably very rare
59 // (e.g. swung 9/8 ??...)
60 result.erase(
61 std::remove_if(
62 result.begin(), result.end(), [](int n) { return n % 27 == 0; }),
63 result.end());
64 return result;
65}
66
67std::vector<int> GetPeakIndices(const std::vector<float>& x)
68{
69 std::vector<int> peakIndices;
70 for (auto j = 0; j < x.size(); ++j)
71 {
72 const auto i = j == 0 ? x.size() - 1 : j - 1;
73 const auto k = j == x.size() - 1 ? 0 : j + 1;
74 if (x[i] < x[j] && x[j] > x[k])
75 peakIndices.push_back(j);
76 }
77 return peakIndices;
78}
79
80std::vector<float> GetNormalizedHann(int size)
81{
82 std::vector<float> window(size);
83 for (auto n = 0; n < size; ++n)
84 window[n] = .5 * (1 - std::cos(2 * pi * n / size));
85 const auto windowSum = std::accumulate(window.begin(), window.end(), 0.f);
86 std::transform(
87 window.begin(), window.end(), window.begin(),
88 [windowSum](float w) { return w / windowSum; });
89 return window;
90}
91} // namespace MIR
constexpr bool IsPrimeDecompositionTwoThreeOnly(int n)
Definition: MirUtils.cpp:23
std::vector< int > GetPowersOf2And3(int lower, int upper)
Definition: MirUtils.cpp:44
std::vector< int > GetPossibleBarDivisors(int lower, int upper)
Function to generate numbers whose prime factorization contains only twos or threes.
Definition: MirUtils.cpp:54
std::vector< float > GetNormalizedHann(int size)
Definition: MirUtils.cpp:80
std::vector< int > GetPeakIndices(const std::vector< float > &x)
Definition: MirUtils.cpp:67