1 /*
2  * Copyright 2019 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 _PLAYER_SIMIPLEMULTIPLAYER_H_
18 #define _PLAYER_SIMIPLEMULTIPLAYER_H_
19 
20 #include <vector>
21 
22 #include <oboe/Oboe.h>
23 
24 #include "OneShotSampleSource.h"
25 #include "SampleBuffer.h"
26 
27 namespace iolib {
28 
29 typedef unsigned char byte;     // an 8-bit unsigned value
30 
31 /**
32  * A simple streaming player for multiple SampleBuffers.
33  */
34 class SimpleMultiPlayer : public oboe::AudioStreamCallback  {
35 public:
36     SimpleMultiPlayer();
37 
38     // Inherited from oboe::AudioStreamCallback
39     oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData,
40             int32_t numFrames) override;
41     virtual void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override;
42     virtual void onErrorBeforeClose(oboe::AudioStream * oboeStream, oboe::Result error) override;
43 
44     void setupAudioStream(int32_t channelCount);
45     void teardownAudioStream();
46 
47     bool openStream();
48     bool startStream();
49 
getSampleRate()50     int getSampleRate() { return mSampleRate; }
51 
52     // Wave Sample Loading...
53     /**
54      * Adds the SampleSource/Samplebuffer pair to the list of source channels.
55      * Transfers ownership of those objects so that they can be deleted/unloaded.
56      * The indexes associated with each source channel is the order in which they
57      * are added.
58      */
59     void addSampleSource(SampleSource* source, SampleBuffer* buffer);
60     /**
61      * Deallocates and deletes all added source/buffer (see addSampleSource()).
62      */
63     void unloadSampleData();
64 
65     void triggerDown(int32_t index);
66     void triggerUp(int32_t index);
67 
68     void resetAll();
69 
getOutputReset()70     bool getOutputReset() { return mOutputReset; }
clearOutputReset()71     void clearOutputReset() { mOutputReset = false; }
72 
73     void setPan(int index, float pan);
74     float getPan(int index);
75 
76     void setGain(int index, float gain);
77     float getGain(int index);
78 
79 private:
80     // Oboe Audio Stream
81     std::shared_ptr<oboe::AudioStream> mAudioStream;
82 
83     // Playback Audio attributes
84     int32_t mChannelCount;
85     int32_t mSampleRate;
86 
87     // Sample Data
88     int32_t mNumSampleBuffers;
89     std::vector<SampleBuffer*>  mSampleBuffers;
90     std::vector<SampleSource*>  mSampleSources;
91 
92     bool    mOutputReset;
93 };
94 
95 }
96 #endif //_PLAYER_SIMIPLEMULTIPLAYER_H_
97