blob: 3220df226b742b5ff6d4dee1607f6506ece08840 [file] [log] [blame]
Phil Burk0433d8f2018-11-21 16:41:25 -08001/*
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 Burkfb358af2019-03-05 12:42:35 -080018#include "common/OboeDebug.h"
Phil Burk0433d8f2018-11-21 16:41:25 -080019#include "oboe/Definitions.h"
Phil Burk0817a2b2019-01-03 14:50:18 -080020#include "SawPingGenerator.h"
21
Phil Burk4ce07142020-07-09 13:17:32 -070022using namespace oboe::flowgraph;
Phil Burk0433d8f2018-11-21 16:41:25 -080023
24SawPingGenerator::SawPingGenerator()
25 : OscillatorBase()
26 , mRequestCount(0)
27 , mAcknowledgeCount(0)
28 , mLevel(0.0f) {
29}
30
31SawPingGenerator::~SawPingGenerator() { }
32
Phil Burk9102c2a2020-11-04 16:19:52 -080033void SawPingGenerator::reset() {
34 FlowGraphNode::reset();
35 mAcknowledgeCount.store(mRequestCount.load());
36}
37
Phil Burk71d97e92019-03-08 12:50:01 -080038int32_t SawPingGenerator::onProcess(int numFrames) {
Phil Burk0433d8f2018-11-21 16:41:25 -080039
Phil Burk91362012019-03-04 12:00:30 -080040 const float *frequencies = frequency.getBuffer();
41 const float *amplitudes = amplitude.getBuffer();
42 float *buffer = output.getBuffer();
Phil Burk0433d8f2018-11-21 16:41:25 -080043
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 Burk0817a2b2019-01-03 14:50:18 -080063 return numFrames;
Phil Burk0433d8f2018-11-21 16:41:25 -080064}
65
Phil Burk9102c2a2020-11-04 16:19:52 -080066void SawPingGenerator::trigger() {
67 mRequestCount++;
Phil Burk0433d8f2018-11-21 16:41:25 -080068}
69