xref: /aosp_15_r20/external/oboe/samples/shared/SynthSound.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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 #ifndef SHARED_SYNTH_SOUND_H
17 #define SHARED_SYNTH_SOUND_H
18 #include <cstdint>
19 #include <atomic>
20 #include <math.h>
21 #include <memory>
22 #include "IRenderableAudio.h"
23 constexpr float kDefaultFrequency = 440.0;
24 constexpr int32_t kDefaultSampleRate = 48000;
25 constexpr float kPi = M_PI;
26 constexpr float kTwoPi = kPi * 2;
27 constexpr int32_t kNumSineWaves = 5;
28 constexpr float kSustainMultiplier = 0.99999;
29 constexpr float kReleaseMultiplier = 0.999;
30 // Stop playing music below this cutoff
31 constexpr float kMasterAmplitudeCutOff = 0.01;
32 
33 class SynthSound : public IRenderableAudio {
34 
35 public:
SynthSound()36     SynthSound() {
37 
38     }
39 
~SynthSound()40     ~SynthSound() {
41 
42     };
43 
noteOn()44     void noteOn() {
45         mTrigger = true; // start a note envelope
46         mAmplitudeScaler = kSustainMultiplier;
47     }
48 
noteOff()49     void noteOff() {
50         mAmplitudeScaler = kReleaseMultiplier;
51     }
52 
setSampleRate(int32_t sampleRate)53     void setSampleRate(int32_t sampleRate) {
54         mSampleRate = sampleRate;
55         updatePhaseIncrement();
56     };
setFrequency(float frequency)57     void setFrequency(float frequency) {
58         mFrequency = frequency;
59         updatePhaseIncrement();
60     };
61     // Amplitudes from https://epubs.siam.org/doi/pdf/10.1137/S00361445003822
setAmplitude(float amplitude)62     inline void setAmplitude(float amplitude) {
63         mAmplitudes[0] = amplitude * .2f;
64         mAmplitudes[1] = amplitude;
65         mAmplitudes[2] = amplitude * .1f;
66         mAmplitudes[3] = amplitude * .02f;
67         mAmplitudes[4] = amplitude * .15f;
68     };
69     // From IRenderableAudio
renderAudio(float * audioData,int32_t numFrames)70     void renderAudio(float *audioData, int32_t numFrames) override {
71         for (int i = 0; i < numFrames; ++i) {
72             if (mTrigger.exchange(false)) {
73                 mMasterAmplitude = 1.0;
74                 mPhase = 0.0f;
75             } else {
76                 mMasterAmplitude *= mAmplitudeScaler;
77             }
78 
79             audioData[i] = 0;
80             if (mMasterAmplitude < kMasterAmplitudeCutOff) {
81                 continue;
82             }
83             for (int j = 0; j < kNumSineWaves; ++j) {
84                 audioData[i] += sinf(mPhase * (j + 1)) * mAmplitudes[j] * mMasterAmplitude;
85             }
86             mPhase += mPhaseIncrement;
87             if (mPhase > kTwoPi) {
88                 mPhase -=  kTwoPi;
89             }
90         }
91     };
92 
93 private:
94     std::atomic<bool> mTrigger { false };
95     float mMasterAmplitude = 0.0f;
96     std::atomic<float> mAmplitudeScaler { 0.0f };
97     std::array<std::atomic<float>, kNumSineWaves> mAmplitudes;
98     float mPhase = 0.0f;
99     std::atomic<float> mPhaseIncrement { 0 };
100     std::atomic<float> mFrequency { kDefaultFrequency };
101     std::atomic<int32_t> mSampleRate { kDefaultSampleRate };
updatePhaseIncrement()102     void updatePhaseIncrement(){
103         // Note how there is a division here. If this file is changed so that updatePhaseIncrement
104         // is called more frequently, please cache 1/mSampleRate. This allows this operation to not
105         // need divisions.
106         mPhaseIncrement = kTwoPi * mFrequency / static_cast<float>(mSampleRate);
107     };
108 };
109 #endif //SHARED_SYNTH_SOUND_H
110