• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LB2_TEST_CONTEXT_H_
18 #define LB2_TEST_CONTEXT_H_
19 
20 #include <memory>
21 
22 #include <SLES/OpenSLES.h>  // for SLuint... types use by performance mode consts
23 #include <SLES/OpenSLES_AndroidConfiguration.h>
24 
25 #include "lb2/audio_buffer.h"
26 
27 // The Java side uses the same numbers as OpenSL ES, and '-1' for default,
28 // see LoopbackApplication.java.
29 enum class PerformanceMode {
30     DEFAULT = -1,
31     NONE = SL_ANDROID_PERFORMANCE_NONE,
32     LATENCY = SL_ANDROID_PERFORMANCE_LATENCY,
33     LATENCY_EFFECTS = SL_ANDROID_PERFORMANCE_LATENCY_EFFECTS,
34     POWER_SAVING = SL_ANDROID_PERFORMANCE_POWER_SAVING
35 };
36 
37 // Generic context describing test parameters.
38 // Made non-copyable because descendants can contain buffers.
39 class TestContext : public CountsConverter<sample_t> {
40   public:
TestContext(PerformanceMode perfMode,int testFrameCount,int channelCount,int samplingRateHz)41     TestContext(PerformanceMode perfMode,
42             int testFrameCount,
43             int channelCount,
44             int samplingRateHz)
45             : CountsConverter<sample_t>(testFrameCount, channelCount),
46               mPerfMode(perfMode),
47               mSamplingRateHz(samplingRateHz) {}
48     TestContext(const TestContext&) = delete;
49     TestContext& operator=(const TestContext&) = delete;
50 
51     // Allocates an audio buffer with the size enough to hold audio test data.
createAudioBuffer()52     AudioBuffer<sample_t> createAudioBuffer() const {
53         return AudioBuffer<sample_t>(getFrameCount(), getChannelCount());
54     }
getPerformanceMode()55     PerformanceMode getPerformanceMode() const { return mPerfMode; }
getSamplingRateHz()56     int getSamplingRateHz() const { return mSamplingRateHz; }
57 
58   private:
59     const PerformanceMode mPerfMode;
60     const int mSamplingRateHz;
61 };
62 
63 
64 // Context describing latency test parameters.
65 // Carries test impulse data, but doesn't own it.
66 // The size of the impulse is assumed to be 1 frame buffer.
67 class LatencyTestContext : public TestContext {
68   public:
LatencyTestContext(PerformanceMode perfMode,int testFrameCount,int channelCount,int samplingRateHz,int inputFramesToDiscard,sample_t * impulse)69     LatencyTestContext(PerformanceMode perfMode,
70             int testFrameCount,
71             int channelCount,
72             int samplingRateHz,
73             int inputFramesToDiscard,
74             sample_t *impulse)
75             : TestContext(perfMode, testFrameCount, channelCount, samplingRateHz),
76               mInputFramesToDiscard(inputFramesToDiscard),
77               mImpulse(impulse, testFrameCount, channelCount) {}
78     LatencyTestContext(const LatencyTestContext&) = delete;
79     LatencyTestContext& operator=(const LatencyTestContext&) = delete;
80 
getInputFramesToDiscard()81     int getInputFramesToDiscard() const { return mInputFramesToDiscard; }
getImpulse()82     AudioBufferView<sample_t> getImpulse() const { return mImpulse; }
83 
84   private:
85     const int mInputFramesToDiscard;
86     const AudioBufferView<sample_t> mImpulse;
87 };
88 
89 
90 // Context describing glitch test parameters.
91 // Generates test signal. Since the period of the test signal
92 // is not necessarily aligned with the test buffer size,
93 // the operation of getting next impulse piece is idempotent.
94 class GlitchTestContext : public TestContext {
95   public:
GlitchTestContext(PerformanceMode perfMode,int testFrameCount,int channelCount,int samplingRateHz,double signalFrequencyHz,AudioBufferView<sample_t> byteBuffer)96     GlitchTestContext(PerformanceMode perfMode,
97             int testFrameCount,
98             int channelCount,
99             int samplingRateHz,
100             double signalFrequencyHz,
101             AudioBufferView<sample_t> byteBuffer)
102             : TestContext(perfMode, testFrameCount, channelCount, samplingRateHz),
103               mByteBuffer(byteBuffer),
104               mPhaseIncrementPerFrame(signalFrequencyHz / samplingRateHz),
105               mSineBuffer(createAudioBuffer()),
106               mPhaseRad(0) {}
107     GlitchTestContext(const GlitchTestContext&) = delete;
108     GlitchTestContext& operator=(const GlitchTestContext&) = delete;
109 
getByteBuffer()110     const AudioBufferView<sample_t>& getByteBuffer() const { return mByteBuffer; }
111     AudioBufferView<sample_t> getNextImpulse(size_t frameCount);  // non-idempotent
112 
113   private:
114     static constexpr double SIGNAL_AMPLITUDE = 0.8;
115 
116     const AudioBufferView<sample_t> mByteBuffer;
117     const double mPhaseIncrementPerFrame;
118     AudioBuffer<sample_t> mSineBuffer;
119     double mPhaseRad;
120 };
121 
122 
123 #endif  // LB2_TEST_CONTEXT_H_
124