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 RHYTHMGAME_SOUNDRECORDING_H
18 #define RHYTHMGAME_SOUNDRECORDING_H
19 
20 #include <cstdint>
21 #include <array>
22 
23 #include <chrono>
24 #include <memory>
25 #include <atomic>
26 
27 #include <android/asset_manager.h>
28 
29 #include "shared/IRenderableAudio.h"
30 #include "DataSource.h"
31 
32 class Player : public IRenderableAudio{
33 
34 public:
35     /**
36      * Construct a new Player from the given DataSource. Players can share the same data source.
37      * For example, you could play two identical sounds concurrently by creating 2 Players with the
38      * same data source.
39      *
40      * @param source
41      */
Player(std::shared_ptr<DataSource> source)42     Player(std::shared_ptr<DataSource> source)
43         : mSource(source)
44     {};
45 
46     void renderAudio(float *targetData, int32_t numFrames);
resetPlayHead()47     void resetPlayHead() { mReadFrameIndex = 0; };
setPlaying(bool isPlaying)48     void setPlaying(bool isPlaying) { mIsPlaying = isPlaying; resetPlayHead(); };
setLooping(bool isLooping)49     void setLooping(bool isLooping) { mIsLooping = isLooping; };
50 
51 private:
52     int32_t mReadFrameIndex = 0;
53     std::atomic<bool> mIsPlaying { false };
54     std::atomic<bool> mIsLooping { false };
55     std::shared_ptr<DataSource> mSource;
56 
57     void renderSilence(float*, int32_t);
58 };
59 
60 #endif //RHYTHMGAME_SOUNDRECORDING_H
61