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
onAudioReady(oboe::AudioStream * audioStream,void * audioData,int numFrames)20 oboe::DataCallbackResult InputStreamCallbackAnalyzer::onAudioReady(
21 oboe::AudioStream *audioStream,
22 void *audioData,
23 int numFrames) {
24 int32_t channelCount = audioStream->getChannelCount();
25
26 printScheduler();
27
28 if (audioStream->getFormat() == oboe::AudioFormat::I16) {
29 int16_t *shortData = (int16_t *) audioData;
30 if (mRecording != nullptr) {
31 mRecording->write(shortData, numFrames);
32 }
33 int16_t *frameData = shortData;
34 for (int iFrame = 0; iFrame < numFrames; iFrame++) {
35 for (int iChannel = 0; iChannel < channelCount; iChannel++) {
36 float sample = frameData[iChannel] / 32768.0f;
37 mPeakDetectors[iChannel].process(sample);
38 }
39 frameData += channelCount;
40 }
41 } else if (audioStream->getFormat() == oboe::AudioFormat::Float) {
42 float *floatData = (float *) audioData;
43 if (mRecording != nullptr) {
44 mRecording->write(floatData, numFrames);
45 }
46 float *frameData = floatData;
47 for (int iFrame = 0; iFrame < numFrames; iFrame++) {
48 for (int iChannel = 0; iChannel < channelCount; iChannel++) {
49 float sample = frameData[iChannel];
50 mPeakDetectors[iChannel].process(sample);
51 }
52 frameData += channelCount;
53 }
54 }
55
56 audioStream->waitForAvailableFrames(mMinimumFramesBeforeRead, oboe::kNanosPerSecond);
57
58 return oboe::DataCallbackResult::Continue;
59 }
60