Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 "AAudioService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 22 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <cstring> |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 24 | #include <utils/Trace.h> |
| 25 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 26 | #include "AAudioMixer.h" |
| 27 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 28 | #ifndef AAUDIO_MIXER_ATRACE_ENABLED |
| 29 | #define AAUDIO_MIXER_ATRACE_ENABLED 1 |
| 30 | #endif |
| 31 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | using android::WrappingBuffer; |
| 33 | using android::FifoBuffer; |
| 34 | using android::fifo_frames_t; |
| 35 | |
| 36 | AAudioMixer::~AAudioMixer() { |
| 37 | delete[] mOutputBuffer; |
| 38 | } |
| 39 | |
| 40 | void 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 | |
| 48 | void AAudioMixer::clear() { |
| 49 | memset(mOutputBuffer, 0, mBufferSizeInBytes); |
| 50 | } |
| 51 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 52 | bool AAudioMixer::mix(int trackIndex, FifoBuffer *fifo, float volume) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 53 | WrappingBuffer wrappingBuffer; |
| 54 | float *destination = mOutputBuffer; |
| 55 | fifo_frames_t framesLeft = mFramesPerBurst; |
| 56 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 57 | #if AAUDIO_MIXER_ATRACE_ENABLED |
| 58 | ATRACE_BEGIN("aaMix"); |
| 59 | #endif /* AAUDIO_MIXER_ATRACE_ENABLED */ |
| 60 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 61 | // Gather the data from the client. May be in two parts. |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 62 | 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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 74 | |
| 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 93 | //ALOGW("AAudioMixer::mix() UNDERFLOW by %d / %d frames ----- UNDERFLOW !!!!!!!!!!", |
| 94 | // framesLeft, mFramesPerBurst); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 96 | #if AAUDIO_MIXER_ATRACE_ENABLED |
| 97 | ATRACE_END(); |
| 98 | #endif /* AAUDIO_MIXER_ATRACE_ENABLED */ |
| 99 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 100 | return (framesLeft > 0); // did not get all the frames we needed, ie. "underflow" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void 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 | |
| 111 | float *AAudioMixer::getOutputBuffer() { |
| 112 | return mOutputBuffer; |
| 113 | } |