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 #define LOG_TAG "AAudioMixer"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #define ATRACE_TAG ATRACE_TAG_AUDIO
22
23 #include <cstring>
24 #include <utils/Trace.h>
25
26 #include "AAudioMixer.h"
27
28 #ifndef AAUDIO_MIXER_ATRACE_ENABLED
29 #define AAUDIO_MIXER_ATRACE_ENABLED 1
30 #endif
31
32 using android::WrappingBuffer;
33 using android::FifoBuffer;
34 using android::fifo_frames_t;
35
allocate(int32_t samplesPerFrame,int32_t framesPerBurst)36 void AAudioMixer::allocate(int32_t samplesPerFrame, int32_t framesPerBurst) {
37 mSamplesPerFrame = samplesPerFrame;
38 mFramesPerBurst = framesPerBurst;
39 int32_t samplesPerBuffer = samplesPerFrame * framesPerBurst;
40 mOutputBuffer = std::make_unique<float[]>(samplesPerBuffer);
41 mBufferSizeInBytes = samplesPerBuffer * sizeof(float);
42 }
43
clear()44 void AAudioMixer::clear() {
45 memset(mOutputBuffer.get(), 0, mBufferSizeInBytes);
46 }
47
mix(int streamIndex,std::shared_ptr<FifoBuffer> fifo,bool allowUnderflow)48 int32_t AAudioMixer::mix(int streamIndex, std::shared_ptr<FifoBuffer> fifo, bool allowUnderflow) {
49 WrappingBuffer wrappingBuffer;
50 float *destination = mOutputBuffer.get();
51
52 #if AAUDIO_MIXER_ATRACE_ENABLED
53 ATRACE_BEGIN("aaMix");
54 #endif /* AAUDIO_MIXER_ATRACE_ENABLED */
55
56 // Gather the data from the client. May be in two parts.
57 fifo_frames_t fullFrames = fifo->getFullDataAvailable(&wrappingBuffer);
58 #if AAUDIO_MIXER_ATRACE_ENABLED
59 if (ATRACE_ENABLED()) {
60 char rdyText[] = "aaMixRdy#";
61 char letter = 'A' + (streamIndex % 26);
62 rdyText[sizeof(rdyText) - 2] = letter;
63 ATRACE_INT(rdyText, fullFrames);
64 }
65 #else /* MIXER_ATRACE_ENABLED */
66 (void) trackIndex;
67 #endif /* AAUDIO_MIXER_ATRACE_ENABLED */
68
69 // If allowUnderflow then always advance by one burst even if we do not have the data.
70 // Otherwise the stream timing will drift whenever there is an underflow.
71 // This actual underflow can then be detected by the client for XRun counting.
72 //
73 // Generally, allowUnderflow will be false when stopping a stream and we want to
74 // use up whatever data is in the queue.
75 fifo_frames_t framesDesired = mFramesPerBurst;
76 if (!allowUnderflow && fullFrames < framesDesired) {
77 framesDesired = fullFrames; // just use what is available then stop
78 }
79
80 // Mix data in one or two parts.
81 int partIndex = 0;
82 int32_t framesLeft = framesDesired;
83 while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
84 fifo_frames_t framesToMixFromPart = framesLeft;
85 fifo_frames_t framesAvailableFromPart = wrappingBuffer.numFrames[partIndex];
86 if (framesAvailableFromPart > 0) {
87 if (framesToMixFromPart > framesAvailableFromPart) {
88 framesToMixFromPart = framesAvailableFromPart;
89 }
90 mixPart(destination, (float *)wrappingBuffer.data[partIndex],
91 framesToMixFromPart);
92
93 destination += framesToMixFromPart * mSamplesPerFrame;
94 framesLeft -= framesToMixFromPart;
95 }
96 partIndex++;
97 }
98 fifo->advanceReadIndex(framesDesired);
99
100 #if AAUDIO_MIXER_ATRACE_ENABLED
101 ATRACE_END();
102 #endif /* AAUDIO_MIXER_ATRACE_ENABLED */
103
104 return (framesDesired - framesLeft); // framesRead
105 }
106
mixPart(float * destination,float * source,int32_t numFrames)107 void AAudioMixer::mixPart(float *destination, float *source, int32_t numFrames) {
108 int32_t numSamples = numFrames * mSamplesPerFrame;
109 // TODO maybe optimize using SIMD
110 for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
111 *destination++ += *source++;
112 }
113 }
114
getOutputBuffer()115 float *AAudioMixer::getOutputBuffer() {
116 return mOutputBuffer.get();
117 }
118