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