Home | History | Annotate | Download | only in flowgraph
      1 /*
      2  * Copyright 2018 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 #include <algorithm>
     18 #include <unistd.h>
     19 
     20 #ifdef __ANDROID__
     21 #include <audio_utils/primitives.h>
     22 #endif
     23 
     24 #include "AudioProcessorBase.h"
     25 #include "SourceI24.h"
     26 
     27 using namespace flowgraph;
     28 
     29 constexpr int kBytesPerI24Packed = 3;
     30 
     31 SourceI24::SourceI24(int32_t channelCount)
     32         : AudioSource(channelCount) {
     33 }
     34 
     35 int32_t SourceI24::onProcess(int64_t framePosition, int32_t numFrames) {
     36     float *floatData = output.getBlock();
     37     int32_t channelCount = output.getSamplesPerFrame();
     38 
     39     int32_t framesLeft = mSizeInFrames - mFrameIndex;
     40     int32_t framesToProcess = std::min(numFrames, framesLeft);
     41     int32_t numSamples = framesToProcess * channelCount;
     42 
     43     const uint8_t *byteBase = (uint8_t *) mData;
     44     const uint8_t *byteData = &byteBase[mFrameIndex * channelCount * kBytesPerI24Packed];
     45 
     46 #ifdef __ANDROID__
     47     memcpy_to_float_from_p24(floatData, byteData, numSamples);
     48 #else
     49     static const float scale = 1. / (float)(1UL << 31);
     50     for (int i = 0; i < numSamples; i++) {
     51         // Assemble the data assuming Little Endian format.
     52         int32_t pad = byteData[2];
     53         pad <<= 8;
     54         pad |= byteData[1];
     55         pad <<= 8;
     56         pad |= byteData[0];
     57         pad <<= 8; // Shift to 32 bit data so the sign is correct.
     58         byteData += kBytesPerI24Packed;
     59         *floatData++ = pad * scale; // scale to range -1.0 to 1.0
     60     }
     61 #endif
     62 
     63     mFrameIndex += framesToProcess;
     64     return framesToProcess;
     65 }