Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 <unistd.h> |
Phil Burk | fb358af | 2019-03-05 12:42:35 -0800 | [diff] [blame] | 18 | #include "common/OboeDebug.h" |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 19 | #include "oboe/Definitions.h" |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 20 | #include "SawPingGenerator.h" |
| 21 | |
Phil Burk | 4ce0714 | 2020-07-09 13:17:32 -0700 | [diff] [blame] | 22 | using namespace oboe::flowgraph; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 23 | |
| 24 | SawPingGenerator::SawPingGenerator() |
| 25 | : OscillatorBase() |
| 26 | , mRequestCount(0) |
| 27 | , mAcknowledgeCount(0) |
| 28 | , mLevel(0.0f) { |
| 29 | } |
| 30 | |
| 31 | SawPingGenerator::~SawPingGenerator() { } |
| 32 | |
Phil Burk | 9102c2a | 2020-11-04 16:19:52 -0800 | [diff] [blame] | 33 | void SawPingGenerator::reset() { |
| 34 | FlowGraphNode::reset(); |
| 35 | mAcknowledgeCount.store(mRequestCount.load()); |
| 36 | } |
| 37 | |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 38 | int32_t SawPingGenerator::onProcess(int numFrames) { |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 39 | |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 40 | const float *frequencies = frequency.getBuffer(); |
| 41 | const float *amplitudes = amplitude.getBuffer(); |
| 42 | float *buffer = output.getBuffer(); |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 43 | |
| 44 | if (mRequestCount.load() > mAcknowledgeCount.load()) { |
| 45 | mPhase = -1.0f; |
| 46 | mLevel = 1.0; |
| 47 | mAcknowledgeCount++; |
| 48 | } |
| 49 | |
| 50 | // Check level to prevent numeric underflow. |
| 51 | if (mLevel > 0.000001) { |
| 52 | for (int i = 0; i < numFrames; i++) { |
| 53 | float sawtooth = incrementPhase(frequencies[i]); |
| 54 | *buffer++ = (float) (sawtooth * mLevel * amplitudes[i]); |
| 55 | mLevel *= 0.999; |
| 56 | } |
| 57 | } else { |
| 58 | for (int i = 0; i < numFrames; i++) { |
| 59 | *buffer++ = 0.0f; |
| 60 | } |
| 61 | } |
| 62 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 63 | return numFrames; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Phil Burk | 9102c2a | 2020-11-04 16:19:52 -0800 | [diff] [blame] | 66 | void SawPingGenerator::trigger() { |
| 67 | mRequestCount++; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 68 | } |
| 69 | |