xref: /aosp_15_r20/external/oboe/samples/minimaloboe/src/main/cpp/SimpleNoiseMaker.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2022 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 SIMPLE_NOISE_MAKER_H
18 #define SIMPLE_NOISE_MAKER_H
19 
20 #include "oboe/Oboe.h"
21 
22 /**
23  * Play white noise using Oboe.
24  */
25 class SimpleNoiseMaker {
26 public:
27 
28     /**
29      * Open an Oboe stream.
30      * @return OK or negative error code.
31      */
32     oboe::Result open();
33 
34     oboe::Result start();
35 
36     oboe::Result stop();
37 
38     oboe::Result close();
39 
40 private:
41 
42     class MyDataCallback : public oboe::AudioStreamDataCallback {
43     public:
44         oboe::DataCallbackResult onAudioReady(
45                 oboe::AudioStream *audioStream,
46                 void *audioData,
47                 int32_t numFrames) override;
48 
49     };
50 
51     class MyErrorCallback : public oboe::AudioStreamErrorCallback {
52     public:
MyErrorCallback(SimpleNoiseMaker * parent)53         MyErrorCallback(SimpleNoiseMaker *parent) : mParent(parent) {}
54 
~MyErrorCallback()55         virtual ~MyErrorCallback() {
56         }
57 
58         void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override;
59 
60     private:
61         SimpleNoiseMaker *mParent;
62     };
63 
64     std::shared_ptr<oboe::AudioStream> mStream;
65     std::shared_ptr<MyDataCallback> mDataCallback;
66     std::shared_ptr<MyErrorCallback> mErrorCallback;
67 
68     static constexpr int kChannelCount = 2;
69 };
70 
71 #endif //SIMPLE_NOISE_MAKER_H
72