Phil Burk | 64dce36 | 2018-03-28 15:30:39 -0700 | [diff] [blame] | 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 "SinkI24.h" |
| 26 | |
| 27 | using namespace flowgraph; |
| 28 | |
| 29 | |
| 30 | SinkI24::SinkI24(int32_t channelCount) |
| 31 | : AudioSink(channelCount) {} |
| 32 | |
| 33 | int32_t SinkI24::read(void *data, int32_t numFrames) { |
| 34 | uint8_t *byteData = (uint8_t *) data; |
| 35 | const int32_t channelCount = input.getSamplesPerFrame(); |
| 36 | |
| 37 | int32_t framesLeft = numFrames; |
| 38 | while (framesLeft > 0) { |
| 39 | // Run the graph and pull data through the input port. |
| 40 | int32_t framesRead = pull(framesLeft); |
| 41 | if (framesRead <= 0) { |
| 42 | break; |
| 43 | } |
| 44 | const float *floatData = input.getBlock(); |
| 45 | int32_t numSamples = framesRead * channelCount; |
| 46 | #ifdef __ANDROID__ |
| 47 | memcpy_to_p24_from_float(byteData, floatData, numSamples); |
| 48 | static const int kBytesPerI24Packed = 3; |
| 49 | byteData += numSamples * kBytesPerI24Packed; |
| 50 | floatData += numSamples; |
| 51 | #else |
| 52 | const int32_t kI24PackedMax = 0x007FFFFF; |
| 53 | const int32_t kI24PackedMin = 0xFF800000; |
| 54 | for (int i = 0; i < numSamples; i++) { |
| 55 | int32_t n = (int32_t) (*floatData++ * 0x00800000); |
| 56 | n = std::min(kI24PackedMax, std::max(kI24PackedMin, n)); // clip |
| 57 | // Write as a packed 24-bit integer in Little Endian format. |
| 58 | *byteData++ = (uint8_t) n; |
| 59 | *byteData++ = (uint8_t) (n >> 8); |
| 60 | *byteData++ = (uint8_t) (n >> 16); |
| 61 | } |
| 62 | #endif |
| 63 | framesLeft -= framesRead; |
| 64 | } |
| 65 | return numFrames - framesLeft; |
| 66 | } |