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 99 /** 100 * Returns true if the hardware supports 24 bit audio. 101 */ 102 bool has24BitHardwareSupport(); 103 getError()104 aaudio_result_t getError() { 105 return mInputError ? mInputError : mOutputError; 106 } 107 108 AAudioStream *mInputStream = nullptr; 109 AAudioStream *mOutputStream = nullptr; 110 aaudio_format_t mActualInputFormat = AAUDIO_FORMAT_INVALID; 111 int16_t *mInputShortData = nullptr; 112 float *mInputFloatData = nullptr; 113 int32_t mOutputSampleRate = 0; 114 115 aaudio_result_t mInputError = AAUDIO_OK; 116 aaudio_result_t mOutputError = AAUDIO_OK; 117 118 aaudio_data_callback_result_t dataCallbackProc( 119 void *audioData, 120 int32_t numFrames); 121 122 private: 123 124 int32_t readFormattedData(int32_t numFrames); 125 126 bool has24BitSupport(aaudio_format_t format); 127 128 WhiteNoiseLatencyAnalyzer mWhiteNoiseLatencyAnalyzer; 129 LoopbackProcessor *mLoopbackProcessor; 130 131 int32_t mInputFramesMaximum = 0; 132 int32_t mActualInputChannelCount = 0; 133 int32_t mActualOutputChannelCount = 0; 134 int32_t mNumCallbacksToDrain = kNumCallbacksToDrain; 135 int32_t mNumCallbacksToNotRead = kNumCallbacksToNotRead; 136 int32_t mNumCallbacksToDiscard = kNumCallbacksToDiscard; 137 int32_t mMinNumFrames = INT32_MAX; 138 int32_t mMaxNumFrames = 0; 139 int32_t mInsufficientReadCount = 0; 140 int32_t mInsufficientReadFrames = 0; 141 int32_t mFramesReadTotal = 0; 142 int32_t mFramesWrittenTotal = 0; 143 bool mIsDone = false; 144 bool mIsLowLatencyStream = false; 145 bool mHas24BitHardwareSupport = false; 146 147 int32_t mOutputDeviceId = 0; 148 int32_t mInputDeviceId = 0; 149 150 static constexpr int kLogPeriodMillis = 1000; 151 static constexpr int kNumInputChannels = 1; 152 static constexpr int kNumCallbacksToDrain = 20; 153 static constexpr int kNumCallbacksToNotRead = 0; // let input fill back up 154 static constexpr int kNumCallbacksToDiscard = 20; 155 static constexpr int kDefaultHangTimeMillis = 50; 156 static constexpr int kMaxGlitchEventsToSave = 32; 157 static constexpr int kDefaultOutputSizeBursts = 2; 158 }; 159 160 #endif // CTS_NATIVE_AUDIO_ANALYZER_H 161