• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 CTS_NATIVE_AUDIO_ANALYZER_H
18 #define CTS_NATIVE_AUDIO_ANALYZER_H
19 
20 #define LOG_TAG "NativeAudioAnalyzer"
21 #include <android/log.h>
22 
23 #ifndef MODULE_NAME
24 #define MODULE_NAME  "NativeAudioAnalyzer"
25 #endif
26 
27 #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, MODULE_NAME, __VA_ARGS__)
28 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, MODULE_NAME, __VA_ARGS__)
29 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, MODULE_NAME, __VA_ARGS__)
30 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, MODULE_NAME, __VA_ARGS__)
31 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, MODULE_NAME, __VA_ARGS__)
32 #define ALOGF(...) __android_log_print(ANDROID_LOG_FATAL, MODULE_NAME, __VA_ARGS__)
33 
34 #include <aaudio/AAudio.h>
35 
36 #include "analyzer/LatencyAnalyzer.h"
37 
38 class NativeAudioAnalyzer {
39 public:
40 
41     /**
42      * Open the audio input and output streams.
43      * @return AAUDIO_OK or negative error
44      */
45     aaudio_result_t openAudio(int inputDeviceId, int outputDeviceId);
46 
47     /**
48      * Start the audio input and output streams.
49      * @return AAUDIO_OK or negative error
50      */
51     aaudio_result_t startAudio();
52 
53     /**
54      * Stop the audio input and output streams.
55      * @return AAUDIO_OK or negative error
56      */
57     aaudio_result_t stopAudio();
58 
59     /**
60      * Close the audio input and output streams.
61      * @return AAUDIO_OK or negative error
62      */
63     aaudio_result_t closeAudio();
64 
65     /**
66      * @return true if enough audio input has been recorded
67      */
68     bool isRecordingComplete();
69 
70     /**
71      * Analyze the input and measure the latency between output and input.
72      * @return AAUDIO_OK or negative error
73      */
74     int analyze();
75 
76     /**
77      * @return the measured latency in milliseconds
78      */
79     double getLatencyMillis();
80 
81     /**
82      * @return the sample rate (in Hz) used for the measurement signal
83      */
84     int getSampleRate();
85 
86     /**
87      * The confidence is based on a normalized correlation.
88      * It ranges from 0.0 to 1.0. Higher is better.
89      *
90      * @return the confidence in the latency result
91      */
92     double getConfidence();
93 
94     /**
95      * Returns true if the stream was successfully opened in low-latency mode.
96      */
97     bool isLowLatencyStream();
98 
getError()99     aaudio_result_t getError() {
100         return mInputError ? mInputError : mOutputError;
101     }
102 
103     AAudioStream      *mInputStream = nullptr;
104     AAudioStream      *mOutputStream = nullptr;
105     aaudio_format_t    mActualInputFormat = AAUDIO_FORMAT_INVALID;
106     int16_t           *mInputShortData = nullptr;
107     float             *mInputFloatData = nullptr;
108     int32_t            mOutputSampleRate = 0;
109 
110     aaudio_result_t    mInputError = AAUDIO_OK;
111     aaudio_result_t    mOutputError = AAUDIO_OK;
112 
113 aaudio_data_callback_result_t dataCallbackProc(
114         void *audioData,
115         int32_t numFrames);
116 
117 private:
118 
119     int32_t readFormattedData(int32_t numFrames);
120 
121     WhiteNoiseLatencyAnalyzer mWhiteNoiseLatencyAnalyzer;
122     LoopbackProcessor   *mLoopbackProcessor;
123 
124     int32_t            mInputFramesMaximum = 0;
125     int32_t            mActualInputChannelCount = 0;
126     int32_t            mActualOutputChannelCount = 0;
127     int32_t            mNumCallbacksToDrain = kNumCallbacksToDrain;
128     int32_t            mNumCallbacksToNotRead = kNumCallbacksToNotRead;
129     int32_t            mNumCallbacksToDiscard = kNumCallbacksToDiscard;
130     int32_t            mMinNumFrames = INT32_MAX;
131     int32_t            mMaxNumFrames = 0;
132     int32_t            mInsufficientReadCount = 0;
133     int32_t            mInsufficientReadFrames = 0;
134     int32_t            mFramesReadTotal = 0;
135     int32_t            mFramesWrittenTotal = 0;
136     bool               mIsDone = false;
137     bool               mIsLowLatencyStream = false;
138 
139     int32_t            mOutputDeviceId = 0;
140     int32_t            mInputDeviceId = 0;
141 
142     static constexpr int kLogPeriodMillis         = 1000;
143     static constexpr int kNumInputChannels        = 1;
144     static constexpr int kNumCallbacksToDrain     = 20;
145     static constexpr int kNumCallbacksToNotRead   = 0; // let input fill back up
146     static constexpr int kNumCallbacksToDiscard   = 20;
147     static constexpr int kDefaultHangTimeMillis   = 50;
148     static constexpr int kMaxGlitchEventsToSave   = 32;
149     static constexpr int kDefaultOutputSizeBursts = 2;
150 };
151 
152 #endif // CTS_NATIVE_AUDIO_ANALYZER_H
153