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>
18 #include "common/OboeDebug.h"
19 #include "oboe/Definitions.h"
20 #include "SawPingGenerator.h"
21 
22 using namespace oboe::flowgraph;
23 
SawPingGenerator()24 SawPingGenerator::SawPingGenerator()
25         : OscillatorBase()
26         , mRequestCount(0)
27         , mAcknowledgeCount(0)
28         , mLevel(0.0f) {
29 }
30 
~SawPingGenerator()31 SawPingGenerator::~SawPingGenerator() { }
32 
reset()33 void SawPingGenerator::reset() {
34     FlowGraphNode::reset();
35     mAcknowledgeCount.store(mRequestCount.load());
36 }
37 
onProcess(int numFrames)38 int32_t SawPingGenerator::onProcess(int numFrames) {
39 
40     const float *frequencies = frequency.getBuffer();
41     const float *amplitudes = amplitude.getBuffer();
42     float *buffer = output.getBuffer();
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 
63     return numFrames;
64 }
65 
trigger()66 void SawPingGenerator::trigger() {
67     mRequestCount++;
68 }
69 
70