blob: 8222734422dbf23c8b3d7a124ba44ef8e08b53c0 [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
17#define LOG_TAG "AAudioService"
18//#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 Burkfd34a932017-07-19 07:03:52 -070052bool AAudioMixer::mix(int trackIndex, FifoBuffer *fifo, float volume) {
Phil Burkc0c70e32017-02-09 13:18:38 -080053 WrappingBuffer wrappingBuffer;
54 float *destination = mOutputBuffer;
55 fifo_frames_t framesLeft = mFramesPerBurst;
56
Phil Burkfd34a932017-07-19 07:03:52 -070057#if AAUDIO_MIXER_ATRACE_ENABLED
58 ATRACE_BEGIN("aaMix");
59#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
60
Phil Burkc0c70e32017-02-09 13:18:38 -080061 // Gather the data from the client. May be in two parts.
Phil Burkfd34a932017-07-19 07:03:52 -070062 fifo_frames_t fullFrames = fifo->getFullDataAvailable(&wrappingBuffer);
63#if AAUDIO_MIXER_ATRACE_ENABLED
64 if (ATRACE_ENABLED()) {
65 char rdyText[] = "aaMixRdy#";
66 char letter = 'A' + (trackIndex % 26);
67 rdyText[sizeof(rdyText) - 2] = letter;
68 ATRACE_INT(rdyText, fullFrames);
69 }
70#else /* MIXER_ATRACE_ENABLED */
71 (void) trackIndex;
72 (void) fullFrames;
73#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
Phil Burkc0c70e32017-02-09 13:18:38 -080074
75 // Mix data in one or two parts.
76 int partIndex = 0;
77 while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
78 fifo_frames_t framesToMix = framesLeft;
79 fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
80 if (framesAvailable > 0) {
81 if (framesToMix > framesAvailable) {
82 framesToMix = framesAvailable;
83 }
84 mixPart(destination, (float *)wrappingBuffer.data[partIndex], framesToMix, volume);
85
86 destination += framesToMix * mSamplesPerFrame;
87 framesLeft -= framesToMix;
88 }
89 partIndex++;
90 }
91 fifo->getFifoControllerBase()->advanceReadIndex(mFramesPerBurst - framesLeft);
92 if (framesLeft > 0) {
Phil Burk71f35bb2017-04-13 16:05:07 -070093 //ALOGW("AAudioMixer::mix() UNDERFLOW by %d / %d frames ----- UNDERFLOW !!!!!!!!!!",
94 // framesLeft, mFramesPerBurst);
Phil Burkc0c70e32017-02-09 13:18:38 -080095 }
Phil Burkfd34a932017-07-19 07:03:52 -070096#if AAUDIO_MIXER_ATRACE_ENABLED
97 ATRACE_END();
98#endif /* AAUDIO_MIXER_ATRACE_ENABLED */
99
Phil Burk71f35bb2017-04-13 16:05:07 -0700100 return (framesLeft > 0); // did not get all the frames we needed, ie. "underflow"
Phil Burkc0c70e32017-02-09 13:18:38 -0800101}
102
103void AAudioMixer::mixPart(float *destination, float *source, int32_t numFrames, float volume) {
104 int32_t numSamples = numFrames * mSamplesPerFrame;
105 // TODO maybe optimize using SIMD
106 for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
107 *destination++ += *source++ * volume;
108 }
109}
110
111float *AAudioMixer::getOutputBuffer() {
112 return mOutputBuffer;
113}