blob: 1c03b7facd7367954e4b078370e371ef5ac569a0 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Phil Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "AAudioMixer"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burkfd34a932017-07-19 07:03:52 -070021#define ATRACE_TAG ATRACE_TAG_AUDIO
22
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <cstring>
Phil Burkfd34a932017-07-19 07:03:52 -070024#include <utils/Trace.h>
25
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include "AAudioMixer.h"
27
Phil Burkfd34a932017-07-19 07:03:52 -070028#ifndef AAUDIO_MIXER_ATRACE_ENABLED
29#define AAUDIO_MIXER_ATRACE_ENABLED 1
30#endif
31
Phil Burkc0c70e32017-02-09 13:18:38 -080032using android::WrappingBuffer;
33using android::FifoBuffer;
34using android::fifo_frames_t;
35
36AAudioMixer::~AAudioMixer() {
37 delete[] mOutputBuffer;
38}
39
40void AAudioMixer::allocate(int32_t samplesPerFrame, int32_t framesPerBurst) {
41 mSamplesPerFrame = samplesPerFrame;
42 mFramesPerBurst = framesPerBurst;
43 int32_t samplesPerBuffer = samplesPerFrame * framesPerBurst;
44 mOutputBuffer = new float[samplesPerBuffer];
45 mBufferSizeInBytes = samplesPerBuffer * sizeof(float);
46}
47
48void AAudioMixer::clear() {
49 memset(mOutputBuffer, 0, mBufferSizeInBytes);
50}
51
Phil Burk23296382017-11-20 15:45:11 -080052int32_t AAudioMixer::mix(int streamIndex, FifoBuffer *fifo, bool allowUnderflow) {
Phil Burkc0c70e32017-02-09 13:18:38 -080053 WrappingBuffer wrappingBuffer;
54 float *destination = mOutputBuffer;
Phil Burkc0c70e32017-02-09 13:18:38 -080055
Phil Burkfd34a932017-07-19 07:03:52 -070056#if AAUDIO_MIXER_ATRACE_ENABLED
57 ATRACE_BEGIN("aaMix");
58#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
59
Phil Burkc0c70e32017-02-09 13:18:38 -080060 // Gather the data from the client. May be in two parts.
Phil Burkfd34a932017-07-19 07:03:52 -070061 fifo_frames_t fullFrames = fifo->getFullDataAvailable(&wrappingBuffer);
62#if AAUDIO_MIXER_ATRACE_ENABLED
63 if (ATRACE_ENABLED()) {
64 char rdyText[] = "aaMixRdy#";
Phil Burk83fb8442017-10-05 16:55:17 -070065 char letter = 'A' + (streamIndex % 26);
Phil Burkfd34a932017-07-19 07:03:52 -070066 rdyText[sizeof(rdyText) - 2] = letter;
67 ATRACE_INT(rdyText, fullFrames);
68 }
69#else /* MIXER_ATRACE_ENABLED */
70 (void) trackIndex;
Phil Burkfd34a932017-07-19 07:03:52 -070071#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
Phil Burkc0c70e32017-02-09 13:18:38 -080072
Phil Burk83fb8442017-10-05 16:55:17 -070073 // If allowUnderflow then always advance by one burst even if we do not have the data.
74 // Otherwise the stream timing will drift whenever there is an underflow.
75 // This actual underflow can then be detected by the client for XRun counting.
76 //
77 // Generally, allowUnderflow will be false when stopping a stream and we want to
78 // use up whatever data is in the queue.
79 fifo_frames_t framesDesired = mFramesPerBurst;
80 if (!allowUnderflow && fullFrames < framesDesired) {
81 framesDesired = fullFrames; // just use what is available then stop
82 }
83
Phil Burkc0c70e32017-02-09 13:18:38 -080084 // Mix data in one or two parts.
85 int partIndex = 0;
Phil Burk83fb8442017-10-05 16:55:17 -070086 int32_t framesLeft = framesDesired;
Phil Burkc0c70e32017-02-09 13:18:38 -080087 while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
Phil Burk83fb8442017-10-05 16:55:17 -070088 fifo_frames_t framesToMixFromPart = framesLeft;
89 fifo_frames_t framesAvailableFromPart = wrappingBuffer.numFrames[partIndex];
90 if (framesAvailableFromPart > 0) {
91 if (framesToMixFromPart > framesAvailableFromPart) {
92 framesToMixFromPart = framesAvailableFromPart;
Phil Burkc0c70e32017-02-09 13:18:38 -080093 }
Phil Burk83fb8442017-10-05 16:55:17 -070094 mixPart(destination, (float *)wrappingBuffer.data[partIndex],
95 framesToMixFromPart);
Phil Burkc0c70e32017-02-09 13:18:38 -080096
Phil Burk83fb8442017-10-05 16:55:17 -070097 destination += framesToMixFromPart * mSamplesPerFrame;
98 framesLeft -= framesToMixFromPart;
Phil Burkc0c70e32017-02-09 13:18:38 -080099 }
100 partIndex++;
101 }
Phil Burk882c5202018-04-23 10:32:45 -0700102 fifo->advanceReadIndex(framesDesired);
Phil Burkfaeb8b22017-07-25 15:15:07 -0700103
Phil Burkfd34a932017-07-19 07:03:52 -0700104#if AAUDIO_MIXER_ATRACE_ENABLED
105 ATRACE_END();
106#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
107
Phil Burk23296382017-11-20 15:45:11 -0800108 return (framesDesired - framesLeft); // framesRead
Phil Burkc0c70e32017-02-09 13:18:38 -0800109}
110
Phil Burk83fb8442017-10-05 16:55:17 -0700111void AAudioMixer::mixPart(float *destination, float *source, int32_t numFrames) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 int32_t numSamples = numFrames * mSamplesPerFrame;
113 // TODO maybe optimize using SIMD
114 for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
Phil Burk83fb8442017-10-05 16:55:17 -0700115 *destination++ += *source++;
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 }
117}
118
119float *AAudioMixer::getOutputBuffer() {
120 return mOutputBuffer;
121}