RavEngine
Loading...
Searching...
No Matches
test_util.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_UTILS_TEST_UTIL_H_
18#define RESONANCE_AUDIO_UTILS_TEST_UTIL_H_
19
20#include <cstddef>
21#include <vector>
22
23#include "base/integral_types.h"
24#include "base/audio_buffer.h"
25
26namespace vraudio {
27
28// Silences an audio channel.
29void GenerateSilence(AudioBuffer::Channel* output);
30
31// Generates a sine wave at the specified frequency in hertz at the given
32// sampling rate in hertz.
33void GenerateSineWave(float frequency_hz, int sample_rate,
34 AudioBuffer::Channel* output);
35
36// Generates a saw tooth signal between -1 and 1 for the given wave form length.
37void GenerateSawToothSignal(size_t tooth_length_samples,
38 AudioBuffer::Channel* output);
39
40// Generates a Dirac impulse filter kernel, which delays filtered signals by
41// the given delay.
42void GenerateDiracImpulseFilter(size_t delay_samples,
43 AudioBuffer::Channel* output);
44
45// Generates a linear ramp signal between -1 and 1.
46void GenerateIncreasingSignal(AudioBuffer::Channel* output);
47
48// Returns the index of the first non-zero element.
49size_t ZeroCompare(const AudioBuffer::Channel& signal, float epsilon);
50
51// Compares the content of two audio channels. Returns true if the absolute
52// difference between all samples is below epsion.
53bool CompareAudioBuffers(const AudioBuffer::Channel& buffer_a,
54 const AudioBuffer::Channel& buffer_b, float epsilon);
55
56// Returns delayed_signal.size() in output if delayed_signal is approximately
57// equal to original_signal delayed by the given amount; otherwise, returns the
58// index of the first unequal element in delayed_signal.
59size_t DelayCompare(const AudioBuffer::Channel& original_signal,
60 const AudioBuffer::Channel& delayed_signal, size_t delay,
61 float epsilon);
62
63// Test if two signals are shifted by a fixed number of samples with zero
64// padding.
65bool TestZeroPaddedDelay(const AudioBuffer::Channel& original_signal,
66 const AudioBuffer::Channel& delayed_signal,
67 size_t delay_samples, float epsilon);
68
69// Returns absolute peak amplitude of a signal.
70double CalculateSignalPeak(const AudioBuffer::Channel& channel);
71
72// Returns energy of a signal.
73double CalculateSignalEnergy(const AudioBuffer::Channel& channel);
74
75// Returns Root Mean Square (RMS) of a signal.
76double CalculateSignalRms(const AudioBuffer::Channel& channel);
77
78// Expresses a magnitude measurement in dB.
79double DbFromMagnitude(double magnitude);
80
81// Expresses a power measurement in dB.
82double DbFromPower(double power);
83
84// Returns the maximum cross correlation value between two signals.
85// To be used only with signals of the same length.
86float MaxCrossCorrelation(const AudioBuffer::Channel& signal_a,
87 const AudioBuffer::Channel& signal_b);
88
89} // namespace vraudio
90
91#endif // RESONANCE_AUDIO_UTILS_TEST_UTIL_H_