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 #ifndef SAMPLES_SOUNDGENERATOR_H
18 #define SAMPLES_SOUNDGENERATOR_H
19 
20 
21 #include <Oscillator.h>
22 #include <TappableAudioSource.h>
23 
24 /**
25  * Generates a fixed frequency tone for each channel.
26  * Implements RenderableTap (sound source with toggle) which is required for AudioEngines.
27  */
28 class SoundGenerator : public TappableAudioSource {
29     static constexpr size_t kSharedBufferSize = 1024;
30 public:
31     /**
32      * Create a new SoundGenerator object.
33      *
34      * @param sampleRate - The output sample rate.
35      * @param maxFrames - The maximum number of audio frames which will be rendered, this is used to
36      * calculate this object's internal buffer size.
37      * @param channelCount - The number of channels in the output, one tone will be created for each
38      * channel, the output will be interlaced.
39      *
40      */
41     SoundGenerator(int32_t sampleRate, int32_t channelCount);
42     ~SoundGenerator() = default;
43 
44     SoundGenerator(SoundGenerator&& other) = default;
45     SoundGenerator& operator= (SoundGenerator&& other) = default;
46 
47     // Switch the tones on
48     void tap(bool isOn) override;
49 
50     void renderAudio(float *audioData, int32_t numFrames) override;
51 
52 private:
53     std::unique_ptr<Oscillator[]> mOscillators;
54     std::unique_ptr<float[]> mBuffer = std::make_unique<float[]>(kSharedBufferSize);
55 };
56 
57 
58 #endif //SAMPLES_SOUNDGENERATOR_H
59