xref: /aosp_15_r20/external/oboe/apps/OboeTester/app/src/main/cpp/InputStreamCallbackAnalyzer.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2015 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 #include "common/OboeDebug.h"
18 #include "InputStreamCallbackAnalyzer.h"
19 
getPeakLevel(int index)20 double InputStreamCallbackAnalyzer::getPeakLevel(int index) {
21     if (mPeakDetectors == nullptr) {
22         LOGE("%s() called before setup()", __func__);
23         return -1.0;
24     } else if (index < 0 || index >= mNumChannels) {
25         LOGE("%s(), index out of range, 0 <= %d < %d", __func__, index, mNumChannels);
26         return -2.0;
27     }
28     return mPeakDetectors[index].getLevel();
29 }
30 
onAudioReady(oboe::AudioStream * audioStream,void * audioData,int numFrames)31 oboe::DataCallbackResult InputStreamCallbackAnalyzer::onAudioReady(
32         oboe::AudioStream *audioStream,
33         void *audioData,
34         int numFrames) {
35     int32_t channelCount = audioStream->getChannelCount();
36 
37     maybeHang(getNanoseconds());
38     printScheduler();
39     mInputConverter->convertToInternalOutput(numFrames * channelCount, audioData);
40     float *floatData = (float *) mInputConverter->getOutputBuffer();
41     if (mRecording != nullptr) {
42         mRecording->write(floatData, numFrames);
43     }
44     int32_t sampleIndex = 0;
45     for (int iFrame = 0; iFrame < numFrames; iFrame++) {
46         for (int iChannel = 0; iChannel < channelCount; iChannel++) {
47             float sample = floatData[sampleIndex++];
48             mPeakDetectors[iChannel].process(sample);
49         }
50     }
51 
52     audioStream->waitForAvailableFrames(mMinimumFramesBeforeRead, oboe::kNanosPerSecond);
53 
54     return oboe::DataCallbackResult::Continue;
55 }
56