HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_metrics_common.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm> // min()
4#include <utility> // pair
5#include <vector>
6
8
9namespace hart
10{
11
12/// @defgroup Metrics Metrics
13/// @brief Common audio-related metrics
14
15/// @brief Helpers to generate common default channel subsets
17{
18 static std::vector<size_t> allChannels (size_t numChannels)
19 {
20 std::vector<size_t> channelIndices (numChannels);
21
22 for (size_t i = 0; i < channelIndices.size(); ++i)
23 channelIndices[i] = i;
24
25 return channelIndices;
26 }
27
28 static std::vector<std::pair<size_t, size_t>> upperTriangleChannelPairs (size_t numChannels)
29 {
30 std::vector<std::pair<size_t, size_t>> channelPairIndices;
31 channelPairIndices.reserve (numChannels * (numChannels - 1) / 2);
32
33 for (size_t channelA = 0; channelA < numChannels; ++channelA)
34 for (size_t channelB = channelA + 1; channelB < numChannels; ++channelB)
35 channelPairIndices.emplace_back (channelA, channelB);
36
37 return channelPairIndices;
38 }
39
40 static std::vector<std::pair<size_t, size_t>> diagonalChannelPairs (size_t numChannels)
41 {
42 std::vector<std::pair<size_t, size_t>> channelPairIndices;
43 channelPairIndices.reserve (numChannels);
44
45 for (size_t channel = 0; channel < numChannels; ++channel)
46 channelPairIndices.emplace_back (channel, channel);
47
48 return channelPairIndices;
49 }
50};
51
52/// @brief A helper to determine the return type of a reducer function
53/// @ingroup Metrics
54/// @private
55template <typename Reducer, typename Iterator>
56struct ReducerResult
57{
58 typedef decltype (
59 std::declval<Reducer>()(
60 std::declval<Iterator>(),
61 std::declval<Iterator>()
62 )
63 )
64 type;
65};
66
67/// @brief An alias to get the return type of a reducer function in a less verbose post-C++11 manner
68/// @ingroup Metrics
69/// @private
70template <typename Reducer, typename Iterator>
71using ReducerResultType =
72 typename ReducerResult<Reducer, Iterator>::type;
73
74// TODO: Replace this method with std::vector generator(s)
75/// @brief A helper to get an iterable of channel indices to process
76/// @ingroup Metrics
77/// @private
78template <typename SampleType>
79std::vector<size_t> getChannelIndicesToProcess (const AudioBuffer<SampleType>& buffer, std::initializer_list<size_t> channels)
80{
81 if (channels.size() != 0)
82 return std::vector<size_t> (channels.begin(), channels.end());
83
84 std::vector<size_t> indices (buffer.getNumChannels());
85
86 for (size_t i = 0; i < indices.size(); ++i)
87 indices[i] = i;
88
89 return indices;
90}
91
92// TODO: Replace this method with std::vector generator(s)
93/// @brief A helper to get an iterable of channel indices to process for a pair of buffers
94/// @details If buffers' channel count is mismatched, returns the shortest subset
95/// @ingroup Metrics
96/// @private
97template <typename SampleType>
98std::vector<size_t> getChannelIndicesToProcess (const AudioBuffer<SampleType>& bufferA, const AudioBuffer<SampleType>& bufferB, std::initializer_list<size_t> channels)
99{
100 if (bufferA.getNumChannels() > bufferB.getNumChannels())
101 return getChannelIndicesToProcess (bufferB, channels);
102
103 return getChannelIndicesToProcess (bufferA, channels);
104}
105
106/// @brief Describes how to look for best cross-correlation
108{
112
113} // namespace hart
Container for audio data.
CorrelationSearchMode
Describes how to look for best cross-correlation.
Helpers to generate common default channel subsets.
static std::vector< std::pair< size_t, size_t > > upperTriangleChannelPairs(size_t numChannels)
static std::vector< std::pair< size_t, size_t > > diagonalChannelPairs(size_t numChannels)
static std::vector< size_t > allChannels(size_t numChannels)